From fb91341d1858bdd5f63a5014b4bef65e747cc7fb Mon Sep 17 00:00:00 2001 From: fanfuxiaoran <495538672@qq.com> Date: Tue, 25 Mar 2014 17:35:33 +0800 Subject: [PATCH] add create script --- .../web/api/ScriptActionController.java | 754 ++++++++++-------- .../bench4q/web/service/ScriptService.java | 539 +++++++------ .../api/test/ScriptActionControllerTest.java | 54 +- 3 files changed, 746 insertions(+), 601 deletions(-) diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/api/ScriptActionController.java b/Bench4Q-Web/src/main/java/org/bench4q/web/api/ScriptActionController.java index c8099a00..aad4856c 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/api/ScriptActionController.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/api/ScriptActionController.java @@ -1,353 +1,401 @@ -package org.bench4q.web.api; - -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.Map; -import java.util.List; - -import javax.servlet.http.HttpServletRequest; -import javax.xml.bind.JAXBException; -import javax.xml.transform.Source; -import javax.xml.transform.stream.StreamSource; -import javax.xml.validation.Schema; -import javax.xml.validation.SchemaFactory; -import javax.xml.validation.Validator; - -import org.bench4q.share.models.agent.RunScenarioModel; -import org.bench4q.share.models.master.OperateScriptServerResponseModel; -import org.bench4q.share.models.master.ScriptModel; -import org.bench4q.web.exception.CustomGenericException; -import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange; -import org.bench4q.web.model.BaseResponseModel; -import org.bench4q.web.service.BaseService; -import org.bench4q.web.service.ScriptService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.SessionAttributes; -import org.springframework.web.multipart.commons.CommonsMultipartFile; -import org.xml.sax.SAXException; - -import com.google.gson.Gson; - -@Controller -@SessionAttributes("accessToken") -public class ScriptActionController { - - private final String baseUrl = "RecordScript"; - private ScriptService scriptService; - private final String BASECALLER = "ScriptActionController:"; - - private String getBaseUrl() { - return baseUrl; - } - - private ScriptService getScriptService() { - return scriptService; - } - - @Autowired - private void setScriptService(ScriptService scriptService) { - this.scriptService = scriptService; - } - - @RequestMapping("loadScript") - public @ResponseBody - BaseResponseModel loadScript( - @ModelAttribute("accessToken") String accessToken) - throws CustomGenericException { - String caller = "ScriptActionController:loadScripts"; - OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this - .getScriptService() - .getCommunicateWithMaster() - .getResponseModel(accessToken, - this.getBaseUrl() + "/loadScriptList", - OperateScriptServerResponseModel.class, null, caller); - - if (operateScriptServerResponseModel.isSuccess()) { - List scripts = operateScriptServerResponseModel - .getScriptModels(); - if(scripts==null) - scripts=new ArrayList(); - return new BaseResponseModel(true, scripts); - } else { - - return new BaseResponseModel(false, - operateScriptServerResponseModel.getFailCauseString()); - } - - } - - @RequestMapping("deleteScript") - public @ResponseBody - BaseResponseModel deletescript(HttpServletRequest request, - @ModelAttribute("accessToken") String accessToken, - @RequestParam String scriptId) throws CustomGenericException { - String caller = new String("ScriptActionComntroller:deleteScript"); - String url = this.getBaseUrl() + "/deleteScript"; - Map params = BaseService.makeParamsMap("scriptId", scriptId); - OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this - .getScriptService() - .getCommunicateWithMaster() - .getResponseModel(accessToken, url, - OperateScriptServerResponseModel.class, params, caller); - if (operateScriptServerResponseModel.isSuccess()) { - return new BaseResponseModel(true); - } else - return new BaseResponseModel(false, - operateScriptServerResponseModel.getFailCauseString()); - } - - @RequestMapping("editScript") - public @ResponseBody - BaseResponseModel editScript(HttpServletRequest request, - @ModelAttribute("accessToken") String accessToken, - @RequestParam String scriptId) throws CustomGenericException { - String caller = this.BASECALLER + "editScript"; - String url = this.getBaseUrl() + "/queryScriptById"; - Map params = BaseService.makeParamsMap("scriptId", scriptId); - OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this - .getScriptService() - .getCommunicateWithMaster() - .getResponseModel(accessToken, url, - OperateScriptServerResponseModel.class, params, caller); - - if (operateScriptServerResponseModel.isSuccess()) { - List scripts = operateScriptServerResponseModel - .getScriptModels(); - if (scripts == null || scripts.isEmpty()) - return new BaseResponseModel(false, "data empty"); - else { - Iterator it = scripts.iterator(); - ScriptModel script = it.next(); - if (script.getScriptContent() == null - || script.getScriptContent().isEmpty()) - return new BaseResponseModel(false, "data empty"); - else { - try { - RunScenarioModel result = (RunScenarioModel) ObjectXmlExchange - .fromXml(RunScenarioModel.class, - script.getScriptContent()); - Gson gson = new Gson(); - String scriptContent = null; - scriptContent = gson.toJson(result); - return new BaseResponseModel(true, - (Object) scriptContent); - } catch (JAXBException e) { - throw new CustomGenericException("1", - "script content is invalidate", caller); - } - } - } - } else - return new BaseResponseModel(false, - operateScriptServerResponseModel.getFailCauseString()); - - } - - @RequestMapping("saveEditScript") - public @ResponseBody - BaseResponseModel saveScript( - @ModelAttribute("accessToken") String accessToken, - @RequestParam String scriptId, @RequestParam String script) - throws CustomGenericException { - String url = this.getBaseUrl() + "/updateScript"; - String caller = this.BASECALLER + "saveScript"; - Gson gson = new Gson(); - RunScenarioModel scriptModel = gson.fromJson(script, - RunScenarioModel.class); - try{ - String scriptContent = ObjectXmlExchange.toXml(RunScenarioModel.class, - scriptModel); - Map params = BaseService.makeParamsMap("scriptId", scriptId); - params.put("content", scriptContent); - - OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this - .getScriptService() - .getCommunicateWithMaster() - .getResponseModel(accessToken, url, - OperateScriptServerResponseModel.class, params, caller); - if (operateScriptServerResponseModel.isSuccess()) { - return new BaseResponseModel(true); - } else - return new BaseResponseModel(false, - operateScriptServerResponseModel.getFailCauseString()); - } - catch(JAXBException e){ - return new BaseResponseModel(false,e.getMessage()); - } - } - - @RequestMapping("startRecordServer") - public @ResponseBody - BaseResponseModel startRecordServer( - @ModelAttribute("accessToken") String accessToken) - throws JAXBException, CustomGenericException { - String url = this.getBaseUrl() + "/startScriptRecordServer"; - String caller = BASECALLER + "startRecordServer"; - OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this - .getScriptService() - .getCommunicateWithMaster() - .getResponseModel(accessToken, url, - OperateScriptServerResponseModel.class, null, caller); - - if (operateScriptServerResponseModel.isSuccess()) - return new BaseResponseModel(true, - operateScriptServerResponseModel); - else - return new BaseResponseModel(false, - operateScriptServerResponseModel.getFailCauseString()); - } - - @RequestMapping("stopRecordServer") - public @ResponseBody - BaseResponseModel stopRecordServer( - @ModelAttribute("accessToken") String accessToken, - @RequestParam String port, @RequestParam String fileNameUUID) - throws CustomGenericException { - String caller = BASECALLER + "stopeRecordServer"; - String url = this.getBaseUrl() + "/stopScriptRecordServer"; - Map params = BaseService.makeParamsMap("port", port); - params.put("fileNameUUID", fileNameUUID); - OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this - .getScriptService() - .getCommunicateWithMaster() - .getResponseModel(accessToken, url, - OperateScriptServerResponseModel.class, params, caller); - if (operateScriptServerResponseModel.isSuccess()) - return new BaseResponseModel(true); - else - return new BaseResponseModel(false, - operateScriptServerResponseModel.getFailCauseString()); - } - - @RequestMapping("saveRecordScript") - public @ResponseBody - BaseResponseModel saveScriptToDB(HttpServletRequest request, - @ModelAttribute("accessToken") String accessToken, - @RequestParam String port, @RequestParam String scriptName, - @RequestParam String fileNameUUID) throws CustomGenericException { - String caller = BASECALLER + "saveRecordScript"; - String url = this.getBaseUrl() + "/saveScriptToDB"; - Map params = BaseService.makeParamsMap("scriptName", - scriptName); - params.put("port", port); - params.put("fileNameUUID", fileNameUUID); - - OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this - .getScriptService() - .getCommunicateWithMaster() - .getResponseModel(accessToken, url, - OperateScriptServerResponseModel.class, params, caller); - if (operateScriptServerResponseModel.isSuccess()) - return new BaseResponseModel(true); - else - return new BaseResponseModel(false, - operateScriptServerResponseModel.getFailCauseString()); - - } - - @RequestMapping("uploadScript") - @ResponseBody - public BaseResponseModel uploadScript( - @ModelAttribute("accessToken") String accessToken, - @RequestParam("script") CommonsMultipartFile script, - @RequestParam String scriptName) throws CustomGenericException { - String caller=BASECALLER+"uploadScript"; - String url=this.getBaseUrl()+"/uploadScript"+"/"+scriptName; - if (script.isEmpty()) { - return new BaseResponseModel(false,"empty file"); - } - if (scriptName == null || scriptName.equals("")) - return new BaseResponseModel(false,"empty file name"); - try{ - String scenarioModel = new String(script.getBytes()); - ObjectXmlExchange.fromXml(RunScenarioModel.class, scenarioModel); - OperateScriptServerResponseModel operateScriptServerResponseModel= - (OperateScriptServerResponseModel)this.getScriptService(). - getCommunicateWithMaster().getResponseModelByPut(accessToken, url, scenarioModel, OperateScriptServerResponseModel.class, caller); - if (operateScriptServerResponseModel.isSuccess()) - return new BaseResponseModel(true,(Object)new String("upload script:" + scriptName + " success!")); - - else - return new BaseResponseModel(false,operateScriptServerResponseModel.getFailCauseString()); - } catch (JAXBException e) { - return new BaseResponseModel(false,"Failed:invalidated script file!"); - } - - } - - @RequestMapping("validateScript") - public @ResponseBody - String validateScript(@ModelAttribute("accessToken") String accessToken, - @RequestParam String content) { - String result; - String message = ""; - String schemaFileName = "src/main/webapp/WEB-INF/validateScript.xsd"; - content = content.replace("runScenario", "runscenario") - .replace("poolSize", "poolsize") - .replace("usePlugin", "useplugin") - .replace("userBehavior", "userbehavior") - .replace("timerBehavior", "timerbehavior") - .replace("userbehaviors", "behaviors"); - try { - result = validateXmlWithSchema(schemaFileName, content); - if (result == "true") { - message = "Success"; - } else { - message = URLEncoder.encode(result, "UTF-8").replace("+", ""); - } - } catch (Exception e) { - System.out.println("analysis of failure:" + e.toString()); - message = "failed to read schema file"; - } - // System.out.println(message); - return message; - } - - private String validateXmlWithSchema(String schemaFileName, String xmlScript) - throws SAXException, IOException { - String validationResult = ""; - SchemaFactory schemaFactory = SchemaFactory - .newInstance("http://www.w3.org/2001/XMLSchema"); - File schemaFile = new File(schemaFileName); - Schema schema = schemaFactory.newSchema(schemaFile); - Validator validator = schema.newValidator(); - InputStream script = new ByteArrayInputStream(xmlScript.getBytes()); - Source source = new StreamSource(script); - try { - // System.out.println("enter validateXmlWithSchema"); - validator.validate(source); - validationResult = "true"; - } catch (Exception ex) { - validationResult = ex.toString(); - } - return validationResult; - } - - @RequestMapping("readSchemaContent") - public @ResponseBody - String readSchemaContent() throws Exception { - String schema = ""; - FileReader fr = new FileReader("src/main/webapp/WEB-INF/schema.json"); - BufferedReader br = new BufferedReader(fr); - StringBuilder sb = new StringBuilder(); - while ((schema = br.readLine()) != null) { - sb.append(schema); - } - br.close(); - String str = sb.toString(); - return str; - } - -} +package org.bench4q.web.api; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Map; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.xml.bind.JAXBException; +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; + +import org.bench4q.share.models.agent.RunScenarioModel; +import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; +import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; +import org.bench4q.share.models.master.OperateScriptServerResponseModel; +import org.bench4q.share.models.master.ScriptModel; +import org.bench4q.web.exception.CustomGenericException; +import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange; +import org.bench4q.web.model.BaseResponseModel; +import org.bench4q.web.service.BaseService; +import org.bench4q.web.service.ScriptService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.SessionAttributes; +import org.springframework.web.multipart.commons.CommonsMultipartFile; +import org.xml.sax.SAXException; + +import com.google.gson.Gson; + +@Controller +@SessionAttributes("accessToken") +public class ScriptActionController { + + private final String baseUrl = "RecordScript"; + private ScriptService scriptService; + private final String BASECALLER = "ScriptActionController:"; + + private String getBaseUrl() { + return baseUrl; + } + + private ScriptService getScriptService() { + return scriptService; + } + + @Autowired + private void setScriptService(ScriptService scriptService) { + this.scriptService = scriptService; + } + + @RequestMapping("loadScript") + public @ResponseBody + BaseResponseModel loadScript( + @ModelAttribute("accessToken") String accessToken) + throws CustomGenericException { + String caller = "ScriptActionController:loadScripts"; + OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this + .getScriptService() + .getCommunicateWithMaster() + .getResponseModel(accessToken, + this.getBaseUrl() + "/loadScriptList", + OperateScriptServerResponseModel.class, null, caller); + + if (operateScriptServerResponseModel.isSuccess()) { + List scripts = operateScriptServerResponseModel + .getScriptModels(); + if (scripts == null) + scripts = new ArrayList(); + return new BaseResponseModel(true, scripts); + } else { + + return new BaseResponseModel(false, + operateScriptServerResponseModel.getFailCauseString()); + } + + } + + @RequestMapping("deleteScript") + public @ResponseBody + BaseResponseModel deletescript(HttpServletRequest request, + @ModelAttribute("accessToken") String accessToken, + @RequestParam String scriptId) throws CustomGenericException { + String caller = new String("ScriptActionComntroller:deleteScript"); + String url = this.getBaseUrl() + "/deleteScript"; + Map params = BaseService.makeParamsMap("scriptId", + scriptId); + OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this + .getScriptService() + .getCommunicateWithMaster() + .getResponseModel(accessToken, url, + OperateScriptServerResponseModel.class, params, caller); + if (operateScriptServerResponseModel.isSuccess()) { + return new BaseResponseModel(true); + } else + return new BaseResponseModel(false, + operateScriptServerResponseModel.getFailCauseString()); + } + + @RequestMapping("editScript") + public @ResponseBody + BaseResponseModel editScript(HttpServletRequest request, + @ModelAttribute("accessToken") String accessToken, + @RequestParam String scriptId) throws CustomGenericException { + String caller = this.BASECALLER + "editScript"; + String url = this.getBaseUrl() + "/queryScriptById"; + Map params = BaseService.makeParamsMap("scriptId", + scriptId); + OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this + .getScriptService() + .getCommunicateWithMaster() + .getResponseModel(accessToken, url, + OperateScriptServerResponseModel.class, params, caller); + + if (operateScriptServerResponseModel.isSuccess()) { + List scripts = operateScriptServerResponseModel + .getScriptModels(); + if (scripts == null || scripts.isEmpty()) + return new BaseResponseModel(false, "data empty"); + else { + Iterator it = scripts.iterator(); + ScriptModel script = it.next(); + if (script.getScriptContent() == null + || script.getScriptContent().isEmpty()) + return new BaseResponseModel(false, "data empty"); + else { + try { + RunScenarioModel result = (RunScenarioModel) ObjectXmlExchange + .fromXml(RunScenarioModel.class, + script.getScriptContent()); + Gson gson = new Gson(); + String scriptContent = null; + scriptContent = gson.toJson(result); + return new BaseResponseModel(true, + (Object) scriptContent); + } catch (JAXBException e) { + throw new CustomGenericException("1", + "script content is invalidate", caller); + } + } + } + } else + return new BaseResponseModel(false, + operateScriptServerResponseModel.getFailCauseString()); + + } + + @RequestMapping("saveEditScript") + public @ResponseBody + BaseResponseModel saveScript( + @ModelAttribute("accessToken") String accessToken, + @RequestParam String scriptId, @RequestParam String script) + throws CustomGenericException { + String url = this.getBaseUrl() + "/updateScript"; + String caller = this.BASECALLER + "saveScript"; + Gson gson = new Gson(); + RunScenarioModel scriptModel = gson.fromJson(script, + RunScenarioModel.class); + try { + String scriptContent = ObjectXmlExchange.toXml( + RunScenarioModel.class, scriptModel); + Map params = BaseService.makeParamsMap("scriptId", + scriptId); + params.put("content", scriptContent); + + OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this + .getScriptService() + .getCommunicateWithMaster() + .getResponseModel(accessToken, url, + OperateScriptServerResponseModel.class, params, + caller); + if (operateScriptServerResponseModel.isSuccess()) { + return new BaseResponseModel(true); + } else + return new BaseResponseModel(false, + operateScriptServerResponseModel.getFailCauseString()); + } catch (JAXBException e) { + return new BaseResponseModel(false, e.getMessage()); + } + } + + @RequestMapping("startRecordServer") + public @ResponseBody + BaseResponseModel startRecordServer( + @ModelAttribute("accessToken") String accessToken) + throws JAXBException, CustomGenericException { + String url = this.getBaseUrl() + "/startScriptRecordServer"; + String caller = BASECALLER + "startRecordServer"; + OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this + .getScriptService() + .getCommunicateWithMaster() + .getResponseModel(accessToken, url, + OperateScriptServerResponseModel.class, null, caller); + + if (operateScriptServerResponseModel.isSuccess()) + return new BaseResponseModel(true, operateScriptServerResponseModel); + else + return new BaseResponseModel(false, + operateScriptServerResponseModel.getFailCauseString()); + } + + @RequestMapping("stopRecordServer") + public @ResponseBody + BaseResponseModel stopRecordServer( + @ModelAttribute("accessToken") String accessToken, + @RequestParam String port, @RequestParam String fileNameUUID) + throws CustomGenericException { + String caller = BASECALLER + "stopeRecordServer"; + String url = this.getBaseUrl() + "/stopScriptRecordServer"; + Map params = BaseService.makeParamsMap("port", port); + params.put("fileNameUUID", fileNameUUID); + OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this + .getScriptService() + .getCommunicateWithMaster() + .getResponseModel(accessToken, url, + OperateScriptServerResponseModel.class, params, caller); + if (operateScriptServerResponseModel.isSuccess()) + return new BaseResponseModel(true); + else + return new BaseResponseModel(false, + operateScriptServerResponseModel.getFailCauseString()); + } + + @RequestMapping("saveRecordScript") + public @ResponseBody + BaseResponseModel saveScriptToDB(HttpServletRequest request, + @ModelAttribute("accessToken") String accessToken, + @RequestParam String port, @RequestParam String scriptName, + @RequestParam String fileNameUUID) throws CustomGenericException { + String caller = BASECALLER + "saveRecordScript"; + String url = this.getBaseUrl() + "/saveScriptToDB"; + Map params = BaseService.makeParamsMap("scriptName", + scriptName); + params.put("port", port); + params.put("fileNameUUID", fileNameUUID); + + OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this + .getScriptService() + .getCommunicateWithMaster() + .getResponseModel(accessToken, url, + OperateScriptServerResponseModel.class, params, caller); + if (operateScriptServerResponseModel.isSuccess()) + return new BaseResponseModel(true); + else + return new BaseResponseModel(false, + operateScriptServerResponseModel.getFailCauseString()); + + } + + @RequestMapping("uploadScript") + @ResponseBody + public BaseResponseModel uploadScript( + @ModelAttribute("accessToken") String accessToken, + @RequestParam("script") CommonsMultipartFile script, + @RequestParam String scriptName) throws CustomGenericException { + if (script.isEmpty()) { + return new BaseResponseModel(false, "empty file"); + } + if (scriptName == null || scriptName.equals("")) + return new BaseResponseModel(false, "empty file name"); + try { + String scenarioModel = new String(script.getBytes()); + ObjectXmlExchange.fromXml(RunScenarioModel.class, scenarioModel); + OperateScriptServerResponseModel operateScriptServerResponseModel = this + .getScriptService().uploadScript(accessToken, + scenarioModel, scriptName); + if (operateScriptServerResponseModel.isSuccess()) + return new BaseResponseModel(true, (Object) new String( + "upload script:" + scriptName + " success!")); + + else + return new BaseResponseModel(false, + operateScriptServerResponseModel.getFailCauseString()); + } catch (JAXBException e) { + return new BaseResponseModel(false, + "Failed:invalidated script file!"); + } + + } + + @RequestMapping("validateScript") + public @ResponseBody + String validateScript(@ModelAttribute("accessToken") String accessToken, + @RequestParam String content) { + String result; + String message = ""; + String schemaFileName = "src/main/webapp/WEB-INF/validateScript.xsd"; + content = content.replace("runScenario", "runscenario") + .replace("poolSize", "poolsize") + .replace("usePlugin", "useplugin") + .replace("userBehavior", "userbehavior") + .replace("timerBehavior", "timerbehavior") + .replace("userbehaviors", "behaviors"); + try { + result = validateXmlWithSchema(schemaFileName, content); + if (result == "true") { + message = "Success"; + } else { + message = URLEncoder.encode(result, "UTF-8").replace("+", ""); + } + } catch (Exception e) { + System.out.println("analysis of failure:" + e.toString()); + message = "failed to read schema file"; + } + // System.out.println(message); + return message; + } + + private String validateXmlWithSchema(String schemaFileName, String xmlScript) + throws SAXException, IOException { + String validationResult = ""; + SchemaFactory schemaFactory = SchemaFactory + .newInstance("http://www.w3.org/2001/XMLSchema"); + File schemaFile = new File(schemaFileName); + Schema schema = schemaFactory.newSchema(schemaFile); + Validator validator = schema.newValidator(); + InputStream script = new ByteArrayInputStream(xmlScript.getBytes()); + Source source = new StreamSource(script); + try { + // System.out.println("enter validateXmlWithSchema"); + validator.validate(source); + validationResult = "true"; + } catch (Exception ex) { + validationResult = ex.toString(); + } + return validationResult; + } + + @RequestMapping("readSchemaContent") + public @ResponseBody + String readSchemaContent() throws Exception { + String schema = ""; + FileReader fr = new FileReader("src/main/webapp/WEB-INF/schema.json"); + BufferedReader br = new BufferedReader(fr); + StringBuilder sb = new StringBuilder(); + while ((schema = br.readLine()) != null) { + sb.append(schema); + } + br.close(); + String str = sb.toString(); + return str; + } + + @RequestMapping(value = "uploadEditedScript") + @ResponseBody + public BaseResponseModel uploadPluginEditedScript( + @ModelAttribute("accessToken") String accessToken, ModelMap model, + @RequestBody List behaviorModels, + @RequestBody List usePluginModels, + @RequestBody String scriptName) throws CustomGenericException { + if (!validateScript(behaviorModels, usePluginModels)) + return new BaseResponseModel(false, "invalidate script"); + RunScenarioModel runScenarioModel = this.getScriptService() + .createRunScenarioModel(behaviorModels, usePluginModels); + try { + String script = ObjectXmlExchange.toXml(RunScenarioModel.class, + runScenarioModel); + + OperateScriptServerResponseModel operateScriptServerResponseModel = this + .getScriptService().uploadScript(accessToken, script, + scriptName); + if (operateScriptServerResponseModel.isSuccess()) + return new BaseResponseModel(true, (Object) new String( + "upload script:" + scriptName + " success!")); + + else + return new BaseResponseModel(false, + operateScriptServerResponseModel.getFailCauseString()); + } catch (JAXBException e) { + return new BaseResponseModel(false, + "Failed:invalidated script file!"); + } + } + + private boolean validateScript(List behaviorModels, + List usePluginModels) { + + if (behaviorModels == null || behaviorModels.size() == 0) + return false; + if (usePluginModels == null || usePluginModels.size() == 0) + return false; + return true; + } + +} diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/service/ScriptService.java b/Bench4Q-Web/src/main/java/org/bench4q/web/service/ScriptService.java index 1fa27773..aceb2acd 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/service/ScriptService.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/service/ScriptService.java @@ -1,245 +1,294 @@ -package org.bench4q.web.service; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.xml.bind.JAXBException; - -import org.bench4q.share.models.agent.ParameterModel; -import org.bench4q.share.models.agent.RunScenarioModel; -import org.bench4q.share.models.agent.scriptrecord.BatchModel; -import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; -import org.bench4q.share.models.agent.scriptrecord.PageModel; -import org.bench4q.share.models.master.OperateScriptServerResponseModel; -import org.bench4q.share.models.master.ScriptModel; -import org.bench4q.web.exception.CustomGenericException; -import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange; -import org.springframework.stereotype.Component; - -@Component -public class ScriptService extends BaseService { - private String baseUrl = "RecordScript"; - - public String getBaseUrl() { - return baseUrl; - } - - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - - public ScriptModel queryScript(int id, String accessToken) - throws CustomGenericException { - // check script id and return throw exception - String caller = "ScriptService:queryScript"; - - OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this - .getCommunicateWithMaster().getResponseModel(accessToken, - this.getBaseUrl() + "/queryScriptById", - OperateScriptServerResponseModel.class, - makeParamsMap("scriptId", String.valueOf(id)), caller); - if (operateScriptServerResponseModel.isSuccess()) - return operateScriptServerResponseModel.getScriptModels().get(0); - else - throw new CustomGenericException("5", "query script failed", caller - + ":" + id); - } - - public List queryScripts(List scriptIds, - String accessToken) throws CustomGenericException { - String caller = "ScriptService:queryScript"; - List scriptModels = new ArrayList(); - for (Integer scriptId : scriptIds) { - OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this - .getCommunicateWithMaster() - .getResponseModel( - accessToken, - this.getBaseUrl() + "/queryScriptById", - OperateScriptServerResponseModel.class, - makeParamsMap("scriptId", String.valueOf(scriptId)), - caller); - if (operateScriptServerResponseModel.isSuccess()) - scriptModels.add(operateScriptServerResponseModel - .getScriptModels().get(0)); - else - throw new CustomGenericException("5", "query script failed", - caller + ":" + scriptId); - } - return scriptModels; - } - - public Map getScriptNameIdMap( - List scriptModels) throws CustomGenericException { - Map map = new HashMap(); - for (ScriptModel scriptModel : scriptModels) { - map.put(scriptModel.getName(), scriptModel.getId()); - } - return map; - } - - public Map getScriptIdNameMap( - List scriptModels) throws CustomGenericException { - Map map = new HashMap(); - for (ScriptModel scriptModel : scriptModels) { - map.put(scriptModel.getId(), scriptModel.getName()); - } - return map; - } - - public Map getPageNameAndIdMap( - List scriptModels) throws JAXBException { - try { - Map map = new HashMap(); - for (ScriptModel scriptModel : scriptModels) { - Map pageMap = getPageNameAndIdMap((RunScenarioModel) - ObjectXmlExchange.fromXml(RunScenarioModel.class, - scriptModel.getScriptContent())); - if (pageMap != null) - map.putAll(pageMap); - } - return map; - } catch (NullPointerException e) { - e.printStackTrace(); - return null; - } - } - - private Map getPageNameAndIdMap( - RunScenarioModel runScenarioModel) { - - List pageModels = runScenarioModel.getPages(); - try { - Map map = new HashMap(); - for (int i = 0; i < pageModels.size(); i++) { - map.put("page_" + i, i); - } - return map; - } catch (NullPointerException e) { - return null; - } - - } - - public Map getBehaviorNameAndIdMap( - List scriptModels) throws JAXBException { - try { - Map map = new HashMap(); - for (ScriptModel scriptModel: scriptModels) { - Map behaviorMap = getBehaviorNameAndIdMap((RunScenarioModel) - ObjectXmlExchange.fromXml(RunScenarioModel.class, - scriptModel.getScriptContent())); - if (behaviorMap != null) - map.putAll(behaviorMap); - } - return map; - } catch (NullPointerException e) { - return null; - } - } - - public List getScriptPages(RunScenarioModel runScenarioModel) { - try { - List scriptPages = new ArrayList(); - for (int i = 1; i < scriptPages.size(); i++) { - scriptPages.add("page_" + i); - } - return scriptPages; - } catch (NullPointerException e) { - e.printStackTrace(); - return null; - } - } - - public List getScriptBehaviorUrl(RunScenarioModel runScenarioModel) { - - List behaviorModelsWithUrl = getBehaviorModelsHasUrl(runScenarioModel); - if (behaviorModelsWithUrl == null) - return null; - List urList = new ArrayList(); - for (BehaviorModel behaviorModelWithUrl : behaviorModelsWithUrl) { - String url = generateUrl(behaviorModelWithUrl.getParameters()); - urList.add(url); - } - return urList; - } - - public Map getBehaviorNameAndIdMap( - RunScenarioModel runScenarioModel) { - try { - Map map = new HashMap(); - List behaviorModels = this - .getBehaviorModelsHasUrl(runScenarioModel); - if (behaviorModels == null) - return null; - for (BehaviorModel behaviorModel : behaviorModels) { - String url = this.generateUrl(behaviorModel.getParameters()); - if (url != null) - map.put(url, behaviorModel.getId()); - } - return map; - - } catch (NullPointerException e) { - e.printStackTrace(); - return null; - } - } - - private String generateUrl(List parameterModels) { - String url = new String(""); - int i = 0; - for (ParameterModel parameterModel : parameterModels) { - if (i == 1 && parameterModel.getValue() != null) - url += "?" + parameterModel.getValue(); - if (i > 1 && parameterModel.getValue() != null) - url += "&" + parameterModel.getValue(); - i++; - } - if (url.length() <= 0) - return null; - return url; - } - - private List getBehaviorModelsHasUrl( - RunScenarioModel runScenarioModel) { - try { - - List behaviorModels = getBehaviorModels(runScenarioModel); - if (behaviorModels == null) - return null; - List behaviorModelsWithUrl = new ArrayList(); - for (BehaviorModel behaviorModel : behaviorModels) { - List parameterModels = new ArrayList(); - parameterModels.addAll(behaviorModel.getParameters()); - for (ParameterModel parameterModel : parameterModels) { - if (parameterModel.getKey().equals("url")) - behaviorModelsWithUrl.add(behaviorModel); - break; - } - } - - return behaviorModelsWithUrl; - } catch (NullPointerException e) { - e.printStackTrace(); - return null; - } - } - - private List getBehaviorModels( - RunScenarioModel runScenarioModel) { - try { - List batchModels = new ArrayList(); - for (PageModel pageModel : runScenarioModel.getPages()) - batchModels.addAll(pageModel.getBatches()); - List behaviorModels = new ArrayList(); - for (BatchModel batchModel : batchModels) - behaviorModels.addAll(batchModel.getBehaviors()); - return behaviorModels; - } catch (NullPointerException e) { - e.printStackTrace(); - return null; - } - } - -} +package org.bench4q.web.service; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.bind.JAXBException; + +import org.bench4q.share.models.agent.ParameterModel; +import org.bench4q.share.models.agent.RunScenarioModel; +import org.bench4q.share.models.agent.scriptrecord.BatchModel; +import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; +import org.bench4q.share.models.agent.scriptrecord.PageModel; +import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; +import org.bench4q.share.models.master.OperateScriptServerResponseModel; +import org.bench4q.share.models.master.ScriptModel; +import org.bench4q.web.exception.CustomGenericException; +import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange; +import org.springframework.stereotype.Component; + +@Component +public class ScriptService extends BaseService { + private String baseUrl = "RecordScript"; + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public ScriptModel queryScript(int id, String accessToken) + throws CustomGenericException { + // check script id and return throw exception + String caller = "ScriptService:queryScript"; + + OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this + .getCommunicateWithMaster().getResponseModel(accessToken, + this.getBaseUrl() + "/queryScriptById", + OperateScriptServerResponseModel.class, + makeParamsMap("scriptId", String.valueOf(id)), caller); + if (operateScriptServerResponseModel.isSuccess()) + return operateScriptServerResponseModel.getScriptModels().get(0); + else + throw new CustomGenericException("5", "query script failed", caller + + ":" + id); + } + + public List queryScripts(List scriptIds, + String accessToken) throws CustomGenericException { + String caller = "ScriptService:queryScript"; + List scriptModels = new ArrayList(); + for (Integer scriptId : scriptIds) { + OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this + .getCommunicateWithMaster() + .getResponseModel( + accessToken, + this.getBaseUrl() + "/queryScriptById", + OperateScriptServerResponseModel.class, + makeParamsMap("scriptId", String.valueOf(scriptId)), + caller); + if (operateScriptServerResponseModel.isSuccess()) + scriptModels.add(operateScriptServerResponseModel + .getScriptModels().get(0)); + else + throw new CustomGenericException("5", "query script failed", + caller + ":" + scriptId); + } + return scriptModels; + } + + public Map getScriptNameIdMap( + List scriptModels) throws CustomGenericException { + Map map = new HashMap(); + for (ScriptModel scriptModel : scriptModels) { + map.put(scriptModel.getName(), scriptModel.getId()); + } + return map; + } + + public Map getScriptIdNameMap( + List scriptModels) throws CustomGenericException { + Map map = new HashMap(); + for (ScriptModel scriptModel : scriptModels) { + map.put(scriptModel.getId(), scriptModel.getName()); + } + return map; + } + + public Map getPageNameAndIdMap( + List scriptModels) throws JAXBException { + try { + Map map = new HashMap(); + for (ScriptModel scriptModel : scriptModels) { + Map pageMap = getPageNameAndIdMap((RunScenarioModel) ObjectXmlExchange + .fromXml(RunScenarioModel.class, + scriptModel.getScriptContent())); + if (pageMap != null) + map.putAll(pageMap); + } + return map; + } catch (NullPointerException e) { + e.printStackTrace(); + return null; + } + } + + private Map getPageNameAndIdMap( + RunScenarioModel runScenarioModel) { + + List pageModels = runScenarioModel.getPages(); + try { + Map map = new HashMap(); + for (int i = 0; i < pageModels.size(); i++) { + map.put("page_" + i, i); + } + return map; + } catch (NullPointerException e) { + return null; + } + + } + + public Map getBehaviorNameAndIdMap( + List scriptModels) throws JAXBException { + try { + Map map = new HashMap(); + for (ScriptModel scriptModel : scriptModels) { + Map behaviorMap = getBehaviorNameAndIdMap((RunScenarioModel) ObjectXmlExchange + .fromXml(RunScenarioModel.class, + scriptModel.getScriptContent())); + if (behaviorMap != null) + map.putAll(behaviorMap); + } + return map; + } catch (NullPointerException e) { + return null; + } + } + + public List getScriptPages(RunScenarioModel runScenarioModel) { + try { + List scriptPages = new ArrayList(); + for (int i = 1; i < scriptPages.size(); i++) { + scriptPages.add("page_" + i); + } + return scriptPages; + } catch (NullPointerException e) { + e.printStackTrace(); + return null; + } + } + + public List getScriptBehaviorUrl(RunScenarioModel runScenarioModel) { + + List behaviorModelsWithUrl = getBehaviorModelsHasUrl(runScenarioModel); + if (behaviorModelsWithUrl == null) + return null; + List urList = new ArrayList(); + for (BehaviorModel behaviorModelWithUrl : behaviorModelsWithUrl) { + String url = generateUrl(behaviorModelWithUrl.getParameters()); + urList.add(url); + } + return urList; + } + + public Map getBehaviorNameAndIdMap( + RunScenarioModel runScenarioModel) { + try { + Map map = new HashMap(); + List behaviorModels = this + .getBehaviorModelsHasUrl(runScenarioModel); + if (behaviorModels == null) + return null; + for (BehaviorModel behaviorModel : behaviorModels) { + String url = this.generateUrl(behaviorModel.getParameters()); + if (url != null) + map.put(url, behaviorModel.getId()); + } + return map; + + } catch (NullPointerException e) { + e.printStackTrace(); + return null; + } + } + + private String generateUrl(List parameterModels) { + String url = new String(""); + int i = 0; + for (ParameterModel parameterModel : parameterModels) { + if (i == 1 && parameterModel.getValue() != null) + url += "?" + parameterModel.getValue(); + if (i > 1 && parameterModel.getValue() != null) + url += "&" + parameterModel.getValue(); + i++; + } + if (url.length() <= 0) + return null; + return url; + } + + private List getBehaviorModelsHasUrl( + RunScenarioModel runScenarioModel) { + try { + + List behaviorModels = getBehaviorModels(runScenarioModel); + if (behaviorModels == null) + return null; + List behaviorModelsWithUrl = new ArrayList(); + for (BehaviorModel behaviorModel : behaviorModels) { + List parameterModels = new ArrayList(); + parameterModels.addAll(behaviorModel.getParameters()); + for (ParameterModel parameterModel : parameterModels) { + if (parameterModel.getKey().equals("url")) + behaviorModelsWithUrl.add(behaviorModel); + break; + } + } + + return behaviorModelsWithUrl; + } catch (NullPointerException e) { + e.printStackTrace(); + return null; + } + } + + private List getBehaviorModels( + RunScenarioModel runScenarioModel) { + try { + List batchModels = new ArrayList(); + for (PageModel pageModel : runScenarioModel.getPages()) + batchModels.addAll(pageModel.getBatches()); + List behaviorModels = new ArrayList(); + for (BatchModel batchModel : batchModels) + behaviorModels.addAll(batchModel.getBehaviors()); + return behaviorModels; + } catch (NullPointerException e) { + e.printStackTrace(); + return null; + } + } + + public RunScenarioModel createRunScenarioModel( + List behaviorModels, + List usedPluginModels) { + RunScenarioModel runScenarioModel = new RunScenarioModel(); + runScenarioModel + .setPages(createPageModelsWithBehaviorList(behaviorModels)); + runScenarioModel.setUsePlugins(usedPluginModels); + return runScenarioModel; + } + + private List createPageModelsWithBehaviorList( + List behaviorModels) { + List pageModels = new ArrayList(); + for (BehaviorModel behaviorModel : behaviorModels) { + pageModels.add(createPageModelWithOneBehaviorModel(behaviorModel)); + } + return pageModels; + + } + + private PageModel createPageModelWithOneBehaviorModel( + BehaviorModel behaviorModel) { + PageModel pageModel = new PageModel(); + pageModel.setBatches(new ArrayList()); + pageModel.getBatches().add( + createBatchModelWithOneBehavior(behaviorModel)); + return pageModel; + } + + private BatchModel createBatchModelWithOneBehavior( + BehaviorModel behaviorModel) { + BatchModel batchModel = new BatchModel(); + batchModel.setBehaviors(new ArrayList()); + batchModel.getBehaviors().add(behaviorModel); + batchModel.setChildId(-1); + batchModel.setParentId(-1); + batchModel.setId(behaviorModel.getId()); + return batchModel; + } + + public OperateScriptServerResponseModel uploadScript(String accessToken, String scenarioModel, + String scriptName) + throws CustomGenericException { + String url = "RecordScript" + "/uploadScript" + "/" + scriptName; + return (OperateScriptServerResponseModel) getCommunicateWithMaster() + .getResponseModelByPut(accessToken, url, scenarioModel, + OperateScriptServerResponseModel.class, "upload script"); + } +} diff --git a/Bench4Q-Web/src/test/java/org/bench4q/web/api/test/ScriptActionControllerTest.java b/Bench4Q-Web/src/test/java/org/bench4q/web/api/test/ScriptActionControllerTest.java index 2ee12f90..f2065b93 100644 --- a/Bench4Q-Web/src/test/java/org/bench4q/web/api/test/ScriptActionControllerTest.java +++ b/Bench4Q-Web/src/test/java/org/bench4q/web/api/test/ScriptActionControllerTest.java @@ -1,7 +1,12 @@ package org.bench4q.web.api.test; +import java.util.ArrayList; +import java.util.List; - +import org.bench4q.share.models.agent.ParameterModel; +import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; +import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; +import org.bench4q.share.models.master.ScriptModel; import org.bench4q.web.api.ScriptActionController; import org.bench4q.web.exception.CustomGenericException; import org.bench4q.web.model.BaseResponseModel; @@ -13,6 +18,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.ui.ModelMap; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "file:src/main/resources/bench4qweb-servlet.xml" }) @@ -20,6 +26,7 @@ public class ScriptActionControllerTest { private ScriptActionController scriptActionController; private LoginHelper loginHelper; private String accessToken; + public LoginHelper getLoginHelper() { return loginHelper; } @@ -32,10 +39,13 @@ public class ScriptActionControllerTest { public ScriptActionController getScriptActionController() { return scriptActionController; } + @Autowired - public void setScriptActionController(ScriptActionController scriptActionController) { - this.scriptActionController=scriptActionController; + public void setScriptActionController( + ScriptActionController scriptActionController) { + this.scriptActionController = scriptActionController; } + @Before public void setUp() { this.accessToken = this.getLoginHelper().Login(); @@ -48,4 +58,42 @@ public class ScriptActionControllerTest { if (baseResponseModel.isSuccess()) Assert.assertNotNull(baseResponseModel.getData()); } + + @Test + public void uploadPluginEditedScript() throws CustomGenericException { + @SuppressWarnings("unchecked") + int insertCountBefore = ((List) scriptActionController + .loadScript(loginHelper.Login()).getData()).size(); + BaseResponseModel baseResponseModel = scriptActionController + .uploadPluginEditedScript(loginHelper.Login(), new ModelMap(), + createBheBehaviorModels(), createUsePluginModels(), + "testSript"); + @SuppressWarnings("unchecked") + int insertCountAfter = ((List) scriptActionController + .loadScript(loginHelper.Login()).getData()).size(); + Assert.assertTrue(baseResponseModel.isSuccess()); + Assert.assertEquals(insertCountBefore + 1, insertCountAfter); + } + + private List createBheBehaviorModels() { + List behaviorModels = new ArrayList(); + ParameterModel parameterModel = new ParameterModel(); + parameterModel.setKey("url"); + parameterModel.setValue("http://133.133.12.3:7979"); + List parameterModels = new ArrayList(); + BehaviorModel behaviorModel = BehaviorModel.UserBehaviorBuilder(0, + "url", "http", parameterModels); + behaviorModels.add(behaviorModel); + return behaviorModels; + + } + + private List createUsePluginModels() { + List usePluginModels = new ArrayList(); + UsePluginModel httpPluginModel = new UsePluginModel(); + httpPluginModel.setId("http"); + httpPluginModel.setName("Http"); + usePluginModels.add(httpPluginModel); + return usePluginModels; + } }