This commit is contained in:
fanfuxiaoran 2014-03-26 17:44:14 +08:00
parent fe711cd04f
commit 05dc5952b4
9 changed files with 311 additions and 273 deletions

View File

@ -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) {

View File

@ -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<ResultInfoModel>());

View File

@ -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<ScriptModel> queryScripts(List<Integer> 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()

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<runScenario>
<pages>
<page>
@ -11,17 +12,12 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/</value>
<value>http://133.133.12.2:8080/</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>header=Accept|value=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
@ -39,7 +35,7 @@
<parameters>
<parameter>
<key>time</key>
<value>58</value>
<value>33</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -58,17 +54,12 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/tomcat.css</value>
<value>http://133.133.12.2:8080/tomcat.css</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>header=Accept|value=text/css,*/*;q=0.1|;header=Referer|value=http://133.133.12.3:8080/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
@ -86,7 +77,7 @@
<parameters>
<parameter>
<key>time</key>
<value>84</value>
<value>29</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -105,17 +96,12 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/tomcat.png</value>
<value>http://133.133.12.2:8080/tomcat.png</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
@ -133,7 +119,7 @@
<parameters>
<parameter>
<key>time</key>
<value>4</value>
<value>5</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -152,17 +138,12 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/bg-nav.png</value>
<value>http://133.133.12.2:8080/bg-nav.png</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
@ -180,7 +161,7 @@
<parameters>
<parameter>
<key>time</key>
<value>91</value>
<value>31</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -199,17 +180,12 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/asf-logo.png</value>
<value>http://133.133.12.2:8080/asf-logo.png</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
@ -227,7 +203,7 @@
<parameters>
<parameter>
<key>time</key>
<value>1</value>
<value>2</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -246,17 +222,12 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/bg-button.png</value>
<value>http://133.133.12.2:8080/bg-button.png</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
@ -274,7 +245,7 @@
<parameters>
<parameter>
<key>time</key>
<value>5</value>
<value>3</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -293,17 +264,12 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/bg-upper.png</value>
<value>http://133.133.12.2:8080/bg-middle.png</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
@ -321,7 +287,7 @@
<parameters>
<parameter>
<key>time</key>
<value>9</value>
<value>4</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -340,17 +306,12 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/bg-middle.png</value>
<value>http://133.133.12.2:8080/bg-upper.png</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
@ -368,7 +329,7 @@
<parameters>
<parameter>
<key>time</key>
<value>49</value>
<value>112</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -387,16 +348,12 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/favicon.ico</value>
<value>http://133.133.12.2:8080/favicon.ico</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>header=Accept|value=*/*|;</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
@ -414,7 +371,7 @@
<parameters>
<parameter>
<key>time</key>
<value>1946</value>
<value>7406</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -433,17 +390,12 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/examples/</value>
<value>http://133.133.12.2:8080/Bench4Q-Web</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>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/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
@ -453,6 +405,10 @@
<id>18</id>
<parentId>-1</parentId>
</batch>
</batches>
</page>
<page>
<batches>
<batch>
<behaviors>
<behaviorModel>
@ -461,7 +417,7 @@
<parameters>
<parameter>
<key>time</key>
<value>1521</value>
<value>21942</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -480,17 +436,12 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/examples/jsp</value>
<value>http://133.133.12.2:8080/</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>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/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
@ -500,6 +451,10 @@
<id>20</id>
<parentId>-1</parentId>
</batch>
</batches>
</page>
<page>
<batches>
<batch>
<behaviors>
<behaviorModel>
@ -508,7 +463,7 @@
<parameters>
<parameter>
<key>time</key>
<value>221</value>
<value>21150</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -527,26 +482,25 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/examples/jsp/</value>
<value>http://133.133.12.2:8080/</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>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/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
</behaviorModel>
</behaviors>
<childId>24</childId>
<childId>-1</childId>
<id>22</id>
<parentId>-1</parentId>
</batch>
</batches>
</page>
<page>
<batches>
<batch>
<behaviors>
<behaviorModel>
@ -555,7 +509,7 @@
<parameters>
<parameter>
<key>time</key>
<value>627</value>
<value>1979</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -574,62 +528,12 @@
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/examples/jsp/images/return.gif
</value>
<value>http://133.133.12.2:8080/docs/realm-howto.html</value>
</parameter>
<parameter>
<key>queryParams</key>
<key>parameters</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/examples/jsp/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
</behaviorModel>
<behaviorModel>
<id>13</id>
<name>Get</name>
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/examples/jsp/images/code.gif
</value>
</parameter>
<parameter>
<key>queryParams</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/examples/jsp/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
</behaviorModel>
<behaviorModel>
<id>14</id>
<name>Get</name>
<parameters>
<parameter>
<key>url</key>
<value>http://133.133.12.3:8080/examples/jsp/images/execute.gif
</value>
</parameter>
<parameter>
<key>queryParams</key>
<value></value>
</parameter>
<parameter>
<key>headers</key>
<value>header=Accept|value=image/webp,*/*;q=0.8|;header=Referer|value=http://133.133.12.3:8080/examples/jsp/|;
</value>
</parameter>
</parameters>
<type>USERBEHAVIOR</type>
<use>http</use>
@ -637,7 +541,7 @@
</behaviors>
<childId>-1</childId>
<id>24</id>
<parentId>22</parentId>
<parentId>-1</parentId>
</batch>
<batch>
<behaviors>
@ -647,7 +551,7 @@
<parameters>
<parameter>
<key>time</key>
<value>1</value>
<value>226</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
@ -658,29 +562,16 @@
<id>25</id>
<parentId>-1</parentId>
</batch>
<batch>
<behaviors>
<behaviorModel>
<id>0</id>
<name>Sleep</name>
<parameters>
<parameter>
<key>time</key>
<value>6</value>
</parameter>
</parameters>
<type>TIMERBEHAVIOR</type>
<use>timer</use>
</behaviorModel>
</behaviors>
<childId>-1</childId>
<id>26</id>
<parentId>-1</parentId>
</batch>
</batches>
</page>
<page>
<batches />
</page>
<page>
<batches />
</page>
</pages>
<poolSize>0</poolSize>
<poolSize>10</poolSize>
<usePlugins>
<usePlugin>
<id>http</id>
@ -693,4 +584,4 @@
<parameters />
</usePlugin>
</usePlugins>
</runScenario>
</runScenario>

View File

@ -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<String, Integer> scriptIdMap = (Map<String, Integer>) 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<SutInfo> sutInfos = (List<SutInfo>) 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();
}
}

View File

@ -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;
}
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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<String> ipList = new ArrayList<>();
ipList.add(MONITOR_IP);
TestPlanRequestModel testPlanRequestModel = new TestPlanRequestModel();
testPlanRequestModel.setIpList(ipList);
testPlanRequestModel.setTestPlanName("forTest");
testPlanRequestModel.setScriptList(generateWebScriptModels());
return testPlanRequestModel;
}
private List<WebScriptModel> generateWebScriptModels()
throws JAXBException, IOException, CustomGenericException {
List<WebScriptModel> scriptList = new ArrayList<WebScriptModel>();
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;
}
}