From 05dc5952b426fc4649c4bf5098bd237cf0753918 Mon Sep 17 00:00:00 2001 From: fanfuxiaoran <495538672@qq.com> Date: Wed, 26 Mar 2014 17:44:14 +0800 Subject: [PATCH] add test --- .../org/bench4q/web/TestPlan/RunTestPlan.java | 6 +- .../web/api/TestPlanActionController.java | 9 +- .../bench4q/web/service/ScriptService.java | 20 +- Bench4Q-Web/src/main/resources/script.xml | 235 +++++------------- .../bench4q/web/api/test/TestResultTest.java | 108 ++++---- .../bench4q/web/tool/test/LoginHelper.java | 21 +- .../bench4q/web/tool/test/ScriptHelper.java | 60 +++++ .../web/tool/test/ScriptReadedFromFile.java | 22 -- .../bench4q/web/tool/test/StartTestPlan.java | 103 ++++++++ 9 files changed, 311 insertions(+), 273 deletions(-) create mode 100644 Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/ScriptHelper.java delete mode 100644 Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/ScriptReadedFromFile.java create mode 100644 Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/StartTestPlan.java diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/TestPlan/RunTestPlan.java b/Bench4Q-Web/src/main/java/org/bench4q/web/TestPlan/RunTestPlan.java index 5aeb1af9..beecc884 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/TestPlan/RunTestPlan.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/TestPlan/RunTestPlan.java @@ -21,7 +21,7 @@ import org.springframework.ui.ModelMap; @Component public class RunTestPlan extends BaseService { - public void runTestPlan(String accessToken, + public TestPlanResultModel runTestPlan(String accessToken, TestPlanRequestModel testPlanRequestModel, ModelMap model) throws CustomGenericException { String url = "testPlan" + "/runTestPlanWithTestPlanModel"; @@ -37,14 +37,14 @@ public class RunTestPlan extends BaseService { .extractTestPlanTaskModel(testPlanRequestModel, testPlanResultModel); addToTestPlanTaskList(testPlanTaskModel, model); + return testPlanResultModel; } catch (JAXBException e) { throw new CustomGenericException("0", e.getMessage(), caller); } } - private TestPlanModel createTestPlan(TestPlanRequestModel testPlan, - int port) { + private TestPlanModel createTestPlan(TestPlanRequestModel testPlan, int port) { TestPlanModel testPlanModel = new TestPlanModel(); if (testPlan.getScriptList() != null && testPlan.getScriptList().size() > 0) { diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/api/TestPlanActionController.java b/Bench4Q-Web/src/main/java/org/bench4q/web/api/TestPlanActionController.java index 17158766..25c084e6 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/api/TestPlanActionController.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/api/TestPlanActionController.java @@ -13,6 +13,7 @@ import javax.xml.bind.JAXBException; import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; import org.bench4q.share.models.master.MonitorModel; import org.bench4q.share.models.master.ScriptModel; +import org.bench4q.share.models.master.TestPlanResultModel; import org.bench4q.share.models.master.TestPlanScriptBriefResultModel; import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel; import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; @@ -117,9 +118,9 @@ public class TestPlanActionController { throw new CustomGenericException("0", "run testplan request model is invalidate", caller); } - this.getRunTestPlan().runTestPlan(accessToken, testPlanRequestModel, - model); - return new BaseResponseModel(true); + TestPlanResultModel testPlanResultModel = this.getRunTestPlan() + .runTestPlan(accessToken, testPlanRequestModel, model); + return new BaseResponseModel(true, (Object) testPlanResultModel); } @RequestMapping(value = "testPlanTaskList", method = RequestMethod.POST) @@ -190,7 +191,7 @@ public class TestPlanActionController { RunningScriptInfoModel runningScriptInfoModel = new RunningScriptInfoModel(); runningScriptInfoModel.setScriptId(scriptId); String scriptName = this.getScriptService() - .queryScript(scriptId, accessToken).getName(); + .queryScriptById(scriptId, accessToken).getName(); runningScriptInfoModel.setScriptName(scriptName); runningScriptInfoModel .setChildResults(new ArrayList()); diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/service/ScriptService.java b/Bench4Q-Web/src/main/java/org/bench4q/web/service/ScriptService.java index aceb2acd..abcee39a 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/service/ScriptService.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/service/ScriptService.java @@ -31,7 +31,7 @@ public class ScriptService extends BaseService { this.baseUrl = baseUrl; } - public ScriptModel queryScript(int id, String accessToken) + public ScriptModel queryScriptById(int id, String accessToken) throws CustomGenericException { // check script id and return throw exception String caller = "ScriptService:queryScript"; @@ -48,6 +48,20 @@ public class ScriptService extends BaseService { + ":" + id); } + public ScriptModel queryScriptByName(String scriptName, String accessToken) + throws CustomGenericException { + OperateScriptServerResponseModel operateScriptServerResponseModel = (OperateScriptServerResponseModel) this + .getCommunicateWithMaster().getResponseModel(accessToken, + this.getBaseUrl() + "/queryScriptByName", + OperateScriptServerResponseModel.class, + makeParamsMap("name", scriptName), ""); + if (operateScriptServerResponseModel.isSuccess()) + return operateScriptServerResponseModel.getScriptModels().get(0); + else + throw new CustomGenericException("5", "query script failed", "" + + ":" + scriptName); + } + public List queryScripts(List scriptIds, String accessToken) throws CustomGenericException { String caller = "ScriptService:queryScript"; @@ -283,8 +297,8 @@ public class ScriptService extends BaseService { return batchModel; } - public OperateScriptServerResponseModel uploadScript(String accessToken, String scenarioModel, - String scriptName) + public OperateScriptServerResponseModel uploadScript(String accessToken, + String scenarioModel, String scriptName) throws CustomGenericException { String url = "RecordScript" + "/uploadScript" + "/" + scriptName; return (OperateScriptServerResponseModel) getCommunicateWithMaster() diff --git a/Bench4Q-Web/src/main/resources/script.xml b/Bench4Q-Web/src/main/resources/script.xml index dd916e21..05d453c2 100644 --- a/Bench4Q-Web/src/main/resources/script.xml +++ b/Bench4Q-Web/src/main/resources/script.xml @@ -1,4 +1,5 @@ - + + @@ -11,17 +12,12 @@ url - http://133.133.12.3:8080/ + http://133.133.12.2:8080/ - queryParams + parameters - - headers - header=Accept|value=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8|; - - USERBEHAVIOR http @@ -39,7 +35,7 @@ time - 58 + 33 TIMERBEHAVIOR @@ -58,17 +54,12 @@ url - http://133.133.12.3:8080/tomcat.css + http://133.133.12.2:8080/tomcat.css - queryParams + parameters - - headers - header=Accept|value=text/css,*/*;q=0.1|;header=Referer|value=http://133.133.12.3:8080/|; - - USERBEHAVIOR http @@ -86,7 +77,7 @@ time - 84 + 29 TIMERBEHAVIOR @@ -105,17 +96,12 @@ url - http://133.133.12.3:8080/tomcat.png + http://133.133.12.2:8080/tomcat.png - queryParams + parameters - - headers - header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|; - - USERBEHAVIOR http @@ -133,7 +119,7 @@ time - 4 + 5 TIMERBEHAVIOR @@ -152,17 +138,12 @@ url - http://133.133.12.3:8080/bg-nav.png + http://133.133.12.2:8080/bg-nav.png - queryParams + parameters - - headers - header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|; - - USERBEHAVIOR http @@ -180,7 +161,7 @@ time - 91 + 31 TIMERBEHAVIOR @@ -199,17 +180,12 @@ url - http://133.133.12.3:8080/asf-logo.png + http://133.133.12.2:8080/asf-logo.png - queryParams + parameters - - headers - header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|; - - USERBEHAVIOR http @@ -227,7 +203,7 @@ time - 1 + 2 TIMERBEHAVIOR @@ -246,17 +222,12 @@ url - http://133.133.12.3:8080/bg-button.png + http://133.133.12.2:8080/bg-button.png - queryParams + parameters - - headers - header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|; - - USERBEHAVIOR http @@ -274,7 +245,7 @@ time - 5 + 3 TIMERBEHAVIOR @@ -293,17 +264,12 @@ url - http://133.133.12.3:8080/bg-upper.png + http://133.133.12.2:8080/bg-middle.png - queryParams + parameters - - headers - header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|; - - USERBEHAVIOR http @@ -321,7 +287,7 @@ time - 9 + 4 TIMERBEHAVIOR @@ -340,17 +306,12 @@ url - http://133.133.12.3:8080/bg-middle.png + http://133.133.12.2:8080/bg-upper.png - queryParams + parameters - - headers - header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|; - - USERBEHAVIOR http @@ -368,7 +329,7 @@ time - 49 + 112 TIMERBEHAVIOR @@ -387,16 +348,12 @@ url - http://133.133.12.3:8080/favicon.ico + http://133.133.12.2:8080/favicon.ico - queryParams + parameters - - headers - header=Accept|value=*/*|; - USERBEHAVIOR http @@ -414,7 +371,7 @@ time - 1946 + 7406 TIMERBEHAVIOR @@ -433,17 +390,12 @@ url - http://133.133.12.3:8080/examples/ + http://133.133.12.2:8080/Bench4Q-Web - queryParams + parameters - - headers - header=Accept|value=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|; - - USERBEHAVIOR http @@ -453,6 +405,10 @@ 18 -1 + + + + @@ -461,7 +417,7 @@ time - 1521 + 21942 TIMERBEHAVIOR @@ -480,17 +436,12 @@ url - http://133.133.12.3:8080/examples/jsp + http://133.133.12.2:8080/ - queryParams + parameters - - headers - header=Accept|value=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/examples/|; - - USERBEHAVIOR http @@ -500,6 +451,10 @@ 20 -1 + + + + @@ -508,7 +463,7 @@ time - 221 + 21150 TIMERBEHAVIOR @@ -527,26 +482,25 @@ url - http://133.133.12.3:8080/examples/jsp/ + http://133.133.12.2:8080/ - queryParams + parameters - - headers - header=Accept|value=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/examples/|; - - USERBEHAVIOR http - 24 + -1 22 -1 + + + + @@ -555,7 +509,7 @@ time - 627 + 1979 TIMERBEHAVIOR @@ -574,62 +528,12 @@ url - http://133.133.12.3:8080/examples/jsp/images/return.gif - + http://133.133.12.2:8080/docs/realm-howto.html - queryParams + parameters - - headers - header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/examples/jsp/|; - - - - USERBEHAVIOR - http - - - 13 - Get - - - url - http://133.133.12.3:8080/examples/jsp/images/code.gif - - - - queryParams - - - - headers - header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/examples/jsp/|; - - - - USERBEHAVIOR - http - - - 14 - Get - - - url - http://133.133.12.3:8080/examples/jsp/images/execute.gif - - - - queryParams - - - - headers - header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/examples/jsp/|; - - USERBEHAVIOR http @@ -637,7 +541,7 @@ -1 24 - 22 + -1 @@ -647,7 +551,7 @@ time - 1 + 226 TIMERBEHAVIOR @@ -658,29 +562,16 @@ 25 -1 - - - - 0 - Sleep - - - time - 6 - - - TIMERBEHAVIOR - timer - - - -1 - 26 - -1 - + + + + + + - 0 + 10 http @@ -693,4 +584,4 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/Bench4Q-Web/src/test/java/org/bench4q/web/api/test/TestResultTest.java b/Bench4Q-Web/src/test/java/org/bench4q/web/api/test/TestResultTest.java index 2f2d77d7..2b248243 100644 --- a/Bench4Q-Web/src/test/java/org/bench4q/web/api/test/TestResultTest.java +++ b/Bench4Q-Web/src/test/java/org/bench4q/web/api/test/TestResultTest.java @@ -4,20 +4,17 @@ import java.util.List; import java.util.Map; import javax.xml.bind.JAXBException; - import junit.framework.Assert; - -import org.bench4q.share.enums.master.TestPlanStatus; -import org.bench4q.share.models.master.TestPlanDBModel; +import org.apache.log4j.Logger; +import org.bench4q.share.helper.ExceptionLog; import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel; import org.bench4q.web.api.TestPlanActionController; -import org.bench4q.web.api.TestPlanHistoryResult; import org.bench4q.web.exception.CustomGenericException; import org.bench4q.web.model.BaseResponseModel; import org.bench4q.web.model.SutInfo; -import org.bench4q.web.model.TestPlanListModel; import org.bench4q.web.model.WebTestPlanResultModel; import org.bench4q.web.tool.test.LoginHelper; +import org.bench4q.web.tool.test.StartTestPlan; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -29,13 +26,21 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(locations = { "file:src/test/resources/bench4qweb-servlet.xml" }) public class TestResultTest { private LoginHelper loginHelper; - + private Logger logger = Logger.getLogger(TestResultTest.class); private TestPlanActionController testPlanActionController; - - private TestPlanHistoryResult testPlanHistoryResult; + private StartTestPlan startTestPlan; private String accessToken; + public StartTestPlan getStartTestPlan() { + return startTestPlan; + } + + @Autowired + public void setStartTestPlan(StartTestPlan startTestPlan) { + this.startTestPlan = startTestPlan; + } + public LoginHelper getLoginHelper() { return loginHelper; } @@ -55,44 +60,35 @@ public class TestResultTest { this.testPlanActionController = testPlanActionController; } - public TestPlanHistoryResult getTestPlanHistoryResult() { - return testPlanHistoryResult; - } - - @Autowired - public void setTestPlanHistoryResult( - TestPlanHistoryResult testPlanHistoryResult) { - this.testPlanHistoryResult = testPlanHistoryResult; - } - @Before public void setUp() { this.accessToken = loginHelper.Login(); + this.startTestPlan.startTest(); + } @Test public void test_getReplaceList() throws CustomGenericException, JAXBException { - if (getTestPlanId() == null) - Assert.assertTrue(false); + Assert.assertNotNull(this.getStartTestPlan().getTestPlanRunId()); BaseResponseModel baseResponseModel = this .getTestPlanActionController().getReplaceList( - loginHelper.Login(), getTestPlanId()); + loginHelper.Login(), + this.startTestPlan.getTestPlanRunId()); @SuppressWarnings("unchecked") Map scriptIdMap = (Map) baseResponseModel .getData(); Assert.assertTrue(scriptIdMap.size() > 0); } - // this is important need to submit a testPlan later @Test public void test_getSutInfo() throws CustomGenericException, JAXBException { - Assert.assertNotNull(this.getTestPlanIdWithMonitor()); + Assert.assertNotNull(this.getStartTestPlan().getTestPlanRunId()); @SuppressWarnings("unchecked") List sutInfos = (List) this .getTestPlanActionController() - .getSutInfo(accessToken, this.getTestPlanIdWithMonitor()) - .getData(); + .getSutInfo(accessToken, + this.getStartTestPlan().getTestPlanRunId()).getData(); for (SutInfo sutInfo : sutInfos) { Assert.assertEquals("5556", sutInfo.getPort()); Assert.assertEquals("133.133.12.3", sutInfo.getIp()); @@ -102,13 +98,14 @@ public class TestResultTest { @Test public void test_getScriptBehaviorBriefModel() throws CustomGenericException, JAXBException { - WebTestPlanResultModel webTestPlanResultModel = getWebTestPlanResultModelWithMonitor(); + Assert.assertNotNull(this.getStartTestPlan().getTestPlanRunId()); + String scriptId = getOneScriptId(this.getStartTestPlan() + .getTestPlanRunId()); + Assert.assertNotNull(scriptId); BaseResponseModel baseResponseModel = this .getTestPlanActionController().getScriptBehaviorBriefModel( accessToken, - webTestPlanResultModel.getTestPlanId().toString(), - new Integer(webTestPlanResultModel.getScriptIds() - .get(0)).toString()); + this.getStartTestPlan().getTestPlanRunId(), scriptId); ScriptBehaviorsBriefModel scriptBehaviorsBriefModel = (ScriptBehaviorsBriefModel) baseResponseModel .getData(); Assert.assertNotNull(scriptBehaviorsBriefModel); @@ -118,42 +115,27 @@ public class TestResultTest { .getTestBehaviorsBriefModel().getBehaviorBriefModels().size() > 0); } - private String getTestPlanIdWithMonitor() throws CustomGenericException, - JAXBException { - WebTestPlanResultModel webTestPlanResultModel = getWebTestPlanResultModelWithMonitor(); - if (webTestPlanResultModel == null) + private String getOneScriptId(String testPlanRunId) { + try { + return queryTestPlanResultModel(testPlanRunId).getScriptIds() + .get(0).toString(); + } catch (CustomGenericException e) { + // TODO Auto-generated catch block + logger.info(ExceptionLog.getStackTrace(e)); return null; - else - return webTestPlanResultModel.getTestPlanId().toString(); + + } catch (JAXBException e) { + // TODO Auto-generated catch block + logger.info(ExceptionLog.getStackTrace(e)); + return null; + } } - private WebTestPlanResultModel getWebTestPlanResultModelWithMonitor() + private WebTestPlanResultModel queryTestPlanResultModel(String testPlanRunId) throws CustomGenericException, JAXBException { - for (TestPlanDBModel testPlanDBModel : ((TestPlanListModel) testPlanHistoryResult - .loadTestPlans(accessToken).getData()).getList()) { - if (testPlanDBModel.getCurrentStatus().equals( - TestPlanStatus.Complete.name())) { - WebTestPlanResultModel webTestPlanResultModel = (WebTestPlanResultModel) this - .getTestPlanActionController() - .queryTestPlan(accessToken, - testPlanDBModel.getTestPlanRunId()).getData(); - if (webTestPlanResultModel.getMonitorModels().size() > 0) - return webTestPlanResultModel; - } - - } - return null; - } - - private String getTestPlanId() throws CustomGenericException { - for (TestPlanDBModel testPlanDBModel : ((TestPlanListModel) testPlanHistoryResult - .loadTestPlans(accessToken).getData()).getList()) { - if (testPlanDBModel.getCurrentStatus().equals( - TestPlanStatus.Complete.name())) { - return testPlanDBModel.getTestPlanRunId(); - } - - } - return null; + return (WebTestPlanResultModel) this + .getTestPlanActionController() + .queryTestPlan(accessToken, + this.getStartTestPlan().getTestPlanRunId()).getData(); } } diff --git a/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/LoginHelper.java b/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/LoginHelper.java index 11d28d05..b636f734 100644 --- a/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/LoginHelper.java +++ b/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/LoginHelper.java @@ -1,37 +1,46 @@ package org.bench4q.web.tool.test; +import junit.framework.Assert; import org.bench4q.share.models.master.UserModel; import org.bench4q.web.api.AuthorizeActionController; import org.bench4q.web.exception.CustomGenericException; +import org.junit.Test; +import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.ui.ModelMap; -@Component public class LoginHelper { private AuthorizeActionController authorizeActionController; public ModelMap modelMap = new ModelMap(); + public AuthorizeActionController getAuthorizeActionController() { return authorizeActionController; } -@Autowired + + @Autowired public void setAuthorizeActionController( AuthorizeActionController authorizeActionController) { this.authorizeActionController = authorizeActionController; } - public String Login(){ + public String Login() { UserModel userModel = new UserModel(); userModel.setUserName("www"); userModel.setPassword("www"); try { + if (this.getAuthorizeActionController() == null) { + System.out.println("null"); + } this.getAuthorizeActionController().login(userModel, modelMap); - String accessToken = (String) modelMap.get("accessToken"); + String accessToken = (String) modelMap.get("accessToken"); return accessToken; } catch (CustomGenericException e) { // TODO Auto-generated catch block e.printStackTrace(); - return ""; + return null; } } + } diff --git a/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/ScriptHelper.java b/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/ScriptHelper.java new file mode 100644 index 00000000..bfd72b5c --- /dev/null +++ b/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/ScriptHelper.java @@ -0,0 +1,60 @@ +package org.bench4q.web.tool.test; + +import java.io.File; +import java.io.IOException; + +import javax.xml.bind.JAXBException; + +import junit.framework.Assert; + +import org.apache.commons.io.FileUtils; +import org.bench4q.share.helper.MarshalHelper; +import org.bench4q.share.models.agent.RunScenarioModel; +import org.bench4q.share.models.master.OperateScriptServerResponseModel; +import org.bench4q.share.models.master.ScriptModel; +import org.bench4q.web.exception.CustomGenericException; +import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange; +import org.bench4q.web.service.ScriptService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ScriptHelper { + + private String filePath = "src/test/resources/script.xml"; + + private ScriptService scriptService; + + public ScriptService getScriptService() { + return scriptService; + } + + @Autowired + public void setScriptService(ScriptService scriptService) { + this.scriptService = scriptService; + } + + public RunScenarioModel getRunScenarioModelFormFile() throws JAXBException, + IOException { + String scriptContent = FileUtils.readFileToString(new File(filePath)); + return (RunScenarioModel) ObjectXmlExchange.fromXml( + RunScenarioModel.class, scriptContent); + } + + public int upLoadScriptAndGetId() throws JAXBException, IOException, + CustomGenericException { + LoginHelper loginHelper = new LoginHelper(); + + String scriptName = "testScript"; + String scriptContent = MarshalHelper.marshal(RunScenarioModel.class, + getRunScenarioModelFormFile()); + OperateScriptServerResponseModel operateScriptServerResponseModel = this + .getScriptService().uploadScript(loginHelper.Login(), + scriptContent, scriptName); + Assert.assertTrue(operateScriptServerResponseModel.isSuccess()); + ScriptModel scriptModel = this.getScriptService().queryScriptByName( + scriptName, loginHelper.Login()); + return scriptModel.getId(); + + } +} diff --git a/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/ScriptReadedFromFile.java b/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/ScriptReadedFromFile.java deleted file mode 100644 index f9ee6408..00000000 --- a/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/ScriptReadedFromFile.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.bench4q.web.tool.test; - -import java.io.File; -import java.io.IOException; - -import javax.xml.bind.JAXBException; - -import org.apache.commons.io.FileUtils; -import org.bench4q.share.models.agent.RunScenarioModel; -import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange; -import org.springframework.stereotype.Component; -@Component -public class ScriptReadedFromFile { - private String filePath = "src/test/resources/script.xml"; - public RunScenarioModel getRunScenarioModelFormFile() throws JAXBException, - IOException { - String scriptContent = FileUtils.readFileToString(new File(filePath)); - return (RunScenarioModel) ObjectXmlExchange.fromXml( - RunScenarioModel.class, scriptContent); - } -} - \ No newline at end of file diff --git a/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/StartTestPlan.java b/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/StartTestPlan.java new file mode 100644 index 00000000..384e2864 --- /dev/null +++ b/Bench4Q-Web/src/test/java/org/bench4q/web/tool/test/StartTestPlan.java @@ -0,0 +1,103 @@ +package org.bench4q.web.tool.test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.JAXBException; + +import org.apache.log4j.Logger; +import org.bench4q.share.helper.ExceptionLog; +import org.bench4q.share.models.master.TestPlanResultModel; +import org.bench4q.web.api.TestPlanActionController; +import org.bench4q.web.exception.CustomGenericException; +import org.bench4q.web.model.TestPlanRequestModel; +import org.bench4q.web.model.WebScriptModel; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.ui.ModelMap; + +@Component +public class StartTestPlan { + private final String MONITOR_IP = "133.133.12.3"; + private ScriptHelper scriptHelper; + private LoginHelper loginHelper; + private TestPlanActionController testPlanActionController; + private String testPlanRunId; + + public TestPlanActionController getTestPlanActionController() { + return testPlanActionController; + } + + @Autowired + public void setTestPlanActionController( + TestPlanActionController testPlanActionController) { + this.testPlanActionController = testPlanActionController; + } + + public ScriptHelper getScriptHelper() { + return scriptHelper; + } + + @Autowired + public void setScriptHelper(ScriptHelper scriptHelper) { + this.scriptHelper = scriptHelper; + } + + public LoginHelper getLoginHelper() { + return loginHelper; + } + + @Autowired + public void setLoginHelper(LoginHelper loginHelper) { + this.loginHelper = loginHelper; + } + + public String getTestPlanRunId() { + return testPlanRunId; + } + + private void setTestPlanRunId(String testPlanRunId) { + this.testPlanRunId = testPlanRunId; + } + + public void startTest() { + + try { + TestPlanResultModel testPlanResultModel = (TestPlanResultModel) this + .getTestPlanActionController() + .runTestPlan(this.getLoginHelper().Login(), new ModelMap(), + createTestPlanWithMonitor()).getData(); + this.setTestPlanRunId(testPlanResultModel.getTestPlanId() + .toString()); + } catch (Exception e) { + Logger.getLogger(StartTestPlan.class).info( + ExceptionLog.getStackTrace(e)); + } + } + + public TestPlanRequestModel createTestPlanWithMonitor() + throws JAXBException, IOException, CustomGenericException { + List ipList = new ArrayList<>(); + ipList.add(MONITOR_IP); + TestPlanRequestModel testPlanRequestModel = new TestPlanRequestModel(); + testPlanRequestModel.setIpList(ipList); + testPlanRequestModel.setTestPlanName("forTest"); + testPlanRequestModel.setScriptList(generateWebScriptModels()); + return testPlanRequestModel; + } + + private List generateWebScriptModels() + throws JAXBException, IOException, CustomGenericException { + List scriptList = new ArrayList(); + WebScriptModel webScriptModel = new WebScriptModel(); + webScriptModel.setCooldown(1); + webScriptModel.setExecuteRange(50); + webScriptModel.setId(this.getScriptHelper().upLoadScriptAndGetId()); + webScriptModel.setLoad(30); + webScriptModel.setWarmup(1); + scriptList.add(webScriptModel); + return scriptList; + + } +}