add scriptController

This commit is contained in:
fanfuxiaoran 2014-05-15 15:14:19 +08:00
parent e3c672a1ef
commit 9783574d0e
18 changed files with 541 additions and 1151 deletions

View File

@ -66,7 +66,7 @@ public class DeviceController extends BaseController {
logger.info(scenarioModel); logger.info(scenarioModel);
OperateScriptServerResponseModel operateScriptServerResponseModel = this OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getScriptMessager().uploadScript(accessToken, .getScriptMessager().uploadScript(accessToken,
script.getName(), scenarioModel, null, null); script.getName(), scenarioModel, null);
if (operateScriptServerResponseModel.isSuccess()) { if (operateScriptServerResponseModel.isSuccess()) {
return success(map); return success(map);
} else { } else {

View File

@ -1,34 +1,20 @@
package org.bench4q.web.api; package org.bench4q.web.api;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
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.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.ExceptionLog; import org.bench4q.share.helper.ExceptionLog;
import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.helper.MarshalHelper;
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.PageModel; import org.bench4q.share.models.agent.scriptrecord.PageModel;
import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; 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.PluginEditScriptRequestModel;
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;
import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange; import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
@ -40,14 +26,12 @@ 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.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.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
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;
import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.xml.sax.SAXException;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -330,69 +314,6 @@ public class ScriptActionController {
} }
@RequestMapping("validateScript")
public @ResponseBody
String validateScript(@ModelAttribute("accessToken") String accessToken,
@RequestParam String content) {
String result;
String message = "";
String schemaFileName = "src/main/webapp/WEB-INF/validateScript.xsd";
content = content.replace("runScenario", "runscenario")
.replace("poolSize", "poolsize")
.replace("usePlugin", "useplugin")
.replace("userBehavior", "userbehavior")
.replace("timerBehavior", "timerbehavior")
.replace("userbehaviors", "behaviors");
try {
result = validateXmlWithSchema(schemaFileName, content);
if (result == "true") {
message = "Success";
} else {
message = URLEncoder.encode(result, "UTF-8").replace("+", "");
}
} catch (Exception e) {
System.out.println("analysis of failure:" + e.toString());
message = "failed to read schema file";
}
// System.out.println(message);
return message;
}
private String validateXmlWithSchema(String schemaFileName, String xmlScript)
throws SAXException, IOException {
String validationResult = "";
SchemaFactory schemaFactory = SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema");
File schemaFile = new File(schemaFileName);
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
InputStream script = new ByteArrayInputStream(xmlScript.getBytes());
Source source = new StreamSource(script);
try {
// System.out.println("enter validateXmlWithSchema");
validator.validate(source);
validationResult = "true";
} catch (Exception ex) {
validationResult = ex.toString();
}
return validationResult;
}
@RequestMapping("readSchemaContent")
public @ResponseBody
String readSchemaContent() throws Exception {
String schema = "";
FileReader fr = new FileReader("src/main/webapp/WEB-INF/schema.json");
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 = "uploadEditedScipt", method = RequestMethod.POST) @RequestMapping(value = "uploadEditedScipt", method = RequestMethod.POST)
@ResponseBody @ResponseBody
public BaseResponseModel uploadEditScript( public BaseResponseModel uploadEditScript(
@ -450,60 +371,6 @@ public class ScriptActionController {
return true; return true;
} }
@RequestMapping(value = "uploadPluginEditedScript", method = RequestMethod.POST)
@ResponseBody
public BaseResponseModel uploadPluginEditedScript(
@ModelAttribute("accessToken") String accessToken,
@RequestBody String content) throws CustomGenericException {
Gson gson = new Gson();
Logger.getLogger(ScriptActionController.class).info(content);
PluginEditScriptRequestModel pluginEditScriptRequestModel = null;
try {
pluginEditScriptRequestModel = gson.fromJson(content,
PluginEditScriptRequestModel.class);
} catch (Exception e) {
Logger.getLogger(ScriptActionController.class).info(
ExceptionLog.getStackTrace(e));
}
List<BehaviorModel> behaviorModelsList = pluginEditScriptRequestModel
.getBehaviorModels();
List<UsePluginModel> usePluginModelsList = pluginEditScriptRequestModel
.getUsePluginModels();
String scriptName = pluginEditScriptRequestModel.getScriptName();
if (!validateScript(behaviorModelsList, usePluginModelsList))
return new BaseResponseModel(false, "invalidate script");
RunScenarioModel runScenarioModel = this
.getScriptService()
.createRunScenarioModel(behaviorModelsList, usePluginModelsList);
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

@ -13,7 +13,6 @@ import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.OperateScriptServerResponseModel; import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
@Component @Component
public class ScriptMessager extends MasterMessager { public class ScriptMessager extends MasterMessager {
@ -54,8 +53,7 @@ public class ScriptMessager extends MasterMessager {
} }
public OperateScriptServerResponseModel uploadScript(String accessToken, public OperateScriptServerResponseModel uploadScript(String accessToken,
String scriptName, String scenarioModel, String scriptName, String scenarioModel, MultipartFile[] paramFiles) {
MultipartRequest multipartRequest,MultipartFile[] paramFiles) {
String url = this.getBaseUrl() + "/uploadScript" + "/" + scriptName; String url = this.getBaseUrl() + "/uploadScript" + "/" + scriptName;
List<String> stringPart = new LinkedList<String>(); List<String> stringPart = new LinkedList<String>();
stringPart.add(scenarioModel); stringPart.add(scenarioModel);
@ -97,7 +95,7 @@ public class ScriptMessager extends MasterMessager {
} }
public OperateScriptServerResponseModel loadScript(String accessToken) { public OperateScriptServerResponseModel loadScripts(String accessToken) {
String url = this.getBaseUrl() + "/loadScriptList"; String url = this.getBaseUrl() + "/loadScriptList";
return this.getOperateScriptServerResponseModelByPost(url, null, return this.getOperateScriptServerResponseModelByPost(url, null,
accessToken); accessToken);
@ -108,8 +106,17 @@ public class ScriptMessager extends MasterMessager {
String url = this.getBaseUrl() + "/queryScriptById"; String url = this.getBaseUrl() + "/queryScriptById";
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put("scriptId", scriptId); params.put("scriptId", scriptId);
return this.getOperateScriptServerResponseModelByPost(url, params, OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getOperateScriptServerResponseModelByPost(url, params,
accessToken); accessToken);
if (!operateScriptServerResponseModel.isSuccess()
|| operateScriptServerResponseModel.getScriptModels().size() != 1) {
operateScriptServerResponseModel.setSuccess(false);
operateScriptServerResponseModel
.setFailCauseString("error get script by id:" + scriptId);
}
return operateScriptServerResponseModel;
} }
public OperateScriptServerResponseModel queryScriptByName( public OperateScriptServerResponseModel queryScriptByName(

View File

@ -62,10 +62,11 @@ public class MonitorController extends BaseController {
cpuInstance, fieldName); cpuInstance, fieldName);
if (resultModels == null) { if (resultModels == null) {
return fail(map, SERVER_ERROR); return fail(map, SERVER_ERROR);
} } else {
success(map); success(map);
map.put("cpuInfo", resultModels); map.put("cpuInfo", resultModels);
return map; return map;
}
} }
@ -90,9 +91,11 @@ public class MonitorController extends BaseController {
monitorMemoryResponseModel.getMemoryModels(), fieldName); monitorMemoryResponseModel.getMemoryModels(), fieldName);
if (resultModels == null) { if (resultModels == null) {
return fail(map, SERVER_ERROR); return fail(map, SERVER_ERROR);
} } else {
map = success(map); map = success(map);
map.put("memInfo", resultModels); map.put("memInfo", resultModels);
return map; return map;
} }
}
} }

View File

@ -52,16 +52,19 @@ public class PluginController extends BaseController {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
PluginResponseModel pluginResponseModel = this.getPluginMessager() PluginResponseModel pluginResponseModel = this.getPluginMessager()
.loadPluginNameList(accessToken); .loadPluginNameList(accessToken);
if (pluginResponseModel.isSuccess()) { if (!pluginResponseModel.isSuccess()) {
map = success(map); map = success(map);
map.put("pluginNames", pluginResponseModel.getPluginList()); map.put("pluginNames", pluginResponseModel.getPluginList());
return map;
} else { } else {
map = fail(map, pluginResponseModel.getFailMessage()); map = fail(map, pluginResponseModel.getFailMessage());
}
return map; return map;
} }
}
@RequestMapping("loadPluginUIList") @RequestMapping("loadPluginUIList")
public @ResponseBody public @ResponseBody
Map<String, Object> loadPluginUIInfo(HttpServletRequest request, Map<String, Object> loadPluginUIInfo(HttpServletRequest request,

View File

@ -0,0 +1,156 @@
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;
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;
import org.springframework.web.multipart.MultipartFile;
@SessionAttributes({ "accessToken", "username", "testPlanTaskList" })
public class ScriptController extends BaseController {
private ScriptMessager scriptMessager;
public ScriptMessager getScriptMessager() {
return scriptMessager;
}
@Autowired
public void setScriptMessager(ScriptMessager scriptMessager) {
this.scriptMessager = scriptMessager;
}
@RequestMapping("loadScripts")
@ResponseBody
public Map<String, Object> loadScript(
@ModelAttribute("accessToken") String accessToken) {
Map<String, Object> map = new HashMap<String, Object>();
OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getScriptMessager().loadScripts(accessToken);
if (!operateScriptServerResponseModel.isSuccess()) {
return fail(map,
operateScriptServerResponseModel.getFailCauseString());
} else {
map = success(map);
map.put("scripts",
operateScriptServerResponseModel.getScriptModels());
return map;
}
}
@RequestMapping("deleteScript")
@ResponseBody
public Map<String, Object> deleteScript(
@ModelAttribute("accessToken") String accessToken, String scriptId) {
Map<String, Object> map = new HashMap<String, Object>();
return processScriptResponse(map, this.getScriptMessager()
.deleteScript(accessToken, scriptId));
}
@RequestMapping("getScript")
@ResponseBody
public Map<String, Object> loadScriptContent(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId) {
Map<String, Object> map = new HashMap<String, Object>();
OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getScriptMessager().queryScriptById(accessToken, scriptId);
if (operateScriptServerResponseModel.isSuccess()) {
map = success(map);
return map;
} else {
map = fail(map,
operateScriptServerResponseModel.getFailCauseString());
return map;
}
}
@RequestMapping("uploadScript")
@ResponseBody
public Map<String, Object> saveScript(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptName, @RequestParam String script,
@RequestParam(required = false) MultipartFile[] paramFiles) {
return processScriptResponse(
new HashMap<String, Object>(),
this.getScriptMessager().uploadScript(accessToken, scriptName,
script, paramFiles));
}
@RequestMapping("updateScript")
@ResponseBody
public Map<String, Object> updateScript(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId, @RequestParam String script) {
return processScriptResponse(new HashMap<String, Object>(), this
.getScriptMessager()
.updateScript(accessToken, scriptId, script));
}
@RequestMapping("startRecordServer")
@ResponseBody
public Map<String, Object> startRecordServer(
@ModelAttribute("accessToken") String accessToken) {
Map<String, Object> map = new HashMap<String, Object>();
OperateScriptServerResponseModel operateScriptServerResponseModel = this
.getScriptMessager().startScriptRecordServer(accessToken);
if (operateScriptServerResponseModel.isSuccess()) {
map = success(map);
map.put("port", operateScriptServerResponseModel.getPort());
return map;
} else {
map = fail(map,
operateScriptServerResponseModel.getFailCauseString());
return map;
}
}
@RequestMapping("stopRecordServer")
@ResponseBody
public Map<String, Object> stopRecordServer(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String port, @RequestParam String scriptRecordUUID) {
return processScriptResponse(
new HashMap<String, Object>(),
this.getScriptMessager().stopScriptRecordServer(accessToken,
port, scriptRecordUUID));
}
@RequestMapping("saveScriptRecorded")
@ResponseBody
public Map<String, Object> saveScriptRecorded(
@ModelAttribute("accessToken") String accessToken,
@RequestParam String port, @RequestParam String scriptRecordUUID,
@RequestParam String scriptName) {
Map<String, Object> map = new HashMap<String, Object>();
return processScriptResponse(
map,
this.getScriptMessager().saveScriptRecorded(accessToken,
scriptName, port, scriptRecordUUID));
}
@RequestMapping()
private Map<String, Object> processScriptResponse(Map<String, Object> map,
OperateScriptServerResponseModel operateScriptServerResponseModel) {
if (operateScriptServerResponseModel.isSuccess()) {
return success(map);
} else {
map = fail(map,
operateScriptServerResponseModel.getFailCauseString());
return map;
}
}
}

View File

@ -56,10 +56,12 @@ public class UserController extends BaseController {
RegisterResponseModel registerResponseModel = this.getUserMessager() RegisterResponseModel registerResponseModel = this.getUserMessager()
.register(userName, password, scope); .register(userName, password, scope);
if (!registerResponseModel.isSuccess()) { if (!registerResponseModel.isSuccess()) {
fail(map, "please try again");
}
return success(map); return success(map);
} else {
return fail(map, "please try again");
}
} }
@RequestMapping("normalLogin") @RequestMapping("normalLogin")
@ -75,12 +77,13 @@ public class UserController extends BaseController {
} }
AuthorizeResponseModel authorizeResponseModel = this.getUserMessager() AuthorizeResponseModel authorizeResponseModel = this.getUserMessager()
.normalLogin(userModel.getUserName(), userModel.getPassword()); .normalLogin(userModel.getUserName(), userModel.getPassword());
if (!authorizeResponseModel.isSuccess()) { if (authorizeResponseModel.isSuccess()) {
return fail(map, "error userName or password");
} else {
updateSessionAttribute(modelMap, authorizeResponseModel, updateSessionAttribute(modelMap, authorizeResponseModel,
userModel.getUserName()); userModel.getUserName());
return success(map); return success(map);
} else {
return fail(map, "error userName or password");
} }
} }
@ -97,12 +100,13 @@ public class UserController extends BaseController {
} }
AuthorizeResponseModel authorizeResponseModel = this.getUserMessager() AuthorizeResponseModel authorizeResponseModel = this.getUserMessager()
.adminLogin(userModel.getUserName(), userModel.getPassword()); .adminLogin(userModel.getUserName(), userModel.getPassword());
if (!authorizeResponseModel.isSuccess()) { if (authorizeResponseModel.isSuccess()) {
return fail(map, "error userName or password");
} else {
updateSessionAttribute(modelMap, authorizeResponseModel, updateSessionAttribute(modelMap, authorizeResponseModel,
userModel.getUserName()); userModel.getUserName());
return success(map); return success(map);
} else {
return fail(map, "error userName or password");
} }
} }

