From 4f482a40ecb4d67c6b2a625df53c0f546f9cb54d Mon Sep 17 00:00:00 2001 From: fanfuxiaoran <495538672@qq.com> Date: Tue, 13 May 2014 17:45:00 +0800 Subject: [PATCH] add behavior js --- Bench4Q-Web/pom.xml | 13 +- .../org/bench4q/web/api/DeviceController.java | 79 +++++++++ .../web/api/PluginActionController.java | 42 ++--- .../web/api/ScriptActionController.java | 4 +- .../org/bench4q/web/model/ScenarioModel.java | 29 +++- .../bench4q/web/newapi/BaseController.java | 1 + .../web/newservice/ScriptServiceNew.java | 41 +++++ .../web/validation/ScriptValidate.java | 5 + .../src/main/resources/ipConfig.properties | 2 +- .../css/{script-editor.css => device.css} | 0 Bench4Q-Web/src/main/webapp/device.jsp | 20 ++- .../src/main/webapp/images/4902937.png | Bin 0 -> 6132 bytes .../webapp/script/device/ContainerFactory.js | 66 +++++++ .../webapp/script/device/DataCollector.js | 164 ++++++++++++++++++ .../webapp/script/device/DeviceFactory.js | 97 +++-------- .../webapp/script/device/EditorFactory.js | 19 +- .../src/main/webapp/script/device/device.js | 114 ++++++++---- .../test/masterMessager/PortMessageTest.java | 59 +++---- .../masterMessager/ScriptMessageTest.java | 140 +++++++++------ 19 files changed, 658 insertions(+), 237 deletions(-) create mode 100644 Bench4Q-Web/src/main/java/org/bench4q/web/api/DeviceController.java create mode 100644 Bench4Q-Web/src/main/java/org/bench4q/web/newservice/ScriptServiceNew.java create mode 100644 Bench4Q-Web/src/main/java/org/bench4q/web/validation/ScriptValidate.java rename Bench4Q-Web/src/main/webapp/css/{script-editor.css => device.css} (100%) create mode 100644 Bench4Q-Web/src/main/webapp/images/4902937.png create mode 100644 Bench4Q-Web/src/main/webapp/script/device/ContainerFactory.js create mode 100644 Bench4Q-Web/src/main/webapp/script/device/DataCollector.js diff --git a/Bench4Q-Web/pom.xml b/Bench4Q-Web/pom.xml index 08e4a971..6988d2f6 100644 --- a/Bench4Q-Web/pom.xml +++ b/Bench4Q-Web/pom.xml @@ -161,13 +161,14 @@ 2.4 - com.github.tomakehurst - wiremock - 1.46 + com.github.tomakehurst + wiremock + 1.46 - - standalone - + + standalone + bench4q-web diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/api/DeviceController.java b/Bench4Q-Web/src/main/java/org/bench4q/web/api/DeviceController.java new file mode 100644 index 00000000..687b6ed4 --- /dev/null +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/api/DeviceController.java @@ -0,0 +1,79 @@ +package org.bench4q.web.api; + +import java.util.HashMap; +import java.util.Map; +import org.apache.log4j.Logger; +import org.bench4q.share.helper.MarshalHelper; +import org.bench4q.share.models.agent.RunScenarioModel; +import org.bench4q.share.models.master.OperateScriptServerResponseModel; +import org.bench4q.web.masterMessager.ScriptMessager; +import org.bench4q.web.model.ScenarioModel; +import org.bench4q.web.newapi.BaseController; +import org.bench4q.web.newservice.ScriptServiceNew; +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.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.SessionAttributes; + +@Controller +@SessionAttributes("accessToken") +public class DeviceController extends BaseController { + private Logger logger = Logger.getLogger(DeviceController.class); + private ScriptServiceNew scriptService; + + private ScriptMessager scriptMessager; + + public ScriptServiceNew getScriptService() { + return scriptService; + } + + @Autowired + public void setScriptService(ScriptServiceNew scriptService) { + this.scriptService = scriptService; + } + + public ScriptMessager getScriptMessager() { + return scriptMessager; + } + + @Autowired + public void setScriptMessager(ScriptMessager scriptMessager) { + this.scriptMessager = scriptMessager; + } + + @RequestMapping(value = "deviceScript", method = RequestMethod.POST) + @ResponseBody + public Map uploadPluginEditedScript( + @ModelAttribute("accessToken") String accessToken, + @RequestBody ScenarioModel script) { + Map map = new HashMap(); + + if (script.getUsePlugins() == null + || script.getUsePlugins().size() == 0) { + fail(map, "empty use plugin"); + } + if (script.getBehaviors() == null || script.getBehaviors().size() == 0) { + fail(map, "empty behaviors"); + } + RunScenarioModel runScenarioModel = this.getScriptService() + .createRunScenarioModel(script.getUsePlugins(), + script.getBehaviors()); + String scenarioModel = MarshalHelper.tryMarshal(runScenarioModel); + logger.info(scenarioModel); + OperateScriptServerResponseModel operateScriptServerResponseModel = this + .getScriptMessager().uploadScript(accessToken, + script.getName(), scenarioModel, null, null); + if (operateScriptServerResponseModel.isSuccess()) { + return success(map); + } else { + logger.info("fail:" + + operateScriptServerResponseModel.getFailCauseString()); + return fail(map, + operateScriptServerResponseModel.getFailCauseString()); + } + } +} diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/api/PluginActionController.java b/Bench4Q-Web/src/main/java/org/bench4q/web/api/PluginActionController.java index 5c60ad09..e37a2099 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/api/PluginActionController.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/api/PluginActionController.java @@ -10,7 +10,6 @@ import javax.xml.bind.JAXBException; import org.apache.log4j.Logger; import org.bench4q.share.helper.MarshalHelper; -import org.bench4q.share.models.master.plugin.BehaviorInfoModel; import org.bench4q.share.models.master.plugin.PluginResponseModel; import org.bench4q.share.models.master.plugin.PluginUIModel; import org.bench4q.web.exception.CustomGenericException; @@ -135,7 +134,6 @@ public class PluginActionController extends BaseController { BaseResponseModel loadPluginUIInfo( @ModelAttribute("accessToken") String accessToken) throws CustomGenericException { - System.out.println("loadPluginUIList"); String caller = new String("PluginActionController:loadPluginUIList"); String url = this.getBaseUrl() + "/loadPluginList"; PluginResponseModel pluginResponseModel = (PluginResponseModel) this @@ -175,37 +173,23 @@ public class PluginActionController extends BaseController { } } - @RequestMapping("loadBehaviorList") + @RequestMapping("loadBehaviorList/{pluginName}") public @ResponseBody - BaseResponseModel loadBehaviorList(HttpServletRequest request, + Map loadBehaviorList(HttpServletRequest request, @ModelAttribute("accessToken") String accessToken, - @RequestParam String pluginName) throws CustomGenericException { - String caller = new String("PluginActionController:loadBehaviorList"); - String url = this.getBaseUrl() + "/loadBehaviors/" + pluginName; - Map params = BaseService.makeParamsMap("pluginName", - pluginName); - PluginResponseModel pluginResponseModel = (PluginResponseModel) this - .getCommunicateWithMaster().getResponseModel(accessToken, url, - PluginResponseModel.class, params, caller); - - try { - Logger.getLogger(PluginActionController.class).info( - MarshalHelper.marshal(PluginResponseModel.class, - pluginResponseModel)); - } catch (JAXBException e) { - e.printStackTrace(); - } - if (pluginResponseModel.isSuccess()) { - List behaviorInfoModels = pluginResponseModel - .getBehaviorInfoModels(); - if (behaviorInfoModels == null) { - behaviorInfoModels = new ArrayList(); - } - return new BaseResponseModel(true, behaviorInfoModels); + @PathVariable String pluginName) throws CustomGenericException { + Map map = new HashMap(); + ; + PluginResponseModel pluginResponseModel = this.getPluginMessager() + .loadBehaviors(accessToken, pluginName); + if (!pluginResponseModel.isSuccess()) { + return fail(map, pluginResponseModel.getFailMessage()); } else { - return new BaseResponseModel(false, - pluginResponseModel.getFailMessage()); + map = success(map); + map.put("behaviors", pluginResponseModel.getBehaviorInfoModels()); } + return map; + } @RequestMapping(value = "uploadPluginFile", method = RequestMethod.POST) 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 e81e808b..c9e3beef 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 @@ -39,7 +39,6 @@ import org.bench4q.web.service.BaseService; import org.bench4q.web.service.CommunicateWithMaster; 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.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -52,7 +51,6 @@ import org.xml.sax.SAXException; import com.google.gson.Gson; -@Controller @SessionAttributes("accessToken") public class ScriptActionController { @@ -412,7 +410,7 @@ public class ScriptActionController { RunScenarioModel runScenarioModel = scenarioModel.getRunScenarioModel(); List pageModels = runScenarioModel.getPages(); List usePluginModels = runScenarioModel.getUsePlugins(); - String scriptName = scenarioModel.getScriptName(); + String scriptName = scenarioModel.getName(); if (!validateScenarioModel(pageModels, usePluginModels)) return new BaseResponseModel(false, "invalidate script"); try { diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/model/ScenarioModel.java b/Bench4Q-Web/src/main/java/org/bench4q/web/model/ScenarioModel.java index 0f814d0f..7f1f5e3f 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/model/ScenarioModel.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/model/ScenarioModel.java @@ -1,15 +1,33 @@ package org.bench4q.web.model; +import java.util.List; + import org.bench4q.share.models.agent.RunScenarioModel; +import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; +import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; public class ScenarioModel { - private String scriptName; + private String name; + private List usePlugins; + private List behaviors; private RunScenarioModel runScenarioModel; - public String getScriptName() { - return scriptName; + public String getName() { + return name; } - public void setScriptName(String scriptName) { - this.scriptName = scriptName; + public void setName(String name) { + this.name = name; + } + public List getUsePlugins() { + return usePlugins; + } + public void setUsePlugins(List usePlugins) { + this.usePlugins = usePlugins; + } + public List getBehaviors() { + return behaviors; + } + public void setBehaviors(List behaviors) { + this.behaviors = behaviors; } public RunScenarioModel getRunScenarioModel() { return runScenarioModel; @@ -17,6 +35,5 @@ public class ScenarioModel { public void setRunScenarioModel(RunScenarioModel runScenarioModel) { this.runScenarioModel = runScenarioModel; } - } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/newapi/BaseController.java b/Bench4Q-Web/src/main/java/org/bench4q/web/newapi/BaseController.java index c22ac48e..ea37c650 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/newapi/BaseController.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/newapi/BaseController.java @@ -3,6 +3,7 @@ package org.bench4q.web.newapi; import java.util.HashMap; import java.util.Map; + public abstract class BaseController { private final String SUCCESS = "success"; private final String FAILMESSAGE = "failedMessage"; diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/newservice/ScriptServiceNew.java b/Bench4Q-Web/src/main/java/org/bench4q/web/newservice/ScriptServiceNew.java new file mode 100644 index 00000000..43d2c681 --- /dev/null +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/newservice/ScriptServiceNew.java @@ -0,0 +1,41 @@ +package org.bench4q.web.newservice; + +import java.util.LinkedList; +import java.util.List; + +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.springframework.stereotype.Component; + +@Component +public class ScriptServiceNew { + + public RunScenarioModel createRunScenarioModel( + List usePlugins, List behaviorModels) { + RunScenarioModel runScenarioModel = new RunScenarioModel(); + runScenarioModel.setUsePlugins(usePlugins); + List pageModels = new LinkedList(); + int count = 0; + for (BehaviorModel behaviorModel : behaviorModels) { + List behaviorList = new LinkedList(); + behaviorModel.setId(count); + behaviorList.add(behaviorModel); + BatchModel batchModel = new BatchModel(); + batchModel.setBehaviors(behaviorList); + batchModel.setChildId(-1); + batchModel.setId(count); + batchModel.setParentId(-1); + List batchModels = new LinkedList(); + batchModels.add(batchModel); + PageModel pageModel = new PageModel(); + pageModel.setBatches(batchModels); + pageModels.add(pageModel); + count++; + } + runScenarioModel.setPages(pageModels); + return runScenarioModel; + } +} diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/validation/ScriptValidate.java b/Bench4Q-Web/src/main/java/org/bench4q/web/validation/ScriptValidate.java new file mode 100644 index 00000000..8e8851dd --- /dev/null +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/validation/ScriptValidate.java @@ -0,0 +1,5 @@ +package org.bench4q.web.validation; + +public class ScriptValidate { + +} diff --git a/Bench4Q-Web/src/main/resources/ipConfig.properties b/Bench4Q-Web/src/main/resources/ipConfig.properties index 02d24748..63e5d3c0 100644 --- a/Bench4Q-Web/src/main/resources/ipConfig.properties +++ b/Bench4Q-Web/src/main/resources/ipConfig.properties @@ -1 +1 @@ -masterAddress=127.0.0.1:10086/ \ No newline at end of file +masterAddress=127.0.0.1:7979/ \ No newline at end of file diff --git a/Bench4Q-Web/src/main/webapp/css/script-editor.css b/Bench4Q-Web/src/main/webapp/css/device.css similarity index 100% rename from Bench4Q-Web/src/main/webapp/css/script-editor.css rename to Bench4Q-Web/src/main/webapp/css/device.css diff --git a/Bench4Q-Web/src/main/webapp/device.jsp b/Bench4Q-Web/src/main/webapp/device.jsp index a5f276f2..c61d80a5 100644 --- a/Bench4Q-Web/src/main/webapp/device.jsp +++ b/Bench4Q-Web/src/main/webapp/device.jsp @@ -7,7 +7,7 @@ - +