From f6acca7006b42c6dda0d29676529d1d4d517229e Mon Sep 17 00:00:00 2001 From: rkv Date: Wed, 13 Mar 2013 12:14:20 +0000 Subject: [PATCH] assignStudyContext is implemented for calling from Python. Unit tests are added. --- .../src/conf/log-messages.properties | 2 + .../src/conf/log-messages_en.properties | 2 + .../common/properties/MessageKeyEnum.java | 10 +- .../org/splat/service/ScenarioService.java | 29 ++- .../splat/service/ScenarioServiceImpl.java | 61 +++++-- .../SimulationContextTypeServiceImpl.java | 17 +- .../src/spring/businessServiceContext.xml | 6 +- .../splat/service/TestScenarioService.java | 166 +++++++++++++++++- 8 files changed, 267 insertions(+), 26 deletions(-) diff --git a/Workspace/Siman-Common/src/conf/log-messages.properties b/Workspace/Siman-Common/src/conf/log-messages.properties index 5ee7cdb..17eee64 100644 --- a/Workspace/Siman-Common/src/conf/log-messages.properties +++ b/Workspace/Siman-Common/src/conf/log-messages.properties @@ -4,6 +4,7 @@ LCK-000002=Lock reference does not exists. LCK-000003=Lock reference protected and can be only deleted or updated by user {0}. LCK-000004=Lock reference is timeout and could have been modified by user {0}. STD-000001=Unable to re-index the study #{0}, reason: {1} +STD-000002=The study #{0} is not found SCN-000001=Scenario doesn't contain the step number #{0} SCN-000002=Scenario doesn't contain the document #{0} SCN-000003=The existing file {0} has been deleted when check-in the scenario #{0} @@ -15,6 +16,7 @@ SCN-000008=The scenario #{0} is already checked out by the user {1} USR-000001=User {0} is not found in the database KNT-000001=Knowledge element type "{0}" already exists SCT-000001=Simulation context type "{0}" already exists +SCT-000002=Simulation context type name could not be blank DCT-000001=Document type "{0}" already exists DCT-000002=Can not delete the document "{0}" because it is used by other documents. DCT-000003=Can not save a document in {0} state. Check the validation cycle. diff --git a/Workspace/Siman-Common/src/conf/log-messages_en.properties b/Workspace/Siman-Common/src/conf/log-messages_en.properties index 5ee7cdb..17eee64 100644 --- a/Workspace/Siman-Common/src/conf/log-messages_en.properties +++ b/Workspace/Siman-Common/src/conf/log-messages_en.properties @@ -4,6 +4,7 @@ LCK-000002=Lock reference does not exists. LCK-000003=Lock reference protected and can be only deleted or updated by user {0}. LCK-000004=Lock reference is timeout and could have been modified by user {0}. STD-000001=Unable to re-index the study #{0}, reason: {1} +STD-000002=The study #{0} is not found SCN-000001=Scenario doesn't contain the step number #{0} SCN-000002=Scenario doesn't contain the document #{0} SCN-000003=The existing file {0} has been deleted when check-in the scenario #{0} @@ -15,6 +16,7 @@ SCN-000008=The scenario #{0} is already checked out by the user {1} USR-000001=User {0} is not found in the database KNT-000001=Knowledge element type "{0}" already exists SCT-000001=Simulation context type "{0}" already exists +SCT-000002=Simulation context type name could not be blank DCT-000001=Document type "{0}" already exists DCT-000002=Can not delete the document "{0}" because it is used by other documents. DCT-000003=Can not save a document in {0} state. Check the validation cycle. diff --git a/Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java b/Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java index 1b88747..5faf78f 100644 --- a/Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java +++ b/Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java @@ -37,6 +37,10 @@ public enum MessageKeyEnum { * Unable to re-index the study #{0}, reason: {1}. */ STD_000001("STD-000001"), + /** + * The study #{0} is not found. + */ + STD_000002("STD-000002"), /** * Scenario doesn't contain the step number #{0}. */ @@ -78,9 +82,13 @@ public enum MessageKeyEnum { */ KNT_000001("KNT-000001"), /** - * Knowledge element type "{0}" already exists. + * Simulation context type "{0}" already exists. */ SCT_000001("SCT-000001"), + /** + * Simulation context type name could not be blank. + */ + SCT_000002("SCT-000002"), /** * Document type "{0}" already exists. */ diff --git a/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java b/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java index e00d678..58a1bbb 100644 --- a/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java +++ b/Workspace/Siman-Common/src/org/splat/service/ScenarioService.java @@ -41,6 +41,26 @@ public interface ScenarioService { */ List getScenarioInfo(long scenarioId); + /** + * Assign context to the study. + * + * @param studyId + * study id + * @param ctxType + * context type name + * @param ctxValue + * context value + * @throws InvalidPropertyException + * if an invalid value is passed to a property + * @throws MissedPropertyException + * if a mandatory property is missed + * @throws MultiplyDefinedException + * if some property is defined several times + */ + public void assignStudyContext(final Long studyId, final String ctxType, + final String ctxValue) throws MissedPropertyException, + InvalidPropertyException, MultiplyDefinedException; + /** * Create a new study. * @@ -62,7 +82,8 @@ public interface ScenarioService { */ long createStudy(final String username, final String title, final String productName, final String description) - throws InvalidPropertyException, MissedPropertyException, MultiplyDefinedException; + throws InvalidPropertyException, MissedPropertyException, + MultiplyDefinedException; /** * Create a new study with one scenario and "product" simulation context. @@ -223,10 +244,12 @@ public interface ScenarioService { * @return true if removal succeeded */ boolean removeKnowledgeElement(Scenario scenario, KnowledgeElement kelm); - + /** * Rename the scenario. - * @param scenario - the scenario with a new title. + * + * @param scenario - + * the scenario with a new title. */ void renameScenario(final Scenario scenario); } diff --git a/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java index 8ee294c..24d624d 100644 --- a/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java @@ -139,6 +139,11 @@ public class ScenarioServiceImpl implements ScenarioService { */ private SimulationContextService _simulationContextService; + /** + * Injected simulation context type service. + */ + private SimulationContextTypeService _simulationContextTypeService; + /** * Injected project service. */ @@ -361,20 +366,36 @@ public class ScenarioServiceImpl implements ScenarioService { return study; } + /** + * {@inheritDoc} + * @see org.splat.service.ScenarioService#assignStudyContext(java.lang.Long, java.lang.String, java.lang.String) + */ @Transactional - public void assignContext() throws MissedPropertyException, + public void assignStudyContext(final Long studyId, final String ctxType, + final String ctxValue) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException { - //TODO: complete the method - SimulationContext.Properties cprop = new SimulationContext.Properties(); - Long id = 0L; - Study study = getStudyDAO().get(id); - if (cprop.getIndex() == 0) { // Input of new project context - cprop.setType(getSimulationContextService().selectType("product")) - .setValue(cprop.getValue()); + // Find the study by the given id + Study study = getStudyDAO().get(studyId); + if (study == null) { + throw new InvalidPropertyException(MessageKeyEnum.STD_000002 + .toString(), studyId); + } + // Find the context type by its name + SimulationContextType celt = getSimulationContextService().selectType( + ctxType); + if (celt == null) { + // Creation of a new context type + celt = getSimulationContextTypeService().createType(ctxType, + getProjectElementService().getFirstStep(study).getStep()); + } + // Find the given context value of the given type + SimulationContext context = getSimulationContextService() + .selectSimulationContext(celt, ctxValue); + if (context == null) { // Input of a new project context + SimulationContext.Properties cprop = new SimulationContext.Properties(); + cprop.setType(celt).setValue(ctxValue); getStudyService().addProjectContext(study, cprop); } else { // Selection of existing project context - SimulationContext context = getSimulationContextService() - .selectSimulationContext(cprop.getIndex()); getStudyService().addProjectContext(study, context); } } @@ -1345,4 +1366,24 @@ public class ScenarioServiceImpl implements ScenarioService { _roleDAO = roleDAO; } + /** + * Get the simulationContextTypeService. + * + * @return the simulationContextTypeService + */ + public SimulationContextTypeService getSimulationContextTypeService() { + return _simulationContextTypeService; + } + + /** + * Set the simulationContextTypeService. + * + * @param simulationContextTypeService + * the simulationContextTypeService to set + */ + public void setSimulationContextTypeService( + final SimulationContextTypeService simulationContextTypeService) { + _simulationContextTypeService = simulationContextTypeService; + } + } diff --git a/Workspace/Siman-Common/src/org/splat/service/SimulationContextTypeServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/SimulationContextTypeServiceImpl.java index 9012aa9..99a1a43 100644 --- a/Workspace/Siman-Common/src/org/splat/service/SimulationContextTypeServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/SimulationContextTypeServiceImpl.java @@ -8,8 +8,9 @@ * @copyright OPEN CASCADE 2012 *****************************************************************************/ -package org.splat.service; +package org.splat.service; +import org.apache.commons.lang3.StringUtils; import org.hibernate.criterion.Restrictions; import org.splat.common.properties.MessageKeyEnum; import org.splat.dal.bo.som.ProgressState; @@ -22,7 +23,7 @@ import org.springframework.transaction.annotation.Transactional; /** * Simulation context type service implementation. - * + * * @author Roman Kozlov (RKV) */ public class SimulationContextTypeServiceImpl implements @@ -46,12 +47,20 @@ public class SimulationContextTypeServiceImpl implements */ @Transactional public SimulationContextType createType(final String name, - final ProjectSettingsService.Step step) throws InvalidPropertyException { - SimulationContextType type = getSimulationContextTypeDAO().findByCriteria(Restrictions.eq("name", name)); + final ProjectSettingsService.Step step) + throws InvalidPropertyException { + if (StringUtils.isBlank(name)) { + // Simulation context type name could not be blank + throw new InvalidPropertyException(MessageKeyEnum.SCT_000002 + .toString()); + } + SimulationContextType type = getSimulationContextTypeDAO() + .findByCriteria(Restrictions.eq("name", name)); if (type == null) { type = new SimulationContextType(name, step); getSimulationContextTypeDAO().create(type); } else { + // Simulation context type already exists LOG.info(MessageKeyEnum.SCT_000001.toString(), name); } diff --git a/Workspace/Siman-Common/src/spring/businessServiceContext.xml b/Workspace/Siman-Common/src/spring/businessServiceContext.xml index a08ebfa..65564d7 100644 --- a/Workspace/Siman-Common/src/spring/businessServiceContext.xml +++ b/Workspace/Siman-Common/src/spring/businessServiceContext.xml @@ -94,8 +94,10 @@ http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> - + + >>>> END testCreateStudyFromPython()"); } + + /** + * Test assigning a simulation context to a study.
+ * Description :
+ * Create a study and assign a simulation context to it.
+ * Action :
+ * 1. call the method for not existing study id.
+ * 2. call the method for not existing context type and context value.
+ * 3. call the method for existing context type and context value.
+ * 4. call the method for existing context type and not existing context value.
+ * 5. call the method for empty context type.
+ * 6. call the method for empty context value.
+ * Test data :
+ * no input parameters
+ * + * Outcome results:
+ * + *
    + *
  • 1: Exception must be thrown.
  • + *
  • 2: The new context type and value must be created. The new context must be assigned to the study first step.
  • + *
  • 3: The existing context must be assigned to the study first step.
  • + *
  • 4: The new context value must be created. The new context must be assigned to the study first step.
  • + *
  • 5: Exception must be thrown.
  • + *
  • 6: Exception must be thrown.
  • + *
