User repository and user service has been refactored completely
This commit is contained in:
parent
e74f032011
commit
4a7e4ec794
|
@ -292,8 +292,7 @@ public class TestPlanController extends BaseController {
|
|||
throw new Bench4QException(HAVE_NO_POWER, HAVE_NO_POWER,
|
||||
"/queryTestPlan/{runId}");
|
||||
}
|
||||
return BusinessModelMapFactory.toModel(this.getTestPlanService()
|
||||
.getTestPlan(runId));
|
||||
return this.getTestPlanService().getTestPlanDBModel(runId);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/removeTestPlanFromPool", method = {
|
||||
|
@ -315,7 +314,7 @@ public class TestPlanController extends BaseController {
|
|||
result.setFailCause(failCause);
|
||||
List<TestPlanDBModel> modelList = new ArrayList<TestPlanDBModel>();
|
||||
for (TestPlanDB testPlanDB : testPlanDBs) {
|
||||
modelList.add(BusinessModelMapFactory.toModel(testPlanDB));
|
||||
modelList.add(this.getBusinessMapFactory().toModel(testPlanDB));
|
||||
}
|
||||
result.setTestPlanDBModels(modelList);
|
||||
return result;
|
||||
|
|
|
@ -191,7 +191,7 @@ public class BusinessModelMapFactory {
|
|||
return ret;
|
||||
}
|
||||
|
||||
public static TestPlanDBModel toModel(TestPlanDB testPlanDB) {
|
||||
public TestPlanDBModel toModel(TestPlanDB testPlanDB) {
|
||||
TestPlanDBModel ret = new TestPlanDBModel();
|
||||
ret.setCreateDateTime(testPlanDB.getCreateDateTime());
|
||||
ret.setCurrentStatus(testPlanDB.getCurrentStatus());
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package org.bench4q.master.exception;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author coderfengyun
|
||||
*
|
||||
*/
|
||||
public class ExceptionUtils {
|
||||
public static class UserAlreadyExistException extends Exception {
|
||||
private static final long serialVersionUID = -5465994901904292643L;
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return "The User Already exists!";
|
||||
}
|
||||
}
|
||||
|
||||
public static class MultiUserWithSameUserNameExist extends Exception {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return "Multi users exist with the same userName!";
|
||||
}
|
||||
}
|
||||
|
||||
public static class TransactionCommitException extends Exception {
|
||||
private static final long serialVersionUID = -6104657204412644717L;
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return "Transaction commit fails!";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package org.bench4q.master.factory;
|
||||
|
||||
import org.bench4q.master.entity.User;
|
||||
import org.bench4q.master.helper.HashHelper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class UserFactory {
|
||||
private HashHelper hashHelper;
|
||||
|
||||
private HashHelper getHashHelper() {
|
||||
return hashHelper;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setHashHelper(HashHelper hashHelper) {
|
||||
this.hashHelper = hashHelper;
|
||||
}
|
||||
|
||||
public User createUser(String userName, String password) {
|
||||
User user = new User();
|
||||
user.setUserName(userName);
|
||||
user.setPassword(this.getHashHelper().sha1Hash(password));
|
||||
user.setScope((byte) 0);
|
||||
return user;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package org.bench4q.master.repository;
|
||||
|
||||
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 SessionHelper getSessionHelper() {
|
||||
return sessionHelper;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
protected void setSessionHelper(SessionHelper sessionHelper) {
|
||||
this.sessionHelper = sessionHelper;
|
||||
}
|
||||
|
||||
protected void releaseSession(Session session) {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package org.bench4q.master.repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bench4q.master.entity.TestPlanDB;
|
||||
import org.bench4q.master.entity.User;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class TestPlanRepository extends AbstractRepositoty {
|
||||
|
||||
public TestPlanDB getTestPlan(UUID testPlanRunId) {
|
||||
TestPlanDB result = null;
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
result = (TestPlanDB) session
|
||||
.createCriteria(TestPlanDB.class)
|
||||
.add(Restrictions.eq("testPlanRunId", testPlanRunId.toString()))
|
||||
.uniqueResult();
|
||||
releaseSession(session);
|
||||
return result;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
package org.bench4q.master.repository;
|
||||
|
||||
import org.bench4q.master.entity.User;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class UserRepository extends AbstractRepositoty {
|
||||
|
||||
public boolean attatch(User user) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
Transaction transaction = session.beginTransaction();
|
||||
User userAlreadyExist = null;
|
||||
try {
|
||||
userAlreadyExist = (User) session.createCriteria(User.class)
|
||||
.add(Restrictions.eq("userName", user.getUserName()))
|
||||
.uniqueResult();
|
||||
if (userAlreadyExist != null) {
|
||||
return false;
|
||||
}
|
||||
session.merge(user);
|
||||
transaction.commit();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
transaction.rollback();
|
||||
return false;
|
||||
} finally {
|
||||
releaseSession(session);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean detach(String userName) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
Transaction transaction = session.beginTransaction();
|
||||
try {
|
||||
User userAlreadyExist = (User) session.createCriteria(User.class)
|
||||
.add(Restrictions.eq("userName", userName)).uniqueResult();
|
||||
if (userAlreadyExist == null) {
|
||||
return false;
|
||||
}
|
||||
session.delete(userAlreadyExist);
|
||||
transaction.commit();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
transaction.rollback();
|
||||
return false;
|
||||
} finally {
|
||||
releaseSession(session);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isExist(User user) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
User userAlreadyExist = (User) session.createCriteria(User.class)
|
||||
.add(Restrictions.eq("userName", user.getUserName()))
|
||||
.add(Restrictions.eq("password", user.getPassword()))
|
||||
.uniqueResult();
|
||||
releaseSession(session);
|
||||
return userAlreadyExist != null;
|
||||
}
|
||||
|
||||
public User getEntity(int id) {
|
||||
User result = null;
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
result = (User) session.get(User.class, id);
|
||||
releaseSession(session);
|
||||
return result;
|
||||
}
|
||||
|
||||
public User getUser(String userName) {
|
||||
User result = null;
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
result = (User) session.createCriteria(User.class)
|
||||
.add(Restrictions.eq("userName", userName)).uniqueResult();
|
||||
releaseSession(session);
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -1,335 +1,331 @@
|
|||
package org.bench4q.master.service.infrastructure;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.bench4q.master.api.modelfactory.BusinessModelMapFactory;
|
||||
import org.bench4q.master.domain.RunningScript;
|
||||
import org.bench4q.master.domain.TestPlanInBusiness;
|
||||
import org.bench4q.master.entity.PlanedConfig;
|
||||
import org.bench4q.master.entity.Script;
|
||||
import org.bench4q.master.entity.TestPlanDB;
|
||||
import org.bench4q.master.entity.TestPlanScript;
|
||||
import org.bench4q.master.entity.User;
|
||||
import org.bench4q.master.exception.ExceptionLog;
|
||||
import org.bench4q.master.helper.SessionHelper;
|
||||
import org.bench4q.share.enums.master.TestPlanStatus;
|
||||
import org.bench4q.share.helper.MarshalHelper;
|
||||
import org.bench4q.share.models.master.TestPlanBusinessModel;
|
||||
import org.bench4q.share.models.master.TestScriptConfig;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class TestPlanService {
|
||||
private SessionHelper sessionHelper;
|
||||
private ScriptService scriptService;
|
||||
private BusinessModelMapFactory businessMapFactory;
|
||||
private static Logger logger = Logger.getLogger(TestPlanService.class);
|
||||
public static final long TIME_UNIT = 1000;
|
||||
|
||||
private SessionHelper getSessionHelper() {
|
||||
return sessionHelper;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setSessionHelper(SessionHelper sessionHelper) {
|
||||
this.sessionHelper = sessionHelper;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setScriptService(ScriptService scriptService) {
|
||||
this.scriptService = scriptService;
|
||||
}
|
||||
|
||||
private BusinessModelMapFactory getBusinessMapFactory() {
|
||||
return businessMapFactory;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setBusinessMapFactory(
|
||||
BusinessModelMapFactory businessMapFactory) {
|
||||
this.businessMapFactory = businessMapFactory;
|
||||
}
|
||||
|
||||
public boolean saveTestPlanToDB(final TestPlanInBusiness testPlanInParam,
|
||||
final User user, final UUID testPlanRunId,
|
||||
final Collection<RunningScript> collection) {
|
||||
Session session = this.sessionHelper.openSession();
|
||||
Transaction transaction = session.beginTransaction();
|
||||
TestPlanDB testPlanInSession;
|
||||
try {
|
||||
testPlanInSession = saveToTestPlan(session,
|
||||
testPlanInParam.getName(), MarshalHelper.marshal(
|
||||
TestPlanBusinessModel.class,
|
||||
this.getBusinessMapFactory().toModel(
|
||||
testPlanInParam)), user, testPlanRunId);
|
||||
if (testPlanInSession == null) {
|
||||
return false;
|
||||
}
|
||||
if (!saveToTestPlanScriptAndScriptConfig(session,
|
||||
testPlanInSession, collection)) {
|
||||
return false;
|
||||
}
|
||||
transaction.commit();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
transaction.rollback();
|
||||
logger.error(ExceptionLog.getStackTrace(e));
|
||||
return false;
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private TestPlanDB saveToTestPlan(Session session, String testPlanName,
|
||||
String modelContent, User user, UUID testPlanRunId) {
|
||||
TestPlanDB ret = new TestPlanDB();
|
||||
ret.setCreateDateTime(new Date());
|
||||
ret.setName(testPlanName);
|
||||
ret.setUser(user);
|
||||
ret.setTestPlanRunId(testPlanRunId.toString());
|
||||
ret.setCurrentStatus(TestPlanStatus.NotStart.name());
|
||||
ret.setFailTimes(0);
|
||||
ret.setTestPlanModelContent(modelContent);
|
||||
return (TestPlanDB) session.merge(ret);
|
||||
|
||||
}
|
||||
|
||||
private boolean saveToTestPlanScriptAndScriptConfig(Session session,
|
||||
TestPlanDB testPlanDB, Collection<RunningScript> runningScripts) {
|
||||
TestPlanScript testPlanScript = new TestPlanScript();
|
||||
for (RunningScript runningScript : runningScripts) {
|
||||
|
||||
testPlanScript = new TestPlanScript();
|
||||
testPlanScript.setTestPlanDB(testPlanDB);
|
||||
Script script = this.scriptService.getScript(runningScript
|
||||
.getScriptId());
|
||||
if (script == null) {
|
||||
logger.error("There is no this script with id "
|
||||
+ runningScript.getScriptId());
|
||||
return false;
|
||||
}
|
||||
testPlanScript.setScript(this.scriptService.getScript(runningScript
|
||||
.getScriptId()));
|
||||
testPlanScript.setRequireLoad(runningScript.getRequireLoad());
|
||||
TestPlanScript testPlanScriptInDB = (TestPlanScript) session
|
||||
.merge(testPlanScript);
|
||||
saveToScriptConfig(session, runningScript, testPlanScriptInDB);
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
private boolean saveToScriptConfig(Session session,
|
||||
RunningScript runningScript, TestPlanScript testPlanScript) {
|
||||
PlanedConfig planConfig = new PlanedConfig();
|
||||
planConfig.setTestPlanScript(testPlanScript);
|
||||
TestScriptConfig config = runningScript.getConfig();
|
||||
if (config == null) {
|
||||
return false;
|
||||
}
|
||||
planConfig.setWarmUp(config.getWarmUp());
|
||||
planConfig.setExecuteRange(config.getExecuteRange());
|
||||
planConfig.setCoolDown(config.getCoolDown());
|
||||
session.merge(planConfig);
|
||||
return true;
|
||||
}
|
||||
|
||||
public TestPlanDB getTestPlan(UUID testPlanRunId) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
try {
|
||||
return doGetTestPlanDB(testPlanRunId, session);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TestPlanDB doGetTestPlanDB(UUID testPlanRunId, Session session) {
|
||||
return (TestPlanDB) session
|
||||
.createCriteria(TestPlanDB.class)
|
||||
.add(Restrictions.eq("testPlanRunId", testPlanRunId.toString()))
|
||||
.uniqueResult();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean removeTestPlanInDB(int testPlanId) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
Transaction transaction = session.beginTransaction();
|
||||
try {
|
||||
TestPlanDB testPlanInDB = (TestPlanDB) session
|
||||
.createCriteria(TestPlanDB.class)
|
||||
.add(Restrictions.eq("id", testPlanId)).uniqueResult();
|
||||
if (testPlanInDB == null) {
|
||||
return false;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
List<TestPlanScript> testPlanScripts = session
|
||||
.createCriteria(TestPlanScript.class)
|
||||
.add(Restrictions.eq("testPlanDB", testPlanInDB)).list();
|
||||
if (testPlanScripts == null) {
|
||||
return false;
|
||||
}
|
||||
for (TestPlanScript testPlanScript : testPlanScripts) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<PlanedConfig> configs = session
|
||||
.createCriteria(PlanedConfig.class)
|
||||
.add(Restrictions.eq("testPlanScript", testPlanScript))
|
||||
.list();
|
||||
if (configs == null) {
|
||||
return false;
|
||||
}
|
||||
for (PlanedConfig config : configs) {
|
||||
session.delete(config);
|
||||
}
|
||||
session.delete(testPlanScript);
|
||||
}
|
||||
session.delete(testPlanInDB);
|
||||
transaction.commit();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
transaction.rollback();
|
||||
return false;
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isReportCreated(UUID testPlanRunID) {
|
||||
// TODO: edit it to check if the file exist
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
try {
|
||||
TestPlanDB testPlanDB = (TestPlanDB) session
|
||||
.createCriteria(TestPlanDB.class)
|
||||
.add(Restrictions.eq("testPlanRunId",
|
||||
testPlanRunID.toString())).uniqueResult();
|
||||
return testPlanDB.isReportCreated();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setReportCreated(UUID testPlanRunID) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
Transaction transaction = session.beginTransaction();
|
||||
try {
|
||||
TestPlanDB testPlanDB = (TestPlanDB) session
|
||||
.createCriteria(TestPlanDB.class)
|
||||
.add(Restrictions.eq("testPlanRunId", testPlanRunID))
|
||||
.uniqueResult();
|
||||
testPlanDB.setReportCreated(true);
|
||||
session.merge(testPlanDB);
|
||||
transaction.commit();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
transaction.rollback();
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void handleTestPlanPendingNoEnoughMaxLoad(UUID testPlanRunID) {
|
||||
updateStatus(TestPlanStatus.PendingNoEnoughMaxLoad, testPlanRunID, 1);
|
||||
}
|
||||
|
||||
public void handleTestPlanPendingNoEnoughCurrentLoad(UUID testPlanRunID) {
|
||||
updateStatus(TestPlanStatus.PendingNoEnoughCurrentLoad, testPlanRunID,
|
||||
1);
|
||||
}
|
||||
|
||||
public void handleTestPlanError(UUID testPlanRunID) {
|
||||
updateStatus(TestPlanStatus.Error, testPlanRunID, 1);
|
||||
}
|
||||
|
||||
public void handleTestPlanRunning(UUID testPlanRunID) {
|
||||
updateStatus(TestPlanStatus.InRunning, testPlanRunID, 0);
|
||||
}
|
||||
|
||||
public void handleTestPlanComplete(UUID testPlanRunID) {
|
||||
updateStatus(TestPlanStatus.Complete, testPlanRunID, 0);
|
||||
}
|
||||
|
||||
private void updateStatus(TestPlanStatus status, UUID testPlanRunID,
|
||||
int failTimes) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
Transaction transaction = session.beginTransaction();
|
||||
try {
|
||||
TestPlanDB testPlanDB = doGetTestPlanDB(testPlanRunID, session);
|
||||
testPlanDB.setCurrentStatus(status.name());
|
||||
testPlanDB.setFailTimes(testPlanDB.getFailTimes() + failTimes);
|
||||
transaction.commit();
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
logger.error("update status of testplan fails, testPlanID:"
|
||||
+ testPlanRunID.toString());
|
||||
transaction.rollback();
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TestPlanStatus queryTestPlanStatus(UUID testPlanRunID) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
try {
|
||||
TestPlanDB testPlanDB = (TestPlanDB) session
|
||||
.createCriteria(TestPlanDB.class)
|
||||
.add(Restrictions.eq("testPlanRunId",
|
||||
testPlanRunID.toString())).uniqueResult();
|
||||
return testPlanDB == null ? null : TestPlanStatus
|
||||
.valueOf(testPlanDB.getCurrentStatus());
|
||||
} catch (Exception e) {
|
||||
logger.error("there is an exception in queryTestPlanStatus!");
|
||||
return null;
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TestPlanDB pickALatestPengdingWithMinimalFailTimes() {
|
||||
// TODO: fulfill this
|
||||
return null;
|
||||
}
|
||||
}
|
||||
package org.bench4q.master.service.infrastructure;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.bench4q.master.api.modelfactory.BusinessModelMapFactory;
|
||||
import org.bench4q.master.domain.RunningScript;
|
||||
import org.bench4q.master.domain.TestPlanInBusiness;
|
||||
import org.bench4q.master.entity.PlanedConfig;
|
||||
import org.bench4q.master.entity.Script;
|
||||
import org.bench4q.master.entity.TestPlanDB;
|
||||
import org.bench4q.master.entity.TestPlanScript;
|
||||
import org.bench4q.master.entity.User;
|
||||
import org.bench4q.master.exception.ExceptionLog;
|
||||
import org.bench4q.master.helper.SessionHelper;
|
||||
import org.bench4q.master.repository.TestPlanRepository;
|
||||
import org.bench4q.share.enums.master.TestPlanStatus;
|
||||
import org.bench4q.share.helper.MarshalHelper;
|
||||
import org.bench4q.share.models.master.TestPlanBusinessModel;
|
||||
import org.bench4q.share.models.master.TestPlanDBModel;
|
||||
import org.bench4q.share.models.master.TestScriptConfig;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class TestPlanService {
|
||||
private SessionHelper sessionHelper;
|
||||
private ScriptService scriptService;
|
||||
private BusinessModelMapFactory businessMapFactory;
|
||||
private TestPlanRepository testPlanRepository;
|
||||
private static Logger logger = Logger.getLogger(TestPlanService.class);
|
||||
public static final long TIME_UNIT = 1000;
|
||||
|
||||
private SessionHelper getSessionHelper() {
|
||||
return sessionHelper;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setSessionHelper(SessionHelper sessionHelper) {
|
||||
this.sessionHelper = sessionHelper;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setScriptService(ScriptService scriptService) {
|
||||
this.scriptService = scriptService;
|
||||
}
|
||||
|
||||
private TestPlanRepository getTestPlanRepository() {
|
||||
return testPlanRepository;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setTestPlanRepository(TestPlanRepository testPlanRepository) {
|
||||
this.testPlanRepository = testPlanRepository;
|
||||
}
|
||||
|
||||
private BusinessModelMapFactory getBusinessMapFactory() {
|
||||
return businessMapFactory;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setBusinessMapFactory(
|
||||
BusinessModelMapFactory businessMapFactory) {
|
||||
this.businessMapFactory = businessMapFactory;
|
||||
}
|
||||
|
||||
public boolean saveTestPlanToDB(final TestPlanInBusiness testPlanInParam,
|
||||
final User user, final UUID testPlanRunId,
|
||||
final Collection<RunningScript> collection) {
|
||||
Session session = this.sessionHelper.openSession();
|
||||
Transaction transaction = session.beginTransaction();
|
||||
TestPlanDB testPlanInSession;
|
||||
try {
|
||||
testPlanInSession = saveToTestPlan(session,
|
||||
testPlanInParam.getName(), MarshalHelper.marshal(
|
||||
TestPlanBusinessModel.class,
|
||||
this.getBusinessMapFactory().toModel(
|
||||
testPlanInParam)), user, testPlanRunId);
|
||||
if (testPlanInSession == null) {
|
||||
return false;
|
||||
}
|
||||
if (!saveToTestPlanScriptAndScriptConfig(session,
|
||||
testPlanInSession, collection)) {
|
||||
return false;
|
||||
}
|
||||
transaction.commit();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
transaction.rollback();
|
||||
logger.error(ExceptionLog.getStackTrace(e));
|
||||
return false;
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private TestPlanDB saveToTestPlan(Session session, String testPlanName,
|
||||
String modelContent, User user, UUID testPlanRunId) {
|
||||
TestPlanDB ret = new TestPlanDB();
|
||||
ret.setCreateDateTime(new Date());
|
||||
ret.setName(testPlanName);
|
||||
ret.setUser(user);
|
||||
ret.setTestPlanRunId(testPlanRunId.toString());
|
||||
ret.setCurrentStatus(TestPlanStatus.NotStart.name());
|
||||
ret.setFailTimes(0);
|
||||
ret.setTestPlanModelContent(modelContent);
|
||||
return (TestPlanDB) session.merge(ret);
|
||||
|
||||
}
|
||||
|
||||
private boolean saveToTestPlanScriptAndScriptConfig(Session session,
|
||||
TestPlanDB testPlanDB, Collection<RunningScript> runningScripts) {
|
||||
TestPlanScript testPlanScript = new TestPlanScript();
|
||||
for (RunningScript runningScript : runningScripts) {
|
||||
|
||||
testPlanScript = new TestPlanScript();
|
||||
testPlanScript.setTestPlanDB(testPlanDB);
|
||||
Script script = this.scriptService.getScript(runningScript
|
||||
.getScriptId());
|
||||
if (script == null) {
|
||||
logger.error("There is no this script with id "
|
||||
+ runningScript.getScriptId());
|
||||
return false;
|
||||
}
|
||||
testPlanScript.setScript(this.scriptService.getScript(runningScript
|
||||
.getScriptId()));
|
||||
testPlanScript.setRequireLoad(runningScript.getRequireLoad());
|
||||
TestPlanScript testPlanScriptInDB = (TestPlanScript) session
|
||||
.merge(testPlanScript);
|
||||
saveToScriptConfig(session, runningScript, testPlanScriptInDB);
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
private boolean saveToScriptConfig(Session session,
|
||||
RunningScript runningScript, TestPlanScript testPlanScript) {
|
||||
PlanedConfig planConfig = new PlanedConfig();
|
||||
planConfig.setTestPlanScript(testPlanScript);
|
||||
TestScriptConfig config = runningScript.getConfig();
|
||||
if (config == null) {
|
||||
return false;
|
||||
}
|
||||
planConfig.setWarmUp(config.getWarmUp());
|
||||
planConfig.setExecuteRange(config.getExecuteRange());
|
||||
planConfig.setCoolDown(config.getCoolDown());
|
||||
session.merge(planConfig);
|
||||
return true;
|
||||
}
|
||||
|
||||
public TestPlanDB getTestPlan(UUID testPlanRunId) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
try {
|
||||
return doGetTestPlanDB(testPlanRunId, session);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TestPlanDB doGetTestPlanDB(UUID testPlanRunId, Session session) {
|
||||
return (TestPlanDB) session
|
||||
.createCriteria(TestPlanDB.class)
|
||||
.add(Restrictions.eq("testPlanRunId", testPlanRunId.toString()))
|
||||
.uniqueResult();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean removeTestPlanInDB(int testPlanId) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
Transaction transaction = session.beginTransaction();
|
||||
try {
|
||||
TestPlanDB testPlanInDB = (TestPlanDB) session
|
||||
.createCriteria(TestPlanDB.class)
|
||||
.add(Restrictions.eq("id", testPlanId)).uniqueResult();
|
||||
if (testPlanInDB == null) {
|
||||
return false;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
List<TestPlanScript> testPlanScripts = session
|
||||
.createCriteria(TestPlanScript.class)
|
||||
.add(Restrictions.eq("testPlanDB", testPlanInDB)).list();
|
||||
if (testPlanScripts == null) {
|
||||
return false;
|
||||
}
|
||||
for (TestPlanScript testPlanScript : testPlanScripts) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<PlanedConfig> configs = session
|
||||
.createCriteria(PlanedConfig.class)
|
||||
.add(Restrictions.eq("testPlanScript", testPlanScript))
|
||||
.list();
|
||||
if (configs == null) {
|
||||
return false;
|
||||
}
|
||||
for (PlanedConfig config : configs) {
|
||||
session.delete(config);
|
||||
}
|
||||
session.delete(testPlanScript);
|
||||
}
|
||||
session.delete(testPlanInDB);
|
||||
transaction.commit();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
transaction.rollback();
|
||||
return false;
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isReportCreated(UUID testPlanRunID) {
|
||||
// TODO: edit it to check if the file exist
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
try {
|
||||
TestPlanDB testPlanDB = (TestPlanDB) session
|
||||
.createCriteria(TestPlanDB.class)
|
||||
.add(Restrictions.eq("testPlanRunId",
|
||||
testPlanRunID.toString())).uniqueResult();
|
||||
return testPlanDB.isReportCreated();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void handleTestPlanPendingNoEnoughMaxLoad(UUID testPlanRunID) {
|
||||
updateStatus(TestPlanStatus.PendingNoEnoughMaxLoad, testPlanRunID, 1);
|
||||
}
|
||||
|
||||
public void handleTestPlanPendingNoEnoughCurrentLoad(UUID testPlanRunID) {
|
||||
updateStatus(TestPlanStatus.PendingNoEnoughCurrentLoad, testPlanRunID,
|
||||
1);
|
||||
}
|
||||
|
||||
public void handleTestPlanError(UUID testPlanRunID) {
|
||||
updateStatus(TestPlanStatus.Error, testPlanRunID, 1);
|
||||
}
|
||||
|
||||
public void handleTestPlanRunning(UUID testPlanRunID) {
|
||||
updateStatus(TestPlanStatus.InRunning, testPlanRunID, 0);
|
||||
}
|
||||
|
||||
public void handleTestPlanComplete(UUID testPlanRunID) {
|
||||
updateStatus(TestPlanStatus.Complete, testPlanRunID, 0);
|
||||
}
|
||||
|
||||
private void updateStatus(TestPlanStatus status, UUID testPlanRunID,
|
||||
int failTimes) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
Transaction transaction = session.beginTransaction();
|
||||
try {
|
||||
TestPlanDB testPlanDB = doGetTestPlanDB(testPlanRunID, session);
|
||||
testPlanDB.setCurrentStatus(status.name());
|
||||
testPlanDB.setFailTimes(testPlanDB.getFailTimes() + failTimes);
|
||||
transaction.commit();
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage());
|
||||
logger.error("update status of testplan fails, testPlanID:"
|
||||
+ testPlanRunID.toString());
|
||||
transaction.rollback();
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TestPlanStatus queryTestPlanStatus(UUID testPlanRunID) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
try {
|
||||
TestPlanDB testPlanDB = (TestPlanDB) session
|
||||
.createCriteria(TestPlanDB.class)
|
||||
.add(Restrictions.eq("testPlanRunId",
|
||||
testPlanRunID.toString())).uniqueResult();
|
||||
return testPlanDB == null ? null : TestPlanStatus
|
||||
.valueOf(testPlanDB.getCurrentStatus());
|
||||
} catch (Exception e) {
|
||||
logger.error("there is an exception in queryTestPlanStatus!");
|
||||
return null;
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TestPlanDB pickALatestPengdingWithMinimalFailTimes() {
|
||||
// TODO: fulfill this
|
||||
return null;
|
||||
}
|
||||
|
||||
public TestPlanDBModel getTestPlanDBModel(UUID runId) {
|
||||
return this.getBusinessMapFactory().toModel(
|
||||
this.getTestPlanRepository().getTestPlan(runId));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,32 +2,17 @@ package org.bench4q.master.service.infrastructure;
|
|||
|
||||
import org.bench4q.master.entity.User;
|
||||
import org.bench4q.master.factory.UserFactory;
|
||||
import org.bench4q.master.helper.HashHelper;
|
||||
import org.bench4q.master.helper.SessionHelper;
|
||||
import org.bench4q.master.repository.UserRepository;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class UserService {
|
||||
private SessionHelper sessionHelper;
|
||||
private UserFactory userFactory;
|
||||
private UserRepository userRepository;
|
||||
private HashHelper hashHelper;
|
||||
public static byte NORAML_AUTHENTICATION = 0;
|
||||
public static byte SUPER_AUTHENTICATION = 1;
|
||||
|
||||
private SessionHelper getSessionHelper() {
|
||||
return sessionHelper;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setSessionHelper(SessionHelper sessionHelper) {
|
||||
this.sessionHelper = sessionHelper;
|
||||
}
|
||||
|
||||
private UserFactory getUserFactory() {
|
||||
return userFactory;
|
||||
}
|
||||
|
@ -46,15 +31,6 @@ public class UserService {
|
|||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
private HashHelper getHashHelper() {
|
||||
return hashHelper;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setHashHelper(HashHelper hashHelper) {
|
||||
this.hashHelper = hashHelper;
|
||||
}
|
||||
|
||||
public boolean register(String userName, String password) {
|
||||
return this.getUserRepository().attatch(
|
||||
this.getUserFactory().createUser(userName, password));
|
||||
|
@ -69,36 +45,11 @@ public class UserService {
|
|||
this.getUserFactory().createUser(userName, password));
|
||||
}
|
||||
|
||||
public String hashPassword(String password) {
|
||||
return this.getHashHelper().sha1Hash(password);
|
||||
}
|
||||
|
||||
public User getUserByName(String userName) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
try {
|
||||
return (User) session.createCriteria(User.class)
|
||||
.add(Restrictions.eq("userName", userName)).uniqueResult();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
return this.getUserRepository().getUser(userName);
|
||||
}
|
||||
|
||||
public User getUserById(int userId) {
|
||||
Session session = this.getSessionHelper().openSession();
|
||||
try {
|
||||
return (User) session.get(User.class, userId);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
if (session != null) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
return this.getUserRepository().getEntity(userId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package org.bench4q.master.test.repository;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bench4q.master.repository.TestPlanRepository;
|
||||
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:repository-test-context.xml" })
|
||||
public class Test_TestPlanRepository {
|
||||
private TestPlanRepository testPlanRepository;
|
||||
|
||||
private TestPlanRepository getTestPlanRepository() {
|
||||
return testPlanRepository;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setTestPlanRepository(TestPlanRepository testPlanRepository) {
|
||||
this.testPlanRepository = testPlanRepository;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetEntity() {
|
||||
fail("Not yet implemented");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTestPlan() {
|
||||
assertNotNull(this.getTestPlanRepository().getTestPlan(
|
||||
UUID.fromString("4c894c8d-712b-44e0-97f8-84667591953a")));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package org.bench4q.master.test.repository;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.bench4q.master.entity.User;
|
||||
import org.bench4q.master.helper.HashHelper;
|
||||
import org.bench4q.master.repository.UserRepository;
|
||||
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:repository-test-context.xml" })
|
||||
public class Test_UserRepository {
|
||||
private UserRepository userRepository;
|
||||
private HashHelper hashHelper;
|
||||
private User userForTest;
|
||||
|
||||
private UserRepository getUserRepository() {
|
||||
return userRepository;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setUserRepository(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
private HashHelper getHashHelper() {
|
||||
return hashHelper;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void setHashHelper(HashHelper hashHelper) {
|
||||
this.hashHelper = hashHelper;
|
||||
init();
|
||||
}
|
||||
|
||||
private User getUserForTest() {
|
||||
return userForTest;
|
||||
}
|
||||
|
||||
private void setUserForTest(User userForTest) {
|
||||
this.userForTest = userForTest;
|
||||
}
|
||||
|
||||
public Test_UserRepository() {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
User user = new User();
|
||||
user.setPassword(this.getHashHelper().sha1Hash("test1"));
|
||||
user.setUserName("test1");
|
||||
user.setScope((byte) 0);
|
||||
this.setUserForTest(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initTest() {
|
||||
assertNotNull(this.getUserRepository());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAttach() {
|
||||
assertTrue(this.getUserRepository().attatch(this.getUserForTest()));
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
this.getUserRepository().detach(this.getUserForTest().getUserName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsExist() {
|
||||
this.getUserRepository().attatch(this.getUserForTest());
|
||||
assertTrue(this.getUserRepository().isExist(this.getUserForTest()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUserByName() {
|
||||
this.getUserRepository().attatch(this.getUserForTest());
|
||||
assertNotNull(this.getUserRepository().getUser("test1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetEntity() {
|
||||
assertNotNull(this.getUserRepository().getEntity(5));
|
||||
}
|
||||
}
|
|
@ -35,11 +35,26 @@ public class Test_UserService {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testIsExist() {
|
||||
public void testValidate() {
|
||||
this.getUserService().register("test1", "test1");
|
||||
assertTrue(this.getUserService().validateUser("test1", "test1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUserById() {
|
||||
assertNotNull(this.getUserService().getUserById(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUserByName() {
|
||||
this.getUserService().register("test1", "test1");
|
||||
assertNotNull(this.getUserService().getUserByName("test1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
this.getUserService().delete("test1");
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
|
||||
|
||||
<context:component-scan
|
||||
base-package="org.bench4q.master.helper, org.bench4q.master.repository, org.bench4q.master.entity" />
|
||||
<mvc:annotation-driven />
|
||||
<task:annotation-driven />
|
||||
</beans>
|
Loading…
Reference in New Issue