Salome HOME
No need in merging user by cascade from ConributorRelation.
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / service / TestProjectSettingsService.java
index da005b31909a577e1a3231c89480ad41fa3c584e..5d9e769c0a65881e7e6cc27d71fa7782976a644e 100644 (file)
@@ -13,20 +13,27 @@ import java.io.IOException;
 import java.sql.SQLException;
 import java.util.List;
 
+import org.splat.dal.bo.som.KnowledgeElementType;
 import org.splat.dal.bo.som.Scenario;
+import org.splat.dal.bo.som.SimulationContextType;
+import org.splat.dal.dao.som.Database;
 import org.splat.log.AppLogger;
 import org.splat.service.DocumentTypeService;
+import org.splat.service.KnowledgeElementTypeService;
+import org.splat.service.SimulationContextService;
 import org.splat.service.technical.ProjectSettingsService;
+import org.splat.service.technical.StepsConfigService;
 import org.splat.service.technical.ProjectSettingsService.Step;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.dao.DuplicateKeyException;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
 import test.splat.common.BaseTest;
 
 /**
- * Test class for KnowledgeElementDAO.
+ * Test class for ProjectService.
  * 
  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
  * 
@@ -46,6 +53,13 @@ public class TestProjectSettingsService extends BaseTest {
        @Qualifier("projectSettings")
        private transient ProjectSettingsService _projectSettings;
 
+       /**
+        * The StepsConfigService. Later injected by Spring.
+        */
+       @Autowired
+       @Qualifier("stepsConfigService")
+       private transient StepsConfigService _stepsConfigService;
+
        /**
         * The DocumentTypeService. Later injected by Spring.
         */
