refactor the test

there are two class to test : Test_RunningAgent, Test_RunningScript
This commit is contained in:
coderfengyun 2014-05-13 17:29:17 +08:00
parent 5b28e3f189
commit 287eb2ad7d
32 changed files with 203 additions and 300 deletions

View File

@ -1,58 +0,0 @@
package org.bench4q.master.api;
import java.util.UUID;
import org.bench4q.master.domain.service.TestPlanScriptResultService;
import org.bench4q.master.domain.service.UserService;
import org.bench4q.master.exception.Bench4QException;
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;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/testPlanResult")
public class TestPlanResultController extends BaseController {
private TestPlanScriptResultService testPlanScriptResultService;
public TestPlanScriptResultService getTestPlanScriptResultService() {
return testPlanScriptResultService;
}
@Autowired
public void setTestPlanScriptResultService(
TestPlanScriptResultService testPlanScriptResultService) {
this.testPlanScriptResultService = testPlanScriptResultService;
}
@RequestMapping(value = "/{testPlanRunId}/loadScriptBirefResults/{scriptId}/{fieldName}", method = RequestMethod.GET)
@ResponseBody
public ResultLoadModel loadScriptBriefResults(
@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}");
}
ResultLoadModel ret = new ResultLoadModel();
ret.setValueTimeModels(this.getTestPlanScriptResultService()
.loadScriptBriefSpecificField(testPlanRunId, scriptId,
fieldName));
return ret;
}
@RequestMapping(value = "/{testPlanRunId}/loadBehaviorsBriefResults/{scriptId}/{fieldName}", method = RequestMethod.GET)
@ResponseBody
public ResultLoadModel loadBehaviorsBriefResults(
@PathVariable UUID testPlanRunId, @PathVariable int scriptId,
@PathVariable String fieldName) throws Bench4QException {
if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
throw new Bench4QException("400", "not permitted",
"/{testPlanRunId}/loadBehaviorsBriefResults/{scriptId}/{fieldName}");
}
return null;
}
}

View File

