From 14c8800a991671fb5b2cb2d4bfcb376294e0966d Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Thu, 26 Dec 2013 17:04:49 +0800 Subject: [PATCH] add test about load result data from database, until now, i just finish the scriptBriefResultMOdel part --- .../master/api/TestPlanResulController.java | 28 +++--- .../master/exception/Bench4QException.java | 3 +- .../master/report/ScriptReportService.java | 2 +- .../db/TestPlanScriptResultService.java | 86 +++++++++++++++++ .../service/db/TestPlanScriptService.java | 27 ++++-- .../master/service/db/TestPlanService.java | 2 +- .../master/test/TestBase_MakeUpTestPlan.java | 93 ++++++++++++++++++ .../org/bench4q/master/test/UserTest.java | 31 ------ .../AgentPoolControllerTest.java | 2 +- .../MonitorControllerTest.java | 2 +- .../RecordPortControllerTest.java | 2 +- .../RecordScriptControllerTest.java | 3 +- .../ScriptControllerTest.java | 16 +++- .../test/{ => controller}/TestBase.java | 2 +- .../TestPlanControllerTest.java | 20 ++-- .../TestPlanScriptResultControllerTest.java | 76 +++++++++++++++ .../UserControllerTest.java} | 6 +- .../master/test/scriptOperateTest.java | 30 ------ .../TestTestPlanScriptResultService.java | 95 +++++++++++++++++++ .../service/TestTestPlanScriptService.java | 6 +- .../master/test/testplan/TestPlanTester.java | 15 +-- .../test/testplan/TestTestPlanResult.java | 10 +- 22 files changed, 441 insertions(+), 116 deletions(-) create mode 100644 src/main/java/org/bench4q/master/service/db/TestPlanScriptResultService.java create mode 100644 src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java delete mode 100644 src/test/java/org/bench4q/master/test/UserTest.java rename src/test/java/org/bench4q/master/test/{ => controller}/AgentPoolControllerTest.java (95%) rename src/test/java/org/bench4q/master/test/{ => controller}/MonitorControllerTest.java (96%) rename src/test/java/org/bench4q/master/test/{ => controller}/RecordPortControllerTest.java (94%) rename src/test/java/org/bench4q/master/test/{recordscript => controller}/RecordScriptControllerTest.java (95%) rename src/test/java/org/bench4q/master/test/{ => controller}/ScriptControllerTest.java (58%) rename src/test/java/org/bench4q/master/test/{ => controller}/TestBase.java (94%) rename src/test/java/org/bench4q/master/test/{testplan => controller}/TestPlanControllerTest.java (59%) create mode 100644 src/test/java/org/bench4q/master/test/controller/TestPlanScriptResultControllerTest.java rename src/test/java/org/bench4q/master/test/{AuthTest.java => controller/UserControllerTest.java} (89%) delete mode 100644 src/test/java/org/bench4q/master/test/scriptOperateTest.java create mode 100644 src/test/java/org/bench4q/master/test/service/TestTestPlanScriptResultService.java diff --git a/src/main/java/org/bench4q/master/api/TestPlanResulController.java b/src/main/java/org/bench4q/master/api/TestPlanResulController.java index a0d74f43..bc955556 100644 --- a/src/main/java/org/bench4q/master/api/TestPlanResulController.java +++ b/src/main/java/org/bench4q/master/api/TestPlanResulController.java @@ -3,9 +3,9 @@ package org.bench4q.master.api; import java.util.UUID; import org.bench4q.master.exception.Bench4QException; -import org.bench4q.master.service.db.TestPlanScriptService; +import org.bench4q.master.service.db.TestPlanScriptResultService; import org.bench4q.master.service.db.UserService; -import org.bench4q.share.models.master.ScriptProcessModel; +import org.bench4q.share.models.master.ResultLoadModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; @@ -14,31 +14,33 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller -@RequestMapping("/TestPlanResult") +@RequestMapping("/testPlanResult") public class TestPlanResulController extends BaseController { - private TestPlanScriptService testPlanScriptService; + private TestPlanScriptResultService testPlanScriptResultService; - public TestPlanScriptService getTestPlanScriptService() { - return testPlanScriptService; + public TestPlanScriptResultService getTestPlanScriptResultService() { + return testPlanScriptResultService; } @Autowired - private void setTestPlanScriptService( - TestPlanScriptService testPlanScriptService) { - this.testPlanScriptService = testPlanScriptService; + public void setTestPlanScriptResultService( + TestPlanScriptResultService testPlanScriptResultService) { + this.testPlanScriptResultService = testPlanScriptResultService; } - @RequestMapping(value = "/{testPlanRunId}/script/{scriptId}/{fieldName}", method = RequestMethod.GET) + @RequestMapping(value = "/{testPlanRunId}/loadScriptBirefResults/{scriptId}/{fieldName}", method = RequestMethod.GET) @ResponseBody - public ScriptProcessModel getScriptResults( + public ResultLoadModel loadScriptBirefResults( @PathVariable UUID testPlanRunId, @PathVariable int scriptId, @PathVariable String fieldName) throws Bench4QException { if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { throw new Bench4QException(400 + "", "not permitted", "/{testPlanRunId}/script/{scriptId}/{fieldName}"); } - ScriptProcessModel ret = new ScriptProcessModel(); - + ResultLoadModel ret = new ResultLoadModel(); + ret.setValueTimeModels(this.getTestPlanScriptResultService() + .loadScriptBriefSpecificField(testPlanRunId, scriptId, + fieldName)); return ret; } } diff --git a/src/main/java/org/bench4q/master/exception/Bench4QException.java b/src/main/java/org/bench4q/master/exception/Bench4QException.java index 0af817b0..3502dbbc 100644 --- a/src/main/java/org/bench4q/master/exception/Bench4QException.java +++ b/src/main/java/org/bench4q/master/exception/Bench4QException.java @@ -39,8 +39,7 @@ public class Bench4QException extends Exception { this.resource = resource; } - public Bench4QException(String code, String message, - String resource) { + public Bench4QException(String code, String message, String resource) { this.setCode(code); this.setMessage(message); this.setResource(resource); diff --git a/src/main/java/org/bench4q/master/report/ScriptReportService.java b/src/main/java/org/bench4q/master/report/ScriptReportService.java index 5bad6b67..d2d99506 100644 --- a/src/main/java/org/bench4q/master/report/ScriptReportService.java +++ b/src/main/java/org/bench4q/master/report/ScriptReportService.java @@ -67,7 +67,7 @@ public class ScriptReportService { } for (TestPlanScript testPlanScript : scripts) { createScriptImage(this.getTestPlanScriptService() - .queryTestPlanScriptResult(testPlanScript), document); + .queryScriptBriefResults(testPlanScript), document); } } diff --git a/src/main/java/org/bench4q/master/service/db/TestPlanScriptResultService.java b/src/main/java/org/bench4q/master/service/db/TestPlanScriptResultService.java new file mode 100644 index 00000000..123b6e0c --- /dev/null +++ b/src/main/java/org/bench4q/master/service/db/TestPlanScriptResultService.java @@ -0,0 +1,86 @@ +package org.bench4q.master.service.db; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import javax.xml.bind.JAXBException; + +import org.apache.log4j.Logger; +import org.bench4q.master.entity.db.TestPlanScriptResult; +import org.bench4q.master.exception.ExceptionLog; +import org.bench4q.share.helper.MarshalHelper; +import org.bench4q.share.models.master.ScriptBriefResultModel; +import org.bench4q.share.models.master.ValueTimeModel; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class TestPlanScriptResultService { + private TestPlanScriptService testPlanScriptService; + private static Logger logger = Logger + .getLogger(TestPlanScriptResultService.class); + + private TestPlanScriptService getTestPlanScriptService() { + return testPlanScriptService; + } + + @Autowired + private void setTestPlanScriptService( + TestPlanScriptService testPlanScriptService) { + this.testPlanScriptService = testPlanScriptService; + } + + /** + * Here I just use the Time that createDateTime in testplanscriptResult + * + * @param testPlanId + * @param scriptId + * @return + */ + public List loadScriptBriefSpecificField(UUID testPlanId, + int scriptId, String fieldName) { + List scriptBriefresults = this + .getTestPlanScriptService().queryScriptBriefResults(testPlanId, + scriptId); + List ret = new ArrayList(); + for (TestPlanScriptResult result : scriptBriefresults) { + try { + ScriptBriefResultModel briefModel = (ScriptBriefResultModel) MarshalHelper + .unmarshal(ScriptBriefResultModel.class, + result.getResultContent()); + ret.add(ValueTimeModel.buildValueTimeModel( + getSpecificFieldValue(briefModel, fieldName), + result.getCreateDatetime())); + } catch (JAXBException e) { + logger.info(ExceptionLog.getExceptionStackTrace(e) + + " When unmarshal the model from ResultCOntent"); + continue; + } catch (NoSuchFieldException e) { + logger.info(ExceptionLog.getExceptionStackTrace(e) + + " When get Field from the model from ResultCOntent"); + continue; + } + } + return ret; + } + + public long getSpecificFieldValue(ScriptBriefResultModel briefModel, + String fieldName) throws NoSuchFieldException { + try { + Field field = getFieldAndSetAccessible(briefModel, fieldName); + return (Long) field.get(briefModel); + } catch (Exception e) { + throw new NoSuchFieldException(); + } + } + + private Field getFieldAndSetAccessible(ScriptBriefResultModel briefModel, + String fieldName) throws NoSuchFieldException, SecurityException { + Field field = briefModel.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + return field; + } + +} diff --git a/src/main/java/org/bench4q/master/service/db/TestPlanScriptService.java b/src/main/java/org/bench4q/master/service/db/TestPlanScriptService.java index c8455392..559da4cc 100644 --- a/src/main/java/org/bench4q/master/service/db/TestPlanScriptService.java +++ b/src/main/java/org/bench4q/master/service/db/TestPlanScriptService.java @@ -79,19 +79,20 @@ public class TestPlanScriptService { } } - public boolean saveScriptBriefResult(final UUID testPlanRunId, final int scriptId, - final ScriptBriefResultModel resultModel) { + public boolean saveScriptBriefResult(final UUID testPlanRunId, + final int scriptId, final ScriptBriefResultModel resultModel) { return saveResultPrivate(testPlanRunId, scriptId, resultModel); } - public boolean saveBehaviorsBriefResult(final UUID testPlanRunId, final int scriptId, + public boolean saveBehaviorsBriefResult(final UUID testPlanRunId, + final int scriptId, final ScriptBehaviorsBriefModel scriptBehaviorsBriefModel) { return saveResultPrivate(testPlanRunId, scriptId, scriptBehaviorsBriefModel); } - public boolean savePageBriefResult(final UUID testPlanRunId, final int scriptId, - final ScriptPageBriefModel pageBriefModel) { + public boolean savePageBriefResult(final UUID testPlanRunId, + final int scriptId, final ScriptPageBriefModel pageBriefModel) { return saveResultPrivate(testPlanRunId, scriptId, pageBriefModel); } @@ -181,15 +182,27 @@ public class TestPlanScriptService { } } - public List queryTestPlanScriptResult( + public List queryScriptBriefResults(UUID testPlanId, + int scriptId) { + return queryScriptBriefResults(this.getTestPlanScript(scriptId, + testPlanId)); + } + + public List queryScriptBriefResults( TestPlanScript testPlanScript) { + return queryScriptResultsByType(testPlanScript, + ScriptBriefResultModel.class.getName()); + } + + private List queryScriptResultsByType( + TestPlanScript testPlanScript, String type) { Session session = this.getSessionHelper().openSession(); try { @SuppressWarnings("unchecked") List resultList = session .createCriteria(TestPlanScriptResult.class) .add(Restrictions.eq("testPlanScript", testPlanScript)) - .list(); + .add(Restrictions.eq("resultType", type)).list(); return resultList; } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/org/bench4q/master/service/db/TestPlanService.java b/src/main/java/org/bench4q/master/service/db/TestPlanService.java index 46314543..3df85e00 100644 --- a/src/main/java/org/bench4q/master/service/db/TestPlanService.java +++ b/src/main/java/org/bench4q/master/service/db/TestPlanService.java @@ -192,7 +192,7 @@ public class TestPlanService { @SuppressWarnings("unchecked") List configs = session .createCriteria(PlanedConfig.class) - .add(Restrictions.eq("testPlanScript", testPlanScripts)) + .add(Restrictions.eq("testPlanScript", testPlanScript)) .list(); if (configs == null) { return false; diff --git a/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java b/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java new file mode 100644 index 00000000..73f311d6 --- /dev/null +++ b/src/test/java/org/bench4q/master/test/TestBase_MakeUpTestPlan.java @@ -0,0 +1,93 @@ +package org.bench4q.master.test; + +import java.util.UUID; + +import org.bench4q.master.service.db.ScriptService; +import org.bench4q.master.service.db.TestPlanScriptResultService; +import org.bench4q.master.service.db.TestPlanScriptService; +import org.bench4q.master.service.db.TestPlanService; +import org.bench4q.master.service.db.UserService; +import org.bench4q.master.test.controller.TestBase; +import org.bench4q.share.models.master.ScriptBriefResultModel; +import org.springframework.beans.factory.annotation.Autowired; + +public class TestBase_MakeUpTestPlan extends TestBase { + private TestPlanScriptResultService testPlanScriptResultService; + private TestPlanScriptService testPlanScriptService; + private TestPlanService testPlanService; + private UserService userService; + private ScriptService scriptService; + private UUID testPlanRunIdUuid; + private int scriptId; + + public TestPlanScriptResultService getTestPlanScriptResultService() { + return testPlanScriptResultService; + } + + @Autowired + public void setTestPlanScriptResultService( + TestPlanScriptResultService testPlanScriptResultService) { + this.testPlanScriptResultService = testPlanScriptResultService; + } + + public TestPlanScriptService getTestPlanScriptService() { + return testPlanScriptService; + } + + @Autowired + public void setTestPlanScriptService( + TestPlanScriptService testPlanScriptService) { + this.testPlanScriptService = testPlanScriptService; + } + + public TestPlanService getTestPlanService() { + return testPlanService; + } + + @Autowired + public void setTestPlanService(TestPlanService testPlanService) { + this.testPlanService = testPlanService; + } + + public UserService getUserService() { + return userService; + } + + @Autowired + public void setUserService(UserService userService) { + this.userService = userService; + } + + public ScriptService getScriptService() { + return scriptService; + } + + @Autowired + public void setScriptService(ScriptService scriptService) { + this.scriptService = scriptService; + } + + public UUID getTestPlanRunIdUuid() { + return testPlanRunIdUuid; + } + + public void setTestPlanRunIdUuid(UUID testPlanRunIdUuid) { + this.testPlanRunIdUuid = testPlanRunIdUuid; + } + + public int getScriptId() { + return scriptId; + } + + public void setScriptId(int scriptId) { + this.scriptId = scriptId; + } + + protected ScriptBriefResultModel buildScriptBriefResultModel(int i) { + ScriptBriefResultModel resultModel = new ScriptBriefResultModel(); + resultModel.setAverageElapsedTime(100 * i); + resultModel.setAverageResponseTime(100 * i); + resultModel.setFailRateThisTime(10 * i); + return resultModel; + } +} diff --git a/src/test/java/org/bench4q/master/test/UserTest.java b/src/test/java/org/bench4q/master/test/UserTest.java deleted file mode 100644 index f46a7313..00000000 --- a/src/test/java/org/bench4q/master/test/UserTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.bench4q.master.test; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import junit.framework.TestCase; - -import org.bench4q.share.communication.HttpRequester; -import org.junit.Test; - -public class UserTest extends TestCase { - private HttpRequester httpRequester = new HttpRequester(); - - @Test - public void authorizeTest() throws IOException { - Map map = new HashMap(); - map.put("userName", "chen"); - map.put("password", "123"); - - HttpRequester.HttpResponse httpResponse = this.httpRequester.sendGet( - "http://localhost:8080/user/authorize", map, null); - assertEquals(true, httpResponse != null); - System.out.println(httpResponse.getContent()); - } - - public static void main(String[] args) throws IOException { - UserTest userTest = new UserTest(); - userTest.authorizeTest(); - } -} diff --git a/src/test/java/org/bench4q/master/test/AgentPoolControllerTest.java b/src/test/java/org/bench4q/master/test/controller/AgentPoolControllerTest.java similarity index 95% rename from src/test/java/org/bench4q/master/test/AgentPoolControllerTest.java rename to src/test/java/org/bench4q/master/test/controller/AgentPoolControllerTest.java index 1ec4e2e8..eb94b446 100644 --- a/src/test/java/org/bench4q/master/test/AgentPoolControllerTest.java +++ b/src/test/java/org/bench4q/master/test/controller/AgentPoolControllerTest.java @@ -1,4 +1,4 @@ -package org.bench4q.master.test; +package org.bench4q.master.test.controller; import java.io.IOException; import java.util.HashMap; diff --git a/src/test/java/org/bench4q/master/test/MonitorControllerTest.java b/src/test/java/org/bench4q/master/test/controller/MonitorControllerTest.java similarity index 96% rename from src/test/java/org/bench4q/master/test/MonitorControllerTest.java rename to src/test/java/org/bench4q/master/test/controller/MonitorControllerTest.java index d548305b..861bb4a1 100644 --- a/src/test/java/org/bench4q/master/test/MonitorControllerTest.java +++ b/src/test/java/org/bench4q/master/test/controller/MonitorControllerTest.java @@ -1,4 +1,4 @@ -package org.bench4q.master.test; +package org.bench4q.master.test.controller; import java.io.IOException; import java.util.HashMap; diff --git a/src/test/java/org/bench4q/master/test/RecordPortControllerTest.java b/src/test/java/org/bench4q/master/test/controller/RecordPortControllerTest.java similarity index 94% rename from src/test/java/org/bench4q/master/test/RecordPortControllerTest.java rename to src/test/java/org/bench4q/master/test/controller/RecordPortControllerTest.java index e71fda78..2c877d1a 100644 --- a/src/test/java/org/bench4q/master/test/RecordPortControllerTest.java +++ b/src/test/java/org/bench4q/master/test/controller/RecordPortControllerTest.java @@ -1,4 +1,4 @@ -package org.bench4q.master.test; +package org.bench4q.master.test.controller; import java.io.IOException; import java.util.HashMap; diff --git a/src/test/java/org/bench4q/master/test/recordscript/RecordScriptControllerTest.java b/src/test/java/org/bench4q/master/test/controller/RecordScriptControllerTest.java similarity index 95% rename from src/test/java/org/bench4q/master/test/recordscript/RecordScriptControllerTest.java rename to src/test/java/org/bench4q/master/test/controller/RecordScriptControllerTest.java index 21524a03..2d009459 100644 --- a/src/test/java/org/bench4q/master/test/recordscript/RecordScriptControllerTest.java +++ b/src/test/java/org/bench4q/master/test/controller/RecordScriptControllerTest.java @@ -1,4 +1,4 @@ -package org.bench4q.master.test.recordscript; +package org.bench4q.master.test.controller; import java.io.IOException; import java.util.HashMap; @@ -8,7 +8,6 @@ import javax.xml.bind.JAXBException; import static org.junit.Assert.*; -import org.bench4q.master.test.TestBase; import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.agent.RunScenarioModel; diff --git a/src/test/java/org/bench4q/master/test/ScriptControllerTest.java b/src/test/java/org/bench4q/master/test/controller/ScriptControllerTest.java similarity index 58% rename from src/test/java/org/bench4q/master/test/ScriptControllerTest.java rename to src/test/java/org/bench4q/master/test/controller/ScriptControllerTest.java index afb01777..27723bf1 100644 --- a/src/test/java/org/bench4q/master/test/ScriptControllerTest.java +++ b/src/test/java/org/bench4q/master/test/controller/ScriptControllerTest.java @@ -1,8 +1,10 @@ -package org.bench4q.master.test; +package org.bench4q.master.test.controller; import static org.junit.Assert.*; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import javax.xml.bind.JAXBException; @@ -25,4 +27,16 @@ public class ScriptControllerTest extends TestBase { httpResponse.getContent()); assertTrue(ret.getScriptModels().size() > 0); } + + @Test + public void deleteScript(int scriptId) throws IOException { + Map params = new HashMap(); + params.put("scriptId", scriptId + ""); + Map properties = new HashMap(); + properties.put(this.AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER + + this.accessTocken); + HttpResponse httpResponse = this.httpRequester.sendPost( + this.controllerUrl + "/deleteScript", params, properties); + System.out.println(httpResponse.getContent()); + } } diff --git a/src/test/java/org/bench4q/master/test/TestBase.java b/src/test/java/org/bench4q/master/test/controller/TestBase.java similarity index 94% rename from src/test/java/org/bench4q/master/test/TestBase.java rename to src/test/java/org/bench4q/master/test/controller/TestBase.java index 10d3630c..d94e49b0 100644 --- a/src/test/java/org/bench4q/master/test/TestBase.java +++ b/src/test/java/org/bench4q/master/test/controller/TestBase.java @@ -1,4 +1,4 @@ -package org.bench4q.master.test; +package org.bench4q.master.test.controller; import java.util.HashMap; import java.util.Map; diff --git a/src/test/java/org/bench4q/master/test/testplan/TestPlanControllerTest.java b/src/test/java/org/bench4q/master/test/controller/TestPlanControllerTest.java similarity index 59% rename from src/test/java/org/bench4q/master/test/testplan/TestPlanControllerTest.java rename to src/test/java/org/bench4q/master/test/controller/TestPlanControllerTest.java index c555da03..392179d3 100644 --- a/src/test/java/org/bench4q/master/test/testplan/TestPlanControllerTest.java +++ b/src/test/java/org/bench4q/master/test/controller/TestPlanControllerTest.java @@ -1,13 +1,15 @@ -package org.bench4q.master.test.testplan; +package org.bench4q.master.test.controller; import java.io.IOException; + import javax.xml.bind.JAXBException; -import org.bench4q.master.test.TestBase; +import org.bench4q.master.test.testplan.TestPlanTester; import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.master.TestPlanBusinessModel; import org.junit.Test; + import static org.junit.Assert.*; public class TestPlanControllerTest extends TestBase { @@ -18,11 +20,17 @@ public class TestPlanControllerTest extends TestBase { @Test public void runTestPlanWithoutLogOn() throws JAXBException, IOException { - HttpResponse httpResponse = this.httpRequester.sendPostXml(this.url - + "/runTestPlanWithTestPlanModel", MarshalHelper.marshal( - TestPlanBusinessModel.class, TestPlanTester.createATestPlan()), - null); + HttpResponse httpResponse = this.httpRequester + .sendPostXml( + this.url + "/runTestPlanWithTestPlanModel", + MarshalHelper + .marshal( + TestPlanBusinessModel.class, + TestPlanTester + .createATestPlan(TestPlanTester.SCRIPTID1)), + null); System.out.println(httpResponse.getContent()); assertEquals(400, httpResponse.getCode()); } + } diff --git a/src/test/java/org/bench4q/master/test/controller/TestPlanScriptResultControllerTest.java b/src/test/java/org/bench4q/master/test/controller/TestPlanScriptResultControllerTest.java new file mode 100644 index 00000000..f62bfd7b --- /dev/null +++ b/src/test/java/org/bench4q/master/test/controller/TestPlanScriptResultControllerTest.java @@ -0,0 +1,76 @@ +package org.bench4q.master.test.controller; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.UUID; + +import javax.xml.bind.JAXBException; + +import org.bench4q.master.api.modelfactory.BusinessModelMapFactory; +import org.bench4q.master.entity.db.User; +import org.bench4q.master.test.TestBase_MakeUpTestPlan; +import org.bench4q.master.test.testplan.TestPlanTester; +import org.bench4q.master.testplan.entity.TestPlanInBusiness; +import org.bench4q.share.communication.HttpRequester.HttpResponse; +import org.bench4q.share.helper.MarshalHelper; +import org.bench4q.share.models.master.ResultLoadModel; +import org.bench4q.share.models.master.ScriptBehaviorsBriefModel; +import org.bench4q.share.models.master.ScriptPageBriefModel; +import org.bench4q.share.models.master.TestPlanBusinessModel; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "classpath:service-test-context.xml" }) +public class TestPlanScriptResultControllerTest extends TestBase_MakeUpTestPlan { + private String urlString = BASE_URL + "/testPlanResult"; + + @Test + public void testGetScriptResults() throws IOException, JAXBException { + HttpResponse httpResponse = this.httpRequester.sendGet(urlString + "/" + + this.getTestPlanRunIdUuid().toString() + + "/loadScriptBirefResults/" + this.getScriptId() + + "/averageResponseTime", null, + this.makeAccessTockenMap(this.getAccessTocken())); + ResultLoadModel loadModel = (ResultLoadModel) MarshalHelper.unmarshal( + ResultLoadModel.class, httpResponse.getContent()); + assertEquals(3, loadModel.getValueTimeModels().size()); + } + + @Before + public void makeUpTestPlanAndTestPlanScriptAndTestPlanScriptResult() { + UUID testPlanRunId = UUID.randomUUID(); + User user = this.getUserService().getUserByName("admin"); + this.setScriptId(this.getScriptService().loadScripts(user).get(0) + .getId()); + TestPlanBusinessModel model = TestPlanTester.createATestPlan(this + .getScriptId()); + TestPlanInBusiness testPlan = BusinessModelMapFactory.toBusiness(model); + this.getTestPlanService().saveTestPlanToDB(testPlan, user, + testPlanRunId, testPlan.getRunningScripts()); + this.setTestPlanRunIdUuid(testPlanRunId); + for (int i = 0; i < 3; i++) { + this.getTestPlanScriptService().saveScriptBriefResult( + testPlanRunId, this.getScriptId(), + buildScriptBriefResultModel(i)); + this.getTestPlanScriptService().saveBehaviorsBriefResult( + testPlanRunId, this.getScriptId(), + new ScriptBehaviorsBriefModel()); + this.getTestPlanScriptService().savePageBriefResult(testPlanRunId, + this.getScriptId(), new ScriptPageBriefModel()); + } + + } + + @After + public void cleanUp() { + this.getTestPlanService().removeTestPlanInDB( + this.getTestPlanService() + .getTestPlan(this.getTestPlanRunIdUuid()).getId()); + } +} diff --git a/src/test/java/org/bench4q/master/test/AuthTest.java b/src/test/java/org/bench4q/master/test/controller/UserControllerTest.java similarity index 89% rename from src/test/java/org/bench4q/master/test/AuthTest.java rename to src/test/java/org/bench4q/master/test/controller/UserControllerTest.java index 204d5b89..1d2c2bb0 100644 --- a/src/test/java/org/bench4q/master/test/AuthTest.java +++ b/src/test/java/org/bench4q/master/test/controller/UserControllerTest.java @@ -1,7 +1,6 @@ -package org.bench4q.master.test; +package org.bench4q.master.test.controller; import java.io.IOException; - import javax.xml.bind.JAXBException; import org.bench4q.share.communication.HttpRequester.HttpResponse; @@ -11,7 +10,7 @@ import org.junit.Test; import static org.junit.Assert.*; -public class AuthTest extends TestBase { +public class UserControllerTest extends TestBase { @Test public void testNormalAuth() throws IOException, JAXBException { @@ -35,4 +34,5 @@ public class AuthTest extends TestBase { httpResponse.getContent()); assertTrue(ret.isSuccess()); } + } diff --git a/src/test/java/org/bench4q/master/test/scriptOperateTest.java b/src/test/java/org/bench4q/master/test/scriptOperateTest.java deleted file mode 100644 index 19f8040a..00000000 --- a/src/test/java/org/bench4q/master/test/scriptOperateTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.bench4q.master.test; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import javax.xml.bind.JAXBException; - -import org.bench4q.share.communication.HttpRequester.HttpResponse; - -public class scriptOperateTest extends TestBase { - private final String _url = TestBase.BASE_URL + "/RecordScript"; - - public void deleteScript(int scriptId) throws IOException { - Map params = new HashMap(); - params.put("scriptId", scriptId + ""); - Map properties = new HashMap(); - properties.put(this.AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER - + this.accessTocken); - HttpResponse httpResponse = this.httpRequester.sendPost(this._url - + "/deleteScript", params, properties); - System.out.println(httpResponse.getContent()); - } - - public static void main(String[] args) throws IOException, JAXBException { - scriptOperateTest test = new scriptOperateTest(); - test.accessTocken = test.login(); - test.deleteScript(2); - } -} diff --git a/src/test/java/org/bench4q/master/test/service/TestTestPlanScriptResultService.java b/src/test/java/org/bench4q/master/test/service/TestTestPlanScriptResultService.java new file mode 100644 index 00000000..953015de --- /dev/null +++ b/src/test/java/org/bench4q/master/test/service/TestTestPlanScriptResultService.java @@ -0,0 +1,95 @@ +package org.bench4q.master.test.service; + +import static org.junit.Assert.*; + +import java.util.List; +import java.util.UUID; + +import org.bench4q.master.api.modelfactory.BusinessModelMapFactory; +import org.bench4q.master.entity.db.User; +import org.bench4q.master.test.TestBase_MakeUpTestPlan; +import org.bench4q.master.test.testplan.TestPlanTester; +import org.bench4q.master.testplan.entity.TestPlanInBusiness; +import org.bench4q.share.models.master.ScriptBehaviorsBriefModel; +import org.bench4q.share.models.master.ScriptBriefResultModel; +import org.bench4q.share.models.master.ScriptPageBriefModel; +import org.bench4q.share.models.master.TestPlanBusinessModel; +import org.bench4q.share.models.master.ValueTimeModel; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "classpath:service-test-context.xml" }) +public class TestTestPlanScriptResultService extends TestBase_MakeUpTestPlan { + + @Test + public void testGetSpecficFieldValue() throws NoSuchFieldException { + long averageResponseTime = this.getTestPlanScriptResultService() + .getSpecificFieldValue(buildScriptBriefResultModel(0), + "averageResponseTime"); + assertEquals(0, averageResponseTime); + } + + @Test + public void testLoadAvgFromScriptBriefResultWithWrongTestPlanID() + throws NoSuchFieldException, SecurityException { + List results = this + .getTestPlanScriptResultService() + .loadScriptBriefSpecificField( + UUID.fromString("af67ccd7-ca7b-4dbf-9544-8ffe81b3af2d"), + 61, + ScriptBriefResultModel.class.getDeclaredField( + "averageResponseTime").getName()); + assertEquals(0, results.size()); + } + + @Test + public void testLoadAvgFromScriptBriefResult() throws NoSuchFieldException, + SecurityException { + List results = this.getTestPlanScriptResultService() + .loadScriptBriefSpecificField( + this.getTestPlanRunIdUuid(), + this.getScriptId(), + ScriptBriefResultModel.class.getDeclaredField( + "averageResponseTime").getName()); + System.out.println(results.size()); + assertTrue(results.size() > 0); + assertTrue(results.get(1).getValue() % 100 == 0); + } + + @Before + public void makeUpTestPlanAndTestPlanScriptAndTestPlanScriptResult() { + UUID testPlanRunId = UUID.randomUUID(); + User user = this.getUserService().getUserByName("admin"); + this.setScriptId(this.getScriptService().loadScripts(user).get(0) + .getId()); + TestPlanBusinessModel model = TestPlanTester.createATestPlan(this + .getScriptId()); + TestPlanInBusiness testPlan = BusinessModelMapFactory.toBusiness(model); + this.getTestPlanService().saveTestPlanToDB(testPlan, user, + testPlanRunId, testPlan.getRunningScripts()); + this.setTestPlanRunIdUuid(testPlanRunId); + for (int i = 0; i < 3; i++) { + this.getTestPlanScriptService().saveScriptBriefResult( + testPlanRunId, this.getScriptId(), + buildScriptBriefResultModel(i)); + this.getTestPlanScriptService().saveBehaviorsBriefResult( + testPlanRunId, this.getScriptId(), + new ScriptBehaviorsBriefModel()); + this.getTestPlanScriptService().savePageBriefResult(testPlanRunId, + this.getScriptId(), new ScriptPageBriefModel()); + } + + } + + @After + public void cleanUp() { + this.getTestPlanService().removeTestPlanInDB( + this.getTestPlanService() + .getTestPlan(this.getTestPlanRunIdUuid()).getId()); + } +} diff --git a/src/test/java/org/bench4q/master/test/service/TestTestPlanScriptService.java b/src/test/java/org/bench4q/master/test/service/TestTestPlanScriptService.java index ef03ea58..6bba91c2 100644 --- a/src/test/java/org/bench4q/master/test/service/TestTestPlanScriptService.java +++ b/src/test/java/org/bench4q/master/test/service/TestTestPlanScriptService.java @@ -41,7 +41,7 @@ public class TestTestPlanScriptService { .getTestPlanScript(WRONG_SCRIPT_ID, UUID.fromString(WRONG_TEST_PLAN_RUN_ID)); List scriptBriefResultModels = this - .getTestPlanScriptService().queryTestPlanScriptResult( + .getTestPlanScriptService().queryScriptBriefResults( testPlanScript); assertTrue(scriptBriefResultModels.size() == 0); } @@ -52,7 +52,7 @@ public class TestTestPlanScriptService { .getTestPlanScript(PROPER_SCRIPT_ID, UUID.fromString(TEST_PLAN_RUN_ID)); List scriptBriefList = this - .getTestPlanScriptService().queryTestPlanScriptResult( + .getTestPlanScriptService().queryScriptBriefResults( testPlanScript); assertTrue(scriptBriefList.size() > 0); } @@ -72,7 +72,7 @@ public class TestTestPlanScriptService { .getTestPlanScript(WRONG_SCRIPT_ID, UUID.fromString(WRONG_TEST_PLAN_RUN_ID)); assertTrue(this.getTestPlanScriptService() - .queryTestPlanScriptResult(testPlanScript).size() == 0); + .queryScriptBriefResults(testPlanScript).size() == 0); } } diff --git a/src/test/java/org/bench4q/master/test/testplan/TestPlanTester.java b/src/test/java/org/bench4q/master/test/testplan/TestPlanTester.java index c20b503d..299a4410 100644 --- a/src/test/java/org/bench4q/master/test/testplan/TestPlanTester.java +++ b/src/test/java/org/bench4q/master/test/testplan/TestPlanTester.java @@ -12,7 +12,7 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; -import org.bench4q.master.test.TestBase; +import org.bench4q.master.test.controller.TestBase; import org.bench4q.share.communication.HttpRequester; import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.enums.master.TestPlanStatus; @@ -30,7 +30,7 @@ public class TestPlanTester extends TestBase { private TestPlanBusinessModel testPlanBusinessModel = new TestPlanBusinessModel(); private String _url = TestBase.BASE_URL + "/testPlan"; private int scriptSumNum; - private static int SCRIPTID1 = 61; + public static int SCRIPTID1 = 61; private static int SCRIPTID2 = 2; // private static int EACH_SCRIPT_LOAD_LargeSCALE = 12000; private static int EACH_SCRIPT_LOAD_SMALLSCALE = 40; @@ -144,17 +144,18 @@ public class TestPlanTester extends TestBase { return stringWriter.toString(); } - public static TestPlanBusinessModel createATestPlan() { + public static TestPlanBusinessModel createATestPlan(int scriptId) { TestPlanBusinessModel testPlanBusinessModel = new TestPlanBusinessModel(); testPlanBusinessModel.setMonitorModels(createOneMonitorList()); - testPlanBusinessModel.setRunningScriptModels(createOneScriptList()); + testPlanBusinessModel + .setRunningScriptModels(createOneScriptList(SCRIPTID1)); return testPlanBusinessModel; } public TestPlanBusinessModel _createATestPlan() { if (this.getScriptSumNum() == 1) { this.testPlanBusinessModel - .setRunningScriptModels(createOneScriptList()); + .setRunningScriptModels(createOneScriptList(SCRIPTID1)); } else if (getScriptSumNum() == 2) { this.testPlanBusinessModel .setRunningScriptModels(createTowScriptList()); @@ -171,9 +172,9 @@ public class TestPlanTester extends TestBase { return monitors; } - private static List createOneScriptList() { + private static List createOneScriptList(int scriptId) { List list = new ArrayList(); - list.add(buildScriptModel(SCRIPTID1)); + list.add(buildScriptModel(scriptId)); return list; } diff --git a/src/test/java/org/bench4q/master/test/testplan/TestTestPlanResult.java b/src/test/java/org/bench4q/master/test/testplan/TestTestPlanResult.java index a20f4afd..dd6a54b3 100644 --- a/src/test/java/org/bench4q/master/test/testplan/TestTestPlanResult.java +++ b/src/test/java/org/bench4q/master/test/testplan/TestTestPlanResult.java @@ -6,10 +6,10 @@ import java.io.IOException; import javax.xml.bind.JAXBException; -import org.bench4q.master.test.TestBase; +import org.bench4q.master.test.controller.TestBase; import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.helper.MarshalHelper; -import org.bench4q.share.models.master.ScriptProcessModel; +import org.bench4q.share.models.master.ResultLoadModel; import org.junit.Test; public class TestTestPlanResult extends TestBase { @@ -26,8 +26,8 @@ public class TestTestPlanResult extends TestBase { + testPlanRunId, null, makeAccessTockenMap(this.getAccessTocken())); assertTrue(httpResponse != null); - ScriptProcessModel resultModel = (ScriptProcessModel) MarshalHelper - .unmarshal(ScriptProcessModel.class, httpResponse.getContent()); - assertTrue(resultModel.getScriptBriefResultModels().size() > 0); + ResultLoadModel resultModel = (ResultLoadModel) MarshalHelper + .unmarshal(ResultLoadModel.class, httpResponse.getContent()); + assertTrue(resultModel.getValueTimeModels().size() > 0); } }