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

@ -1,353 +1,401 @@
package org.bench4q.web.api; package org.bench4q.web.api;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema; import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory; 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.master.OperateScriptServerResponseModel; import org.bench4q.share.models.agent.scriptrecord.BehaviorModel;
import org.bench4q.share.models.master.ScriptModel; import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import org.bench4q.web.exception.CustomGenericException; import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange; import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.model.BaseResponseModel; import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.service.BaseService; import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
import org.bench4q.web.service.ScriptService; import org.bench4q.web.model.BaseResponseModel;
import org.springframework.beans.factory.annotation.Autowired; import org.bench4q.web.service.BaseService;
import org.springframework.stereotype.Controller; import org.bench4q.web.service.ScriptService;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.bind.annotation.RequestMapping;
import org.xml.sax.SAXException; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson; import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
@Controller import org.xml.sax.SAXException;
@SessionAttributes("accessToken")
public class ScriptActionController { import com.google.gson.Gson;
private final String baseUrl = "RecordScript"; @Controller
private ScriptService scriptService; @SessionAttributes("accessToken")
private final String BASECALLER = "ScriptActionController:"; public class ScriptActionController {
private String getBaseUrl() { private final String baseUrl = "RecordScript";
return baseUrl; private ScriptService scriptService;
} private final String BASECALLER = "ScriptActionController:";
private ScriptService getScriptService() { private String getBaseUrl() {
return scriptService; return baseUrl;
} }
@Autowired private ScriptService getScriptService() {
private void setScriptService(ScriptService scriptService) { return scriptService;
this.scriptService = scriptService; }
}
@Autowired
@RequestMapping("loadScript") private void setScriptService(ScriptService scriptService) {
public @ResponseBody this.scriptService = scriptService;
BaseResponseModel loadScript( }
@ModelAttribute("accessToken") String accessToken)
throws CustomGenericException { @RequestMapping("loadScript")
String caller = "ScriptActionController:loadScripts"; public @ResponseBody
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this BaseResponseModel loadScript(
.getScriptService() @ModelAttribute("accessToken") String accessToken)
.getCommunicateWithMaster() throws CustomGenericException {
.getResponseModel(accessToken, String caller = "ScriptActionController:loadScripts";
this.getBaseUrl() + "/loadScriptList", OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
OperateScriptServerResponseModel.class, null, caller); .getScriptService()
.getCommunicateWithMaster()
if (operateScriptServerResponseModel.isSuccess()) { .getResponseModel(accessToken,
List<ScriptModel> scripts = operateScriptServerResponseModel this.getBaseUrl() + "/loadScriptList",
.getScriptModels(); OperateScriptServerResponseModel.class, null, caller);
if(scripts==null)
scripts=new ArrayList<ScriptModel>(); if (operateScriptServerResponseModel.isSuccess()) {
return new BaseResponseModel(true, scripts); List<ScriptModel> scripts = operateScriptServerResponseModel
} else { .getScriptModels();
if (scripts == null)
return new BaseResponseModel(false, scripts = new ArrayList<ScriptModel>();
operateScriptServerResponseModel.getFailCauseString()); 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 { @RequestMapping("deleteScript")
String caller = new String("ScriptActionComntroller:deleteScript"); public @ResponseBody
String url = this.getBaseUrl() + "/deleteScript"; BaseResponseModel deletescript(HttpServletRequest request,
Map<String, String> params = BaseService.makeParamsMap("scriptId", scriptId); @ModelAttribute("accessToken") String accessToken,
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this @RequestParam String scriptId) throws CustomGenericException {
.getScriptService() String caller = new String("ScriptActionComntroller:deleteScript");
.getCommunicateWithMaster() String url = this.getBaseUrl() + "/deleteScript";
.getResponseModel(accessToken, url, Map<String, String> params = BaseService.makeParamsMap("scriptId",
OperateScriptServerResponseModel.class, params, caller); scriptId);
if (operateScriptServerResponseModel.isSuccess()) { OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
return new BaseResponseModel(true); .getScriptService()
} else .getCommunicateWithMaster()
return new BaseResponseModel(false, .getResponseModel(accessToken, url,
operateScriptServerResponseModel.getFailCauseString()); OperateScriptServerResponseModel.class, params, caller);
} if (operateScriptServerResponseModel.isSuccess()) {
return new BaseResponseModel(true);
@RequestMapping("editScript") } else
public @ResponseBody return new BaseResponseModel(false,
BaseResponseModel editScript(HttpServletRequest request, operateScriptServerResponseModel.getFailCauseString());
@ModelAttribute("accessToken") String accessToken, }
@RequestParam String scriptId) throws CustomGenericException {
String caller = this.BASECALLER + "editScript"; @RequestMapping("editScript")
String url = this.getBaseUrl() + "/queryScriptById"; public @ResponseBody
Map<String, String> params = BaseService.makeParamsMap("scriptId", scriptId); BaseResponseModel editScript(HttpServletRequest request,
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this @ModelAttribute("accessToken") String accessToken,
.getScriptService() @RequestParam String scriptId) throws CustomGenericException {
.getCommunicateWithMaster() String caller = this.BASECALLER + "editScript";
.getResponseModel(accessToken, url, String url = this.getBaseUrl() + "/queryScriptById";
OperateScriptServerResponseModel.class, params, caller); Map<String, String> params = BaseService.makeParamsMap("scriptId",
scriptId);
if (operateScriptServerResponseModel.isSuccess()) { OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
List<ScriptModel> scripts = operateScriptServerResponseModel .getScriptService()
.getScriptModels(); .getCommunicateWithMaster()
if (scripts == null || scripts.isEmpty()) .getResponseModel(accessToken, url,
return new BaseResponseModel(false, "data empty"); OperateScriptServerResponseModel.class, params, caller);
else {
Iterator<ScriptModel> it = scripts.iterator(); if (operateScriptServerResponseModel.isSuccess()) {
ScriptModel script = it.next(); List<ScriptModel> scripts = operateScriptServerResponseModel
if (script.getScriptContent() == null .getScriptModels();
|| script.getScriptContent().isEmpty()) if (scripts == null || scripts.isEmpty())
return new BaseResponseModel(false, "data empty"); return new BaseResponseModel(false, "data empty");
else { else {
try { Iterator<ScriptModel> it = scripts.iterator();
RunScenarioModel result = (RunScenarioModel) ObjectXmlExchange ScriptModel script = it.next();
.fromXml(RunScenarioModel.class, if (script.getScriptContent() == null
script.getScriptContent()); || script.getScriptContent().isEmpty())
Gson gson = new Gson(); return new BaseResponseModel(false, "data empty");
String scriptContent = null; else {
scriptContent = gson.toJson(result); try {
return new BaseResponseModel(true, RunScenarioModel result = (RunScenarioModel) ObjectXmlExchange
(Object) scriptContent); .fromXml(RunScenarioModel.class,
} catch (JAXBException e) { script.getScriptContent());
throw new CustomGenericException("1", Gson gson = new Gson();
"script content is invalidate", caller); String scriptContent = null;
} scriptContent = gson.toJson(result);
} return new BaseResponseModel(true,
} (Object) scriptContent);
} else } catch (JAXBException e) {
return new BaseResponseModel(false, throw new CustomGenericException("1",
operateScriptServerResponseModel.getFailCauseString()); "script content is invalidate", caller);
}
} }
}
@RequestMapping("saveEditScript") } else
public @ResponseBody return new BaseResponseModel(false,
BaseResponseModel saveScript( operateScriptServerResponseModel.getFailCauseString());
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId, @RequestParam String script) }
throws CustomGenericException {
String url = this.getBaseUrl() + "/updateScript"; @RequestMapping("saveEditScript")
String caller = this.BASECALLER + "saveScript"; public @ResponseBody
Gson gson = new Gson(); BaseResponseModel saveScript(
RunScenarioModel scriptModel = gson.fromJson(script, @ModelAttribute("accessToken") String accessToken,
RunScenarioModel.class); @RequestParam String scriptId, @RequestParam String script)
try{ throws CustomGenericException {
String scriptContent = ObjectXmlExchange.toXml(RunScenarioModel.class, String url = this.getBaseUrl() + "/updateScript";
scriptModel); String caller = this.BASECALLER + "saveScript";
Map<String, String> params = BaseService.makeParamsMap("scriptId", scriptId); Gson gson = new Gson();
params.put("content", scriptContent); RunScenarioModel scriptModel = gson.fromJson(script,
RunScenarioModel.class);
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this try {
.getScriptService() String scriptContent = ObjectXmlExchange.toXml(
.getCommunicateWithMaster() RunScenarioModel.class, scriptModel);
.getResponseModel(accessToken, url, Map<String, String> params = BaseService.makeParamsMap("scriptId",
OperateScriptServerResponseModel.class, params, caller); scriptId);
if (operateScriptServerResponseModel.isSuccess()) { params.put("content", scriptContent);
return new BaseResponseModel(true);
} else OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
return new BaseResponseModel(false, .getScriptService()
operateScriptServerResponseModel.getFailCauseString()); .getCommunicateWithMaster()
} .getResponseModel(accessToken, url,
catch(JAXBException e){ OperateScriptServerResponseModel.class, params,
return new BaseResponseModel(false,e.getMessage()); caller);
} if (operateScriptServerResponseModel.isSuccess()) {
} return new BaseResponseModel(true);
} else
@RequestMapping("startRecordServer") return new BaseResponseModel(false,
public @ResponseBody operateScriptServerResponseModel.getFailCauseString());
BaseResponseModel startRecordServer( } catch (JAXBException e) {
@ModelAttribute("accessToken") String accessToken) return new BaseResponseModel(false, e.getMessage());
throws JAXBException, CustomGenericException { }
String url = this.getBaseUrl() + "/startScriptRecordServer"; }
String caller = BASECALLER + "startRecordServer";
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this @RequestMapping("startRecordServer")
.getScriptService() public @ResponseBody
.getCommunicateWithMaster() BaseResponseModel startRecordServer(
.getResponseModel(accessToken, url, @ModelAttribute("accessToken") String accessToken)
OperateScriptServerResponseModel.class, null, caller); throws JAXBException, CustomGenericException {
String url = this.getBaseUrl() + "/startScriptRecordServer";
if (operateScriptServerResponseModel.isSuccess()) String caller = BASECALLER + "startRecordServer";
return new BaseResponseModel(true, OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
operateScriptServerResponseModel); .getScriptService()
else .getCommunicateWithMaster()
return new BaseResponseModel(false, .getResponseModel(accessToken, url,
operateScriptServerResponseModel.getFailCauseString()); OperateScriptServerResponseModel.class, null, caller);
}
if (operateScriptServerResponseModel.isSuccess())
@RequestMapping("stopRecordServer") return new BaseResponseModel(true, operateScriptServerResponseModel);
public @ResponseBody else
BaseResponseModel stopRecordServer( return new BaseResponseModel(false,
@ModelAttribute("accessToken") String accessToken, operateScriptServerResponseModel.getFailCauseString());
@RequestParam String port, @RequestParam String fileNameUUID) }
throws CustomGenericException {
String caller = BASECALLER + "stopeRecordServer"; @RequestMapping("stopRecordServer")
String url = this.getBaseUrl() + "/stopScriptRecordServer"; public @ResponseBody
Map<String, String> params = BaseService.makeParamsMap("port", port); BaseResponseModel stopRecordServer(
params.put("fileNameUUID", fileNameUUID); @ModelAttribute("accessToken") String accessToken,
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this @RequestParam String port, @RequestParam String fileNameUUID)
.getScriptService() throws CustomGenericException {
.getCommunicateWithMaster() String caller = BASECALLER + "stopeRecordServer";
.getResponseModel(accessToken, url, String url = this.getBaseUrl() + "/stopScriptRecordServer";
OperateScriptServerResponseModel.class, params, caller); Map<String, String> params = BaseService.makeParamsMap("port", port);
if (operateScriptServerResponseModel.isSuccess()) params.put("fileNameUUID", fileNameUUID);
return new BaseResponseModel(true); OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
else .getScriptService()
return new BaseResponseModel(false, .getCommunicateWithMaster()
operateScriptServerResponseModel.getFailCauseString()); .getResponseModel(accessToken, url,
} OperateScriptServerResponseModel.class, params, caller);
if (operateScriptServerResponseModel.isSuccess())
@RequestMapping("saveRecordScript") return new BaseResponseModel(true);
public @ResponseBody else
BaseResponseModel saveScriptToDB(HttpServletRequest request, return new BaseResponseModel(false,
@ModelAttribute("accessToken") String accessToken, operateScriptServerResponseModel.getFailCauseString());
@RequestParam String port, @RequestParam String scriptName, }
@RequestParam String fileNameUUID) throws CustomGenericException {
String caller = BASECALLER + "saveRecordScript"; @RequestMapping("saveRecordScript")
String url = this.getBaseUrl() + "/saveScriptToDB"; public @ResponseBody
Map<String, String> params = BaseService.makeParamsMap("scriptName", BaseResponseModel saveScriptToDB(HttpServletRequest request,
scriptName); @ModelAttribute("accessToken") String accessToken,
params.put("port", port); @RequestParam String port, @RequestParam String scriptName,
params.put("fileNameUUID", fileNameUUID); @RequestParam String fileNameUUID) throws CustomGenericException {
String caller = BASECALLER + "saveRecordScript";
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this String url = this.getBaseUrl() + "/saveScriptToDB";
.getScriptService() Map<String, String> params = BaseService.makeParamsMap("scriptName",
.getCommunicateWithMaster() scriptName);
.getResponseModel(accessToken, url, params.put("port", port);
OperateScriptServerResponseModel.class, params, caller); params.put("fileNameUUID", fileNameUUID);
if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true); OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
else .getScriptService()
return new BaseResponseModel(false, .getCommunicateWithMaster()
operateScriptServerResponseModel.getFailCauseString()); .getResponseModel(accessToken, url,
OperateScriptServerResponseModel.class, params, caller);
} if (operateScriptServerResponseModel.isSuccess())
return new BaseResponseModel(true);
@RequestMapping("uploadScript") else
@ResponseBody return new BaseResponseModel(false,
public BaseResponseModel uploadScript( operateScriptServerResponseModel.getFailCauseString());
@ModelAttribute("accessToken") String accessToken,
@RequestParam("script") CommonsMultipartFile script, }
@RequestParam String scriptName) throws CustomGenericException {
String caller=BASECALLER+"uploadScript"; @RequestMapping("uploadScript")
String url=this.getBaseUrl()+"/uploadScript"+"/"+scriptName; @ResponseBody
if (script.isEmpty()) { public BaseResponseModel uploadScript(
return new BaseResponseModel(false,"empty file"); @ModelAttribute("accessToken") String accessToken,
} @RequestParam("script") CommonsMultipartFile script,
if (scriptName == null || scriptName.equals("")) @RequestParam String scriptName) throws CustomGenericException {
return new BaseResponseModel(false,"empty file name"); if (script.isEmpty()) {
try{ return new BaseResponseModel(false, "empty file");
String scenarioModel = new String(script.getBytes()); }
ObjectXmlExchange.fromXml(RunScenarioModel.class, scenarioModel); if (scriptName == null || scriptName.equals(""))
OperateScriptServerResponseModel operateScriptServerResponseModel= return new BaseResponseModel(false, "empty file name");
(OperateScriptServerResponseModel)this.getScriptService(). try {
getCommunicateWithMaster().getResponseModelByPut(accessToken, url, scenarioModel, OperateScriptServerResponseModel.class, caller); String scenarioModel = new String(script.getBytes());
if (operateScriptServerResponseModel.isSuccess()) ObjectXmlExchange.fromXml(RunScenarioModel.class, scenarioModel);
return new BaseResponseModel(true,(Object)new String("upload script:" + scriptName + " success!")); OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getScriptService().uploadScript(accessToken,
else scenarioModel, scriptName);
return new BaseResponseModel(false,operateScriptServerResponseModel.getFailCauseString()); if (operateScriptServerResponseModel.isSuccess())
} catch (JAXBException e) { return new BaseResponseModel(true, (Object) new String(
return new BaseResponseModel(false,"Failed:invalidated script file!"); "upload script:" + scriptName + " success!"));
}
else
} return new BaseResponseModel(false,
operateScriptServerResponseModel.getFailCauseString());
@RequestMapping("validateScript") } catch (JAXBException e) {
public @ResponseBody return new BaseResponseModel(false,
String validateScript(@ModelAttribute("accessToken") String accessToken, "Failed:invalidated script file!");
@RequestParam String content) { }
String result;
String message = ""; }
String schemaFileName = "src/main/webapp/WEB-INF/validateScript.xsd";
content = content.replace("runScenario", "runscenario") @RequestMapping("validateScript")
.replace("poolSize", "poolsize") public @ResponseBody
.replace("usePlugin", "useplugin") String validateScript(@ModelAttribute("accessToken") String accessToken,
.replace("userBehavior", "userbehavior") @RequestParam String content) {
.replace("timerBehavior", "timerbehavior") String result;
.replace("userbehaviors", "behaviors"); String message = "";
try { String schemaFileName = "src/main/webapp/WEB-INF/validateScript.xsd";
result = validateXmlWithSchema(schemaFileName, content); content = content.replace("runScenario", "runscenario")
if (result == "true") { .replace("poolSize", "poolsize")
message = "Success"; .replace("usePlugin", "useplugin")
} else { .replace("userBehavior", "userbehavior")
message = URLEncoder.encode(result, "UTF-8").replace("+", ""); .replace("timerBehavior", "timerbehavior")
} .replace("userbehaviors", "behaviors");
} catch (Exception e) { try {
System.out.println("analysis of failure:" + e.toString()); result = validateXmlWithSchema(schemaFileName, content);
message = "failed to read schema file"; if (result == "true") {
} message = "Success";
// System.out.println(message); } else {
return message; message = URLEncoder.encode(result, "UTF-8").replace("+", "");
} }
} catch (Exception e) {
private String validateXmlWithSchema(String schemaFileName, String xmlScript) System.out.println("analysis of failure:" + e.toString());
throws SAXException, IOException { message = "failed to read schema file";
String validationResult = ""; }
SchemaFactory schemaFactory = SchemaFactory // System.out.println(message);
.newInstance("http://www.w3.org/2001/XMLSchema"); return message;
File schemaFile = new File(schemaFileName); }
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator(); private String validateXmlWithSchema(String schemaFileName, String xmlScript)
InputStream script = new ByteArrayInputStream(xmlScript.getBytes()); throws SAXException, IOException {
Source source = new StreamSource(script); String validationResult = "";
try { SchemaFactory schemaFactory = SchemaFactory
// System.out.println("enter validateXmlWithSchema"); .newInstance("http://www.w3.org/2001/XMLSchema");
validator.validate(source); File schemaFile = new File(schemaFileName);
validationResult = "true"; Schema schema = schemaFactory.newSchema(schemaFile);
} catch (Exception ex) { Validator validator = schema.newValidator();
validationResult = ex.toString(); InputStream script = new ByteArrayInputStream(xmlScript.getBytes());
} Source source = new StreamSource(script);
return validationResult; try {
} // System.out.println("enter validateXmlWithSchema");
validator.validate(source);
@RequestMapping("readSchemaContent") validationResult = "true";
public @ResponseBody } catch (Exception ex) {
String readSchemaContent() throws Exception { validationResult = ex.toString();
String schema = ""; }
FileReader fr = new FileReader("src/main/webapp/WEB-INF/schema.json"); return validationResult;
BufferedReader br = new BufferedReader(fr); }
StringBuilder sb = new StringBuilder();
while ((schema = br.readLine()) != null) { @RequestMapping("readSchemaContent")
sb.append(schema); public @ResponseBody
} String readSchemaContent() throws Exception {
br.close(); String schema = "";
String str = sb.toString(); FileReader fr = new FileReader("src/main/webapp/WEB-INF/schema.json");
return str; 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<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

@ -1,245 +1,294 @@
package org.bench4q.web.service; package org.bench4q.web.service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import org.bench4q.share.models.agent.ParameterModel; import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel; 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.master.OperateScriptServerResponseModel; import org.bench4q.share.models.agent.scriptrecord.UsePluginModel;
import org.bench4q.share.models.master.ScriptModel; import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.bench4q.web.exception.CustomGenericException; import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange; import org.bench4q.web.exception.CustomGenericException;
import org.springframework.stereotype.Component; import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
import org.springframework.stereotype.Component;
@Component
public class ScriptService extends BaseService { @Component
private String baseUrl = "RecordScript"; public class ScriptService extends BaseService {
private String baseUrl = "RecordScript";
public String getBaseUrl() {
return baseUrl; public String getBaseUrl() {
} return baseUrl;
}
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl; public void setBaseUrl(String baseUrl) {
} this.baseUrl = baseUrl;
}
public ScriptModel queryScript(int id, String accessToken)
throws CustomGenericException { public ScriptModel queryScript(int id, String accessToken)
// check script id and return throw exception throws CustomGenericException {
String caller = "ScriptService:queryScript"; // check script id and return throw exception
String caller = "ScriptService:queryScript";
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getCommunicateWithMaster().getResponseModel(accessToken, OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
this.getBaseUrl() + "/queryScriptById", .getCommunicateWithMaster().getResponseModel(accessToken,
OperateScriptServerResponseModel.class, this.getBaseUrl() + "/queryScriptById",
makeParamsMap("scriptId", String.valueOf(id)), caller); OperateScriptServerResponseModel.class,
if (operateScriptServerResponseModel.isSuccess()) makeParamsMap("scriptId", String.valueOf(id)), caller);
return operateScriptServerResponseModel.getScriptModels().get(0); if (operateScriptServerResponseModel.isSuccess())
else return operateScriptServerResponseModel.getScriptModels().get(0);
throw new CustomGenericException("5", "query script failed", caller else
+ ":" + id); throw new CustomGenericException("5", "query script failed", caller
} + ":" + id);
}
public List<ScriptModel> queryScripts(List<Integer> scriptIds,
String accessToken) throws CustomGenericException { public List<ScriptModel> queryScripts(List<Integer> scriptIds,
String caller = "ScriptService:queryScript"; String accessToken) throws CustomGenericException {
List<ScriptModel> scriptModels = new ArrayList<ScriptModel>(); String caller = "ScriptService:queryScript";
for (Integer scriptId : scriptIds) { List<ScriptModel> scriptModels = new ArrayList<ScriptModel>();
OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this for (Integer scriptId : scriptIds) {
.getCommunicateWithMaster() OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this
.getResponseModel( .getCommunicateWithMaster()
accessToken, .getResponseModel(
this.getBaseUrl() + "/queryScriptById", accessToken,
OperateScriptServerResponseModel.class, this.getBaseUrl() + "/queryScriptById",
makeParamsMap("scriptId", String.valueOf(scriptId)), OperateScriptServerResponseModel.class,
caller); makeParamsMap("scriptId", String.valueOf(scriptId)),
if (operateScriptServerResponseModel.isSuccess()) caller);
scriptModels.add(operateScriptServerResponseModel if (operateScriptServerResponseModel.isSuccess())
.getScriptModels().get(0)); scriptModels.add(operateScriptServerResponseModel
else .getScriptModels().get(0));
throw new CustomGenericException("5", "query script failed", else
caller + ":" + scriptId); throw new CustomGenericException("5", "query script failed",
} caller + ":" + scriptId);
return scriptModels; }
} return scriptModels;
}
public Map<String, Integer> getScriptNameIdMap(
List<ScriptModel> scriptModels) throws CustomGenericException { public Map<String, Integer> getScriptNameIdMap(
Map<String, Integer> map = new HashMap<String, Integer>(); List<ScriptModel> scriptModels) throws CustomGenericException {
for (ScriptModel scriptModel : scriptModels) { Map<String, Integer> map = new HashMap<String, Integer>();
map.put(scriptModel.getName(), scriptModel.getId()); for (ScriptModel scriptModel : scriptModels) {
} map.put(scriptModel.getName(), scriptModel.getId());
return map; }
} return map;
}
public Map<Integer, String> getScriptIdNameMap(
List<ScriptModel> scriptModels) throws CustomGenericException { public Map<Integer, String> getScriptIdNameMap(
Map<Integer, String> map = new HashMap<Integer, String>(); List<ScriptModel> scriptModels) throws CustomGenericException {
for (ScriptModel scriptModel : scriptModels) { Map<Integer, String> map = new HashMap<Integer, String>();
map.put(scriptModel.getId(), scriptModel.getName()); for (ScriptModel scriptModel : scriptModels) {
} map.put(scriptModel.getId(), scriptModel.getName());
return map; }
} return map;
}
public Map<String, Integer> getPageNameAndIdMap(
List<ScriptModel> scriptModels) throws JAXBException { public Map<String, Integer> getPageNameAndIdMap(
try { List<ScriptModel> scriptModels) throws JAXBException {
Map<String, Integer> map = new HashMap<String, Integer>(); try {
for (ScriptModel scriptModel : scriptModels) { Map<String, Integer> map = new HashMap<String, Integer>();
Map<String, Integer> pageMap = getPageNameAndIdMap((RunScenarioModel) for (ScriptModel scriptModel : scriptModels) {
ObjectXmlExchange.fromXml(RunScenarioModel.class, Map<String, Integer> pageMap = getPageNameAndIdMap((RunScenarioModel) ObjectXmlExchange
scriptModel.getScriptContent())); .fromXml(RunScenarioModel.class,
if (pageMap != null) scriptModel.getScriptContent()));
map.putAll(pageMap); if (pageMap != null)
} map.putAll(pageMap);
return map; }
} catch (NullPointerException e) { return map;
e.printStackTrace(); } catch (NullPointerException e) {
return null; e.printStackTrace();
} return null;
} }
}
private Map<String, Integer> getPageNameAndIdMap(
RunScenarioModel runScenarioModel) { private Map<String, Integer> getPageNameAndIdMap(
RunScenarioModel runScenarioModel) {
List<PageModel> pageModels = runScenarioModel.getPages();
try { List<PageModel> pageModels = runScenarioModel.getPages();
Map<String, Integer> map = new HashMap<String, Integer>(); try {
for (int i = 0; i < pageModels.size(); i++) { Map<String, Integer> map = new HashMap<String, Integer>();
map.put("page_" + i, i); for (int i = 0; i < pageModels.size(); i++) {
} map.put("page_" + i, i);
return map; }
} catch (NullPointerException e) { return map;
return null; } catch (NullPointerException e) {
} return null;
}
}
}
public Map<String, Integer> getBehaviorNameAndIdMap(
List<ScriptModel> scriptModels) throws JAXBException { public Map<String, Integer> getBehaviorNameAndIdMap(
try { List<ScriptModel> scriptModels) throws JAXBException {
Map<String, Integer> map = new HashMap<String, Integer>(); try {
for (ScriptModel scriptModel: scriptModels) { Map<String, Integer> map = new HashMap<String, Integer>();
Map<String, Integer> behaviorMap = getBehaviorNameAndIdMap((RunScenarioModel) for (ScriptModel scriptModel : scriptModels) {
ObjectXmlExchange.fromXml(RunScenarioModel.class, Map<String, Integer> behaviorMap = getBehaviorNameAndIdMap((RunScenarioModel) ObjectXmlExchange
scriptModel.getScriptContent())); .fromXml(RunScenarioModel.class,
if (behaviorMap != null) scriptModel.getScriptContent()));
map.putAll(behaviorMap); if (behaviorMap != null)
} map.putAll(behaviorMap);
return map; }
} catch (NullPointerException e) { return map;
return null; } catch (NullPointerException e) {
} return null;
} }
}
public List<String> getScriptPages(RunScenarioModel runScenarioModel) {
try { public List<String> getScriptPages(RunScenarioModel runScenarioModel) {
List<String> scriptPages = new ArrayList<String>(); try {
for (int i = 1; i < scriptPages.size(); i++) { List<String> scriptPages = new ArrayList<String>();
scriptPages.add("page_" + i); for (int i = 1; i < scriptPages.size(); i++) {
} scriptPages.add("page_" + i);
return scriptPages; }
} catch (NullPointerException e) { return scriptPages;
e.printStackTrace(); } catch (NullPointerException e) {
return null; e.printStackTrace();
} return null;
} }
}
public List<String> getScriptBehaviorUrl(RunScenarioModel runScenarioModel) {
public List<String> getScriptBehaviorUrl(RunScenarioModel runScenarioModel) {
List<BehaviorModel> behaviorModelsWithUrl = getBehaviorModelsHasUrl(runScenarioModel);
if (behaviorModelsWithUrl == null) List<BehaviorModel> behaviorModelsWithUrl = getBehaviorModelsHasUrl(runScenarioModel);
return null; if (behaviorModelsWithUrl == null)
List<String> urList = new ArrayList<String>(); return null;
for (BehaviorModel behaviorModelWithUrl : behaviorModelsWithUrl) { List<String> urList = new ArrayList<String>();
String url = generateUrl(behaviorModelWithUrl.getParameters()); for (BehaviorModel behaviorModelWithUrl : behaviorModelsWithUrl) {
urList.add(url); String url = generateUrl(behaviorModelWithUrl.getParameters());
} urList.add(url);
return urList; }
} return urList;
}
public Map<String, Integer> getBehaviorNameAndIdMap(
RunScenarioModel runScenarioModel) { public Map<String, Integer> getBehaviorNameAndIdMap(
try { RunScenarioModel runScenarioModel) {
Map<String, Integer> map = new HashMap<String, Integer>(); try {
List<BehaviorModel> behaviorModels = this Map<String, Integer> map = new HashMap<String, Integer>();
.getBehaviorModelsHasUrl(runScenarioModel); List<BehaviorModel> behaviorModels = this
if (behaviorModels == null) .getBehaviorModelsHasUrl(runScenarioModel);
return null; if (behaviorModels == null)
for (BehaviorModel behaviorModel : behaviorModels) { return null;
String url = this.generateUrl(behaviorModel.getParameters()); for (BehaviorModel behaviorModel : behaviorModels) {
if (url != null) String url = this.generateUrl(behaviorModel.getParameters());
map.put(url, behaviorModel.getId()); if (url != null)
} map.put(url, behaviorModel.getId());
return map; }
return map;
} catch (NullPointerException e) {
e.printStackTrace(); } catch (NullPointerException e) {
return null; e.printStackTrace();
} return null;
} }
}
private String generateUrl(List<ParameterModel> parameterModels) {
String url = new String(""); private String generateUrl(List<ParameterModel> parameterModels) {
int i = 0; String url = new String("");
for (ParameterModel parameterModel : parameterModels) { int i = 0;
if (i == 1 && parameterModel.getValue() != null) for (ParameterModel parameterModel : parameterModels) {
url += "?" + parameterModel.getValue(); if (i == 1 && parameterModel.getValue() != null)
if (i > 1 && parameterModel.getValue() != null) url += "?" + parameterModel.getValue();
url += "&" + parameterModel.getValue(); if (i > 1 && parameterModel.getValue() != null)
i++; url += "&" + parameterModel.getValue();
} i++;
if (url.length() <= 0) }
return null; if (url.length() <= 0)
return url; return null;
} return url;
}
private List<BehaviorModel> getBehaviorModelsHasUrl(
RunScenarioModel runScenarioModel) { private List<BehaviorModel> getBehaviorModelsHasUrl(
try { RunScenarioModel runScenarioModel) {
try {
List<BehaviorModel> behaviorModels = getBehaviorModels(runScenarioModel);
if (behaviorModels == null) List<BehaviorModel> behaviorModels = getBehaviorModels(runScenarioModel);
return null; if (behaviorModels == null)
List<BehaviorModel> behaviorModelsWithUrl = new ArrayList<BehaviorModel>(); return null;
for (BehaviorModel behaviorModel : behaviorModels) { List<BehaviorModel> behaviorModelsWithUrl = new ArrayList<BehaviorModel>();
List<ParameterModel> parameterModels = new ArrayList<ParameterModel>(); for (BehaviorModel behaviorModel : behaviorModels) {
parameterModels.addAll(behaviorModel.getParameters()); List<ParameterModel> parameterModels = new ArrayList<ParameterModel>();
for (ParameterModel parameterModel : parameterModels) { parameterModels.addAll(behaviorModel.getParameters());
if (parameterModel.getKey().equals("url")) for (ParameterModel parameterModel : parameterModels) {
behaviorModelsWithUrl.add(behaviorModel); if (parameterModel.getKey().equals("url"))
break; behaviorModelsWithUrl.add(behaviorModel);
} break;
} }
}
return behaviorModelsWithUrl;
} catch (NullPointerException e) { return behaviorModelsWithUrl;
e.printStackTrace(); } catch (NullPointerException e) {
return null; e.printStackTrace();
} return null;
} }
}
private List<BehaviorModel> getBehaviorModels(
RunScenarioModel runScenarioModel) { private List<BehaviorModel> getBehaviorModels(
try { RunScenarioModel runScenarioModel) {
List<BatchModel> batchModels = new ArrayList<BatchModel>(); try {
for (PageModel pageModel : runScenarioModel.getPages()) List<BatchModel> batchModels = new ArrayList<BatchModel>();
batchModels.addAll(pageModel.getBatches()); for (PageModel pageModel : runScenarioModel.getPages())
List<BehaviorModel> behaviorModels = new ArrayList<BehaviorModel>(); batchModels.addAll(pageModel.getBatches());
for (BatchModel batchModel : batchModels) List<BehaviorModel> behaviorModels = new ArrayList<BehaviorModel>();
behaviorModels.addAll(batchModel.getBehaviors()); for (BatchModel batchModel : batchModels)
return behaviorModels; behaviorModels.addAll(batchModel.getBehaviors());
} catch (NullPointerException e) { return behaviorModels;
e.printStackTrace(); } catch (NullPointerException e) {
return null; e.printStackTrace();
} return null;
} }
}
}
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;
}
} }