add create script

This commit is contained in:
fanfuxiaoran 2014-03-25 17:35:33 +08:00
parent bac8098f27
commit fb91341d18
3 changed files with 746 additions and 601 deletions

View File

@ -21,6 +21,8 @@ import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator; import javax.xml.validation.Validator;
import org.bench4q.share.models.agent.RunScenarioModel; 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.OperateScriptServerResponseModel;
import org.bench4q.share.models.master.ScriptModel; import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.exception.CustomGenericException; import org.bench4q.web.exception.CustomGenericException;
@ -30,7 +32,9 @@ import org.bench4q.web.service.BaseService;
import org.bench4q.web.service.ScriptService; import org.bench4q.web.service.ScriptService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
@ -42,9 +46,9 @@ import com.google.gson.Gson;
@Controller @Controller
@SessionAttributes("accessToken") @SessionAttributes("accessToken")
public class ScriptActionController { public class ScriptActionController {
private final String baseUrl = "RecordScript"; private final String baseUrl = "RecordScript";
private ScriptService scriptService; private ScriptService scriptService;
private final String BASECALLER = "ScriptActionController:"; private final String BASECALLER = "ScriptActionController:";
@ -77,9 +81,9 @@ public class ScriptActionController {
if (operateScriptServerResponseModel.isSuccess()) { if (operateScriptServerResponseModel.isSuccess()) {
List<ScriptModel> scripts = operateScriptServerResponseModel List<ScriptModel> scripts = operateScriptServerResponseModel
.getScriptModels(); .getScriptModels();
if(scripts==null) if (scripts == null)
scripts=new ArrayList<ScriptModel>(); scripts = new ArrayList<ScriptModel>();
return new BaseResponseModel(true, scripts); return new BaseResponseModel(true, scripts);
} else { } else {
return new BaseResponseModel(false, return new BaseResponseModel(false,
@ -95,7 +99,8 @@ public class ScriptActionController {
@RequestParam String scriptId) throws CustomGenericException { @RequestParam String scriptId) throws CustomGenericException {
String caller = new String("ScriptActionComntroller:deleteScript"); String caller = new String("ScriptActionComntroller:deleteScript");
String url = this.getBaseUrl() + "/deleteScript"; String url = this.getBaseUrl() + "/deleteScript";
Map<String, String> params = BaseService.makeParamsMap("scriptId", scriptId); Map<String, String> params = BaseService.makeParamsMap("scriptId",
scriptId);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService() .getScriptService()
.getCommunicateWithMaster() .getCommunicateWithMaster()
@ -115,7 +120,8 @@ public class ScriptActionController {
@RequestParam String scriptId) throws CustomGenericException { @RequestParam String scriptId) throws CustomGenericException {
String caller = this.BASECALLER + "editScript"; String caller = this.BASECALLER + "editScript";
String url = this.getBaseUrl() + "/queryScriptById"; String url = this.getBaseUrl() + "/queryScriptById";
Map<String, String> params = BaseService.makeParamsMap("scriptId", scriptId); Map<String, String> params = BaseService.makeParamsMap("scriptId",
scriptId);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService() .getScriptService()
.getCommunicateWithMaster() .getCommunicateWithMaster()
@ -166,25 +172,26 @@ public class ScriptActionController {
Gson gson = new Gson(); Gson gson = new Gson();
RunScenarioModel scriptModel = gson.fromJson(script, RunScenarioModel scriptModel = gson.fromJson(script,
RunScenarioModel.class); RunScenarioModel.class);
try{ try {
String scriptContent = ObjectXmlExchange.toXml(RunScenarioModel.class, String scriptContent = ObjectXmlExchange.toXml(
scriptModel); RunScenarioModel.class, scriptModel);
Map<String, String> params = BaseService.makeParamsMap("scriptId", scriptId); Map<String, String> params = BaseService.makeParamsMap("scriptId",
params.put("content", scriptContent); scriptId);
params.put("content", scriptContent);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getScriptService() .getScriptService()
.getCommunicateWithMaster() .getCommunicateWithMaster()
.getResponseModel(accessToken, url, .getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, params, caller); OperateScriptServerResponseModel.class, params,
if (operateScriptServerResponseModel.isSuccess()) { caller);
return new BaseResponseModel(true); if (operateScriptServerResponseModel.isSuccess()) {
} else return new BaseResponseModel(true);
return new BaseResponseModel(false, } else
operateScriptServerResponseModel.getFailCauseString()); return new BaseResponseModel(false,
} operateScriptServerResponseModel.getFailCauseString());
catch(JAXBException e){ } catch (JAXBException e) {
return new BaseResponseModel(false,e.getMessage()); return new BaseResponseModel(false, e.getMessage());
} }
} }
@ -202,8 +209,7 @@ public class ScriptActionController {
OperateScriptServerResponseModel.class, null, caller); OperateScriptServerResponseModel.class, null, caller);
if (operateScriptServerResponseModel.isSuccess()) if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true, return new BaseResponseModel(true, operateScriptServerResponseModel);
operateScriptServerResponseModel);
else else
return new BaseResponseModel(false, return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString()); operateScriptServerResponseModel.getFailCauseString());
@ -263,26 +269,27 @@ public class ScriptActionController {
@ModelAttribute("accessToken") String accessToken, @ModelAttribute("accessToken") String accessToken,
@RequestParam("script") CommonsMultipartFile script, @RequestParam("script") CommonsMultipartFile script,
@RequestParam String scriptName) throws CustomGenericException { @RequestParam String scriptName) throws CustomGenericException {
String caller=BASECALLER+"uploadScript";
String url=this.getBaseUrl()+"/uploadScript"+"/"+scriptName;
if (script.isEmpty()) { if (script.isEmpty()) {
return new BaseResponseModel(false,"empty file"); return new BaseResponseModel(false, "empty file");
} }
if (scriptName == null || scriptName.equals("")) if (scriptName == null || scriptName.equals(""))
return new BaseResponseModel(false,"empty file name"); return new BaseResponseModel(false, "empty file name");
try{ try {
String scenarioModel = new String(script.getBytes()); String scenarioModel = new String(script.getBytes());
ObjectXmlExchange.fromXml(RunScenarioModel.class, scenarioModel); ObjectXmlExchange.fromXml(RunScenarioModel.class, scenarioModel);
OperateScriptServerResponseModel operateScriptServerResponseModel= OperateScriptServerResponseModel operateScriptServerResponseModel = this
(OperateScriptServerResponseModel)this.getScriptService(). .getScriptService().uploadScript(accessToken,
getCommunicateWithMaster().getResponseModelByPut(accessToken, url, scenarioModel, OperateScriptServerResponseModel.class, caller); scenarioModel, scriptName);
if (operateScriptServerResponseModel.isSuccess()) if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true,(Object)new String("upload script:" + scriptName + " success!")); return new BaseResponseModel(true, (Object) new String(
"upload script:" + scriptName + " success!"));
else else
return new BaseResponseModel(false,operateScriptServerResponseModel.getFailCauseString()); return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
} catch (JAXBException e) { } catch (JAXBException e) {
return new BaseResponseModel(false,"Failed:invalidated script file!"); return new BaseResponseModel(false,
"Failed:invalidated script file!");
} }
} }
@ -350,4 +357,45 @@ public class ScriptActionController {
return str; return str;
} }
@RequestMapping(value = "uploadEditedScript")
@ResponseBody
public BaseResponseModel uploadPluginEditedScript(
@ModelAttribute("accessToken") String accessToken, ModelMap model,
@RequestBody List<BehaviorModel> behaviorModels,
@RequestBody List<UsePluginModel> 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<BehaviorModel> behaviorModels,
List<UsePluginModel> usePluginModels) {
if (behaviorModels == null || behaviorModels.size() == 0)
return false;
if (usePluginModels == null || usePluginModels.size() == 0)
return false;
return true;
}
} }

