diff --git a/src/main/java/org/bench4q/master/api/modelfactory/BusinessModelMapFactory.java b/src/main/java/org/bench4q/master/api/modelfactory/BusinessModelMapFactory.java index 156ac085..7fa24962 100644 --- a/src/main/java/org/bench4q/master/api/modelfactory/BusinessModelMapFactory.java +++ b/src/main/java/org/bench4q/master/api/modelfactory/BusinessModelMapFactory.java @@ -186,7 +186,6 @@ public class BusinessModelMapFactory { ret.setFailTimes(testPlanDB.getFailTimes()); ret.setId(testPlanDB.getId()); ret.setName(testPlanDB.getName()); - ret.setTestPlanModelContent(testPlanDB.getTestPlanModelContent()); ret.setTestPlanRunId(testPlanDB.getTestPlanRunId()); ret.setUserModel(toModel(testPlanDB.getUser())); return ret; diff --git a/src/main/java/org/bench4q/master/entity/PlanedConfig.java b/src/main/java/org/bench4q/master/entity/PlanedConfig.java index 039c277d..23159da0 100644 --- a/src/main/java/org/bench4q/master/entity/PlanedConfig.java +++ b/src/main/java/org/bench4q/master/entity/PlanedConfig.java @@ -1,69 +1,56 @@ -package org.bench4q.master.entity; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.OneToOne; -import javax.persistence.Table; - -@Entity -@Table(name = "planedConfig") -public class PlanedConfig { - private int id; - private TestPlanScript testPlanScript; - private long warmUp; - private long executeRange; - private long coolDown; - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id", nullable = false) - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - @OneToOne - @JoinColumn(name = "testPlanScriptId", nullable = false) - public TestPlanScript getTestPlanScript() { - return testPlanScript; - } - - public void setTestPlanScript(TestPlanScript testPlanScript) { - this.testPlanScript = testPlanScript; - } - - @Column(name = "warmUp", nullable = false) - public long getWarmUp() { - return warmUp; - } - - public void setWarmUp(long warmUp) { - this.warmUp = warmUp; - } - - @Column(name = "excuteRange", nullable = false) - public long getExecuteRange() { - return executeRange; - } - - public void setExecuteRange(long executeRange) { - this.executeRange = executeRange; - } - - @Column(name = "coolDown", nullable = false) - public long getCoolDown() { - return coolDown; - } - - public void setCoolDown(long coolDown) { - this.coolDown = coolDown; - } - -} +package org.bench4q.master.entity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "planedConfig") +public class PlanedConfig { + private int id; + private long warmUp; + private long executeRange; + private long coolDown; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", nullable = false) + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + @Column(name = "warmUp", nullable = false) + public long getWarmUp() { + return warmUp; + } + + public void setWarmUp(long warmUp) { + this.warmUp = warmUp; + } + + @Column(name = "excuteRange", nullable = false) + public long getExecuteRange() { + return executeRange; + } + + public void setExecuteRange(long executeRange) { + this.executeRange = executeRange; + } + + @Column(name = "coolDown", nullable = false) + public long getCoolDown() { + return coolDown; + } + + public void setCoolDown(long coolDown) { + this.coolDown = coolDown; + } + +} diff --git a/src/main/java/org/bench4q/master/entity/TestPlanDB.java b/src/main/java/org/bench4q/master/entity/TestPlanDB.java index c8016169..cf7e3b62 100644 --- a/src/main/java/org/bench4q/master/entity/TestPlanDB.java +++ b/src/main/java/org/bench4q/master/entity/TestPlanDB.java @@ -1,14 +1,18 @@ package org.bench4q.master.entity; import java.util.Date; +import java.util.Set; +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @@ -19,9 +23,9 @@ public class TestPlanDB { private Date createDateTime; private User user; private String testPlanRunId; - private String testPlanModelContent; private String currentStatus; private int failTimes; + private Set testPlanScripts; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -71,15 +75,6 @@ public class TestPlanDB { this.user = user; } - @Column(name = "testPlanModelContent", columnDefinition = "LONGText", nullable = false) - public String getTestPlanModelContent() { - return testPlanModelContent; - } - - public void setTestPlanModelContent(String testPlanModelContent) { - this.testPlanModelContent = testPlanModelContent; - } - @Column(name = "currentStatus", nullable = false) public String getCurrentStatus() { return currentStatus; @@ -98,4 +93,22 @@ public class TestPlanDB { this.failTimes = failTimes; } + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) + @JoinColumn(name = "testPlanId") + public Set getTestPlanScripts() { + return testPlanScripts; + } + + public void setTestPlanScripts(Set testPlanScripts) { + this.testPlanScripts = testPlanScripts; + } + + public TestPlanScript extracSpecifiedScript(Script script) { + for (TestPlanScript testPlanScript : testPlanScripts) { + if (testPlanScript.getScript().getId() == script.getId()) { + return testPlanScript; + } + } + return null; + } } diff --git a/src/main/java/org/bench4q/master/entity/TestPlanScript.java b/src/main/java/org/bench4q/master/entity/TestPlanScript.java index 0d6bf7e9..81eff040 100644 --- a/src/main/java/org/bench4q/master/entity/TestPlanScript.java +++ b/src/main/java/org/bench4q/master/entity/TestPlanScript.java @@ -1,61 +1,62 @@ -package org.bench4q.master.entity; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.OneToOne; -import javax.persistence.Table; - -@Entity -@Table(name = "TestPlanScript") -public class TestPlanScript { - private int id; - private Script script; - private int requireLoad; - private TestPlanDB testPlanDB; - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id", nullable = false) - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - @OneToOne - @JoinColumn(name = "scriptId", nullable = false) - public Script getScript() { - return script; - } - - public void setScript(Script script) { - this.script = script; - } - - @Column(name = "requireLoad", nullable = false) - public int getRequireLoad() { - return requireLoad; - } - - public void setRequireLoad(int requireLoad) { - this.requireLoad = requireLoad; - } - - @ManyToOne - @JoinColumn(name = "testPlanId", nullable = false) - public TestPlanDB getTestPlanDB() { - return testPlanDB; - } - - public void setTestPlanDB(TestPlanDB testPlanDB) { - this.testPlanDB = testPlanDB; - } - -} +package org.bench4q.master.entity; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "TestPlanScript") +public class TestPlanScript { + private int id; + private Script script; + private int requireLoad; + private PlanedConfig planedConfig; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", nullable = false) + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + @OneToOne + @JoinColumn(name = "scriptId", nullable = false) + public Script getScript() { + return script; + } + + public void setScript(Script script) { + this.script = script; + } + + @Column(name = "requireLoad", nullable = false) + public int getRequireLoad() { + return requireLoad; + } + + public void setRequireLoad(int requireLoad) { + this.requireLoad = requireLoad; + } + + @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) + @JoinColumn(name = "configId", nullable = false) + public PlanedConfig getPlanedConfig() { + return planedConfig; + } + + public void setPlanedConfig(PlanedConfig planedConfig) { + this.planedConfig = planedConfig; + } + +} diff --git a/src/main/java/org/bench4q/master/factory/TestPlanFactory.java b/src/main/java/org/bench4q/master/factory/TestPlanFactory.java new file mode 100644 index 00000000..c41ac90d --- /dev/null +++ b/src/main/java/org/bench4q/master/factory/TestPlanFactory.java @@ -0,0 +1,70 @@ +package org.bench4q.master.factory; + +import java.util.Date; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +import org.bench4q.master.entity.PlanedConfig; +import org.bench4q.master.entity.TestPlanDB; +import org.bench4q.master.entity.TestPlanScript; +import org.bench4q.master.entity.User; +import org.bench4q.master.service.infrastructure.ScriptService; +import org.bench4q.share.enums.master.TestPlanStatus; +import org.bench4q.share.models.master.RunningScriptModel; +import org.bench4q.share.models.master.TestPlanBusinessModel; +import org.bench4q.share.models.master.TestScriptConfig; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class TestPlanFactory { + private ScriptService scriptService; + + private ScriptService getScriptService() { + return scriptService; + } + + @Autowired + private void setScriptService(ScriptService scriptService) { + this.scriptService = scriptService; + } + + public TestPlanDB createATestPlanWithoutIdentity( + TestPlanBusinessModel testPlanBusinessModel, User user, UUID runId) { + TestPlanDB result = new TestPlanDB(); + result.setCreateDateTime(new Date()); + result.setCurrentStatus(TestPlanStatus.NotStart.name()); + result.setFailTimes(0); + result.setName(testPlanBusinessModel.getName()); + result.setTestPlanRunId(runId.toString()); + result.setUser(user); + Set testPlanScripts = new HashSet(); + for (RunningScriptModel runningScriptModel : testPlanBusinessModel + .getRunningScriptModels()) { + testPlanScripts.add(createATestPlanScriptWithoutId( + runningScriptModel.getRequireLoad(), + runningScriptModel.getScriptId(), + runningScriptModel.getConfig())); + } + result.setTestPlanScripts(testPlanScripts); + return result; + } + + public TestPlanScript createATestPlanScriptWithoutId(int requireLoad, + int scriptId, TestScriptConfig config) { + TestPlanScript testPlanScript = new TestPlanScript(); + testPlanScript.setRequireLoad(requireLoad); + testPlanScript.setScript(this.getScriptService().getScript(scriptId)); + testPlanScript.setPlanedConfig(createAPlanedConfigWithoutId(config)); + return testPlanScript; + } + + private PlanedConfig createAPlanedConfigWithoutId(TestScriptConfig config) { + PlanedConfig planedConfig = new PlanedConfig(); + planedConfig.setWarmUp(config.getWarmUp()); + planedConfig.setExecuteRange(config.getExecuteRange()); + planedConfig.setCoolDown(config.getCoolDown()); + return planedConfig; + } +} diff --git a/src/main/java/org/bench4q/master/report/ScriptReportService.java b/src/main/java/org/bench4q/master/report/ScriptReportService.java index 4130fcc7..9b2f04b3 100644 --- a/src/main/java/org/bench4q/master/report/ScriptReportService.java +++ b/src/main/java/org/bench4q/master/report/ScriptReportService.java @@ -1,128 +1,140 @@ -package org.bench4q.master.report; - -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -import org.apache.log4j.Logger; -import org.bench4q.master.entity.TestPlanScript; -import org.bench4q.master.entity.TestPlanScriptResult; -import org.bench4q.master.service.infrastructure.MonitorResultService; -import org.bench4q.master.service.infrastructure.TestPlanScriptService; -import org.bench4q.master.service.infrastructure.TestPlanService; -import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; -import org.jfree.data.time.Second; -import org.jfree.data.time.TimeSeries; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import com.lowagie.text.Document; - -@Component -public class ScriptReportService { - public static String SCRIPT_TITLE = "Script result"; - public static String Average_Response_Time = "averageResponseTime(unit: ms)"; - public static String MAX_RESPONSE_TIME = "maxResponseTime(unit : time)"; - private TestPlanScriptService testPlanScriptService; - private TestPlanService testPlanService; - private MonitorResultService monitorResultService; - private Logger logger = Logger.getLogger(ScriptReportService.class); - - public TestPlanScriptService getTestPlanScriptService() { - return testPlanScriptService; - } - - @Autowired - public void setTestPlanScriptService( - TestPlanScriptService testPlanScriptService) { - this.testPlanScriptService = testPlanScriptService; - } - - public TestPlanService getTestPlanService() { - return testPlanService; - } - - @Autowired - public void setTestPlanService(TestPlanService testPlanService) { - this.testPlanService = testPlanService; - } - - public MonitorResultService getMonitorResultService() { - return monitorResultService; - } - - @Autowired - public void setMonitorResultService( - MonitorResultService monitorResultService) { - this.monitorResultService = monitorResultService; - } - - void createScriptsResultsImage(UUID testPlanRunId, Document document) { - - List scripts = this.getTestPlanScriptService() - .queryTestPlanScripts(testPlanRunId); - if (scripts == null) { - return; - } - for (TestPlanScript testPlanScript : scripts) { - createScriptImage(this.getTestPlanScriptService() - .queryScriptBriefResults(testPlanScript), document); - } - - } - - private void createScriptImage(List results, - Document document) { - - if (results == null) { - return; - } - createAverageReponseTimeImage(results, document); - } - - private void createAverageReponseTimeImage( - List results, Document document) { - List timeSeriesList = new ArrayList(); - if (results == null) { - return; - } - addScriptSeries(results, timeSeriesList, "averageResponseTime"); - addScriptSeries(results, timeSeriesList, "maxResponseTime"); - try { - ReportService.writeImageIntoPdf( - ReportService.buildChartStream(document, - timeSeriesList.size(), timeSeriesList, - Average_Response_Time + "&" + MAX_RESPONSE_TIME) - .toByteArray(), document); - } catch (Exception e) { - e.printStackTrace(); - } - } - - // TODO: refactor this to a kind of command mode - private void addScriptSeries(List results, - List timeSeriesList, String fieldName) { - try { - TimeSeries timeSeries = new TimeSeries(fieldName); - for (TestPlanScriptResult result : results) { - if (!result.getResultType().equals( - ScriptBriefResultModel.class.getName())) { - continue; - } - ScriptBriefResultModel scriptBriefResultModel = result - .extractScriptBriefResultModel(); - Field field = scriptBriefResultModel.getClass() - .getDeclaredField(fieldName); - field.setAccessible(true); - timeSeries.addOrUpdate(new Second(result.getCreateDatetime()), - field.getLong(scriptBriefResultModel)); - } - timeSeriesList.add(timeSeries); - } catch (Exception e) { - logger.error(e.toString() - + " in addScriptSeries where fieldName is : " + fieldName); - } - - } -} +package org.bench4q.master.report; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +import org.apache.log4j.Logger; +import org.bench4q.master.entity.TestPlanScript; +import org.bench4q.master.entity.TestPlanScriptResult; +import org.bench4q.master.repository.TestPlanRepository; +import org.bench4q.master.service.infrastructure.MonitorResultService; +import org.bench4q.master.service.infrastructure.TestPlanScriptService; +import org.bench4q.master.service.infrastructure.TestPlanService; +import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; +import org.jfree.data.time.Second; +import org.jfree.data.time.TimeSeries; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.lowagie.text.Document; + +@Component +public class ScriptReportService { + public static String SCRIPT_TITLE = "Script result"; + public static String Average_Response_Time = "averageResponseTime(unit: ms)"; + public static String MAX_RESPONSE_TIME = "maxResponseTime(unit : time)"; + private TestPlanScriptService testPlanScriptService; + private TestPlanService testPlanService; + private TestPlanRepository testPlanRepository; + private MonitorResultService monitorResultService; + private Logger logger = Logger.getLogger(ScriptReportService.class); + + public TestPlanScriptService getTestPlanScriptService() { + return testPlanScriptService; + } + + @Autowired + public void setTestPlanScriptService( + TestPlanScriptService testPlanScriptService) { + this.testPlanScriptService = testPlanScriptService; + } + + public TestPlanService getTestPlanService() { + return testPlanService; + } + + @Autowired + public void setTestPlanService(TestPlanService testPlanService) { + this.testPlanService = testPlanService; + } + + public MonitorResultService getMonitorResultService() { + return monitorResultService; + } + + @Autowired + public void setMonitorResultService( + MonitorResultService monitorResultService) { + this.monitorResultService = monitorResultService; + } + + private TestPlanRepository getTestPlanRepository() { + return testPlanRepository; + } + + @Autowired + private void setTestPlanRepository(TestPlanRepository testPlanRepository) { + this.testPlanRepository = testPlanRepository; + } + + void createScriptsResultsImage(UUID testPlanRunId, Document document) { + + Set scripts = this.getTestPlanRepository() + .getTestPlan(testPlanRunId).getTestPlanScripts(); + if (scripts == null) { + return; + } + for (TestPlanScript testPlanScript : scripts) { + createScriptImage(this.getTestPlanScriptService() + .queryScriptBriefResults(testPlanScript), document); + } + + } + + private void createScriptImage(List results, + Document document) { + + if (results == null) { + return; + } + createAverageReponseTimeImage(results, document); + } + + private void createAverageReponseTimeImage( + List results, Document document) { + List timeSeriesList = new ArrayList(); + if (results == null) { + return; + } + addScriptSeries(results, timeSeriesList, "averageResponseTime"); + addScriptSeries(results, timeSeriesList, "maxResponseTime"); + try { + ReportService.writeImageIntoPdf( + ReportService.buildChartStream(document, + timeSeriesList.size(), timeSeriesList, + Average_Response_Time + "&" + MAX_RESPONSE_TIME) + .toByteArray(), document); + } catch (Exception e) { + e.printStackTrace(); + } + } + + // TODO: refactor this to a kind of command mode + private void addScriptSeries(List results, + List timeSeriesList, String fieldName) { + try { + TimeSeries timeSeries = new TimeSeries(fieldName); + for (TestPlanScriptResult result : results) { + if (!result.getResultType().equals( + ScriptBriefResultModel.class.getName())) { + continue; + } + ScriptBriefResultModel scriptBriefResultModel = result + .extractScriptBriefResultModel(); + Field field = scriptBriefResultModel.getClass() + .getDeclaredField(fieldName); + field.setAccessible(true); + timeSeries.addOrUpdate(new Second(result.getCreateDatetime()), + field.getLong(scriptBriefResultModel)); + } + timeSeriesList.add(timeSeries); + } catch (Exception e) { + logger.error(e.toString() + + " in addScriptSeries where fieldName is : " + fieldName); + } + + } +} diff --git a/src/main/java/org/bench4q/master/repository/TestPlanRepository.java b/src/main/java/org/bench4q/master/repository/TestPlanRepository.java index ffe786e4..13c71b0c 100644 --- a/src/main/java/org/bench4q/master/repository/TestPlanRepository.java +++ b/src/main/java/org/bench4q/master/repository/TestPlanRepository.java @@ -3,9 +3,8 @@ package org.bench4q.master.repository; import java.util.List; import java.util.UUID; -import org.bench4q.master.entity.PlanedConfig; +import org.apache.log4j.Logger; 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.hibernate.Session; @@ -15,6 +14,23 @@ import org.springframework.stereotype.Component; @Component public class TestPlanRepository extends AbstractRepositoty { + private Logger logger = Logger.getLogger(TestPlanRepository.class); + + public boolean attach(TestPlanDB testPlanDB) { + Session session = this.getSessionHelper().openSession(); + Transaction transaction = session.beginTransaction(); + try { + session.merge(testPlanDB); + transaction.commit(); + return true; + } catch (Exception e) { + logger.error(ExceptionLog.getStackTrace(e)); + transaction.rollback(); + return false; + } finally { + releaseSession(session); + } + } public boolean detach(int testPlanId) { Session session = this.getSessionHelper().openSession(); @@ -26,27 +42,6 @@ public class TestPlanRepository extends AbstractRepositoty { if (testPlanInDB == null) { return false; } - @SuppressWarnings("unchecked") - List testPlanScripts = session - .createCriteria(TestPlanScript.class) - .add(Restrictions.eq("testPlanDB", testPlanInDB)).list(); - if (testPlanScripts == null) { - return false; - } - for (TestPlanScript testPlanScript : testPlanScripts) { - @SuppressWarnings("unchecked") - List 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; diff --git a/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptResultService.java b/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptResultService.java index 9250bf40..8f2c2950 100644 --- a/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptResultService.java +++ b/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptResultService.java @@ -1,193 +1,221 @@ -package org.bench4q.master.service.infrastructure; - -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.entity.TestPlanScriptResult; -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; -import org.hibernate.Session; -import org.hibernate.criterion.Order; -import org.hibernate.criterion.Restrictions; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class TestPlanScriptResultService { - private TestPlanScriptService testPlanScriptService; - 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; - } - - @Autowired - public void setSessionHelper(SessionHelper sessionHelper) { - this.sessionHelper = sessionHelper; - } - - /** - * 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 loadScriptBriefSpecificField(UUID testPlanId, - int scriptId, String fieldName) { - List scriptBriefresults = this - .getTestPlanScriptService().queryScriptBriefResults(testPlanId, - scriptId); - List ret = new ArrayList(); - for (TestPlanScriptResult result : scriptBriefresults) { - try { - ScriptBriefResultModel briefModel = (ScriptBriefResultModel) MarshalHelper - .unmarshal(ScriptBriefResultModel.class, - result.getResultContent()); - ret.add(ValueTimeModel.buildValueTimeModel( - getSpecificFieldValue(briefModel, fieldName), - result.getCreateDatetime())); - } catch (JAXBException e) { - logger.info(ExceptionLog.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 loadScriptBriefWithDuation( - UUID testPlanId, int scriptId, long startTime) { - Session session = this.getSessionHelper().openSession(); - List result = new ArrayList(); - try { - List scriptResults = doGetTestPlanScriptResults( - testPlanId, scriptId, startTime, - ScriptBriefResultModel.class, session); - for (TestPlanScriptResult testPlanScriptResult : scriptResults) { - result.add((ScriptBriefResultModel) MarshalHelper.unmarshal( - ScriptBriefResultModel.class, - testPlanScriptResult.getResultContent())); - } - } catch (Exception e) { - logger.error(ExceptionLog.getStackTrace(e)); - } - return result; - } - - private List doGetTestPlanScriptResults( - UUID testPlanId, int scriptId, long startTime, Class resultType, - Session session) { - @SuppressWarnings("unchecked") - List results = (List) session - .createCriteria(TestPlanScriptResult.class) - .add(Restrictions.eq( - "testPlanScript", - this.getTestPlanScriptService().doGetTestPlanScript( - scriptId, testPlanId, session))) - .add(Restrictions.eq("resultType", resultType.getName())) - .add(Restrictions.ge("createDatetime", new Date(startTime))) - .list(); - return results; - } - - public ScriptBehaviorsBriefModel loadScriptBehaviorsBrief(UUID testPlanId, - int scriptId) { - Session session = this.getSessionHelper().openSession(); - try { - TestPlanScriptResult testPlanScriptResult = doGetLastSampleResult( - testPlanId, scriptId, ScriptBehaviorsBriefModel.class, - session); - if (testPlanScriptResult == null) { - return new ScriptBehaviorsBriefModel(); - } - return (ScriptBehaviorsBriefModel) MarshalHelper.unmarshal( - ScriptBehaviorsBriefModel.class, - testPlanScriptResult.getResultContent()); - } catch (Exception e) { - logger.error(ExceptionLog.getStackTrace(e)); - } - return new ScriptBehaviorsBriefModel(); - } - - private TestPlanScriptResult doGetLastSampleResult(UUID testPlanId, - int scriptId, Class resultType, Session session) { - @SuppressWarnings("unchecked") - List results = (List) session - .createCriteria(TestPlanScriptResult.class) - .add(Restrictions.eq( - "testPlanScript", - this.getTestPlanScriptService().getTestPlanScript( - scriptId, testPlanId))) - .add(Restrictions.eq("resultType", resultType.getName())) - .addOrder(Order.desc("createDatetime")).setFetchSize(1).list(); - if (results.size() > 0) { - return results.get(0); - } - return null; - } - - public ScriptPagesBriefModel loadScriptPagesBrief(UUID testPlanId, - int scriptId) { - Session session = this.getSessionHelper().openSession(); - ScriptPagesBriefModel result = new ScriptPagesBriefModel(); - try { - TestPlanScriptResult testPlanScriptResult = doGetLastSampleResult( - testPlanId, scriptId, ScriptPagesBriefModel.class, session); - if (testPlanScriptResult == null) { - return result; - } - result = (ScriptPagesBriefModel) MarshalHelper.unmarshal( - ScriptPagesBriefModel.class, - testPlanScriptResult.getResultContent()); - } catch (Exception e) { - logger.error(ExceptionLog.getStackTrace(e)); - } - return result; - } -} +package org.bench4q.master.service.infrastructure; + +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.entity.TestPlanScript; +import org.bench4q.master.entity.TestPlanScriptResult; +import org.bench4q.master.exception.ExceptionLog; +import org.bench4q.master.helper.SessionHelper; +import org.bench4q.master.repository.TestPlanRepository; +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; +import org.hibernate.Session; +import org.hibernate.criterion.Order; +import org.hibernate.criterion.Restrictions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class TestPlanScriptResultService { + private TestPlanScriptService testPlanScriptService; + private TestPlanRepository testPlanRepository; + private ScriptService scriptService; + 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; + } + + @Autowired + public void setSessionHelper(SessionHelper sessionHelper) { + this.sessionHelper = sessionHelper; + } + + private TestPlanRepository getTestPlanRepository() { + return testPlanRepository; + } + + @Autowired + private void setTestPlanRepository(TestPlanRepository testPlanRepository) { + this.testPlanRepository = testPlanRepository; + } + + private ScriptService getScriptService() { + return scriptService; + } + + @Autowired + private void setScriptService(ScriptService scriptService) { + this.scriptService = scriptService; + } + + /** + * 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 loadScriptBriefSpecificField(UUID testPlanId, + int scriptId, String fieldName) { + List scriptBriefresults = this + .getTestPlanScriptService().queryScriptBriefResults(testPlanId, + scriptId); + List ret = new ArrayList(); + for (TestPlanScriptResult result : scriptBriefresults) { + try { + ScriptBriefResultModel briefModel = (ScriptBriefResultModel) MarshalHelper + .unmarshal(ScriptBriefResultModel.class, + result.getResultContent()); + ret.add(ValueTimeModel.buildValueTimeModel( + getSpecificFieldValue(briefModel, fieldName), + result.getCreateDatetime())); + } catch (JAXBException e) { + logger.info(ExceptionLog.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 loadScriptBriefWithDuation( + UUID testPlanId, int scriptId, long startTime) { + Session session = this.getSessionHelper().openSession(); + List result = new ArrayList(); + try { + List scriptResults = doGetTestPlanScriptResults( + testPlanId, scriptId, startTime, + ScriptBriefResultModel.class, session); + for (TestPlanScriptResult testPlanScriptResult : scriptResults) { + result.add((ScriptBriefResultModel) MarshalHelper.unmarshal( + ScriptBriefResultModel.class, + testPlanScriptResult.getResultContent())); + } + } catch (Exception e) { + logger.error(ExceptionLog.getStackTrace(e)); + } + return result; + } + + private List doGetTestPlanScriptResults( + UUID testPlanId, int scriptId, long startTime, Class resultType, + Session session) { + // TestPlanScript testPlanScript = this.getTestPlanScriptService() + // .doGetTestPlanScript(scriptId, testPlanId, session); + TestPlanScript testPlanScript = this + .getTestPlanRepository() + .getTestPlan(testPlanId) + .extracSpecifiedScript( + this.getScriptService().getScript(scriptId)); + @SuppressWarnings("unchecked") + List results = (List) session + .createCriteria(TestPlanScriptResult.class) + .add(Restrictions.eq("testPlanScript", testPlanScript)) + .add(Restrictions.eq("resultType", resultType.getName())) + .add(Restrictions.ge("createDatetime", new Date(startTime))) + .list(); + return results; + } + + public ScriptBehaviorsBriefModel loadScriptBehaviorsBrief(UUID testPlanId, + int scriptId) { + Session session = this.getSessionHelper().openSession(); + try { + TestPlanScriptResult testPlanScriptResult = doGetLastSampleResult( + testPlanId, scriptId, ScriptBehaviorsBriefModel.class, + session); + if (testPlanScriptResult == null) { + return new ScriptBehaviorsBriefModel(); + } + return (ScriptBehaviorsBriefModel) MarshalHelper.unmarshal( + ScriptBehaviorsBriefModel.class, + testPlanScriptResult.getResultContent()); + } catch (Exception e) { + logger.error(ExceptionLog.getStackTrace(e)); + } + return new ScriptBehaviorsBriefModel(); + } + + private TestPlanScriptResult doGetLastSampleResult(UUID testPlanId, + int scriptId, Class resultType, Session session) { + TestPlanScript testPlanScript = this + .getTestPlanRepository() + .getTestPlan(testPlanId) + .extracSpecifiedScript( + this.getScriptService().getScript(scriptId)); + @SuppressWarnings("unchecked") + List results = (List) session + .createCriteria(TestPlanScriptResult.class) + .add(Restrictions.eq("testPlanScript", testPlanScript)) + .add(Restrictions.eq("resultType", resultType.getName())) + .addOrder(Order.desc("createDatetime")).setFetchSize(1).list(); + if (results.size() > 0) { + return results.get(0); + } + return null; + } + + public ScriptPagesBriefModel loadScriptPagesBrief(UUID testPlanId, + int scriptId) { + Session session = this.getSessionHelper().openSession(); + ScriptPagesBriefModel result = new ScriptPagesBriefModel(); + try { + TestPlanScriptResult testPlanScriptResult = doGetLastSampleResult( + testPlanId, scriptId, ScriptPagesBriefModel.class, session); + if (testPlanScriptResult == null) { + return result; + } + result = (ScriptPagesBriefModel) MarshalHelper.unmarshal( + ScriptPagesBriefModel.class, + testPlanScriptResult.getResultContent()); + } catch (Exception e) { + logger.error(ExceptionLog.getStackTrace(e)); + } + return result; + } +} diff --git a/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptService.java b/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptService.java index 7470bdf4..3ff142fb 100644 --- a/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptService.java +++ b/src/main/java/org/bench4q/master/service/infrastructure/TestPlanScriptService.java @@ -1,10 +1,12 @@ package org.bench4q.master.service.infrastructure; +import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Observable; import java.util.Observer; import java.util.UUID; + import javax.xml.bind.JAXBException; import org.apache.log4j.Logger; @@ -63,27 +65,10 @@ public class TestPlanScriptService implements Observer { } public TestPlanScript getTestPlanScript(int scriptId, UUID testPlanRunId) { - Session session = this.getSessionHelper().openSession(); - try { - return doGetTestPlanScript(scriptId, testPlanRunId, session); - } catch (Exception e) { - e.printStackTrace(); - return null; - } finally { - if (session != null) { - session.close(); - } - } - } - - TestPlanScript doGetTestPlanScript(int scriptId, UUID testPlanRunId, - Session session) { - return (TestPlanScript) session - .createCriteria(TestPlanScript.class) - .add(Restrictions.eq("script", this.getScriptService() - .getScript(scriptId))) - .add(Restrictions.eq("testPlanDB", this.getTestPlanRepository() - .getTestPlan(testPlanRunId))).uniqueResult(); + TestPlanDB testPlan = this.getTestPlanRepository().getTestPlan( + testPlanRunId); + return testPlan == null ? null : testPlan.extracSpecifiedScript(this + .getScriptService().getScript(scriptId)); } public boolean saveScriptBriefResult(final UUID testPlanRunId, @@ -131,10 +116,9 @@ public class TestPlanScriptService implements Observer { if (script == null) { return; } - TestPlanScript testPlanScript = (TestPlanScript) session - .createCriteria(TestPlanScript.class) - .add(Restrictions.eq("testPlanDB", testPlanDB)) - .add(Restrictions.eq("script", script)).uniqueResult(); + + TestPlanScript testPlanScript = testPlanDB + .extracSpecifiedScript(script); if (testPlanScript == null) { return; } @@ -169,23 +153,9 @@ public class TestPlanScriptService implements Observer { return result; } - @SuppressWarnings("unchecked") - public List queryTestPlanScripts(UUID testPlanRunId) { - Session session = this.getSessionHelper().openSession(); - try { - return session - .createCriteria(TestPlanScript.class) - .add(Restrictions - .eq("testPlanDB", this.getTestPlanRepository() - .getTestPlan(testPlanRunId))).list(); - } catch (Exception e) { - e.printStackTrace(); - return null; - } finally { - if (session != null) { - session.close(); - } - } + public Collection queryTestPlanScripts(UUID testPlanRunId) { + return this.getTestPlanRepository().getTestPlan(testPlanRunId) + .getTestPlanScripts(); } public List queryScriptBriefResults(UUID testPlanId, diff --git a/src/main/java/org/bench4q/master/service/infrastructure/TestPlanService.java b/src/main/java/org/bench4q/master/service/infrastructure/TestPlanService.java index ad2e8bf0..db890be6 100644 --- a/src/main/java/org/bench4q/master/service/infrastructure/TestPlanService.java +++ b/src/main/java/org/bench4q/master/service/infrastructure/TestPlanService.java @@ -1,27 +1,19 @@ 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.factory.TestPlanFactory; 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.springframework.beans.factory.annotation.Autowired; @@ -30,9 +22,9 @@ import org.springframework.stereotype.Component; @Component public class TestPlanService { private SessionHelper sessionHelper; - private ScriptService scriptService; private BusinessModelMapFactory businessMapFactory; private TestPlanRepository testPlanRepository; + private TestPlanFactory testPlanFactory; private static Logger logger = Logger.getLogger(TestPlanService.class); public static final long TIME_UNIT = 1000; @@ -41,11 +33,6 @@ public class TestPlanService { this.sessionHelper = sessionHelper; } - @Autowired - private void setScriptService(ScriptService scriptService) { - this.scriptService = scriptService; - } - private TestPlanRepository getTestPlanRepository() { return testPlanRepository; } @@ -65,24 +52,24 @@ public class TestPlanService { this.businessMapFactory = businessMapFactory; } + private TestPlanFactory getTestPlanFactory() { + return testPlanFactory; + } + + @Autowired + private void setTestPlanFactory(TestPlanFactory testPlanFactory) { + this.testPlanFactory = testPlanFactory; + } + public boolean saveTestPlanToDB(final TestPlanInBusiness testPlanInParam, final User user, final UUID testPlanRunId) { Session session = this.sessionHelper.openSession(); Transaction transaction = session.beginTransaction(); - TestPlanDB testPlanInSession; try { - testPlanInSession = saveToTestPlan(session, - testPlanInParam.getName(), MarshalHelper.marshal( - TestPlanBusinessModel.class, + session.merge(this.getTestPlanFactory() + .createATestPlanWithoutIdentity( this.getBusinessMapFactory().toModel( - testPlanInParam)), user, testPlanRunId); - if (testPlanInSession == null) { - return false; - } - if (!saveToTestPlanScriptAndScriptConfig(session, - testPlanInSession, testPlanInParam.getRunningScripts())) { - return false; - } + testPlanInParam), user, testPlanRunId)); transaction.commit(); return true; } catch (Exception e) { @@ -96,59 +83,6 @@ public class TestPlanService { } } - 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 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 List loadTestPlans(User user) { return this.getTestPlanRepository().loadEntities(user); } diff --git a/src/test/java/org/bench4q/master/test/service/Test_TestPlanService.java b/src/test/java/org/bench4q/master/test/service/Test_TestPlanService.java index 01d52701..7fa07c33 100644 --- a/src/test/java/org/bench4q/master/test/service/Test_TestPlanService.java +++ b/src/test/java/org/bench4q/master/test/service/Test_TestPlanService.java @@ -4,6 +4,7 @@ import static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.UUID; import org.bench4q.master.api.modelfactory.BusinessModelMapFactory; @@ -101,6 +102,7 @@ public class Test_TestPlanService extends TestBase_MakeUpTestPlan { User user = this.getUserRepository().getUser("admin"); int planCountBeforeSubmit = this.getTestPlanRepository() .loadEntities(user).size(); + System.out.println(planCountBeforeSubmit); UUID randomUUID = UUID.randomUUID(); TestPlanInBusiness testPlanInBusiness = new TestPlanInBusiness(); testPlanInBusiness.setName("test1"); @@ -123,10 +125,14 @@ public class Test_TestPlanService extends TestBase_MakeUpTestPlan { int planCountAfterSubmit = this.getTestPlanRepository() .loadEntities(user).size(); assertEquals(planCountBeforeSubmit + 1, planCountAfterSubmit); - TestPlanScript testPlanScript = this.getTestPlanScriptService() - .getTestPlanScript(userScript, randomUUID); - assertNotNull(testPlanScript); - assertEquals(userScript, testPlanScript.getScript().getId()); + Set testPlanScripts = this.getTestPlanRepository() + .getTestPlan(randomUUID).getTestPlanScripts(); + assertNotNull(testPlanScripts); + assertEquals(1, testPlanScripts.size()); + for (TestPlanScript testPlanScript : testPlanScripts) { + assertEquals(userScript, testPlanScript.getScript().getId()); + assertEquals(0, testPlanScript.getPlanedConfig().getWarmUp()); + } this.getTestPlanRepository().detach( this.getTestPlanRepository().getTestPlan(randomUUID).getId()); }