add script api and test api

This commit is contained in:
fanfuxiaoran 2014-05-15 17:31:02 +08:00
parent 9783574d0e
commit 5d7e8e995c
9 changed files with 319 additions and 13 deletions

View File

@ -314,7 +314,7 @@ public class ScriptActionController {
}
@RequestMapping(value = "uploadEditedScipt", method = RequestMethod.POST)
@RequestMapping(value = "updateScript", method = RequestMethod.POST)
@ResponseBody
public BaseResponseModel uploadEditScript(
@ModelAttribute("accessToken") String accessToken,
@ -371,6 +371,4 @@ public class ScriptActionController {
return true;
}
}

View File

@ -25,8 +25,9 @@ public class TestPlanMessager extends MasterMessager {
super("/testPlan");
}
public TestPlanResultModel submitTestPlan(String accessToken,
public TestPlanResultModel runTestPlan(String accessToken,
String testPlanXmlContent) {
String url = this.getBaseUrl() + "/runTestPlanWithTestPlanModel";
HttpResponse httpResponse = null;
try {

View File

@ -3,17 +3,21 @@ package org.bench4q.web.newapi;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
public abstract class BaseController {
public class BaseController {
private final String SUCCESS = "success";
private final String FAILMESSAGE = "failedMessage";
protected final String SERVER_ERROR = "server error";
private Logger logger=Logger.getLogger(BaseController.class);
protected Map<String, Object> fail(Map<String, Object> map, String message) {
if (map == null)
map = new HashMap<String, Object>();
map.put(SUCCESS, new Boolean(false));
map.put(FAILMESSAGE, message);
logger.info(message);
return map;
}

View File

@ -0,0 +1,77 @@
package org.bench4q.web.newapi;
import java.util.HashMap;
import java.util.Map;
import org.bench4q.share.models.master.OrganizeRecordPortResponseModel;
import org.bench4q.web.masterMessager.RecordPortMessager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
@SessionAttributes({ "accessToken" })
public class RecordPortController extends BaseController {
private RecordPortMessager recordPortMessager;
public RecordPortMessager getRecordPortMessager() {
return recordPortMessager;
}
@Autowired
public void setRecordPortMessager(RecordPortMessager recordPortMessager) {
this.recordPortMessager = recordPortMessager;
}
@RequestMapping("addPort")
@ResponseBody
public Map<String, Object> addPort(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String port) {
Map<String, Object> map = new HashMap<String, Object>();
return processRecordPortResponse(map, this.getRecordPortMessager()
.addPort(accessToken, port));
}
@RequestMapping("removePort")
@ResponseBody
public Map<String, Object> deletePort(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String port) {
Map<String, Object> map = new HashMap<String, Object>();
return processRecordPortResponse(map, this.getRecordPortMessager()
.deletePort(accessToken, port));
}
@RequestMapping("loadPorts")
@ResponseBody
public Map<String, Object> loadPorts(
@ModelAttribute("accessToken") String accessToken) {
Map<String, Object> map = new HashMap<String, Object>();
OrganizeRecordPortResponseModel organizeRecordPortResponseModel = this
.getRecordPortMessager().loadPorts(accessToken);
if (organizeRecordPortResponseModel.isSuccess()) {
map = success(map);
map.put("ports", organizeRecordPortResponseModel.getPortModels());
return map;
} else {
return fail(map,
organizeRecordPortResponseModel.getFailCauseString());
}
}
private Map<String, Object> processRecordPortResponse(
Map<String, Object> map,
OrganizeRecordPortResponseModel organizeRecordPortResponseModel) {
if (organizeRecordPortResponseModel.isSuccess()) {
return success(map);
} else {
return fail(map,
organizeRecordPortResponseModel.getFailCauseString());
}
}
}

View File

@ -1,8 +1,6 @@
package org.bench4q.web.newapi;
import java.util.HashMap;
import java.util.Map;
import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.bench4q.web.masterMessager.ScriptMessager;
import org.springframework.beans.factory.annotation.Autowired;
@ -12,8 +10,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
@SessionAttributes({ "accessToken", "username", "testPlanTaskList" })
@SessionAttributes({ "accessToken" })
public class ScriptController extends BaseController {
private ScriptMessager scriptMessager;
@ -140,8 +137,7 @@ public class ScriptController extends BaseController {
scriptName, port, scriptRecordUUID));
}
@RequestMapping()
private Map<String, Object> processScriptResponse(Map<String, Object> map,
OperateScriptServerResponseModel operateScriptServerResponseModel) {
if (operateScriptServerResponseModel.isSuccess()) {

View File

@ -0,0 +1,114 @@
package org.bench4q.web.newapi;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger;
import org.bench4q.share.helper.ExceptionLog;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.TestPlanModel;
import org.bench4q.share.models.master.TestPlanResultModel;
import org.bench4q.web.masterMessager.TestPlanMessager;
import org.bench4q.web.model.TestPlanRequestModel;
import org.bench4q.web.model.TestPlanTaskModel;
import org.bench4q.web.newservice.TestPlanService;
import org.bench4q.web.validation.TestPlanValidate;
import org.bench4q.web.validation.ValidateResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
@SessionAttributes({ "accessToken" })
public class TestPlanController extends BaseController {
private TestPlanMessager testPlanMessager;
private TestPlanValidate testPlanValidate;
private TestPlanService testPlanService;
private final int monitorPort = 5556;
private TestPlanMessager getTestPlanMessager() {
return testPlanMessager;
}
@Autowired
private void setTestPlanMessager(TestPlanMessager testPlanMessager) {
this.testPlanMessager = testPlanMessager;
}
private TestPlanValidate getTestPlanValidate() {
return testPlanValidate;
}
@Autowired
private void setTestPlanValidate(TestPlanValidate testPlanValidate) {
this.testPlanValidate = testPlanValidate;
}
private TestPlanService getTestPlanService() {
return testPlanService;
}
@Autowired
private void setTestPlanService(TestPlanService testPlanService) {
this.testPlanService = testPlanService;
}
// not add to testPlan taskList=====not need
@RequestMapping(value = "runTestPlan", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> runTestPlan(
@ModelAttribute("accessToken") String accessToken,
@RequestBody TestPlanRequestModel testPlanRequestModel) {
Map<String, Object> map = new HashMap<String, Object>();
ValidateResponseModel validateResponseModel = this
.getTestPlanValidate().validateTestPlanSubmited(
testPlanRequestModel);
if (!validateResponseModel.isSuccess()) {
return fail(map, validateResponseModel.getMessage());
} else {
TestPlanModel testPlanModel = this.getTestPlanService()
.createTestPlan(testPlanRequestModel, monitorPort);
String testPlanContent;
try {
testPlanContent = MarshalHelper.marshal(TestPlanModel.class,
testPlanModel);
} catch (JAXBException e) {
Logger.getLogger(TestPlanController.class).info(
ExceptionLog.getStackTrace(e));
return fail(map, "error testPlan");
}
TestPlanResultModel testPlanResultModel = this
.getTestPlanMessager().runTestPlan(accessToken,
testPlanContent);
if (testPlanResultModel.getTestPlanId() != null) {
map = success(map);
map.put("testPlanResult", testPlanResultModel);
return map;
} else {
return fail(map, "empty test plan id");
}
}
}
@RequestMapping(value = "testPlanTaskList", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> getTestPlanTaskList(
@ModelAttribute("accessToken") String accessToken,
@ModelAttribute("testPlanTaskList") List<TestPlanTaskModel> testPlanTaskModels,
ModelMap model) {
Map<String, Object> map = new HashMap<String, Object>();
return map;
}
}

View File

@ -0,0 +1,82 @@
package org.bench4q.web.newservice;
import java.util.ArrayList;
import java.util.List;
import org.bench4q.share.models.master.MonitorModel;
import org.bench4q.share.models.master.RunningScriptModel;
import org.bench4q.share.models.master.TestPlanModel;
import org.bench4q.share.models.master.TestPlanResultModel;
import org.bench4q.share.models.master.TestScriptConfig;
import org.bench4q.web.model.TestPlanRequestModel;
import org.bench4q.web.model.TestPlanTaskModel;
import org.bench4q.web.model.WebScriptModel;
import org.springframework.stereotype.Component;
@Component
public class TestPlanService {
public TestPlanModel createTestPlan(TestPlanRequestModel testPlan, int port) {
TestPlanModel testPlanModel = new TestPlanModel();
if (testPlan.getScriptList() != null
&& testPlan.getScriptList().size() > 0) {
List<RunningScriptModel> scriptList = createRunningScriptModel(testPlan
.getScriptList());
testPlanModel.setRunningScriptModels(scriptList);
}
if (testPlan.getIpList() != null && testPlan.getIpList().size() > 0) {
List<MonitorModel> ipList = createMonitorModel(
testPlan.getIpList(), port);
testPlanModel.setMonitorModels(ipList);
} else
testPlanModel.setMonitorModels(new ArrayList<MonitorModel>());
return testPlanModel;
}
private List<RunningScriptModel> createRunningScriptModel(
List<WebScriptModel> scriptParam) {
List<RunningScriptModel> scriptList = new ArrayList<RunningScriptModel>();
for (WebScriptModel scriptModel : scriptParam) {
RunningScriptModel runnningScriptModel = new RunningScriptModel();
runnningScriptModel.setScriptId(scriptModel.getId());
runnningScriptModel.setRequireLoad(scriptModel.getLoad());
TestScriptConfig testScriptConfig = new TestScriptConfig();
testScriptConfig.setCoolDown(scriptModel.getCooldown());
testScriptConfig.setExecuteRange(scriptModel.getExecuteRange());
testScriptConfig.setWarmUp(scriptModel.getWarmup());
runnningScriptModel.setConfig(testScriptConfig);
scriptList.add(runnningScriptModel);
}
return scriptList;
}
private List<MonitorModel> createMonitorModel(List<String> ipParam, int port) {
List<MonitorModel> ipList = new ArrayList<MonitorModel>();
MonitorModel monitorModel = new MonitorModel();
for (String ip : ipParam) {
monitorModel.setHostName(ip);
monitorModel.setPort(port);
ipList.add(monitorModel);
}
return ipList;
}
public TestPlanTaskModel extractTestPlanTaskModel(
TestPlanRequestModel testPlanRequestModel,
TestPlanResultModel testPlanResultModel) {
TestPlanTaskModel testPlanTaskModel;
if (testPlanResultModel.getTestPlanId() != null)
testPlanTaskModel = new TestPlanTaskModel(
testPlanRequestModel.getTestPlanName(), testPlanResultModel
.getTestPlanId().toString(), testPlanResultModel
.getCurrentStatus().toString());
else
testPlanTaskModel = new TestPlanTaskModel(
testPlanRequestModel.getTestPlanName(), "-1",
testPlanResultModel.getCurrentStatus().toString());
return testPlanTaskModel;
}
}

View File

@ -0,0 +1,34 @@
package org.bench4q.web.validation;
import org.bench4q.web.model.TestPlanRequestModel;
import org.bench4q.web.model.WebScriptModel;
import org.springframework.stereotype.Component;
@Component
public class TestPlanValidate {
public ValidateResponseModel validateTestPlanSubmited(
TestPlanRequestModel testPlanRequestModel) {
if (testPlanRequestModel.getScriptList() == null
|| testPlanRequestModel.getScriptList().size() == 0) {
return new ValidateResponseModel(false, "empty script in test");
}
for (WebScriptModel scriptModel : testPlanRequestModel.getScriptList()) {
if (!isValidateScriptModel(scriptModel)) {
return new ValidateResponseModel(false, "error script config");
}
}
return new ValidateResponseModel(true, "");
}
private boolean isValidateScriptModel(WebScriptModel scriptModel) {
if (scriptModel.getId() == 0)
return true;
if (scriptModel.getLoad() == 0)
return true;
if (scriptModel.getExecuteRange() == 0)
return true;
return false;
}
}

View File

@ -165,7 +165,7 @@ function submitScriptByAjax(scriptModel) {
formData.append("content", JSON.stringify(scriptModel));
$.ajax({
url : "uploadEditedScipt",
url : "uploadScript",
type : "POST",
contentType : false,
processData : false,