View File

@ -12,6 +12,7 @@ import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.scriptrecord.BatchModel; import org.bench4q.share.models.agent.scriptrecord.BatchModel;
import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.agent.scriptrecord.PageModel; 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.OperateScriptServerResponseModel;
import org.bench4q.share.models.master.ScriptModel; import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.exception.CustomGenericException; import org.bench4q.web.exception.CustomGenericException;
@ -93,9 +94,9 @@ public class ScriptService extends BaseService {
try { try {
Map<String, Integer> map = new HashMap<String, Integer>(); Map<String, Integer> map = new HashMap<String, Integer>();
for (ScriptModel scriptModel : scriptModels) { for (ScriptModel scriptModel : scriptModels) {
Map<String, Integer> pageMap = getPageNameAndIdMap((RunScenarioModel) Map<String, Integer> pageMap = getPageNameAndIdMap((RunScenarioModel) ObjectXmlExchange
ObjectXmlExchange.fromXml(RunScenarioModel.class, .fromXml(RunScenarioModel.class,
scriptModel.getScriptContent())); scriptModel.getScriptContent()));
if (pageMap != null) if (pageMap != null)
map.putAll(pageMap); map.putAll(pageMap);
} }
@ -126,10 +127,10 @@ public class ScriptService extends BaseService {
List<ScriptModel> scriptModels) throws JAXBException { List<ScriptModel> scriptModels) throws JAXBException {
try { try {
Map<String, Integer> map = new HashMap<String, Integer>(); Map<String, Integer> map = new HashMap<String, Integer>();
for (ScriptModel scriptModel: scriptModels) { for (ScriptModel scriptModel : scriptModels) {
Map<String, Integer> behaviorMap = getBehaviorNameAndIdMap((RunScenarioModel) Map<String, Integer> behaviorMap = getBehaviorNameAndIdMap((RunScenarioModel) ObjectXmlExchange
ObjectXmlExchange.fromXml(RunScenarioModel.class, .fromXml(RunScenarioModel.class,
scriptModel.getScriptContent())); scriptModel.getScriptContent()));
if (behaviorMap != null) if (behaviorMap != null)
map.putAll(behaviorMap); map.putAll(behaviorMap);
} }
@ -242,4 +243,52 @@ public class ScriptService extends BaseService {
} }
} }
public RunScenarioModel createRunScenarioModel(
List<BehaviorModel> behaviorModels,
List<UsePluginModel> usedPluginModels) {
RunScenarioModel runScenarioModel = new RunScenarioModel();
runScenarioModel
.setPages(createPageModelsWithBehaviorList(behaviorModels));
runScenarioModel.setUsePlugins(usedPluginModels);
return runScenarioModel;
}
private List<PageModel> createPageModelsWithBehaviorList(
List<BehaviorModel> behaviorModels) {
List<PageModel> pageModels = new ArrayList<PageModel>();
for (BehaviorModel behaviorModel : behaviorModels) {
pageModels.add(createPageModelWithOneBehaviorModel(behaviorModel));
}
return pageModels;
}
private PageModel createPageModelWithOneBehaviorModel(
BehaviorModel behaviorModel) {
PageModel pageModel = new PageModel();
pageModel.setBatches(new ArrayList<BatchModel>());
pageModel.getBatches().add(
createBatchModelWithOneBehavior(behaviorModel));
return pageModel;
}
private BatchModel createBatchModelWithOneBehavior(
BehaviorModel behaviorModel) {
BatchModel batchModel = new BatchModel();
batchModel.setBehaviors(new ArrayList<BehaviorModel>());
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");
}
} }

View File

@ -1,7 +1,12 @@
package org.bench4q.web.api.test; 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.api.ScriptActionController;
import org.bench4q.web.exception.CustomGenericException; import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.model.BaseResponseModel; 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.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.ModelMap;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/resources/bench4qweb-servlet.xml" }) @ContextConfiguration(locations = { "file:src/main/resources/bench4qweb-servlet.xml" })
@ -20,6 +26,7 @@ public class ScriptActionControllerTest {
private ScriptActionController scriptActionController; private ScriptActionController scriptActionController;
private LoginHelper loginHelper; private LoginHelper loginHelper;
private String accessToken; private String accessToken;
public LoginHelper getLoginHelper() { public LoginHelper getLoginHelper() {
return loginHelper; return loginHelper;
} }
@ -32,10 +39,13 @@ public class ScriptActionControllerTest {
public ScriptActionController getScriptActionController() { public ScriptActionController getScriptActionController() {
return scriptActionController; return scriptActionController;
} }
@Autowired @Autowired
public void setScriptActionController(ScriptActionController scriptActionController) { public void setScriptActionController(
this.scriptActionController=scriptActionController; ScriptActionController scriptActionController) {
this.scriptActionController = scriptActionController;
} }
@Before @Before
public void setUp() { public void setUp() {
this.accessToken = this.getLoginHelper().Login(); this.accessToken = this.getLoginHelper().Login();
@ -48,4 +58,42 @@ public class ScriptActionControllerTest {
if (baseResponseModel.isSuccess()) if (baseResponseModel.isSuccess())
Assert.assertNotNull(baseResponseModel.getData()); Assert.assertNotNull(baseResponseModel.getData());
} }
@Test
public void uploadPluginEditedScript() throws CustomGenericException {
@SuppressWarnings("unchecked")
int insertCountBefore = ((List<ScriptModel>) scriptActionController
.loadScript(loginHelper.Login()).getData()).size();
BaseResponseModel baseResponseModel = scriptActionController
.uploadPluginEditedScript(loginHelper.Login(), new ModelMap(),
createBheBehaviorModels(), createUsePluginModels(),
"testSript");
@SuppressWarnings("unchecked")
int insertCountAfter = ((List<ScriptModel>) scriptActionController
.loadScript(loginHelper.Login()).getData()).size();
Assert.assertTrue(baseResponseModel.isSuccess());
Assert.assertEquals(insertCountBefore + 1, insertCountAfter);
}
private List<BehaviorModel> createBheBehaviorModels() {
List<BehaviorModel> behaviorModels = new ArrayList<BehaviorModel>();
ParameterModel parameterModel = new ParameterModel();
parameterModel.setKey("url");
parameterModel.setValue("http://133.133.12.3:7979");
List<ParameterModel> parameterModels = new ArrayList<ParameterModel>();
BehaviorModel behaviorModel = BehaviorModel.UserBehaviorBuilder(0,
"url", "http", parameterModels);
behaviorModels.add(behaviorModel);
return behaviorModels;
}
private List<UsePluginModel> createUsePluginModels() {
List<UsePluginModel> usePluginModels = new ArrayList<UsePluginModel>();
UsePluginModel httpPluginModel = new UsePluginModel();
httpPluginModel.setId("http");
httpPluginModel.setName("Http");
usePluginModels.add(httpPluginModel);
return usePluginModels;
}
} }