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 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;
@ -30,7 +32,9 @@ 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;
@ -42,9 +46,9 @@ import com.google.gson.Gson;
@Controller
@SessionAttributes("accessToken")
public class ScriptActionController {
public class ScriptActionController {
private final String baseUrl = "RecordScript";
private final String baseUrl = "RecordScript";
private ScriptService scriptService;
private final String BASECALLER = "ScriptActionController:";
@ -77,9 +81,9 @@ public class ScriptActionController {
if (operateScriptServerResponseModel.isSuccess()) {
List<ScriptModel> scripts = operateScriptServerResponseModel
.getScriptModels();
if(scripts==null)
scripts=new ArrayList<ScriptModel>();
return new BaseResponseModel(true, scripts);
if (scripts == null)
scripts = new ArrayList<ScriptModel>();
return new BaseResponseModel(true, scripts);
} else {
return new BaseResponseModel(false,
@ -95,7 +99,8 @@ public class ScriptActionController {
@RequestParam String scriptId) throws CustomGenericException {
String caller = new String("ScriptActionComntroller: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
.getScriptService()
.getCommunicateWithMaster()
@ -115,7 +120,8 @@ public class ScriptActionController {
@RequestParam String scriptId) throws CustomGenericException {
String caller = this.BASECALLER + "editScript";
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
.getScriptService()
.getCommunicateWithMaster()
@ -166,25 +172,26 @@ public class ScriptActionController {
Gson gson = new Gson();
RunScenarioModel scriptModel = gson.fromJson(script,
RunScenarioModel.class);
try{
String scriptContent = ObjectXmlExchange.toXml(RunScenarioModel.class,
scriptModel);
Map<String, String> params = BaseService.makeParamsMap("scriptId", scriptId);
params.put("content", scriptContent);
try {
String scriptContent = ObjectXmlExchange.toXml(
RunScenarioModel.class, scriptModel);
Map<String, String> 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());
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());
}
}
@ -202,8 +209,7 @@ public class ScriptActionController {
OperateScriptServerResponseModel.class, null, caller);
if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true,
operateScriptServerResponseModel);
return new BaseResponseModel(true, operateScriptServerResponseModel);
else
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
@ -263,26 +269,27 @@ public class ScriptActionController {
@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");
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());
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);
OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getScriptService().uploadScript(accessToken,
scenarioModel, scriptName);
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
return new BaseResponseModel(false,operateScriptServerResponseModel.getFailCauseString());
return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
} 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;
}
@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.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;
@ -93,9 +94,9 @@ public class ScriptService extends BaseService {
try {
Map<String, Integer> map = new HashMap<String, Integer>();
for (ScriptModel scriptModel : scriptModels) {
Map<String, Integer> pageMap = getPageNameAndIdMap((RunScenarioModel)
ObjectXmlExchange.fromXml(RunScenarioModel.class,
scriptModel.getScriptContent()));
Map<String, Integer> pageMap = getPageNameAndIdMap((RunScenarioModel) ObjectXmlExchange
.fromXml(RunScenarioModel.class,
scriptModel.getScriptContent()));
if (pageMap != null)
map.putAll(pageMap);
}
@ -126,10 +127,10 @@ public class ScriptService extends BaseService {
List<ScriptModel> scriptModels) throws JAXBException {
try {
Map<String, Integer> map = new HashMap<String, Integer>();
for (ScriptModel scriptModel: scriptModels) {
Map<String, Integer> behaviorMap = getBehaviorNameAndIdMap((RunScenarioModel)
ObjectXmlExchange.fromXml(RunScenarioModel.class,
scriptModel.getScriptContent()));
for (ScriptModel scriptModel : scriptModels) {
Map<String, Integer> behaviorMap = getBehaviorNameAndIdMap((RunScenarioModel) ObjectXmlExchange
.fromXml(RunScenarioModel.class,
scriptModel.getScriptContent()));
if (behaviorMap != null)
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;
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<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;
}
}