View File

@ -1,5 +1,4 @@
package org.bench4q.web.validation; package org.bench4q.web.validation;
public class ScriptValidate { public class ScriptValidate {
} }

View File

@ -1 +1 @@
masterAddress=133.133.12.1:7979/ masterAddress=127.0.0.1:7979/

View File

@ -553,12 +553,14 @@
font-family: arial, sans-serif; font-family: arial, sans-serif;
font-size: 10pt; font-size: 10pt;
color: #1A1A1A; color: #1A1A1A;
margin-left:100px;
} }
.jsoneditor .search { .jsoneditor .search {
position: absolute; position: absolute;
right: 2px; right: 2px;
top: 2px; top: 2px;
} }
.jsoneditor .search .frame { .jsoneditor .search .frame {

View File

@ -133,13 +133,13 @@
<hr> <hr>
<jsp:include page="publiccontrol/footer.jsp"></jsp:include> <jsp:include page="publiccontrol/footer.jsp"></jsp:include>
<script <script
src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.0.min.js"></script> src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.0.min.js"></script>
<script <script
src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.4/jquery-ui.js"></script> src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.4/jquery-ui.js"></script>
<script <script
src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script> src='http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js'></script>
<script <script
src="http://ajax.aspnetcdn.com/ajax/bootstrap/2.3.1/bootstrap.min.js"></script> src="http://ajax.aspnetcdn.com/ajax/bootstrap/2.3.1/bootstrap.min.js"></script>
<script src="lib/jqueryi18n/jquery.i18n.properties-1.0.9.js"></script> <script src="lib/jqueryi18n/jquery.i18n.properties-1.0.9.js"></script>

View File

@ -1,63 +0,0 @@
package org.bench4q.web.api.test;
import org.bench4q.share.models.master.UserModel;
import org.bench4q.web.api.AuthorizeActionController;
import org.bench4q.web.exception.CustomGenericException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.ui.ModelMap;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/bench4qweb-servlet.xml" })
public class AuthorizeActionControllerTest {
@Autowired
private AuthorizeActionController authorizeActionController;
private ModelMap modelMap;
private UserModel user;
@Before
public void setUp() {
this.modelMap = new ModelMap();
this.user = new UserModel();
}
@Test
public void loginTest() throws CustomGenericException {
// right
this.user.setUserName("www");
this.user.setPassword("www");
Assert.assertTrue(this.authorizeActionController.login(user, modelMap)
.isSuccess());
this.user.setUserName("www");
this.user.setPassword("123");
Assert.assertFalse(this.authorizeActionController.login(user, modelMap)
.isSuccess());
}
@Test
public void adminLoginTest() {
try {
// right
this.user.setUserName("www");
this.user.setPassword("www");
this.user.setUserName("www");
this.user.setPassword("123");
Assert.assertFalse(this.authorizeActionController.adminLogin(user,
modelMap).isSuccess());
Assert.assertFalse(this.authorizeActionController.adminLogin(null,
modelMap).isSuccess());
} catch (CustomGenericException e) {
e.printStackTrace();
}
}
}

View File

@ -1,87 +0,0 @@
package org.bench4q.web.api.test;
import java.util.ArrayList;
import java.util.List;
import org.bench4q.share.models.master.ScriptModel;
import org.bench4q.web.TestPlan.RunTestPlan;
import org.bench4q.web.api.ScriptActionController;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.model.TestPlanRequestModel;
import org.bench4q.web.model.WebScriptModel;
import org.bench4q.web.tool.test.LoginHelper;
import org.junit.Before;
import org.junit.Test;
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" })
public class RunTestPlanTest {
private ScriptActionController scriptActionController;
private RunTestPlan runTestPlan;
private LoginHelper loginHelper;
private String accessToken;
private ScriptActionController getScriptActionController() {
return scriptActionController;
}
@Autowired
private void setScriptActionController(
ScriptActionController scriptActionController) {
this.scriptActionController = scriptActionController;
}
private RunTestPlan getRunTestPlan() {
return runTestPlan;
}
@Autowired
private void setRunTestPlan(RunTestPlan runTestPlan) {
this.runTestPlan = runTestPlan;
}
public LoginHelper getLoginHelper() {
return loginHelper;
}
@Autowired
public void setLoginHelper(LoginHelper loginHelper) {
this.loginHelper = loginHelper;
}
@Before
public void setUp() {
this.accessToken = this.getLoginHelper().login();
}
@SuppressWarnings("unchecked")
@Test
public void runTestPlanTest() throws CustomGenericException {
TestPlanRequestModel testPlanRequestModel = new TestPlanRequestModel();
WebScriptModel webScriptModel = new WebScriptModel();
webScriptModel.setCooldown(1);
webScriptModel.setWarmup(1);
webScriptModel.setLoad(100);
webScriptModel.setExecuteRange(50);
List<ScriptModel> scripts = (List<ScriptModel>) this
.getScriptActionController().loadScript(accessToken).getData();
webScriptModel.setId(scripts.get(0).getId());
List<WebScriptModel> webScriptModels = new ArrayList<WebScriptModel>();
webScriptModels.add(webScriptModel);
testPlanRequestModel.setScriptList(webScriptModels);
this.getRunTestPlan().runTestPlan(accessToken, testPlanRequestModel,
new ModelMap());
testPlanRequestModel.setTestPlanName("test");
testPlanRequestModel.setIpList(new ArrayList<String>());
}
}

View File

@ -1,120 +0,0 @@
package org.bench4q.web.api.test;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
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.PluginEditScriptRequestModel;
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;
import org.bench4q.web.tool.test.LoginHelper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
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 com.google.gson.Gson;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/resources/bench4qweb-servlet.xml" })
public class ScriptActionControllerTest {
private ScriptActionController scriptActionController;
private String accessToken;
private LoginHelper loginHelper;
public ScriptActionController getScriptActionController() {
return scriptActionController;
}
@Autowired
public void setScriptActionController(
ScriptActionController scriptActionController) {
this.scriptActionController = scriptActionController;
}
public LoginHelper getLoginHelper() {
return loginHelper;
}
@Autowired
public void setLoginHelper(LoginHelper loginHelper) {
this.loginHelper = loginHelper;
}
@Before
public void setUp() {
this.accessToken = this.getLoginHelper().login();
}
@Test
public void loadScriptsTest() throws CustomGenericException {
BaseResponseModel baseResponseModel = this.getScriptActionController()
.loadScript(this.accessToken);
if (baseResponseModel.isSuccess())
Assert.assertNotNull(baseResponseModel.getData());
@SuppressWarnings("unchecked")
List<ScriptModel> scriptModels = (List<ScriptModel>) baseResponseModel
.getData();
Logger.getLogger(ScriptActionControllerTest.class).info(
scriptModels.size());
assertTrue(scriptModels.size() > 0);
}
@Test
public void uploadPluginEditedScript() throws CustomGenericException {
@SuppressWarnings("unchecked")
int insertCountBefore = ((List<ScriptModel>) scriptActionController
.loadScript(accessToken).getData()).size();
PluginEditScriptRequestModel pluginEditScriptRequestModel = new PluginEditScriptRequestModel();
pluginEditScriptRequestModel
.setBehaviorModels(createBheBehaviorModels());
pluginEditScriptRequestModel
.setUsePluginModels(createUsePluginModels());
pluginEditScriptRequestModel.setScriptName("testScriptName");
Gson gson = new Gson();
BaseResponseModel baseResponseModel = scriptActionController
.uploadPluginEditedScript(accessToken,
gson.toJson(pluginEditScriptRequestModel));
@SuppressWarnings("unchecked")
int insertCountAfter = ((List<ScriptModel>) scriptActionController
.loadScript(accessToken).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;
}
}

View File

@ -1,112 +0,0 @@
package org.bench4q.web.api.test;
import static org.junit.Assert.*;
import java.util.List;
import javax.xml.bind.JAXBException;
import junit.framework.Assert;
import org.apache.log4j.Logger;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.web.api.MonitorController;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.model.BaseResponseModel;
import org.bench4q.web.model.ResultModel;
import org.bench4q.web.model.WebTestPlanResultModel;
import org.bench4q.web.tool.test.GsonHelper;
import org.bench4q.web.tool.test.LoginHelper;
import org.bench4q.web.tool.test.TestPlanHelper;
import org.junit.Before;
import org.junit.Test;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/bench4qweb-servlet.xml" })
public class TestMonitorResult extends LoginHelper {
private MonitorController monitorController;
private TestPlanHelper startTestPlan;
private String accessToken;
private String testPlanId;
private Logger logger = Logger.getLogger(TestMonitorResult.class);
public MonitorController getMonitorController() {
return monitorController;
}
@Autowired
public void setMonitorController(MonitorController monitorController) {
this.monitorController = monitorController;
}
public TestPlanHelper getStartTestPlan() {
return startTestPlan;
}
@Autowired
public void setStartTestPlan(TestPlanHelper startTestPlan) {
this.startTestPlan = startTestPlan;
}
@Before
public void setUp() {
this.accessToken = login();
this.getStartTestPlan().startTest(accessToken);
this.testPlanId = this.getStartTestPlan().getTestPlanRunId();
}
@SuppressWarnings("unchecked")
@Test
public void test_getMemoryResult() throws InterruptedException,
CustomGenericException, JAXBException {
testTestPlanStatus();
BaseResponseModel baseResponseModel = this.getMonitorController()
.getMemoryStatus(accessToken, testPlanId,
TestPlanHelper.MONITOR_IP, "5556", "pagesPerSecond",
"0");
assertTrue(baseResponseModel.isSuccess());
List<ResultModel> resultModels = (List<ResultModel>) baseResponseModel
.getData();
logger.info(GsonHelper.getGson().toJson(resultModels));
assertTrue(resultModels.size() > 0);
}
@Test
public void test_getCpuResult() throws InterruptedException, CustomGenericException, JAXBException{
testTestPlanStatus();
BaseResponseModel baseResponseModel = this.getMonitorController()
.getCpuResult(accessToken, testPlanId, TestPlanHelper.MONITOR_IP, "5556", "cpu0", "processorTimePercent", "0");
assertTrue(baseResponseModel.isSuccess());
@SuppressWarnings("unchecked")
List<ResultModel> resultModels = (List<ResultModel>) baseResponseModel
.getData();
logger.info(GsonHelper.getGson().toJson(resultModels));
assertTrue(resultModels.size() > 0);
}
@Test
public void test_getNetwork() throws InterruptedException, CustomGenericException, JAXBException{
testTestPlanStatus();
BaseResponseModel baseResponseModel = this.getMonitorController()
.getCpuResult(accessToken, testPlanId, TestPlanHelper.MONITOR_IP, "5556", "cpu0", "processorTimePercent", "0");
assertTrue(baseResponseModel.isSuccess());
@SuppressWarnings("unchecked")
List<ResultModel> resultModels = (List<ResultModel>) baseResponseModel
.getData();
logger.info(GsonHelper.getGson().toJson(resultModels));
assertTrue(resultModels.size() > 0);
}
private void testTestPlanStatus() throws InterruptedException,
CustomGenericException, JAXBException {
Assert.assertNotNull(this.getStartTestPlan().getTestPlanRunId());
assertNotNull(testPlanId);
Thread.sleep(6000);
WebTestPlanResultModel webTestPlanResultModel = this.getStartTestPlan()
.queryTestPlanResultModel(testPlanId, accessToken);
assertEquals(TestPlanStatus.InRunning,
webTestPlanResultModel.getCurrentStatus());
}
}

View File

@ -1,217 +0,0 @@
package org.bench4q.web.api.test;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBException;
import junit.framework.Assert;
import org.apache.log4j.Logger;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.helper.ExceptionLog;
import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel;
import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel;
import org.bench4q.web.api.TestPlanActionController;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.model.BaseResponseModel;
import org.bench4q.web.model.ResultModel;
import org.bench4q.web.model.SutInfo;
import org.bench4q.web.model.WebTestPlanResultModel;
import org.bench4q.web.service.TestPlanService;
import org.bench4q.web.tool.test.GsonHelper;
import org.bench4q.web.tool.test.LoginHelper;
import org.bench4q.web.tool.test.TestPlanHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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 com.google.gson.Gson;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/bench4qweb-servlet.xml" })
public class TestScriptResultTest extends LoginHelper {
private TestPlanActionController testPlanActionController;
private TestPlanHelper testPlanHelper;
private String testPlanId;
private String accessToken;
private Logger logger = Logger.getLogger(TestScriptResultTest.class);
private TestPlanService testPlanService;
private String scriptId;
public TestPlanActionController getTestPlanActionController() {
return testPlanActionController;
}
@Autowired
public void setTestPlanActionController(
TestPlanActionController testPlanActionController) {
this.testPlanActionController = testPlanActionController;
}
public TestPlanHelper getStartTestPlan() {
return testPlanHelper;
}
@Autowired
public void setStartTestPlan(TestPlanHelper startTestPlan) {
this.testPlanHelper = startTestPlan;
}
public TestPlanService getTestPlanService() {
return testPlanService;
}
@Autowired
public void setTestPlanService(TestPlanService testPlanService) {
this.testPlanService = testPlanService;
}
@Before
public void setUp() {
this.accessToken = this.login();
this.testPlanHelper.startTest(accessToken);
this.testPlanId = this.testPlanHelper.getTestPlanRunId();
this.scriptId = this.getOneScriptId(testPlanId, accessToken);
}
@After
public void clean() throws CustomGenericException {
this.getStartTestPlan().cleanUpTest(this.accessToken);
}
@Test
public void test_getReplaceList() throws CustomGenericException,
JAXBException {
Assert.assertNotNull(this.getStartTestPlan().getTestPlanRunId());
BaseResponseModel baseResponseModel = this
.getTestPlanActionController().getReplaceList(this.accessToken,
this.testPlanHelper.getTestPlanRunId());
@SuppressWarnings("unchecked")
Map<String, Integer> scriptIdMap = (Map<String, Integer>) baseResponseModel
.getData();
Assert.assertTrue(scriptIdMap.size() > 0);
}
@Test
public void test_getTestPageBriefModel() throws InterruptedException,
CustomGenericException, JAXBException {
assertNotNull(this.testPlanId);
assertNotNull(this.scriptId);
Thread.sleep(3000);
WebTestPlanResultModel webTestPlanResultModel = this.getStartTestPlan()
.queryTestPlanResultModel(testPlanId, accessToken);
assertEquals(TestPlanStatus.InRunning,
webTestPlanResultModel.getCurrentStatus());
BaseResponseModel baseResponseModel = this
.getTestPlanActionController().getScriptPageBriefModel(
accessToken, testPlanId, scriptId);
ScriptPagesBriefModel scriptPagesBriefModel = (ScriptPagesBriefModel) baseResponseModel
.getData();
logger.info(GsonHelper.getGson().toJson(scriptPagesBriefModel));
assertTrue(scriptPagesBriefModel.getScriptPageBriefModels().size() > 0);
}
@SuppressWarnings("unchecked")
@Test
public void test_getScriptBriefStatus() throws NoSuchFieldException,
CustomGenericException, JAXBException, InterruptedException {
Assert.assertNotNull(testPlanId);
String scriptId = getOneScriptId(this.getStartTestPlan()
.getTestPlanRunId(), accessToken);
Assert.assertNotNull(scriptId);
Thread.sleep(3000);
BaseResponseModel baseResponseModelForTestPlan = this
.getTestPlanActionController().queryTestPlan(accessToken,
this.testPlanId);
assertTrue(baseResponseModelForTestPlan.isSuccess());
WebTestPlanResultModel webTestPlanResultModel = (WebTestPlanResultModel) baseResponseModelForTestPlan
.getData();
assertEquals(TestPlanStatus.InRunning,
webTestPlanResultModel.getCurrentStatus());
BaseResponseModel baseResponseModel = this
.getTestPlanActionController().getScriptBriefStatus(
accessToken, this.testPlanId,
this.getOneScriptId(this.testPlanId, accessToken),
"averageResponseTime", "0");
assertTrue(baseResponseModel.isSuccess());
List<ResultModel> resultModels = (List<ResultModel>) baseResponseModel
.getData();
Logger.getLogger("get result model");
Logger.getLogger(TestScriptResultTest.class).info(
"resultModel:" + GsonHelper.getGson().toJson(resultModels));
assertTrue(resultModels.size() > 0);
}
@Test
public void test_getScriptBehaviorBriefModel()
throws CustomGenericException, JAXBException, InterruptedException {
assertNotNull(this.testPlanId);
String scriptId = getOneScriptId(this.getStartTestPlan()
.getTestPlanRunId(), accessToken);
Thread.sleep(3000);
BaseResponseModel baseResponseModel = this
.getTestPlanActionController().getScriptBehaviorBriefModel(
accessToken, this.testPlanId, scriptId);
ScriptBehaviorsBriefModel scriptBehaviorsBriefModel = (ScriptBehaviorsBriefModel) baseResponseModel
.getData();
logger.info(GsonHelper.getGson().toJson(scriptBehaviorsBriefModel));
Assert.assertNotNull(scriptBehaviorsBriefModel);
Assert.assertNotNull(scriptBehaviorsBriefModel
.getTestBehaviorsBriefModel());
Assert.assertTrue(scriptBehaviorsBriefModel
.getTestBehaviorsBriefModel().getBehaviorBriefModels().size() > 0);
}
private String getOneScriptId(String testPlanRunId, String accessToken) {
try {
return this.getStartTestPlan()
.queryTestPlanResultModel(testPlanRunId, accessToken)
.getScriptIds().get(0).toString();
} catch (CustomGenericException e) {
logger.info(ExceptionLog.getStackTrace(e));
return null;
} catch (JAXBException e) {
logger.info(ExceptionLog.getStackTrace(e));
return null;
}
}
@SuppressWarnings("unchecked")
@Test
public void test_getSutInfo() throws CustomGenericException, JAXBException,
InterruptedException {
testTestPlanStatus();
List<SutInfo> sutInfos = (List<SutInfo>) this
.getTestPlanActionController()
.getSutInfo(accessToken, testPlanId).getData();
for (SutInfo sutInfo : sutInfos) {
assertEquals("5556", sutInfo.getPort());
assertEquals(TestPlanHelper.MONITOR_IP, sutInfo.getIp());
Gson gson = new Gson();
logger.info(gson.toJson(sutInfo));
}
assertEquals(1, sutInfos.size());
}
private void testTestPlanStatus() throws InterruptedException,
CustomGenericException, JAXBException {
Assert.assertNotNull(this.getStartTestPlan().getTestPlanRunId());
assertNotNull(testPlanId);
Thread.sleep(6000);
WebTestPlanResultModel webTestPlanResultModel = this.getStartTestPlan()
.queryTestPlanResultModel(testPlanId, accessToken);
assertEquals(TestPlanStatus.InRunning,
webTestPlanResultModel.getCurrentStatus());
}
}

View File

@ -1,52 +0,0 @@
package org.bench4q.web.api.test;
import static org.junit.Assert.*;
import org.apache.log4j.Logger;
import org.bench4q.web.api.TestPlanHistoryResult;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.model.BaseResponseModel;
import org.bench4q.web.model.TestPlanListModel;
import org.bench4q.web.tool.test.GsonHelper;
import org.bench4q.web.tool.test.LoginHelper;
import org.junit.Before;
import org.junit.Test;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/bench4qweb-servlet.xml" })
public class TestTestHistory extends LoginHelper {
private TestPlanHistoryResult testPlanHistoryResult;
private String accessToken;
private Logger logger = Logger.getLogger(TestTestHistory.class);
public TestPlanHistoryResult getTestPlanHistoryResult() {
return testPlanHistoryResult;
}
@Autowired
public void setTestPlanHistoryResult(
TestPlanHistoryResult testPlanHistoryResult) {
this.testPlanHistoryResult = testPlanHistoryResult;
}
@Before
public void setUp() {
this.accessToken = login();
}
@Test
public void test_loadTestHistory() throws CustomGenericException {
BaseResponseModel baseResponseModel = this.getTestPlanHistoryResult()
.loadTestPlans(accessToken);
logger.info(GsonHelper.getGson().toJson(baseResponseModel));
assertTrue(baseResponseModel.isSuccess());
TestPlanListModel testPlanListModel = (TestPlanListModel) baseResponseModel
.getData();
assertTrue(testPlanListModel.getList().size() > 0);
}
}