Salome HOME
The method createStudy from Python is marked as Transactional in ScenarioService.
authorrkv <rkv@opencascade.com>
Wed, 13 Mar 2013 08:02:09 +0000 (08:02 +0000)
committerrkv <rkv@opencascade.com>
Wed, 13 Mar 2013 08:02:09 +0000 (08:02 +0000)
Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java
Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java
Workspace/Siman-Common/src/test/splat/service/TestStudyService.java

index 85815f726033facc427668f4627166c7733c1ac4..35c29e13e70366fc039da10f7c26c8cc4bfa8008 100644 (file)
@@ -282,7 +282,7 @@ public class ScenarioServiceImpl implements ScenarioService {
         * 
         * @see org.splat.service.ScenarioService#createStudy(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
         */
-       @Override
+       @Transactional
        public long createStudy(final String username, final String title,
                        final String productName, final String description)
                        throws InvalidPropertyException, MissedPropertyException,
index e70d404366c3eb9b2d5c026501a76bfb077049ee..7ec5b8bc97f96b237ada7a627628d10548b60fba 100644 (file)
@@ -1047,7 +1047,7 @@ public class TestScenarioService extends BaseTest {
         * <ul>
         * <li>1: The new study must be created. The new product simulation context must be created.</li>
         * <li>2: The new study must be created.</li>
-        * <li>3: The new study must not be created. Exception is thrown.</li>
+        * <li>3: The new study must not be created. Exception must be thrown.</li>
         * </ul>
         * </i>
         * 
@@ -1134,7 +1134,7 @@ public class TestScenarioService extends BaseTest {
         * <ul>
         * <li>1: The new study must be created. The new product simulation context must be created.</li>
         * <li>2: The new study must be created.</li>
-        * <li>3: The new study must not be created. Exception is thrown.</li>
+        * <li>3: The new study must not be created. Exception must be thrown.</li>
         * </ul>
         * </i>
         * 
@@ -1151,6 +1151,8 @@ public class TestScenarioService extends BaseTest {
                LOG.debug(">>>>> BEGIN testCreateStudyFromPython()");
                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");
@@ -1165,10 +1167,15 @@ 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",
                                "Test Study 1", productName, "Test description");
                Assert.assertTrue(studyId1 > 0);
 
+               ht.flush();
+               ht.clear();
                try {
                        _scenarioService.createStudy("badbadUser", "Test Study 2",
                                        productName, "Test description");
@@ -1177,6 +1184,8 @@ public class TestScenarioService extends BaseTest {
                        LOG.debug("Expected exception: " + ipe.getMessage());
                }
 
+               ht.flush();
+               ht.clear();
                long studyId3 = _scenarioService.createStudy("goodUser",
                                "Test Study 3", productName, "Test description");
                Assert.assertTrue(studyId3 > 0);
index b54b62cbdb05fdb0a259643e09cf7a49606cd63c..2c2f93a249b26331bf6061770954822acfba1359 100644 (file)
@@ -17,17 +17,29 @@ import java.util.List;
 import java.util.Map;
 
 import org.splat.dal.bo.kernel.User;
+import org.splat.dal.bo.som.ContributorRelation;
 import org.splat.dal.bo.som.DescriptionAttribute;
 import org.splat.dal.bo.som.Document;
 import org.splat.dal.bo.som.DocumentType;
+import org.splat.dal.bo.som.KnowledgeElement;
+import org.splat.dal.bo.som.KnowledgeElementType;
+import org.splat.dal.bo.som.ProgressState;
 import org.splat.dal.bo.som.ProjectElement;
 import org.splat.dal.bo.som.Publication;
 import org.splat.dal.bo.som.Scenario;
+import org.splat.dal.bo.som.SimulationContext;
 import org.splat.dal.bo.som.Study;
+import org.splat.dal.bo.som.ValidationCycle;
+import org.splat.dal.bo.som.ValidationCycleRelation;
+import org.splat.dal.bo.som.ValidationStep;
 import org.splat.dal.bo.som.Document.Properties;
 import org.splat.dal.dao.kernel.UserDAO;
 import org.splat.dal.dao.som.Database;
+import org.splat.dal.dao.som.KnowledgeElementDAO;
+import org.splat.dal.dao.som.KnowledgeElementTypeDAO;
+import org.splat.dal.dao.som.ScenarioDAO;
 import org.splat.dal.dao.som.StudyDAO;
+import org.splat.dal.dao.som.ValidationCycleDAO;
 import org.splat.exception.BusinessException;
 import org.splat.exception.InvalidParameterException;
 import org.splat.kernel.InvalidPropertyException;
@@ -36,6 +48,7 @@ import org.splat.kernel.MultiplyDefinedException;
 import org.splat.log.AppLogger;
 import org.splat.service.DocumentTypeService;
 import org.splat.service.PublicationService;
+import org.splat.service.SimulationContextService;
 import org.splat.service.StepService;
 import org.splat.service.StudyService;
 import org.splat.service.technical.ProjectSettingsService;
@@ -104,7 +117,7 @@ public class TestStudyService extends BaseTest {
        @Autowired
        @Qualifier("studyService")
        private transient StudyService _studyService;
-       
+
        /**
         * The UserDAO. Later injected by Spring.
         */
@@ -112,6 +125,41 @@ public class TestStudyService extends BaseTest {
        @Qualifier("userDAO")
        private transient UserDAO _userDAO;
 
+       /**
+        * The KnowledgeElementDAO. Later injected by Spring.
+        */
+       @Autowired
+       @Qualifier("knowledgeElementDAO")
+       private transient KnowledgeElementDAO _knowledgeElementDAO;
+
+       /**
+        * The KnowledgeElementTypeDAO. Later injected by Spring.
+        */
+       @Autowired
+       @Qualifier("knowledgeElementTypeDAO")
+       private transient KnowledgeElementTypeDAO _knowledgeElementTypeDAO;
+
+       /**
+        * The ScenarioDAO. Later injected by Spring.
+        */
+       @Autowired
+       @Qualifier("scenarioDAO")
+       private transient ScenarioDAO _scenarioDAO;
+
+       /**
+        * The SimulationContextService. Later injected by Spring.
+        */
+       @Autowired
+       @Qualifier("simulationContextService")
+       private transient SimulationContextService _simulationContextService;
+
+       /**
+        * The ValidationCycleDAO. Later injected by Spring.
+        */
+       @Autowired
+       @Qualifier("validationCycleDAO")
+       private transient ValidationCycleDAO _validationCycleDAO;
+
        /**
         * Create a persistent scenario for tests.
         * 
@@ -400,7 +448,7 @@ public class TestStudyService extends BaseTest {
                Assert.assertEquals(ind, ht.get(Study.class, aStudyFound.getIndex())
                                .getLastLocalIndex(),
                                "Incremented index must be saved in the database.");
-               aStudy = (Study)ht.find(
+               aStudy = (Study) ht.find(
                                "from Study where rid = " + aStudyFound.getIndex()).get(0);
                Assert.assertEquals(ind, aStudy.getLastLocalIndex(),
                                "Incremented index must be saved in the database.");
@@ -408,44 +456,46 @@ public class TestStudyService extends BaseTest {
                rollbackNestedTransaction();
                LOG.debug(">>>>> END testGenerateLocalIndex()");
        }
-       
+
        /**
         * Test of retrieval of a study description.
-        *
-        * @throws BusinessException if there is something wrong likely unrelated to the tested method
+        * 
+        * @throws BusinessException
+        *             if there is something wrong likely unrelated to the tested method
         */
        @Test
        public void testGetDescription() throws BusinessException {
                LOG.debug(">>>>> BEGIN testGetDescription()");
                startNestedTransaction();
-               
+
                User user = TestEntitiesGenerator.getTestUser("GoodUser");
                _userDAO.create(user);
                Study study = TestEntitiesGenerator.getTestStudy(user);
                _studyDAO.create(study);
                _studyDAO.flush();
                Long studyId = Long.valueOf(study.getIndex());
-               
-               //Empty description:
-               Assert.assertNull(_studyService.getDescription(studyId), 
+
+               // Empty description:
+               Assert.assertNull(_studyService.getDescription(studyId),
                                "returned value for study without description must be null");
-               
-               //Not empty description:
+
+               // Not empty description:
                study.setAttribute(new DescriptionAttribute(study, "description"));
                _studyDAO.update(study);
                _studyDAO.flush();
-               Assert.assertEquals("description", _studyService.getDescription(studyId));
-               
-               //null id
+               Assert.assertEquals("description", _studyService
+                               .getDescription(studyId));
+
+               // null id
                try {
                        _studyService.getDescription(null);
                        Assert.fail("retrieval with null study id must fail");
-               } catch(InvalidParameterException e){
+               } catch (InvalidParameterException e) {
                        LOG.debug("Expected exception is thrown: "
-                               + e.getClass().getSimpleName() + ": " + e.getMessage());
+                                       + e.getClass().getSimpleName() + ": " + e.getMessage());
                }
-               
-               //Non-existing id
+
+               // Non-existing id
                Study tmpStudy = TestEntitiesGenerator.getTestStudy(user);
                _studyDAO.create(tmpStudy);
                Long nonExistingId = tmpStudy.getIndex();
@@ -453,105 +503,108 @@ public class TestStudyService extends BaseTest {
                try {
                        _studyService.getDescription(nonExistingId);
                        Assert.fail("retrieval with non-existing study id must fail");
-               } catch(InvalidParameterException e){
+               } catch (InvalidParameterException e) {
                        LOG.debug("Expected exception is thrown: "
-                               + e.getClass().getSimpleName() + ": " + e.getMessage());
+                                       + e.getClass().getSimpleName() + ": " + e.getMessage());
                }
 
                rollbackNestedTransaction();
                LOG.debug(">>>>> END testGetDescription()");
        }
-       
+
        /**
         * Test of setting of a study description.
-        *
-        * @throws BusinessException if there is something wrong likely unrelated to the tested method
+        * 
+        * @throws BusinessException
+        *             if there is something wrong likely unrelated to the tested method
         */
        @Test
        public void testSetDescription() throws BusinessException {
                LOG.debug(">>>>> BEGIN testSetDescription()");
                startNestedTransaction();
-               
+
                User user = TestEntitiesGenerator.getTestUser("GoodUser");
                _userDAO.create(user);
                Study study = TestEntitiesGenerator.getTestStudy(user);
                _studyDAO.create(study);
                _studyDAO.flush();
                Long studyId = Long.valueOf(study.getIndex());
-               
-               //Setting description for study without any
+
+               // Setting description for study without any
                _studyService.setDescription(studyId, "description");
 
-               //Resetting description
+               // Resetting description
                _studyService.setDescription(studyId, "replaced description");
-               
-               //null id
+
+               // null id
                try {
                        _studyService.setDescription(null, "description");
                        Assert.fail("setting with null study id must fail");
-               } catch(InvalidParameterException e){
+               } catch (InvalidParameterException e) {
                        LOG.debug("Expected exception is thrown: "
-                               + e.getClass().getSimpleName() + ": " + e.getMessage());
+                                       + e.getClass().getSimpleName() + ": " + e.getMessage());
                }
-               
-               //Non-existing id
+
+               // Non-existing id
                Study tmpStudy = TestEntitiesGenerator.getTestStudy(user);
                _studyDAO.create(tmpStudy);
                Long nonExistingId = tmpStudy.getIndex();
                _studyDAO.delete(tmpStudy);
-               
+
                try {
                        _studyService.setDescription(nonExistingId, "description");
                        Assert.fail("retrieval with non-existing study id must fail");
-               } catch(InvalidParameterException e){
+               } catch (InvalidParameterException e) {
                        LOG.debug("Expected exception is thrown: "
-                               + e.getClass().getSimpleName() + ": " + e.getMessage());
+                                       + e.getClass().getSimpleName() + ": " + e.getMessage());
                }
 
                rollbackNestedTransaction();
                LOG.debug(">>>>> END testSetDescription()");
        }
-       
+
        /**
         * Test of removal of a study description.
-        *
-        * @throws BusinessException if there is something wrong likely unrelated to the tested method
+        * 
+        * @throws BusinessException
+        *             if there is something wrong likely unrelated to the tested method
         */
        @Test
        public void testRemoveDescription() throws BusinessException {
                LOG.debug(">>>>> BEGIN testRemoveDescription()");
                startNestedTransaction();
-               
+
                User user = TestEntitiesGenerator.getTestUser("GoodUser");
                _userDAO.create(user);
                Study study = TestEntitiesGenerator.getTestStudy(user);
                _studyDAO.create(study);
                _studyDAO.flush();
                Long studyId = Long.valueOf(study.getIndex());
-               
-               //Empty description:
-               Assert.assertFalse(_studyService.removeDescription(studyId), 
+
+               // Empty description:
+               Assert.assertFalse(_studyService.removeDescription(studyId),
                                "returned value for study without description must be null");
-               
-               //Not empty description:
+
+               // Not empty description:
                study.setAttribute(new DescriptionAttribute(study, "description"));
                _studyDAO.update(study);
                _studyDAO.flush();
-               Assert.assertTrue(_studyService.removeDescription(studyId), "existing description removal must return true");
-               
-               
-               Assert.assertNull(_studyService.getDescription(studyId), "description hasn't been successfully removed");
-               
-               //null id
+               Assert.assertTrue(_studyService.removeDescription(studyId),
+                               "existing description removal must return true");
+
+               Assert.assertNull(_studyService.getDescription(studyId),
+                               "description hasn't been successfully removed");
+
+               // null id
                try {
                        _studyService.removeDescription(null);
                        Assert.fail("removal with null study id must fail");
-               } catch(InvalidParameterException e){
+               } catch (InvalidParameterException e) {
                        LOG.debug("Expected exception is thrown: "
-                               + e.getClass().getSimpleName() + ": " + e.getMessage());
+                                       + e.getClass().getSimpleName() + ": " + e.getMessage());
                }
-               
-               //Non-existing id
+
+               // Non-existing id
                Study tmpStudy = TestEntitiesGenerator.getTestStudy(user);
                _studyDAO.create(tmpStudy);
                Long nonExistingId = tmpStudy.getIndex();
@@ -559,12 +612,149 @@ public class TestStudyService extends BaseTest {
                try {
                        _studyService.removeDescription(nonExistingId);
                        Assert.fail("removal with non-existing study id must fail");
-               } catch(InvalidParameterException e){
+               } catch (InvalidParameterException e) {
                        LOG.debug("Expected exception is thrown: "
-                               + e.getClass().getSimpleName() + ": " + e.getMessage());
+                                       + e.getClass().getSimpleName() + ": " + e.getMessage());
                }
 
                rollbackNestedTransaction();
                LOG.debug(">>>>> END testGetDescription()");
        }
+
+       /**
+        * Test study removal.<BR>
+        * <B>Description :</B> <BR>
+        * <i>Delete a study.</i><BR>
+        * <B>Action : </B><BR>
+        * <i>1. call the method for a not existing study id expecting an exception.</i><BR>
+        * <i>2. call the method for an existing study id.</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 study and all its objects must be removed.</li>
+        * </ul>
+        * </i>
+        * 
+        * @throws BusinessException
+        *             if test data creation is failed
+        */
+       @Test
+       public void testRemoveStudy() throws BusinessException {
+               LOG.debug(">>>>> BEGIN testRemoveStudy()");
+               startNestedTransaction();
+
+               HibernateTemplate ht = getHibernateTemplate();
+
+               Database.getInstance().reset();
+               _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
+               // Load workflow customization
+               try {
+                       _projectSettings.configure("classpath:test/som.xml");
+               } catch (Exception e) {
+                       Assert.fail("Can't load som.xml: ", e);
+               }
+
+               User goodUser = TestEntitiesGenerator.getTestUser("GoodUser");
+               _userDAO.create(goodUser);
+               User otherUser = TestEntitiesGenerator.getTestUser("otherUser");
+               _userDAO.create(otherUser);
+               User thirdUser = TestEntitiesGenerator.getTestUser("thirdUser");
+               _userDAO.create(thirdUser);
+               KnowledgeElementType ktype1 = new KnowledgeElementType("testKType1");
+               KnowledgeElementType ktype2 = new KnowledgeElementType("testKType2");
+               ktype2.setState(ProgressState.inWORK);
+               _knowledgeElementTypeDAO.create(ktype1);
+               _knowledgeElementTypeDAO.create(ktype2);
+
+               // Create private study
+               Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
+               aStudy.setTitle("0.This is private study");
+               Long studyId = _studyDAO.create(aStudy);
+
+               // Add simulation context to the study
+               SimulationContext.Properties cprop = new SimulationContext.Properties();
+               cprop.setType(_simulationContextService.selectType("product"))
+                               .setValue("Test Simulation Context: Product");
+               SimulationContext ctx = _studyService.addProjectContext(aStudy, cprop);
+               ht.flush();
+
+               // Add a scenario to the study
+               Scenario scen = TestEntitiesGenerator.getTestScenario(aStudy);
+               _scenarioDAO.create(scen);
+               ht.flush();
+
+               // Add knowledge elements to the scenario
+               KnowledgeElement kelm01 = TestEntitiesGenerator
+                               .getTestKnowledgeElement(scen, ktype1, "TestKelm01 title");
+               KnowledgeElement kelm02 = TestEntitiesGenerator
+                               .getTestKnowledgeElement(scen, ktype2, "TestKelm02 title");
+               _knowledgeElementDAO.create(kelm01);
+               _knowledgeElementDAO.create(kelm02);
+               ht.flush();
+
+               // Add contributor relation
+               aStudy.addRelation(new ContributorRelation(aStudy, otherUser));
+               ht.flush();
+
+               // Add a validation cycle with otherUser as a reviewer
+               ValidationCycle.Properties vprop = new ValidationCycle.Properties();
+               DocumentType type = _documentTypeService.selectType("specification");
+               vprop.setDocumentType(type);
+               vprop.setActor(ValidationStep.REVIEW, otherUser);
+               ValidationCycle cycle = new ValidationCycle(aStudy, vprop);
+               _validationCycleDAO.create(cycle);
+               ValidationCycleRelation link = cycle.getContext();
+               aStudy.addRelation(link);
+               ht.flush();
+
+               // Add documents to the first study activity
+               // Add a version relation
+               // Add a converts relation
+               // Add documents to the first scenario activity
+               // Add uses relations
+
+               ht.clear();
+               _studyService.removeStudy(studyId);
+               ht.flush();
+
+               // Check removal of the study
+               Study foundStudy = ht.get(Study.class, studyId);
+               Assert.assertNull(foundStudy);
+               // Check removal of relations to validation cycle
+               List found = ht.find("from ValidationCycleRelation where owner="
+                               + studyId);
+               Assert.assertEquals(found.size(), 0,
+                               "ValidationCycleRelation is not removed");
+               // Check removal of validation cycles
+               found = ht.find("from ValidationCycle where rid=" + cycle.getIndex());
+               Assert.assertEquals(found.size(), 0, "ValidationCycle is not removed");
+               // Check removal of contributors relation
+               found = ht.find("from ContributorRelation where owner=" + studyId);
+               Assert.assertEquals(found.size(), 0,
+                               "ContributorRelation is not removed");
+               // Check that contributors are not removed
+               User foundUser = ht.get(User.class, otherUser.getIndex());
+               Assert.assertNotNull(foundUser);
+               // Check removal of the scenario
+               found = ht.find("from Scenario where owner=" + studyId);
+               Assert.assertEquals(found.size(), 0, "Scenario is not removed");
+               // Check removal of publications
+               found = ht
+                               .find("from Publication pub left join pub.owner as own where own.rid="
+                                               + studyId + " or own.rid=" + scen.getIndex());
+               Assert.assertEquals(found.size(), 0, "Publication is not removed");
+               // Check removal of documents
+               // Check removal of version relations
+               // Check removal of converts relations
+               // Check removal of files
+               // Check removal of uses relations
+               // Check removal of usedBy relations
+
+               rollbackNestedTransaction();
+               LOG.debug(">>>>> END testRemoveStudy()");
+       }
 }