@ -236,7 +236,7 @@ public class TestPlanScript implements RunningScriptInterface {
.getResultModelFromAgent();
List<TestPlanScriptResult> testPlanScriptResultList = this
.getTestPlanFactory().createScriptResultsWithoutId(
scriptResultModel, testPlan, this, new Date());
scriptResultModel, this, new Date());
return testPlanScriptResultList;
} catch (Exception e) {
logger.info(ExceptionLog.getStackTrace(e));

View File

@ -175,8 +175,8 @@ public class TestPlanFactory {
// new interface
public List<TestPlanScriptResult> createScriptResultsWithoutId(
ScriptResultModel scriptResultModel, TestPlan testPlan,
TestPlanScript testPlanScript, Date createDatetime) {
ScriptResultModel scriptResultModel, TestPlanScript testPlanScript,
Date createDatetime) {
List<TestPlanScriptResult> testPlanScriptResults = new LinkedList<TestPlanScriptResult>();
Field[] fields = scriptResultModel.getClass().getDeclaredFields();

View File

@ -73,6 +73,25 @@ public class AgentRepository extends AbstractRepositoty {
}
}
public boolean detach(String hostName) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Agent agent = getAgentBy(hostName, session);
if (agent == null) {
return false;
}
session.delete(agent);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
return false;
} finally {
releaseSession(session);
}
}
public boolean update(Agent agentForUpdate) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
@ -113,8 +132,7 @@ public class AgentRepository extends AbstractRepositoty {
public Agent getAgentBy(String hostName) {
Session session = this.getSessionHelper().openSession();
try {
Agent agent = (Agent) session.createCriteria(Agent.class)
.add(Restrictions.eq("hostName", hostName)).uniqueResult();
Agent agent = getAgentBy(hostName, session);
return agent;
} catch (Exception e) {
return null;
@ -123,6 +141,11 @@ public class AgentRepository extends AbstractRepositoty {
}
}
private Agent getAgentBy(String hostName, Session session) {
return (Agent) session.createCriteria(Agent.class)
.add(Restrictions.eq("hostName", hostName)).uniqueResult();
}
public Agent getEntity(int id) {
Session session = this.getSessionHelper().openSession();
try {

View File

@ -1,13 +1,10 @@
package org.bench4q.master.domain.service;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.entity.TestPlanScriptResult;
@ -15,7 +12,6 @@ import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.SessionHelper;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.ValueTimeModel;
import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel;
import org.bench4q.share.models.master.statistics.ScriptBriefResultModel;
import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel;
@ -27,22 +23,11 @@ import org.springframework.stereotype.Component;
@Component
public class TestPlanScriptResultService {
private TestPlanScriptService testPlanScriptService;
private TestPlanRepository testPlanRepository;
private SessionHelper sessionHelper;
private static Logger logger = Logger
.getLogger(TestPlanScriptResultService.class);
private TestPlanScriptService getTestPlanScriptService() {
return testPlanScriptService;
}
@Autowired
private void setTestPlanScriptService(
TestPlanScriptService testPlanScriptService) {
this.testPlanScriptService = testPlanScriptService;
}
public SessionHelper getSessionHelper() {
return sessionHelper;
}
@ -61,58 +46,6 @@ public class TestPlanScriptResultService {
this.testPlanRepository = testPlanRepository;
}
/**
* Here I just use the Time that createDateTime in testplanscriptResult, If
* it's not accuracy, i just need to modify the way of pick createDateTime
*
* @param testPlanId
* @param scriptId
* @return
*/
public List<ValueTimeModel> loadScriptBriefSpecificField(UUID testPlanId,
int scriptId, String fieldName) {
List<TestPlanScriptResult> scriptBriefresults = this
.getTestPlanScriptService().queryScriptBriefResults(testPlanId,
scriptId);
List<ValueTimeModel> ret = new ArrayList<ValueTimeModel>();
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.getStackTrace(e)
+ " When unmarshal the model from ResultCOntent");
continue;
} catch (NoSuchFieldException e) {
logger.info(ExceptionLog.getStackTrace(e)
+ " When get Field from the model from ResultCOntent");
continue;
}
}
return ret;
}
private 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;
}
public List<ScriptBriefResultModel> loadScriptBriefWithDuation(
UUID testPlanId, int scriptId, long startTime) {
Session session = this.getSessionHelper().openSession();
@ -136,7 +69,8 @@ public class TestPlanScriptResultService {
UUID testPlanId, int scriptId, long startTime, Class<?> resultType,
Session session) {
TestPlanScript testPlanScript = this.getTestPlanRepository()
.getTestPlanInDomainBy(testPlanId).extracSpecifiedScript(scriptId);
.getTestPlanInDomainBy(testPlanId)
.extracSpecifiedScript(scriptId);
@SuppressWarnings("unchecked")
List<TestPlanScriptResult> results = (List<TestPlanScriptResult>) session
.createCriteria(TestPlanScriptResult.class)
@ -169,7 +103,8 @@ public class TestPlanScriptResultService {
private TestPlanScriptResult doGetLastSampleResult(UUID testPlanId,
int scriptId, Class<?> resultType, Session session) {
TestPlanScript testPlanScript = this.getTestPlanRepository()
.getTestPlanInDomainBy(testPlanId).extracSpecifiedScript(scriptId);
.getTestPlanInDomainBy(testPlanId)
.extracSpecifiedScript(scriptId);
@SuppressWarnings("unchecked")
List<TestPlanScriptResult> results = (List<TestPlanScriptResult>) session
.createCriteria(TestPlanScriptResult.class)

View File

@ -155,6 +155,7 @@ public class HighAvailablePoolImpl extends CurrentLoadSubject implements
continue;
}
runningAgent.substituteOnBoard();
this.getAgentRunIdShouldBeSubstitute().remove(agentRunId);
}
}

View File

@ -25,7 +25,6 @@ public class Test_HighAvailableWithActualMessenger extends
@Before
public void prepare() {
}
@After

View File

@ -1,4 +1,4 @@
package org.bench4q.master.unitTest.service;
package org.bench4q.master.integrated;
import static org.junit.Assert.*;

View File

@ -2,7 +2,6 @@ package org.bench4q.master.integrated;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Set;
import org.bench4q.master.domain.entity.RunningAgentDB;
@ -11,7 +10,6 @@ import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.models.master.ValueTimeModel;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -77,12 +75,6 @@ public class Test_TestPlan extends TestBase_MakeUpTestPlan {
assertNotNull(runningAgentDB);
assertEquals(EACH_SCRIPT_LOAD_SMALLSCALE, runningAgentDB.getLoadInUse());
Thread.sleep(10000);
List<ValueTimeModel> valueTimeModels = this
.getTestPlanScriptResultService().loadScriptBriefSpecificField(
getTestPlanRunIdUuid(), getScriptId(),
"averageResponseTime");
assertNotNull(valueTimeModels);
assertTrue(valueTimeModels.size() > 0);
while (!TestPlanStatus.valueOf(
this.getTestPlanRepository()
.getTestPlanInDomainBy(getTestPlanRunIdUuid())

View File

@ -1,4 +1,4 @@
package org.bench4q.master.unitTest.infrastructure.communication;
package org.bench4q.master.integrated.communication;
import static org.junit.Assert.*;

View File

@ -1,4 +1,4 @@
package org.bench4q.master.unitTest.infrastructure.communication;
package org.bench4q.master.integrated.communication;
import static org.junit.Assert.assertNotNull;

View File

@ -10,6 +10,7 @@ import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.AgentModel;
import org.bench4q.share.models.master.AgentResponseModel;
import org.junit.Before;
import org.junit.Test;
public class AgentPoolControllerTest extends TestBase {
@ -17,6 +18,11 @@ public class AgentPoolControllerTest extends TestBase {
private final static String HOSTNAME1 = "127.0.0.1";
private final static String HOSTNAME2 = "133.133.12.9";
@Before
public void prepare() {
this.setAccessTocken(this.login());
}
@Test
public void testAdd() throws JAXBException, IOException {
this.setAccessTocken(this.login());

View File

@ -66,6 +66,7 @@ public class PluginControllerTest extends TestBase {
this.paramInPluginCount = 7;
this.getTest_PluginHelper().addPlugin(fileName,
this.getPluginService(), pluginName);
this.setAccessTocken(this.login());
}
@Test

View File

@ -7,12 +7,18 @@ import java.util.Map;
import javax.xml.bind.JAXBException;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.junit.Before;
public class RecordPortControllerTest extends TestBase {
private final String URLSTRING = BASE_URL + "/RecordPort";
private final String PORT = "1100";
private final String PARAM1 = "port";
@Before
public void prepare() {
this.setAccessTocken(this.login());
}
public void AddPortToPool() throws IOException, JAXBException {
String accesTocken = this.login();
Map<String, String> params = new HashMap<String, String>();

View File

@ -13,12 +13,18 @@ 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.junit.Before;
import org.junit.Test;
public class RecordScriptControllerTest extends TestBase {
private static final int RECORD_TIME = 60000;
private final String SCRIPT_URL = TestBase.BASE_URL + "/RecordScript";
@Before
public void prepare() {
this.setAccessTocken(this.login());
}
public OperateScriptServerResponseModel startRecord() throws IOException,
JAXBException {
HttpResponse httpResponse = this.httpRequester.sendPost(this.SCRIPT_URL

View File

@ -11,12 +11,18 @@ import javax.xml.bind.JAXBException;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.OperateScriptServerResponseModel;
import org.junit.Before;
import org.junit.Test;
public class ScriptControllerTest extends TestBase {
private String controllerUrl = BASE_URL + "/RecordScript";
@Before
public void prepare() {
this.setAccessTocken(this.login());
}
@Test
public void loadScriptsTest() throws IOException, JAXBException {
String url = this.controllerUrl + "/loadScriptList";

View File

@ -28,7 +28,6 @@ public class TestBase {
}
public TestBase() {
this.setAccessTocken(this.login());
}
public String login() {
@ -39,7 +38,7 @@ public class TestBase {
HttpResponse httpResponse;
try {
httpResponse = this.httpRequester.sendGet(urlString, map, null);
System.out.println("http content:"+httpResponse.getContent());
System.out.println("http content:" + httpResponse.getContent());
return ((AuthorizeResponseModel) MarshalHelper.unmarshal(
AuthorizeResponseModel.class, httpResponse.getContent()))
.getAccessToken();

View File

@ -25,6 +25,11 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class TestPlanScriptResultControllerTest extends TestBase_MakeUpTestPlan {
private String urlString = BASE_URL + "/testPlanResult";
@Before
public void prepare() {
this.setAccessTocken(this.login());
}
@Test
public void testGetScriptResults() throws IOException, JAXBException {
HttpResponse httpResponse = this.httpRequester.sendGet(urlString + "/"

View File

@ -8,14 +8,18 @@ import java.util.Map;
import javax.xml.bind.JAXBException;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.communication.HttpRequester;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.MonitorMemoryResponseModel;
import org.junit.Before;
import org.junit.Test;
public class Test_MonitorController extends TestBase_MakeUpTestPlan {
public class Test_MonitorController extends TestBase {
@Before
public void prepare() {
this.setAccessTocken(this.login());
}
@Test
public void testMemoryInfo() throws IOException, JAXBException {

View File

@ -27,6 +27,7 @@ import org.bench4q.share.models.master.TestPlanResultModel;
import org.bench4q.share.models.master.TestPlanScriptBriefResultModel;
import org.bench4q.share.models.master.statistics.ScriptBriefResultModel;
import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@ -72,6 +73,11 @@ public class Test_TestPlanController extends TestBase_MakeUpTestPlan {
this.runAndGetBrief();
}
@Before
public void prepare() {
this.setAccessTocken(this.login());
}
@Test
public void testWithoutBrief() throws JAXBException, IOException,
InterruptedException {

View File

@ -9,12 +9,18 @@ import javax.xml.bind.JAXBException;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.ResultLoadModel;
import org.junit.Before;
import org.junit.Test;
public class Test_TestPlanResultController extends TestBase {
private static String URL = BASE_URL + "/TestPlanResult";
private static String testPlanRunId = "0cdc3398-5b61-4bff-be48-8075e5d2fa64";
@Before
public void prepare() {
this.setAccessTocken(this.login());
}
@Test
public void test() {
fail("Not yet implemented");

View File

@ -10,11 +10,16 @@ import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.AuthorizeResponseModel;
import org.bench4q.share.models.master.RegisterResponseModel;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class UserControllerTest extends TestBase {
@Before
public void prepare() {
this.setAccessTocken(this.login());
}
@Test
public void testNormalAuth() throws IOException, JAXBException {
@ -45,7 +50,7 @@ public class UserControllerTest extends TestBase {
clearUser(userName);
String url = BASE_URL + "/user/register";
Map<String, String> params = new HashMap<String, String>();
params.put("userName", userName);
params.put("password", "test");
params.put("scope", "1");
@ -68,7 +73,7 @@ public class UserControllerTest extends TestBase {
AuthorizeResponseModel authorizeResponseModel = (AuthorizeResponseModel) MarshalHelper
.tryUnmarshal(AuthorizeResponseModel.class,
httpResponse.getContent());
return authorizeResponseModel;
}
}

View File

@ -8,11 +8,10 @@ import java.util.UUID;
import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.factory.RunningAgentFactory;
import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.helper.SessionHelper;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.infrastructure.highavailable.impl.HighAvailablePoolImpl;
import org.bench4q.share.communication.HttpRequester;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.AgentStatus;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -20,14 +19,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import stubs.Mock_AgentMessenger;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { HighAvailablePoolImpl.class,
AgentRepository.class, Mock_AgentMessenger.class, HttpRequester.class,
SessionHelper.class, RunningAgentFactory.class })
public class Test_highAvailableWithMockMessenger {
@ContextConfiguration(locations = { "classpath:mockHttpMesseger-context.xml" })
public class Test_highAvailableWithMockMessenger extends
TestBase_MakeUpTestPlan {
private List<Agent> testCase1 = new ArrayList<Agent>();
{
testCase1.add(Agent.createAgentWithoutId("127.0.0.1", 6565, 500));
@ -42,12 +37,40 @@ public class Test_highAvailableWithMockMessenger {
testCase1.add(agent2);
}
private List<Agent> testcase2 = new ArrayList<Agent>();
{
testcase2.add(Agent.createAgentWithoutId("127.0.0.1", 6565));
testcase2.add(Agent.createAgentWithoutId("127.0.0.2", 6565));
}
@Autowired
private HighAvailablePoolImpl ha;
@Test
public void test_BreakDown() {
assertNotNull(this.ha);
this.prepareWith(this.testcase2);
synchronized (this.ha) {
assertEquals(1000, this.ha.getCurrentAvailableLoad());
}
this.submitATestPlanWithOneScript();
TestPlan testPlan = this.getTestPlanRepository().getTestPlanInDomainBy(
getTestPlanRunIdUuid());
testPlan.run();
// Let ha get the agent's runIds
this.ha.checkAllHeartBeat();
TestPlanScript runningScript = testPlan
.extracSpecifiedScript(getScriptId());
assertEquals(1, runningScript.getRunningAgents().size());
RunningAgentInterface runningAgentInterface = (RunningAgentInterface) runningScript
.getRunningAgents().toArray()[0];
// Kill the agent In run
runningAgentInterface.getAgent().setCurrentEnumStatus(
AgentStatus.BreakDown);
// activate ha ckeckAllHeartBeats
this.ha.checkAllHeartBeat();
assertEquals(2, runningScript.getRunningAgents().size());
this.cleanUpWith(this.testcase2);
}
@Test
@ -59,15 +82,22 @@ public class Test_highAvailableWithMockMessenger {
private void prepareWith(List<Agent> agents) {
this.ha.getPool().clear();
for (Agent agent : this.testCase1) {
for (Agent agent : agents) {
this.ha.add(agent);
}
this.ha.checkAllHeartBeat();
}
private void cleanUpWith(List<Agent> agents) {
for (Agent agent : agents) {
this.getAgentRepository().detach(agent.getId());
}
}
@Test
public void test_applyFor() {
prepareWith(this.testcase2);
int loadToApply = 10;
this.ha.checkAllHeartBeat();
List<RunningAgentInterface> result = null;
synchronized (this.ha) {
assertTrue(this.ha.getCurrentAvailableLoad() > loadToApply);

View File

@ -11,6 +11,7 @@ import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.domain.service.MonitorResultService;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
@ -28,13 +29,15 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
@ContextConfiguration(locations = { "classpath:mockHttpMesseger-context.xml" })
public class Test_MonitorResultService extends TestBase_MakeUpTestPlan {
private String monitor = "133.133.12.3";
private int port = 5556;
private MonitorResultService monitorResultService;
private TestPlanRepository testPlanRepository;
private TestPlanFactory testPlanFactory;
@Autowired
private AgentService agentService;
public TestPlanFactory getTestPlanFactory() {
return testPlanFactory;
@ -72,12 +75,15 @@ public class Test_MonitorResultService extends TestBase_MakeUpTestPlan {
@Before
public void makeUpTestPlanAndTestPlanScriptAndTestPlanScriptResult()
throws JAXBException {
this.agentService.addAgentToPool(Agent.createAgentWithoutId(
"127.0.0.1", 6565));
prepareForTestPlanRunning();
}
@After
public void cleanUp() {
cleanUpForTestPlanRunning();
this.getAgentRepository().detach(
this.getAgentRepository().getAgentBy("127.0.0.1").getId());
}
@Test
@ -124,5 +130,6 @@ public class Test_MonitorResultService extends TestBase_MakeUpTestPlan {
assertTrue(networkInterfaceModels.size() >= 1);
cleanUpForTestPlanRunning();
}
}

View File

@ -18,7 +18,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
@ContextConfiguration(locations = { "classpath:mockHttpMesseger-context.xml" })
public class Test_PluginService extends Test_PluginHelper {
private PluginService pluginService;
private String pluginName;

View File

@ -2,8 +2,6 @@ package org.bench4q.master.unitTest.service;
import static org.junit.Assert.*;
import java.util.UUID;
import org.bench4q.master.domain.service.report.ReportService;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.junit.Test;
@ -32,10 +30,4 @@ public class Test_ReportService extends TestBase_MakeUpTestPlan {
assertFalse(this.getReportService().isReportCreated(
getTestPlanRunIdUuid()));
}
@Test
public void testCreateReport() {
this.getReportService().createReport(
UUID.fromString("a75b14de-d99a-49ad-80b8-6ad6078ae726"));
}
}

View File

@ -26,7 +26,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
@ContextConfiguration(locations = { "classpath:mockHttpMesseger-context.xml" })
public class Test_ScriptService {
private ScriptService scriptService;
private UserService userService;

View File

@ -1,38 +1,54 @@
package org.bench4q.master.unitTest.service;
import java.util.Date;
import java.util.UUID;
import org.bench4q.master.Main;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.helper.TestHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
@ContextConfiguration(locations = { "classpath:mockHttpMesseger-context.xml" })
@FixMethodOrder(MethodSorters.JVM)
public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan {
@Autowired
private AgentService agentService;
@Before
public void prepare() {
prepareForTestPlanRunning();
submitATestPlanWithOneScript();
synchronized (Test_TestPlanEngine.class) {
this.getAgentRepository().detach("147.0.0.1");
this.agentService.addAgentToPool(Agent.createAgentWithoutId(
"147.0.0.1", 6565));
prepareForTestPlanRunning();
submitATestPlanWithOneScript();
}
}
@After
public void cleanUp() {
cleanUpForTestPlanRunning();
synchronized (Test_TestPlanEngine.class) {
cleanUpForTestPlanRunning();
Agent agent = null;
if ((agent = this.getAgentRepository().getAgentBy("147.0.01")) != null) {
this.getAgentRepository().detach(agent.getId());
}
}
}
@Test
@ -73,11 +89,8 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan {
public void testRunWithWronglyWhenNoEnoughLoadInPool()
throws InterruptedException {
System.out.println("This need each agent not in running");
User user = this.getUserRepository().getUser("admin");
int scriptId = getUserFirstScript(user);
this.submitATestPlanWithOneScript();
this.getHaPool().getPool().clear();
this.setTestPlanRunIdUuid(this.getTestPlanEngine().runWith(
createATestPlanWithOneScript(scriptId), user));
Thread.sleep(10000);
TestPlan testPlan = this.getTestPlanRepository().getTestPlanInDomainBy(
getTestPlanRunIdUuid());
@ -105,19 +118,20 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan {
TestPlanStatus.valueOf(testPlanPicked.getCurrentStatus()));
}
@Test
public void testPickAndRunByCycle() throws InterruptedException {
this.getHaPool().checkAllHeartBeat();
Main.getPortToServe();
UUID testPlanRunId = this.submitATestPlanWithOneScript();
Thread.sleep(71000);
assertEquals(
TestPlanStatus.InRunning,
TestPlanStatus.valueOf(this.getTestPlanRepository()
.getTestPlanInDomainBy(testPlanRunId)
.getCurrentStatus()));
deleteTestPlan();
}
// @Test
// public void testPickAndRunByCycle() throws InterruptedException {
// this.getHaPool().checkAllHeartBeat();
// assertEquals(500, this.getHaPool().getCurrentAvailableLoad());
// Main.getPortToServe();
// UUID testPlanRunId = this.submitATestPlanWithOneScript();
// Thread.sleep(71000);
// assertEquals(
// TestPlanStatus.InRunning,
// TestPlanStatus.valueOf(this.getTestPlanRepository()
// .getTestPlanInDomainBy(testPlanRunId)
// .getCurrentStatus()));
// deleteTestPlan();
// }
private void testForStatus(TestPlanStatus status) throws Exception {
this.submitATestPlanWithOneScript();

View File

@ -6,10 +6,11 @@ import java.util.List;
import java.util.UUID;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.service.TestResultSave;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.helper.TestHelper;
import org.bench4q.share.models.master.TestPlanModel;
import org.bench4q.share.models.master.ValueTimeModel;
import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel;
import org.bench4q.share.models.master.statistics.ScriptBriefResultModel;
import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel;
@ -17,49 +18,21 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
public class Test_TestPlanScriptResultService extends TestBase_MakeUpTestPlan {
@Test
public void testGetSpecficFieldValue() throws Exception {
long averageResponseTime = (Long) TestHelper.invokePrivate(
this.getTestPlanScriptResultService(), "getSpecificFieldValue",
new Class[] { ScriptBriefResultModel.class, String.class },
new Object[] { buildScriptBriefResultModel(0),
"averageResponseTime" });
assertEquals(0, averageResponseTime);
}
@Autowired
private TestResultSave testResultSave;
@Test
public void testLoadAvgFromScriptBriefResultWithWrongTestPlanID()
throws NoSuchFieldException, SecurityException {
List<ValueTimeModel> results = this
.getTestPlanScriptResultService()
.loadScriptBriefSpecificField(
UUID.fromString("af67ccd7-ca7b-4dbf-9544-8ffe81b3af2d"),
61,
ScriptBriefResultModel.class.getDeclaredField(
"averageResponseTime").getName());
assertEquals(0, results.size());
}
@Autowired
private TestPlanFactory testPlanFactory;
@Test
public void testLoadAvgFromScriptBriefResult() throws NoSuchFieldException,
SecurityException, InterruptedException {
Thread.sleep(5000);
List<ValueTimeModel> 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);
}
@Autowired
private TestPlanRepository testPlanRepository;
@Test
public void testLoadScriptBriefWithWrongTestPlanId() {
@ -85,27 +58,6 @@ public class Test_TestPlanScriptResultService extends TestBase_MakeUpTestPlan {
assertNotNull(behavirosResults.isFinished());
}
@Test
public void testLoadScriptBriefWithDuation() throws InterruptedException {
Thread.sleep(5000);
List<ScriptBriefResultModel> results = this
.getTestPlanScriptResultService().loadScriptBriefWithDuation(
this.getTestPlanRunIdUuid(), this.getScriptId(), 0);
ScriptPagesBriefModel scriptPagesBriefModel = this
.getTestPlanScriptResultService().getScriptPagesBrief(
this.getTestPlanRunIdUuid(), this.getScriptId());
ScriptBehaviorsBriefModel behavirosResults = this
.getTestPlanScriptResultService().getScriptBehaviorsBrief(
this.getTestPlanRunIdUuid(), this.getScriptId());
assertEquals(3, results.size());
assertNotNull(scriptPagesBriefModel);
assertTrue(scriptPagesBriefModel.getScriptPageBriefModels().size() > 0);
assertTrue(scriptPagesBriefModel.getScriptPageBriefModels().get(0)
.getMaxResponseTimeFromBegin() >= 200);
assertNotNull(behavirosResults);
assertNotNull(behavirosResults.isFinished());
}
@Before
public void makeUpTestPlanAndTestPlanScriptAndTestPlanScriptResult() {
UUID testPlanRunId = UUID.randomUUID();
@ -114,35 +66,14 @@ public class Test_TestPlanScriptResultService extends TestBase_MakeUpTestPlan {
TestPlanModel model = createATestPlanWithOneScript(this.getScriptId());
this.getTestPlanEngine().submitTestPlan(model, user, testPlanRunId);
this.setTestPlanRunIdUuid(testPlanRunId);
/*
* for (int i = 0; i < 3; i++) {
* this.getTestScriptResultSave().doSaveResult(testPlanRunId,
* this.getScriptId(), buildScriptBriefResultModel(i), new Date());
*
* this.getTestScriptResultSave().doSaveResult(testPlanRunId,
* this.getScriptId(), new ScriptBehaviorsBriefModel(), new Date());
* this.getTestScriptResultSave().doSaveResult(testPlanRunId,
* this.getScriptId(), makeUpScriptPagesBriefModel(i), new Date()); }
*/
this.testPlanRepository.getTestPlanInDomainBy(getTestPlanRunIdUuid());
}
// private ScriptPagesBriefModel makeUpScriptPagesBriefModel(int i) {
// ScriptPagesBriefModel scriptPagesBriefModel = new
// ScriptPagesBriefModel();
// ScriptPageBriefModel scriptPageBriefModel = new ScriptPageBriefModel();
// scriptPageBriefModel.setMaxResponseTimeFromBegin(200 + 10 * i);
// scriptPageBriefModel.setMinResponseTimeFromBegin(200 - 10 * i);
// scriptPagesBriefModel.getScriptPageBriefModels().add(
// scriptPageBriefModel);
// return scriptPagesBriefModel;
// }
@After
public void cleanUp() {
this.getTestPlanService().removeTestPlanInDB(
this.getTestPlanRepository()
.getTestPlanInDomainBy(this.getTestPlanRunIdUuid())
.getId());
this.testPlanRepository.getTestPlanInDomainBy(
this.getTestPlanRunIdUuid()).getId());
}
}

View File

@ -17,7 +17,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
@ContextConfiguration(locations = { "classpath:mockHttpMesseger-context.xml" })
public class Test_TestPlanScriptService extends TestBase_MakeUpTestPlan {
private static final int WRONG_SCRIPT_ID = 99999;
private static final String WRONG_TEST_PLAN_RUN_ID = "0cdca8da-5b61-4bff-be48-8075e5d2fa64";
@ -26,10 +26,6 @@ public class Test_TestPlanScriptService extends TestBase_MakeUpTestPlan {
public void prepare() {
submitATestPlanWithOneScript();
System.out.println(getScriptId());
/*
* this.getTestScriptResultSave().doSaveResult(getTestPlanRunIdUuid(),
* getScriptId(), new ScriptBriefResultModel(), new Date());
*/
}
@After

View File

@ -45,24 +45,15 @@ public class Test_ScriptLoadCommand extends TestBase_MakeUpTestPlan {
@Before
public void prepare() {
buildUpHaPool();
this.agentService.addAgentToPool(Agent.createAgentWithoutId(
"127.0.0.1", 6565));
}
@After
public void cleanUp() {
Agent agent = this.getAgentRepository().getAgentBy(Test_AGENT_HOSTNAME);
this.getAgentMessenger().stop(agent, this.getAgentRunId());
agent.setRemainLoad(agent.getMaxLoad());
this.getAgentRepository().update(agent);
}
/**
* This need a agent in running
*/
private void buildUpHaPool() {
assertTrue(this.agentService.addAgentToPool(Agent.createAgentWithoutId(
"127.0.0.1", 6565)));
this.getAgentRepository().detach(agent.getId());
}
@Test

View File

@ -45,7 +45,7 @@ public class Mock_AgentMessenger implements AgentMessenger {
result.setScenarioBriefModel(new AgentBriefStatusModel());
result.setPagesBriefModel(new AgentPagesBriefModel());
result.setBehaviorsBriefModel(new AgentBehaviorsBriefModel());
return null;
return result;
}
@Override
@ -64,7 +64,7 @@ public class Mock_AgentMessenger implements AgentMessenger {
} else if (agent.getCurrentEnumStatus() == AgentStatus.BreakDown) {
return null;
} else if (agent.getCurrentEnumStatus() == AgentStatus.InRunning) {
result.getRunningTests().add(UUID.randomUUID());
result.getRunningTests().add(this.testId);
return result;
}
return null;