]> SALOME platform Git repositories - tools/siman.git/commitdiff
Salome HOME
assignStudyContext is implemented for calling from Python. Unit tests are added.
authorrkv <rkv@opencascade.com>
Wed, 13 Mar 2013 12:14:20 +0000 (12:14 +0000)
committerrkv <rkv@opencascade.com>
Wed, 13 Mar 2013 12:14:20 +0000 (12:14 +0000)
Workspace/Siman-Common/src/conf/log-messages.properties
Workspace/Siman-Common/src/conf/log-messages_en.properties
Workspace/Siman-Common/src/org/splat/common/properties/MessageKeyEnum.java
Workspace/Siman-Common/src/org/splat/service/ScenarioService.java
Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java
Workspace/Siman-Common/src/org/splat/service/SimulationContextTypeServiceImpl.java
Workspace/Siman-Common/src/spring/businessServiceContext.xml
Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java

index 5ee7cdbc88c4b90163f1ef7e9381cbc26226c8ce..17eee64f15915c0cfdae5f620b1f8d668aa26bdc 100644 (file)
@@ -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.
index 5ee7cdbc88c4b90163f1ef7e9381cbc26226c8ce..17eee64f15915c0cfdae5f620b1f8d668aa26bdc 100644 (file)
@@ -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.
index 1b887474915dc56a4ffa917b4c11c5ac912eeadf..5faf78f48a67367ecfc124f0a8fd58e11ca9facf 100644 (file)
@@ -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.
         */
index e00d67869d2f6ff142cbf0aeed2449b513a1f40e..58a1bbb6ff4ca048025fbcb112d4f0a6a27a0a6c 100644 (file)
@@ -41,6 +41,26 @@ public interface ScenarioService {
         */
        List<StepDTO> 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);
 }
index 8ee294c347ebc8b99a7dbb461b68f4e8d63e30c4..24d624d77b139b7d1fed080e7ce5adfbfd96485f 100644 (file)
@@ -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;
+       }
+
 }
index 9012aa9a68cc62595d4aae12c1c348975b49ac5f..99a1a4362d5391517db4ce95225468fcff772d88 100644 (file)
@@ -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 <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
  */
 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);
                }
 
index a08ebfae6737d530ac02b9dbf02513fac1bba519..65564d760b3543d050d05c382a5339cf633063ae 100644 (file)
@@ -94,8 +94,10 @@ http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
         <property name="roleDAO" ref="roleDAO" />
                <property name="knowledgeElementTypeDAO"
                        ref="knowledgeElementTypeDAO" />
-               <property name="simulationContextService"
-                       ref="simulationContextService" />
+        <property name="simulationContextService"
+            ref="simulationContextService" />
+        <property name="simulationContextTypeService"
+            ref="simulationContextTypeService" />
        </bean>
 
        <bean id="searchService"
index 819a9259551e2b838512a1a148f5ffe7bbf42dcf..607e62dd123aab33bb37550c42461917874df2fa 100644 (file)
@@ -16,6 +16,7 @@ import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -1168,7 +1169,7 @@ public class TestScenarioService extends BaseTest {
                                                "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",
@@ -1194,15 +1195,168 @@ public class TestScenarioService extends BaseTest {
                // Check that the simulation context is the same
                Study study1 = _studyService.selectStudy(studyId1);
                Study study3 = _studyService.selectStudy(studyId3);
-               Assert.assertEquals(study1.SimulationContextIterator().next(),
-                               study3.SimulationContextIterator().next());
-               
+               Assert.assertEquals(study1.SimulationContextIterator().next(), study3
+                               .SimulationContextIterator().next());
+
                // Check the title of the created scenario
                String scTitle = study1.getScenarii()[0].getTitle();
-               Assert.assertEquals(scTitle, I18nUtils.getMessageLocaleDefault("label.scenario") + " 1");
+               Assert.assertEquals(scTitle, I18nUtils
+                               .getMessageLocaleDefault("label.scenario")
+                               + " 1");
                Assert.assertFalse(scTitle.equals("label.scenario 1"));
-               
+
                rollbackNestedTransaction();
                LOG.debug(">>>>> END testCreateStudyFromPython()");
        }
+
+       /**
+        * Test assigning a simulation context to a study.<BR>
+        * <B>Description :</B> <BR>
+        * <i>Create a study and assign a simulation context to it.</i><BR>
+        * <B>Action : </B><BR>
+        * <i>1. call the method for not existing study id.</i><BR>
+        * <i>2. call the method for not existing context type and context value.</i><BR>
+        * <i>3. call the method for existing context type and context value.</i><BR>
+        * <i>4. call the method for existing context type and not existing context value.</i><BR>
+        * <i>5. call the method for empty context type.</i><BR>
+        * <i>6. call the method for empty context value.</i><BR>
+        * <B>Test data : </B><BR>
+        * <i>no input parameters</i><BR>
+        * 
+        * <B>Outcome results:</B><BR>
+        * <i>
+        * <ul>
+        * <li>1: Exception must be thrown.</li>
+        * <li>2: The new context type and value must be created. The new context must be assigned to the study first step.</li>
+        * <li>3: The existing context must be assigned to the study first step.</li>
+        * <li>4: The new context value must be created. The new context must be assigned to the study first step.</li>
+        * <li>5: Exception must be thrown.</li>
+        * <li>6: Exception must be thrown.</li>
+        * </ul>
+        * </i>
+        * 
+        * @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.</i><BR>
+               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.</i><BR>
+               _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.</i><BR>
+               _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.</i><BR>
+               _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.</i><BR>
+               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.</i><BR>
+               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<SimulationContext> 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.");
+       }
 }