User repository and user service has been refactored completely

This commit is contained in:
coderfengyun 2014-02-17 10:19:43 +08:00
parent e74f032011
commit 4a7e4ec794
13 changed files with 706 additions and 391 deletions

View File

@ -292,8 +292,7 @@ public class TestPlanController extends BaseController {
throw new Bench4QException(HAVE_NO_POWER, HAVE_NO_POWER, throw new Bench4QException(HAVE_NO_POWER, HAVE_NO_POWER,
"/queryTestPlan/{runId}"); "/queryTestPlan/{runId}");
} }
return BusinessModelMapFactory.toModel(this.getTestPlanService() return this.getTestPlanService().getTestPlanDBModel(runId);
.getTestPlan(runId));
} }
@RequestMapping(value = "/removeTestPlanFromPool", method = { @RequestMapping(value = "/removeTestPlanFromPool", method = {
@ -315,7 +314,7 @@ public class TestPlanController extends BaseController {
result.setFailCause(failCause); result.setFailCause(failCause);
List<TestPlanDBModel> modelList = new ArrayList<TestPlanDBModel>(); List<TestPlanDBModel> modelList = new ArrayList<TestPlanDBModel>();
for (TestPlanDB testPlanDB : testPlanDBs) { for (TestPlanDB testPlanDB : testPlanDBs) {
modelList.add(BusinessModelMapFactory.toModel(testPlanDB)); modelList.add(this.getBusinessMapFactory().toModel(testPlanDB));
} }
result.setTestPlanDBModels(modelList); result.setTestPlanDBModels(modelList);
return result; return result;

View File

@ -191,7 +191,7 @@ public class BusinessModelMapFactory {
return ret; return ret;
} }
public static TestPlanDBModel toModel(TestPlanDB testPlanDB) { public TestPlanDBModel toModel(TestPlanDB testPlanDB) {
TestPlanDBModel ret = new TestPlanDBModel(); TestPlanDBModel ret = new TestPlanDBModel();
ret.setCreateDateTime(testPlanDB.getCreateDateTime()); ret.setCreateDateTime(testPlanDB.getCreateDateTime());
ret.setCurrentStatus(testPlanDB.getCurrentStatus()); ret.setCurrentStatus(testPlanDB.getCurrentStatus());

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,335 +1,331 @@
package org.bench4q.master.service.infrastructure; package org.bench4q.master.service.infrastructure;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.master.api.modelfactory.BusinessModelMapFactory; import org.bench4q.master.api.modelfactory.BusinessModelMapFactory;
import org.bench4q.master.domain.RunningScript; import org.bench4q.master.domain.RunningScript;
import org.bench4q.master.domain.TestPlanInBusiness; import org.bench4q.master.domain.TestPlanInBusiness;
import org.bench4q.master.entity.PlanedConfig; import org.bench4q.master.entity.PlanedConfig;
import org.bench4q.master.entity.Script; import org.bench4q.master.entity.Script;
import org.bench4q.master.entity.TestPlanDB; import org.bench4q.master.entity.TestPlanDB;
import org.bench4q.master.entity.TestPlanScript; import org.bench4q.master.entity.TestPlanScript;
import org.bench4q.master.entity.User; import org.bench4q.master.entity.User;
import org.bench4q.master.exception.ExceptionLog; import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.SessionHelper; import org.bench4q.master.helper.SessionHelper;
import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.master.repository.TestPlanRepository;
import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.models.master.TestPlanBusinessModel; import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.TestScriptConfig; import org.bench4q.share.models.master.TestPlanBusinessModel;
import org.hibernate.Session; import org.bench4q.share.models.master.TestPlanDBModel;
import org.hibernate.Transaction; import org.bench4q.share.models.master.TestScriptConfig;
import org.hibernate.criterion.Restrictions; import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired; import org.hibernate.Transaction;
import org.springframework.stereotype.Component; import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
@Component import org.springframework.stereotype.Component;
public class TestPlanService {
private SessionHelper sessionHelper; @Component
private ScriptService scriptService; public class TestPlanService {
private BusinessModelMapFactory businessMapFactory; private SessionHelper sessionHelper;
private static Logger logger = Logger.getLogger(TestPlanService.class); private ScriptService scriptService;
public static final long TIME_UNIT = 1000; private BusinessModelMapFactory businessMapFactory;
private TestPlanRepository testPlanRepository;
private SessionHelper getSessionHelper() { private static Logger logger = Logger.getLogger(TestPlanService.class);
return sessionHelper; public static final long TIME_UNIT = 1000;
}
private SessionHelper getSessionHelper() {
@Autowired return sessionHelper;
private void setSessionHelper(SessionHelper sessionHelper) { }
this.sessionHelper = sessionHelper;
} @Autowired
private void setSessionHelper(SessionHelper sessionHelper) {
@Autowired this.sessionHelper = sessionHelper;
private void setScriptService(ScriptService scriptService) { }
this.scriptService = scriptService;
} @Autowired
private void setScriptService(ScriptService scriptService) {
private BusinessModelMapFactory getBusinessMapFactory() { this.scriptService = scriptService;
return businessMapFactory; }
}
private TestPlanRepository getTestPlanRepository() {
@Autowired return testPlanRepository;
private void setBusinessMapFactory( }
BusinessModelMapFactory businessMapFactory) {
this.businessMapFactory = businessMapFactory; @Autowired
} private void setTestPlanRepository(TestPlanRepository testPlanRepository) {
this.testPlanRepository = testPlanRepository;
public boolean saveTestPlanToDB(final TestPlanInBusiness testPlanInParam, }
final User user, final UUID testPlanRunId,
final Collection<RunningScript> collection) { private BusinessModelMapFactory getBusinessMapFactory() {
Session session = this.sessionHelper.openSession(); return businessMapFactory;
Transaction transaction = session.beginTransaction(); }
TestPlanDB testPlanInSession;
try { @Autowired
testPlanInSession = saveToTestPlan(session, private void setBusinessMapFactory(
testPlanInParam.getName(), MarshalHelper.marshal( BusinessModelMapFactory businessMapFactory) {
TestPlanBusinessModel.class, this.businessMapFactory = businessMapFactory;
this.getBusinessMapFactory().toModel( }
testPlanInParam)), user, testPlanRunId);
if (testPlanInSession == null) { public boolean saveTestPlanToDB(final TestPlanInBusiness testPlanInParam,
return false; final User user, final UUID testPlanRunId,
} final Collection<RunningScript> collection) {
if (!saveToTestPlanScriptAndScriptConfig(session, Session session = this.sessionHelper.openSession();
testPlanInSession, collection)) { Transaction transaction = session.beginTransaction();
return false; TestPlanDB testPlanInSession;
} try {
transaction.commit(); testPlanInSession = saveToTestPlan(session,
return true; testPlanInParam.getName(), MarshalHelper.marshal(
} catch (Exception e) { TestPlanBusinessModel.class,
transaction.rollback(); this.getBusinessMapFactory().toModel(
logger.error(ExceptionLog.getStackTrace(e)); testPlanInParam)), user, testPlanRunId);
return false; if (testPlanInSession == null) {
} finally { return false;
if (session != null) { }
session.close(); if (!saveToTestPlanScriptAndScriptConfig(session,
} testPlanInSession, collection)) {
} return false;
} }
transaction.commit();
private TestPlanDB saveToTestPlan(Session session, String testPlanName, return true;
String modelContent, User user, UUID testPlanRunId) { } catch (Exception e) {
TestPlanDB ret = new TestPlanDB(); transaction.rollback();
ret.setCreateDateTime(new Date()); logger.error(ExceptionLog.getStackTrace(e));
ret.setName(testPlanName); return false;
ret.setUser(user); } finally {
ret.setTestPlanRunId(testPlanRunId.toString()); if (session != null) {
ret.setCurrentStatus(TestPlanStatus.NotStart.name()); session.close();
ret.setFailTimes(0); }
ret.setTestPlanModelContent(modelContent); }
return (TestPlanDB) session.merge(ret); }
} private TestPlanDB saveToTestPlan(Session session, String testPlanName,
String modelContent, User user, UUID testPlanRunId) {
private boolean saveToTestPlanScriptAndScriptConfig(Session session, TestPlanDB ret = new TestPlanDB();
TestPlanDB testPlanDB, Collection<RunningScript> runningScripts) { ret.setCreateDateTime(new Date());
TestPlanScript testPlanScript = new TestPlanScript(); ret.setName(testPlanName);
for (RunningScript runningScript : runningScripts) { ret.setUser(user);
ret.setTestPlanRunId(testPlanRunId.toString());
testPlanScript = new TestPlanScript(); ret.setCurrentStatus(TestPlanStatus.NotStart.name());
testPlanScript.setTestPlanDB(testPlanDB); ret.setFailTimes(0);
Script script = this.scriptService.getScript(runningScript ret.setTestPlanModelContent(modelContent);
.getScriptId()); return (TestPlanDB) session.merge(ret);
if (script == null) {
logger.error("There is no this script with id " }
+ runningScript.getScriptId());
return false; private boolean saveToTestPlanScriptAndScriptConfig(Session session,
} TestPlanDB testPlanDB, Collection<RunningScript> runningScripts) {
testPlanScript.setScript(this.scriptService.getScript(runningScript TestPlanScript testPlanScript = new TestPlanScript();
.getScriptId())); for (RunningScript runningScript : runningScripts) {
testPlanScript.setRequireLoad(runningScript.getRequireLoad());
TestPlanScript testPlanScriptInDB = (TestPlanScript) session testPlanScript = new TestPlanScript();
.merge(testPlanScript); testPlanScript.setTestPlanDB(testPlanDB);
saveToScriptConfig(session, runningScript, testPlanScriptInDB); Script script = this.scriptService.getScript(runningScript
} .getScriptId());
return true; if (script == null) {
logger.error("There is no this script with id "
} + runningScript.getScriptId());
return false;
private boolean saveToScriptConfig(Session session, }
RunningScript runningScript, TestPlanScript testPlanScript) { testPlanScript.setScript(this.scriptService.getScript(runningScript
PlanedConfig planConfig = new PlanedConfig(); .getScriptId()));
planConfig.setTestPlanScript(testPlanScript); testPlanScript.setRequireLoad(runningScript.getRequireLoad());
TestScriptConfig config = runningScript.getConfig(); TestPlanScript testPlanScriptInDB = (TestPlanScript) session
if (config == null) { .merge(testPlanScript);
return false; saveToScriptConfig(session, runningScript, testPlanScriptInDB);
} }
planConfig.setWarmUp(config.getWarmUp()); return true;
planConfig.setExecuteRange(config.getExecuteRange());
planConfig.setCoolDown(config.getCoolDown()); }
session.merge(planConfig);
return true; private boolean saveToScriptConfig(Session session,
} RunningScript runningScript, TestPlanScript testPlanScript) {
PlanedConfig planConfig = new PlanedConfig();
public TestPlanDB getTestPlan(UUID testPlanRunId) { planConfig.setTestPlanScript(testPlanScript);
Session session = this.getSessionHelper().openSession(); TestScriptConfig config = runningScript.getConfig();
try { if (config == null) {
return doGetTestPlanDB(testPlanRunId, session); return false;
} catch (Exception e) { }
e.printStackTrace(); planConfig.setWarmUp(config.getWarmUp());
return null; planConfig.setExecuteRange(config.getExecuteRange());
} finally { planConfig.setCoolDown(config.getCoolDown());
if (session != null) { session.merge(planConfig);
session.close(); return true;
} }
}
} public TestPlanDB getTestPlan(UUID testPlanRunId) {
Session session = this.getSessionHelper().openSession();
TestPlanDB doGetTestPlanDB(UUID testPlanRunId, Session session) { try {
return (TestPlanDB) session return doGetTestPlanDB(testPlanRunId, session);
.createCriteria(TestPlanDB.class) } catch (Exception e) {
.add(Restrictions.eq("testPlanRunId", testPlanRunId.toString())) e.printStackTrace();
.uniqueResult(); return null;
} } finally {
if (session != null) {
public List<TestPlanDB> loadTestPlans(User user) { session.close();
Session session = this.getSessionHelper().openSession(); }
try { }
@SuppressWarnings("unchecked") }
List<TestPlanDB> ret = session.createCriteria(TestPlanDB.class)
.add(Restrictions.eq("user", user)).list(); TestPlanDB doGetTestPlanDB(UUID testPlanRunId, Session session) {
return ret; return (TestPlanDB) session
} catch (Exception e) { .createCriteria(TestPlanDB.class)
e.printStackTrace(); .add(Restrictions.eq("testPlanRunId", testPlanRunId.toString()))
return null; .uniqueResult();
} finally { }
if (session != null) {
session.close(); public List<TestPlanDB> loadTestPlans(User user) {
} Session session = this.getSessionHelper().openSession();
} try {
} @SuppressWarnings("unchecked")
List<TestPlanDB> ret = session.createCriteria(TestPlanDB.class)
public boolean removeTestPlanInDB(int testPlanId) { .add(Restrictions.eq("user", user)).list();
Session session = this.getSessionHelper().openSession(); return ret;
Transaction transaction = session.beginTransaction(); } catch (Exception e) {
try { e.printStackTrace();
TestPlanDB testPlanInDB = (TestPlanDB) session return null;
.createCriteria(TestPlanDB.class) } finally {
.add(Restrictions.eq("id", testPlanId)).uniqueResult(); if (session != null) {
if (testPlanInDB == null) { session.close();
return false; }
} }
@SuppressWarnings("unchecked") }
List<TestPlanScript> testPlanScripts = session
.createCriteria(TestPlanScript.class) public boolean removeTestPlanInDB(int testPlanId) {
.add(Restrictions.eq("testPlanDB", testPlanInDB)).list(); Session session = this.getSessionHelper().openSession();
if (testPlanScripts == null) { Transaction transaction = session.beginTransaction();
return false; try {
} TestPlanDB testPlanInDB = (TestPlanDB) session
for (TestPlanScript testPlanScript : testPlanScripts) { .createCriteria(TestPlanDB.class)
@SuppressWarnings("unchecked") .add(Restrictions.eq("id", testPlanId)).uniqueResult();
List<PlanedConfig> configs = session if (testPlanInDB == null) {
.createCriteria(PlanedConfig.class) return false;
.add(Restrictions.eq("testPlanScript", testPlanScript)) }
.list(); @SuppressWarnings("unchecked")
if (configs == null) { List<TestPlanScript> testPlanScripts = session
return false; .createCriteria(TestPlanScript.class)
} .add(Restrictions.eq("testPlanDB", testPlanInDB)).list();
for (PlanedConfig config : configs) { if (testPlanScripts == null) {
session.delete(config); return false;
} }
session.delete(testPlanScript); for (TestPlanScript testPlanScript : testPlanScripts) {
} @SuppressWarnings("unchecked")
session.delete(testPlanInDB); List<PlanedConfig> configs = session
transaction.commit(); .createCriteria(PlanedConfig.class)
return true; .add(Restrictions.eq("testPlanScript", testPlanScript))
} catch (Exception e) { .list();
e.printStackTrace(); if (configs == null) {
transaction.rollback(); return false;
return false; }
} finally { for (PlanedConfig config : configs) {
if (session != null) { session.delete(config);
session.close(); }
} session.delete(testPlanScript);
} }
} session.delete(testPlanInDB);
transaction.commit();
public boolean isReportCreated(UUID testPlanRunID) { return true;
// TODO: edit it to check if the file exist } catch (Exception e) {
Session session = this.getSessionHelper().openSession(); e.printStackTrace();
try { transaction.rollback();
TestPlanDB testPlanDB = (TestPlanDB) session return false;
.createCriteria(TestPlanDB.class) } finally {
.add(Restrictions.eq("testPlanRunId", if (session != null) {
testPlanRunID.toString())).uniqueResult(); session.close();
return testPlanDB.isReportCreated(); }
} catch (Exception e) { }
e.printStackTrace(); }
return false;
} finally { public boolean isReportCreated(UUID testPlanRunID) {
if (session != null) { // TODO: edit it to check if the file exist
session.close(); Session session = this.getSessionHelper().openSession();
} try {
} TestPlanDB testPlanDB = (TestPlanDB) session
} .createCriteria(TestPlanDB.class)
.add(Restrictions.eq("testPlanRunId",
public void setReportCreated(UUID testPlanRunID) { testPlanRunID.toString())).uniqueResult();
Session session = this.getSessionHelper().openSession(); return testPlanDB.isReportCreated();
Transaction transaction = session.beginTransaction(); } catch (Exception e) {
try { e.printStackTrace();
TestPlanDB testPlanDB = (TestPlanDB) session return false;
.createCriteria(TestPlanDB.class) } finally {
.add(Restrictions.eq("testPlanRunId", testPlanRunID)) if (session != null) {
.uniqueResult(); session.close();
testPlanDB.setReportCreated(true); }
session.merge(testPlanDB); }
transaction.commit(); }
} catch (Exception e) {
e.printStackTrace(); public void handleTestPlanPendingNoEnoughMaxLoad(UUID testPlanRunID) {
transaction.rollback(); updateStatus(TestPlanStatus.PendingNoEnoughMaxLoad, testPlanRunID, 1);
} finally { }
if (session != null) {
session.close(); public void handleTestPlanPendingNoEnoughCurrentLoad(UUID testPlanRunID) {
} updateStatus(TestPlanStatus.PendingNoEnoughCurrentLoad, testPlanRunID,
} 1);
} }
public void handleTestPlanPendingNoEnoughMaxLoad(UUID testPlanRunID) { public void handleTestPlanError(UUID testPlanRunID) {
updateStatus(TestPlanStatus.PendingNoEnoughMaxLoad, testPlanRunID, 1); updateStatus(TestPlanStatus.Error, testPlanRunID, 1);
} }
public void handleTestPlanPendingNoEnoughCurrentLoad(UUID testPlanRunID) { public void handleTestPlanRunning(UUID testPlanRunID) {
updateStatus(TestPlanStatus.PendingNoEnoughCurrentLoad, testPlanRunID, updateStatus(TestPlanStatus.InRunning, testPlanRunID, 0);
1); }
}
public void handleTestPlanComplete(UUID testPlanRunID) {
public void handleTestPlanError(UUID testPlanRunID) { updateStatus(TestPlanStatus.Complete, testPlanRunID, 0);
updateStatus(TestPlanStatus.Error, testPlanRunID, 1); }
}
private void updateStatus(TestPlanStatus status, UUID testPlanRunID,
public void handleTestPlanRunning(UUID testPlanRunID) { int failTimes) {
updateStatus(TestPlanStatus.InRunning, testPlanRunID, 0); Session session = this.getSessionHelper().openSession();
} Transaction transaction = session.beginTransaction();
try {
public void handleTestPlanComplete(UUID testPlanRunID) { TestPlanDB testPlanDB = doGetTestPlanDB(testPlanRunID, session);
updateStatus(TestPlanStatus.Complete, testPlanRunID, 0); testPlanDB.setCurrentStatus(status.name());
} testPlanDB.setFailTimes(testPlanDB.getFailTimes() + failTimes);
transaction.commit();
private void updateStatus(TestPlanStatus status, UUID testPlanRunID, } catch (Exception e) {
int failTimes) { logger.error(e.getMessage());
Session session = this.getSessionHelper().openSession(); logger.error("update status of testplan fails, testPlanID:"
Transaction transaction = session.beginTransaction(); + testPlanRunID.toString());
try { transaction.rollback();
TestPlanDB testPlanDB = doGetTestPlanDB(testPlanRunID, session); } finally {
testPlanDB.setCurrentStatus(status.name()); if (session != null) {
testPlanDB.setFailTimes(testPlanDB.getFailTimes() + failTimes); session.close();
transaction.commit(); }
} catch (Exception e) { }
logger.error(e.getMessage()); }
logger.error("update status of testplan fails, testPlanID:"
+ testPlanRunID.toString()); public TestPlanStatus queryTestPlanStatus(UUID testPlanRunID) {
transaction.rollback(); Session session = this.getSessionHelper().openSession();
} finally { try {
if (session != null) { TestPlanDB testPlanDB = (TestPlanDB) session
session.close(); .createCriteria(TestPlanDB.class)
} .add(Restrictions.eq("testPlanRunId",
} testPlanRunID.toString())).uniqueResult();
} return testPlanDB == null ? null : TestPlanStatus
.valueOf(testPlanDB.getCurrentStatus());
public TestPlanStatus queryTestPlanStatus(UUID testPlanRunID) { } catch (Exception e) {
Session session = this.getSessionHelper().openSession(); logger.error("there is an exception in queryTestPlanStatus!");
try { return null;
TestPlanDB testPlanDB = (TestPlanDB) session } finally {
.createCriteria(TestPlanDB.class) if (session != null) {
.add(Restrictions.eq("testPlanRunId", session.close();
testPlanRunID.toString())).uniqueResult(); }
return testPlanDB == null ? null : TestPlanStatus }
.valueOf(testPlanDB.getCurrentStatus()); }
} catch (Exception e) {
logger.error("there is an exception in queryTestPlanStatus!"); public TestPlanDB pickALatestPengdingWithMinimalFailTimes() {
return null; // TODO: fulfill this
} finally { return null;
if (session != null) { }
session.close();
} public TestPlanDBModel getTestPlanDBModel(UUID runId) {
} return this.getBusinessMapFactory().toModel(
} this.getTestPlanRepository().getTestPlan(runId));
}
public TestPlanDB pickALatestPengdingWithMinimalFailTimes() { }
// TODO: fulfill this
return null;
}
}

View File

@ -2,32 +2,17 @@ package org.bench4q.master.service.infrastructure;
import org.bench4q.master.entity.User; import org.bench4q.master.entity.User;
import org.bench4q.master.factory.UserFactory; 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.bench4q.master.repository.UserRepository;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
public class UserService { public class UserService {
private SessionHelper sessionHelper;
private UserFactory userFactory; private UserFactory userFactory;
private UserRepository userRepository; private UserRepository userRepository;
private HashHelper hashHelper;
public static byte NORAML_AUTHENTICATION = 0; public static byte NORAML_AUTHENTICATION = 0;
public static byte SUPER_AUTHENTICATION = 1; public static byte SUPER_AUTHENTICATION = 1;
private SessionHelper getSessionHelper() {
return sessionHelper;
}
@Autowired
public void setSessionHelper(SessionHelper sessionHelper) {
this.sessionHelper = sessionHelper;
}
private UserFactory getUserFactory() { private UserFactory getUserFactory() {
return userFactory; return userFactory;
} }
@ -46,15 +31,6 @@ public class UserService {
this.userRepository = userRepository; this.userRepository = userRepository;
} }
private HashHelper getHashHelper() {
return hashHelper;
}
@Autowired
private void setHashHelper(HashHelper hashHelper) {
this.hashHelper = hashHelper;
}
public boolean register(String userName, String password) { public boolean register(String userName, String password) {
return this.getUserRepository().attatch( return this.getUserRepository().attatch(
this.getUserFactory().createUser(userName, password)); this.getUserFactory().createUser(userName, password));
@ -69,36 +45,11 @@ public class UserService {
this.getUserFactory().createUser(userName, password)); this.getUserFactory().createUser(userName, password));
} }
public String hashPassword(String password) {
return this.getHashHelper().sha1Hash(password);
}
public User getUserByName(String userName) { public User getUserByName(String userName) {
Session session = this.getSessionHelper().openSession(); return this.getUserRepository().getUser(userName);
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();
}
}
} }
public User getUserById(int userId) { public User getUserById(int userId) {
Session session = this.getSessionHelper().openSession(); return this.getUserRepository().getEntity(userId);
try {
return (User) session.get(User.class, userId);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (session != null) {
session.close();
}
}
} }
} }

View File

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

View File

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

View File

@ -35,11 +35,26 @@ public class Test_UserService {
} }
@Test @Test
public void testIsExist() { public void testValidate() {
this.getUserService().register("test1", "test1"); this.getUserService().register("test1", "test1");
assertTrue(this.getUserService().validateUser("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 @After
public void cleanUp() { public void cleanUp() {
this.getUserService().delete("test1"); this.getUserService().delete("test1");

View File

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