+ *
+ * + * @throws IOException + * if application configuration loading is failed + * @throws SQLException + * if application configuration loading is failed + * @throws BusinessException + * if test data creation is failed + */ + @Test(groups = { "study", "sevice", "functional", "business" }) + public void testAssignStudyContextFromPython() throws IOException, + SQLException, BusinessException { + LOG.debug(">>>>> BEGIN testAssignStudyContextFromPython()"); + startNestedTransaction(); + + HibernateTemplate ht = getHibernateTemplate(); + + Database.getInstance().reset(); + _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again + _projectSettings.configure("classpath:test/som.xml"); + + // Create a test user + User goodUser = TestEntitiesGenerator.getTestUser("goodUser"); + _userDAO.create(goodUser); + SimulationContextType prodtype = _simulationContextService + .selectType("product"); + Assert + .assertNotNull(prodtype, + "Simulation context type 'product' must be created in the database."); + + String productName = "New Test Product " + new Date().toString(); + + ht.flush(); + ht.clear(); + long studyId1 = _scenarioService.createStudy("goodUser", + "Test Study 1", productName, "Test description"); + Assert.assertTrue(studyId1 > 0); + + ht.flush(); + ht.clear(); + + // //////// START OF TESTS + // 1. call the method for not existing study id.
+ try { + _scenarioService.assignStudyContext(-1L, "new context type", + "new context value"); + Assert.fail("Not existing study must not be found."); + } catch (InvalidPropertyException ipe) { + LOG.debug("Expected exception: " + ipe.getMessage()); + } + + // 2. call the method for not existing context type and context value.
+ _scenarioService.assignStudyContext(studyId1, "new context type", + "new context value"); + + ht.flush(); + ht.clear(); + + // Check the assigned simulation context + checkCtx(studyId1, "new context type", "new context value"); + + // 3. call the method for existing context type and context value.
+ _scenarioService.assignStudyContext(studyId1, "new context type", + "new context value"); + + ht.flush(); + ht.clear(); + + // Check the assigned simulation context + checkCtx(studyId1, "new context type", "new context value"); + + // 4. call the method for existing context type and not existing context value.
+ _scenarioService.assignStudyContext(studyId1, "new context type", + "new context value1"); + + ht.flush(); + ht.clear(); + + // Check the assigned simulation context + checkCtx(studyId1, "new context type", "new context value1"); + + // 5. call the method for empty context type.
+ try { + _scenarioService.assignStudyContext(studyId1, "", + "new context value"); + Assert.fail("Empty context type name must be forbidden."); + } catch (InvalidPropertyException ipe) { + LOG.debug("Expected exception: " + ipe.getMessage()); + } + // 6. call the method for empty context value.
+ try { + _scenarioService.assignStudyContext(studyId1, "new context type", + ""); + Assert.fail("Empty context value must be forbidden."); + } catch (InvalidPropertyException ipe) { + LOG.debug("Expected exception: " + ipe.getMessage()); + } + + rollbackNestedTransaction(); + LOG.debug(">>>>> END testAssignStudyContextFromPython()"); + } + + /** + * Check if the context is assigned to the study. + * + * @param studyId1 + * the study id + * @param ctxType + * the context type name + * @param ctxValue + * the context value + */ + private void checkCtx(final long studyId1, final String ctxType, + final String ctxValue) { + // Check the assigned simulation context + Study study1 = _studyService.selectStudy(studyId1); + Iterator it = study1.SimulationContextIterator(); + SimulationContext ctx; + boolean isFound = false; + while ((!isFound) && it.hasNext()) { + ctx = it.next(); + isFound = ctx.getType().getName().equals(ctxType) + && ctx.getValue().equals(ctxValue); + } + Assert.assertTrue(isFound, "Context must be assigned to the study."); + } } -- 2.39.2