@@ -53,6 +67,20 @@ public class TestProjectSettingsService extends BaseTest {
        @Qualifier("documentTypeService")
        private transient DocumentTypeService _documentTypeService;
 
+       /**
+        * The KnowledgeElementTypeService. Later injected by Spring.
+        */
+       @Autowired
+       @Qualifier("knowledgeElementTypeService")
+       private transient KnowledgeElementTypeService _knowledgeElementTypeService;
+
+       /**
+        * The DocumentTypeService. Later injected by Spring.
+        */
+       @Autowired
+       @Qualifier("simulationContextService")
+       private transient SimulationContextService _simulationContextService;
+       
        /**
         * Test of loading document mappings to file formats from customization XML file.<BR>
         * <B>Description :</B> <BR>
@@ -85,19 +113,19 @@ public class TestProjectSettingsService extends BaseTest {
         */
        @Test
        public void testLoadMappings() throws IOException, SQLException {
-               LOG.debug(">>>>> BEGIN testGetScenarioInfo()");
+               LOG.debug(">>>>> BEGIN testLoadMappings()()");
+               startNestedTransaction();
                // ////// Load good workflow customization
                /*
                 * geometry: brep model: med loads: c3m results: med
                 */
                _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
                try {
-                       _projectSettings.configure(ClassLoader.getSystemResource(
-                                       "test/som.xml").getPath());
+                       _projectSettings.configure("classpath:test/som.xml");
                } catch (FileNotFoundException e) {
                        Assert.fail("Can't find configuration file: ", e);
                }
-               List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
+               List<Step> steps = _stepsConfigService.getStepsOf(Scenario.class);
                Assert.assertTrue(steps.size() > 0, "No steps are created.");
                Assert.assertTrue(_projectSettings.doImport("geometry", "brep"));
                Assert.assertTrue(_projectSettings.doImport("model", "med"));
@@ -107,12 +135,11 @@ public class TestProjectSettingsService extends BaseTest {
                // ////// Load workflow customization with empty mappings
                _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
                try {
-                       _projectSettings.configure(ClassLoader.getSystemResource(
-                                       "test/som-without-mappings.xml").getPath());
+                       _projectSettings.configure("classpath:test/som-without-mappings.xml");
                } catch (FileNotFoundException e) {
                        Assert.fail("Can't find configuration file: ", e);
                }
-               steps = _projectSettings.getStepsOf(Scenario.class);
+               steps = _stepsConfigService.getStepsOf(Scenario.class);
                Assert.assertTrue(steps.size() > 0, "No steps are created.");
                Assert.assertFalse(_projectSettings.doImport("geometry", "brep"));
                Assert.assertFalse(_projectSettings.doImport("model", "med"));
@@ -122,16 +149,15 @@ public class TestProjectSettingsService extends BaseTest {
                // ////// Load workflow customization from not existing file
                _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
                try {
-                       _projectSettings.configure(ClassLoader.getSystemResource("/")
-                                       .getPath()
-                                       + "test/xxx.xml");
+                       _projectSettings.configure("classpath:test/xxx.xml");
                        Assert
                                        .fail("Customization loading must fail for not existing configuration file.");
                } catch (FileNotFoundException e) {
                        LOG.debug("Configuration file must not be found.", e);
                }
 
-               LOG.debug(">>>>> END testGetScenarioInfo()");
+               rollbackNestedTransaction();
+               LOG.debug(">>>>> END testLoadMappings()()");
        }
 
        /**
@@ -212,18 +238,21 @@ public class TestProjectSettingsService extends BaseTest {
        @Test
        public void testLoadDefaultDocTypes() throws IOException, SQLException {
                LOG.debug(">>>>> BEGIN testLoadDefaultDocTypes()");
+               startNestedTransaction();
                // ////// Load good workflow customization
                /*
                 * geometry: brep model: med loads: c3m results: med
                 */
+               Database.getInstance().reset();
                _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
                try {
-                       _projectSettings.configure(ClassLoader.getSystemResource(
-                                       "test/som.xml").getPath());
+                       _projectSettings.configure("classpath:test/som.xml");
                } catch (FileNotFoundException e) {
                        Assert.fail("Can't find configuration file: ", e);
                }
-               List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
+               Assert.assertTrue(_documentTypeService.selectAllTypes().size() > 0,
+                               "No document types are created.");
+               List<Step> steps = _stepsConfigService.getStepsOf(Scenario.class);
                Assert.assertTrue(steps.size() > 0, "No steps are created.");
 
                for (Step step : steps) {
@@ -258,11 +287,6 @@ public class TestProjectSettingsService extends BaseTest {
                                        Assert.assertEquals(defTypes.size(), 3);
                                        Assert.assertNull(_projectSettings.getDefaultDocumentType(
                                                        step, "pdf"));
-                                       Assert.assertNotNull(_projectSettings
-                                                       .getDefaultDocumentType(step, "xml"));
-                                       Assert.assertEquals(_projectSettings
-                                                       .getDefaultDocumentType(step, "xml").getName(),
-                                                       "design");
                                        Assert.assertNotNull(_projectSettings
                                                        .getDefaultDocumentType(step, "doc"));
                                        Assert.assertEquals(_projectSettings
@@ -273,9 +297,14 @@ public class TestProjectSettingsService extends BaseTest {
                                        Assert.assertEquals(_projectSettings
                                                        .getDefaultDocumentType(step, "docx").getName(),
                                                        "design");
+                                       Assert.assertNotNull(_projectSettings
+                                                       .getDefaultDocumentType(step, "xml"));
+                                       Assert.assertEquals(_projectSettings
+                                                       .getDefaultDocumentType(step, "xml").getName(),
+                                                       "design");
                                        break;
                                case 3:
-                                       Assert.assertEquals(defTypes.size(), 7);
+                                       Assert.assertEquals(defTypes.size(), 8);
                                        Assert.assertNull(_projectSettings.getDefaultDocumentType(
                                                        step, "pdf"));
                                        Assert.assertNotNull(_projectSettings
@@ -298,6 +327,11 @@ public class TestProjectSettingsService extends BaseTest {
                                        Assert.assertEquals(_projectSettings
                                                        .getDefaultDocumentType(step, "py").getName(),
                                                        "geometry");
+                                       Assert.assertNotNull(_projectSettings
+                                                       .getDefaultDocumentType(step, "brep"));
+                                       Assert.assertEquals(_projectSettings
+                                                       .getDefaultDocumentType(step, "brep").getName(),
+                                                       "geometry");
                                        Assert.assertNotNull(_projectSettings
                                                        .getDefaultDocumentType(step, "doc"));
                                        Assert.assertEquals(_projectSettings
@@ -370,12 +404,11 @@ public class TestProjectSettingsService extends BaseTest {
                // ////// Load workflow customization with empty mappings
                _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
                try {
-                       _projectSettings.configure(ClassLoader.getSystemResource(
-                                       "test/som-without-mappings.xml").getPath());
+                       _projectSettings.configure("classpath:test/som-without-mappings.xml");
                } catch (FileNotFoundException e) {
                        Assert.fail("Can't find configuration file: ", e);
                }
-               steps = _projectSettings.getStepsOf(Scenario.class);
+               steps = _stepsConfigService.getStepsOf(Scenario.class);
                Assert.assertTrue(steps.size() > 0, "No steps are created.");
                for (Step step : steps) {
                        List<String> defTypes = _projectSettings.getDefaultFormats(step);
@@ -405,15 +438,165 @@ public class TestProjectSettingsService extends BaseTest {
                // ////// Load workflow customization from not existing file
                _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
                try {
-                       _projectSettings.configure(ClassLoader.getSystemResource("/")
-                                       .getPath()
-                                       + "test/xxx.xml");
+                       _projectSettings.configure("classpath:test/xxx.xml");
                        Assert
                                        .fail("Customization loading must fail for not existing configuration file.");
                } catch (FileNotFoundException e) {
                        LOG.debug("Configuration file must not be found.", e);
                }
 
+               rollbackNestedTransaction();
                LOG.debug(">>>>> END testLoadDefaultDocTypes()");
        }
+
+       /**
+        * Test of repeated database configuration method (dynamic reconfiguration).<BR>
+        * The problem - duplication of the simer user admin role. <B>Description :</B> <BR>
+        * <i>Load customization twice and check the result.</i><BR>
+        * <B>Action : </B><BR>
+        * <i>1. call the method twice for som.xml</i><BR>
+        * <B>Test data : </B><BR>
+        * <i>test/som.xml</i><BR>
+        * 
+        * <B>Outcome results:</B><BR>
+        * <i>
+        * <ul>
+        * <li>step must be configured<BR>
+        * </li>
+        * </ul>
+        * </i>
+        * 
+        * @throws IOException
+        *             if configuration loading is failed
+        * @throws SQLException
+        *             if configuration loading is failed
+        */
+       @Test
+       public void testConfigure() throws IOException, SQLException {
+               LOG.debug(">>>>> BEGIN testConfigure()");
+               startNestedTransaction();
+               // ///////////////////////////////////////////////////
+               // ////// Load good workflow customization
+               getHibernateTemplate().clear();
+               _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
+               Database.getInstance().reset();
+               try {
+                       _projectSettings.configure("classpath:test/som.xml");
+               } catch (FileNotFoundException e) {
+                       Assert.fail("Can't find configuration file: ", e);
+               }
+
+               getHibernateTemplate().flush();
+               List<Step> steps = _stepsConfigService.getStepsOf(Scenario.class);
+               Assert.assertTrue(steps.size() > 0, "No steps are created.");
+               KnowledgeElementType ucase = _knowledgeElementTypeService.selectType("usecase");
+               Assert.assertNotNull(ucase, "Knowledge type 'usecase' must be created in the database.");
+               SimulationContextType prodtype = _simulationContextService.selectType("product");
+               Assert.assertNotNull(prodtype, "Simulation context type 'product' must be created in the database.");
+
+               // /////////////////////////////////////////////////////////
+               // ////// Test reconfiguration attempt
+               Database.getInstance().reset();
+               _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
+               try {
+                       _projectSettings.configure("classpath:test/som.xml");
+               } catch (FileNotFoundException e) {
+                       Assert.fail("Can't find configuration file: ", e);
+               }
+               steps = _stepsConfigService.getStepsOf(Scenario.class);
+               Assert.assertTrue(steps.size() > 0, "No steps are created.");
+               ucase = _knowledgeElementTypeService.selectType("usecase");
+               Assert.assertNotNull(ucase, "Knowledge type 'usecase' must be created in the database.");
+               prodtype = _simulationContextService.selectType("product");
+               Assert.assertNotNull(prodtype, "Simulation context type 'product' must be created in the database.");
+
+               try {
+                       /*
+                        * The next call to flush() must not throw the following exception: org.springframework.dao.DuplicateKeyException: a different
+                        * object with the same identifier value was already associated with the session: [org.splat.dal.bo.kernel.Role#simer]; nested
+                        * exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated
+                        * with the session: [org.splat.dal.bo.kernel.Role#simer] at
+                        * org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:662) at
+                        * org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at
+                        * org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) at
+                        * org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at
+                        * org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:881)
+                        */
+                       getHibernateTemplate().flush();
+               } catch (DuplicateKeyException dke) {
+                       Assert.fail(
+                                       "User creation failed during the database reconfiguration: "
+                                                       + dke.getMessage(), dke);
+               }
+
+               rollbackNestedTransaction();
+               LOG.debug(">>>>> END testConfigure()");
+       }
+
+       /**
+        * Test of repeated database configuration method (dynamic reconfiguration).<BR>
+        * The problem - duplication of the simer user admin role. <B>Description :</B> <BR>
+        * <i>Load customization twice and check the result.</i><BR>
+        * <B>Action : </B><BR>
+        * <i>1. call the method twice for som.xml</i><BR>
+        * <B>Test data : </B><BR>
+        * <i>test/som.xml</i><BR>
+        * 
+        * <B>Outcome results:</B><BR>
+        * <i>
+        * <ul>
+        * <li>step must be configured<BR>
+        * </li>
+        * </ul>
+        * </i>
+        * 
+        * @throws IOException
+        *             if configuration loading is failed
+        * @throws SQLException
+        *             if configuration loading is failed
+        */
+       @Test
+       public void testDatabaseInitialize() throws IOException, SQLException {
+               LOG.debug(">>>>> BEGIN testDatabaseInitialize()");
+               startNestedTransaction();
+               // ///////////////////////////////////////////////////
+               // ////// Load good workflow customization
+               getHibernateTemplate().bulkUpdate("delete from User");
+               getHibernateTemplate().bulkUpdate("delete from Role");
+               getHibernateTemplate().flush();
+               Database.getInstance().reset();
+               _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
+               try {
+                       _projectSettings.configure("classpath:test/som.xml");
+               } catch (FileNotFoundException e) {
+                       Assert.fail("Can't find configuration file: ", e);
+               }
+               try {
+                       /*
+                        * The next call to flush() must not throw the following exception: org.springframework.dao.DuplicateKeyException: a different
+                        * object with the same identifier value was already associated with the session: [org.splat.dal.bo.kernel.Role#simer]; nested
+                        * exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated
+                        * with the session: [org.splat.dal.bo.kernel.Role#simer] at
+                        * org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:662) at
+                        * org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at
+                        * org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) at
+                        * org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at
+                        * org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:881)
+                        */
+                       Database.getInstance().initialize();
+                       getHibernateTemplate().flush();
+               } catch (DuplicateKeyException dke) {
+                       Assert.fail(
+                                       "User creation failed during the database reconfiguration: "
+                                                       + dke.getMessage(), dke);
+               }
+
+               KnowledgeElementType ucase = _knowledgeElementTypeService.selectType("usecase");
+               Assert.assertNotNull(ucase, "Knowledge type 'usecase' must be created in the database.");
+               SimulationContextType prodtype = _simulationContextService.selectType("product");
+               Assert.assertNotNull(prodtype, "Simulation context type 'product' must be created in the database.");
+               
+               rollbackNestedTransaction();
+               LOG.debug(">>>>> END testDatabaseInitialize()");
+       }
 }