add all tests about handle the status of the test plan

This commit is contained in:
coderfengyun 2014-02-17 14:22:18 +08:00
parent 4a7e4ec794
commit 20f8e1e0f0
6 changed files with 169 additions and 23 deletions

View File

@ -277,10 +277,9 @@ public class TestPlanController extends BaseController {
}
List<TestPlanDB> testPlanDBs = this.testPlanService.loadTestPlans(this
.getPrincipal());
if (testPlanDBs == null) {
return buildTestPlanResponseModel(false, "exception", null);
}
return buildTestPlanResponseModel(true, null, testPlanDBs);
return testPlanDBs == null ? buildTestPlanResponseModel(false,
"exception", null) : buildTestPlanResponseModel(true, null,
testPlanDBs);
}
@RequestMapping(value = "/queryTestPlan/{runId}", method = RequestMethod.GET)

View File

@ -1,11 +1,13 @@
package org.bench4q.master.repository;
import org.apache.log4j.Logger;
import org.bench4q.master.helper.SessionHelper;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class AbstractRepositoty {
private SessionHelper sessionHelper;
protected Logger logger = Logger.getLogger(AbstractRepositoty.class);
protected SessionHelper getSessionHelper() {
return sessionHelper;

View File

@ -5,7 +5,9 @@ import java.util.UUID;
import org.bench4q.master.entity.TestPlanDB;
import org.bench4q.master.entity.User;
import org.bench4q.master.exception.ExceptionLog;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
@ -23,7 +25,7 @@ public class TestPlanRepository extends AbstractRepositoty {
return result;
}
public List<TestPlanDB> loadTestPlans(User user) {
public List<TestPlanDB> loadEntities(User user) {
Session session = this.getSessionHelper().openSession();
try {
@SuppressWarnings("unchecked")
@ -31,12 +33,26 @@ public class TestPlanRepository extends AbstractRepositoty {
.add(Restrictions.eq("user", user)).list();
return ret;
} catch (Exception e) {
e.printStackTrace();
logger.error(ExceptionLog.getStackTrace(e));
return null;
} finally {
if (session != null) {
session.close();
releaseSession(session);
}
}
public boolean updateEntity(TestPlanDB testPlanDB) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
session.merge(testPlanDB);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
} finally {
releaseSession(session);
}
}
}

View File

@ -178,20 +178,7 @@ public class TestPlanService {
}
public List<TestPlanDB> loadTestPlans(User user) {
Session session = this.getSessionHelper().openSession();
try {
@SuppressWarnings("unchecked")
List<TestPlanDB> ret = session.createCriteria(TestPlanDB.class)
.add(Restrictions.eq("user", user)).list();
return ret;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (session != null) {
session.close();
}
}
return this.getTestPlanRepository().loadEntities(user);
}
public boolean removeTestPlanInDB(int testPlanId) {
@ -279,6 +266,10 @@ public class TestPlanService {
updateStatus(TestPlanStatus.Complete, testPlanRunID, 0);
}
public void handleTestPlanNotStart(UUID testPlanRunID) {
updateStatus(TestPlanStatus.NotStart, testPlanRunID, 0);
}
private void updateStatus(TestPlanStatus status, UUID testPlanRunID,
int failTimes) {
Session session = this.getSessionHelper().openSession();
@ -328,4 +319,8 @@ public class TestPlanService {
return this.getBusinessMapFactory().toModel(
this.getTestPlanRepository().getTestPlan(runId));
}
public String getTestPlanStatus(UUID runId) {
return this.getTestPlan(runId).getCurrentStatus();
}
}

View File

@ -4,7 +4,11 @@ import static org.junit.Assert.*;
import java.util.UUID;
import org.bench4q.master.entity.TestPlanDB;
import org.bench4q.master.repository.TestPlanRepository;
import org.bench4q.master.repository.UserRepository;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -15,6 +19,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(locations = { "classpath:repository-test-context.xml" })
public class Test_TestPlanRepository {
private TestPlanRepository testPlanRepository;
private UserRepository userRepository;
private TestPlanRepository getTestPlanRepository() {
return testPlanRepository;
@ -25,6 +30,15 @@ public class Test_TestPlanRepository {
this.testPlanRepository = testPlanRepository;
}
private UserRepository getUserRepository() {
return userRepository;
}
@Autowired
private void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Test
public void testGetEntity() {
fail("Not yet implemented");
@ -35,4 +49,33 @@ public class Test_TestPlanRepository {
assertNotNull(this.getTestPlanRepository().getTestPlan(
UUID.fromString("4c894c8d-712b-44e0-97f8-84667591953a")));
}
@Test
public void testLoadEntities() {
assertTrue(this.getTestPlanRepository()
.loadEntities(this.getUserRepository().getUser("admin")).size() > 0);
}
@Test
public void testUpdateEntity() {
TestPlanDB testPlanDB = this.getTestPlanRepository().getTestPlan(
UUID.fromString("4c894c8d-712b-44e0-97f8-84667591953a"));
assertNotNull(testPlanDB);
assertEquals(TestPlanStatus.NotStart.name(),
testPlanDB.getCurrentStatus());
testPlanDB.setCurrentStatus(TestPlanStatus.Complete.name());
this.getTestPlanRepository().updateEntity(testPlanDB);
TestPlanDB afterModify = this.getTestPlanRepository().getTestPlan(
UUID.fromString("4c894c8d-712b-44e0-97f8-84667591953a"));
assertEquals(TestPlanStatus.Complete.name(),
afterModify.getCurrentStatus());
}
@After
public void restoreStatus() {
TestPlanDB testPlanDB = this.getTestPlanRepository().getTestPlan(
UUID.fromString("4c894c8d-712b-44e0-97f8-84667591953a"));
testPlanDB.setCurrentStatus(TestPlanStatus.NotStart.name());
this.getTestPlanRepository().updateEntity(testPlanDB);
}
}

View File

@ -0,0 +1,91 @@
package org.bench4q.master.test.service;
import static org.junit.Assert.*;
import java.util.UUID;
import org.bench4q.master.service.infrastructure.TestPlanService;
import org.bench4q.master.service.infrastructure.UserService;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.junit.After;
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_TestPlanService {
private TestPlanService testPlanService;
private UserService userService;
public UUID testUUID = UUID
.fromString("4c894c8d-712b-44e0-97f8-84667591953a");
public TestPlanService getTestPlanService() {
return testPlanService;
}
@Autowired
private void setTestPlanService(TestPlanService testPlanService) {
this.testPlanService = testPlanService;
}
private UserService getUserService() {
return userService;
}
@Autowired
private void setUserService(UserService userService) {
this.userService = userService;
}
@Test
public void testLoadTestPlans() {
assertTrue(this.getTestPlanService()
.loadTestPlans(this.getUserService().getUserByName("admin"))
.size() > 0);
}
@Test
public void testHandleTestPlanComplete() {
this.getTestPlanService().handleTestPlanComplete(testUUID);
assertEquals(TestPlanStatus.Complete.name(),
testPlanService.getTestPlanStatus(testUUID));
}
@Test
public void testHandleTestPlanPendingNoEnoughMaxLoad() {
this.getTestPlanService()
.handleTestPlanPendingNoEnoughMaxLoad(testUUID);
assertEquals(TestPlanStatus.PendingNoEnoughMaxLoad.name(),
testPlanService.getTestPlanStatus(testUUID));
}
@Test
public void testHandleTestPlanPendingNoEnoughCurrentLoad() {
this.getTestPlanService().handleTestPlanPendingNoEnoughCurrentLoad(
testUUID);
assertEquals(TestPlanStatus.PendingNoEnoughCurrentLoad.name(),
testPlanService.getTestPlanStatus(testUUID));
}
@Test
public void testHandleTestPlanError() {
this.getTestPlanService().handleTestPlanError(testUUID);
assertEquals(TestPlanStatus.Error.name(),
testPlanService.getTestPlanStatus(testUUID));
}
@Test
public void testHandleTestPlanRunning() {
this.getTestPlanService().handleTestPlanRunning(testUUID);
assertEquals(TestPlanStatus.InRunning.name(), this.getTestPlanService()
.getTestPlanStatus(testUUID));
}
@After
public void cleanUp() {
this.getTestPlanService().handleTestPlanNotStart(testUUID);
}
}