Salome HOME
Search knowledge is implemented. Unit tests are improved. Lucene index is not used...
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / service / TestSearchService.java
index 2c2f85af1c521408ec8a76b972245d95b9ae9555..b8b03fec8d1be5fcb9321ac76da9556c813ccb44 100644 (file)
@@ -9,12 +9,17 @@
 package test.splat.service;
 
 import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 
 import org.splat.dal.bo.kernel.User;
 import org.splat.dal.bo.som.ContributorRelation;
 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.Scenario;
 import org.splat.dal.bo.som.SimulationContext;
 import org.splat.dal.bo.som.Study;
 import org.splat.dal.bo.som.ValidationCycle;
@@ -23,6 +28,9 @@ import org.splat.dal.bo.som.ValidationStep;
 import org.splat.dal.bo.som.Visibility;
 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;
@@ -33,6 +41,7 @@ import org.splat.service.SearchService;
 import org.splat.service.SimulationContextService;
 import org.splat.service.StepService;
 import org.splat.service.StudyService;
+import org.splat.service.dto.KnowledgeSearchFilterDTO;
 import org.splat.service.dto.Proxy;
 import org.splat.service.dto.StudySearchFilterDTO;
 import org.splat.service.technical.ProjectSettingsService;
@@ -66,6 +75,27 @@ public class TestSearchService extends BaseTest {
        @Qualifier("studyDAO")
        private transient StudyDAO _studyDAO;
 
+       /**
+        * The ScenarioDAO. Later injected by Spring.
+        */
+       @Autowired
+       @Qualifier("scenarioDAO")
+       private transient ScenarioDAO _scenarioDAO;
+
+       /**
+        * 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 PublicationService. Later injected by Spring.
         */
@@ -161,10 +191,13 @@ public class TestSearchService extends BaseTest {
         */
        @Test
        public void testSelectStudiesWhere() throws BusinessException {
-               LOG.debug(">>>>> BEGIN testGenerateLocalIndex()");
+               LOG.debug(">>>>> BEGIN testSelectStudiesWhere()");
                startNestedTransaction();
 
                HibernateTemplate ht = getHibernateTemplate();
+               Calendar calend = java.util.Calendar.getInstance();
+               calend.roll(Calendar.DATE, false);
+               Date beforeCreation = calend.getTime();
 
                Database.getInstance().reset();
                _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
@@ -183,6 +216,7 @@ public class TestSearchService extends BaseTest {
                User thirdUser = TestEntitiesGenerator.getTestUser("thirdUser");
                _userDAO.create(thirdUser);
 
+               // ============== STUDY 0: Private : goodUser
                // Create private study
                Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
                aStudy.setTitle("0.This is private study");
@@ -195,6 +229,7 @@ public class TestSearchService extends BaseTest {
                SimulationContext ctx = _studyService.addProjectContext(aStudy, cprop);
                ht.flush();
 
+               // ============== STUDY 1: Public : goodUser : otherUser is contributor
                // Create public study with predefined title
                aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
                aStudy.setVisibility(Visibility.PUBLIC);
@@ -202,13 +237,13 @@ public class TestSearchService extends BaseTest {
                aStudy.addRelation(new ContributorRelation(aStudy, otherUser));
                Long titleStudyId = _studyDAO.create(aStudy);
 
-               // Add simulation context to the study 2
-               SimulationContext.Properties cprop1 = new SimulationContext.Properties();
+               // Add simulation context to the study 1
                cprop.setType(_simulationContextService.selectType("product"))
                                .setValue("Test Simulation Context: Product1");
-               SimulationContext ctx1 = _studyService.addProjectContext(aStudy, cprop);
+               _studyService.addProjectContext(aStudy, cprop);
                ht.flush();
 
+               // ============== STUDY 2: Public : otherUser
                // Create public study with predefined reference
                aStudy = TestEntitiesGenerator.getTestStudy(otherUser);
                aStudy.setVisibility(Visibility.PUBLIC);
@@ -216,6 +251,7 @@ public class TestSearchService extends BaseTest {
                aStudy.setReference("TEST_REF");
                Long refStudyId = _studyDAO.create(aStudy);
 
+               // ============== STUDY 3: Private : thirdUser : otherUser is contributor
                // Create private study of third user with otherUser as a reviewer
                aStudy = TestEntitiesGenerator.getTestStudy(thirdUser);
                aStudy.setTitle("3.This is a private study of third user");
@@ -231,7 +267,11 @@ public class TestSearchService extends BaseTest {
                ValidationCycleRelation link = cycle.getContext();
                aStudy.addRelation(link);
                ht.flush();
+               calend = java.util.Calendar.getInstance();
+               calend.roll(Calendar.DATE, true);
+               Date afterCreation = calend.getTime();
 
+               // ============== START OF TEST CALLS
                // Search by study author
                StudySearchFilterDTO filter = new StudySearchFilterDTO();
                filter.setAuthor(String.valueOf(goodUser.getIndex()));
@@ -257,8 +297,8 @@ public class TestSearchService extends BaseTest {
                Assert.assertNotNull(res);
                Assert.assertEquals(res.size(), 3);
                Assert.assertEquals(res.get(0).getIndex(), privateStudyId); // goodUser is author
-               Assert.assertEquals(res.get(2).getIndex(), titleStudyId); // Public study
                Assert.assertEquals(res.get(1).getIndex(), refStudyId); // Public study
+               Assert.assertEquals(res.get(2).getIndex(), titleStudyId); // Public study
 
                // Search by study title contents
                filter = new StudySearchFilterDTO();
@@ -288,16 +328,307 @@ public class TestSearchService extends BaseTest {
                Assert.assertEquals(res.size(), 1);
                Assert.assertEquals(res.get(0).getIndex(), privateStudyId);
                // Check simualtion contexts of the found study
-               aStudy = _studyService.selectStudy(res.get(0).getIndex());
-               Iterator<SimulationContext> ctxIter = aStudy.SimulationContextIterator();
+               for (Proxy proxy: res) {
+               aStudy = _studyService.selectStudy(proxy.getIndex());
+               Iterator<SimulationContext> ctxIter = aStudy
+                               .SimulationContextIterator();
                contexts.clear();
                while (ctxIter.hasNext()) {
                        contexts.add(ctxIter.next());
                }
                Assert.assertEquals(contexts.size(), 1);
                Assert.assertEquals(contexts.get(0), ctx);
+               }
 
+               // Search for dates
+               filter = new StudySearchFilterDTO();
+               filter.setCreatedAfter(beforeCreation);
+               filter.setCreatedBefore(afterCreation);
+               res = _searchService.selectStudiesWhere(filter);
+               Assert.assertNotNull(res);
+               Assert.assertEquals(res.size(), 2);
+               Assert.assertEquals(res.get(0).getIndex(), refStudyId);
+               Assert.assertEquals(res.get(1).getIndex(), titleStudyId);
+               
+               // Search for dates with no result
+               filter = new StudySearchFilterDTO();
+               filter.setCreatedAfter(afterCreation);
+               res = _searchService.selectStudiesWhere(filter);
+               Assert.assertNotNull(res);
+               Assert.assertEquals(res.size(), 0);
+               
+               rollbackNestedTransaction();
+               LOG.debug(">>>>> END testSelectStudiesWhere()");
+       }
+
+       /**
+        * Test of searching a study.<BR>
+        * <B>Description :</B> <BR>
+        * <i>Create studies and try to find them according next document index.</i><BR>
+        * <B>Action : </B><BR>
+        * <i>
+        * <ol>
+        * <li>Search study by state</li>
+        * <li>Search study by reference</li>
+        * <li>Search study by author</li>
+        * <li>Search study by actor</li>
+        * <li>Search study by title</li>
+        * <li>Search study by simulation contexts</li>
+        * <li>Search study by all criteria</li>
+        * <li>Search study by any criteria</li>
+        * </ol>
+        * </i> <B>Test data : </B><BR>
+        * <i>no input parameters</i><BR>
+        * 
+        * <B>Outcome results:</B><BR>
+        * <i>
+        * <ul>
+        * <li>A study must be found according to the given criteria<BR>
+        * </li>
+        * </ul>
+        * </i>
+        * 
+        * @throws BusinessException
+        *             if can't create test user or study
+        */
+       @Test
+       public void testSelectKnowledgeElementsWhere() throws BusinessException {
+               LOG.debug(">>>>> BEGIN testSelectKnowledgeElementsWhere()");
+               startNestedTransaction();
+
+               HibernateTemplate ht = getHibernateTemplate();
+               
+               Calendar calend = java.util.Calendar.getInstance();
+               calend.roll(Calendar.DATE, false);
+               Date beforeCreation = calend.getTime();
+
+               Database.getInstance().reset();
+               _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
+               // Load workflow customization
+               try {
+                       _projectSettings.configure(ClassLoader.getSystemResource(
+                                       "test/som.xml").getPath());
+               } 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");
+               _knowledgeElementTypeDAO.create(ktype1);
+               _knowledgeElementTypeDAO.create(ktype2);
+
+               // ============== STUDY 0: Private : goodUser
+               // Create private study
+               Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
+               aStudy.setTitle("0.This is private study");
+               Long privateStudyId = _studyDAO.create(aStudy);
+
+               // Add simulation context to the study 0
+               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 0
+               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();
+
+               // ============== STUDY 1: Public : goodUser : otherUser is contributor
+               // Create public study with predefined title
+               aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
+               aStudy.setVisibility(Visibility.PUBLIC);
+               aStudy.setTitle("2.This is a public study");
+               aStudy.addRelation(new ContributorRelation(aStudy, otherUser));
+               Long titleStudyId = _studyDAO.create(aStudy);
+
+               // Add a scenario to the study 1
+               scen = TestEntitiesGenerator.getTestScenario(aStudy);
+               _scenarioDAO.create(scen);
+               ht.flush();
+
+               // Add knowledge elements to the scenario
+               KnowledgeElement kelm11 = TestEntitiesGenerator
+                               .getTestKnowledgeElement(scen, ktype1, "TestKelm11 title");
+               KnowledgeElement kelm12 = TestEntitiesGenerator
+                               .getTestKnowledgeElement(scen, ktype2, "TestKelm12 title");
+               _knowledgeElementDAO.create(kelm11);
+               _knowledgeElementDAO.create(kelm12);
+
+               // Add simulation context to the study 1
+               cprop.setType(_simulationContextService.selectType("product"))
+                               .setValue("Test Simulation Context: Product1");
+               _studyService.addProjectContext(aStudy, cprop);
+               ht.flush();
+
+               // ============== STUDY 2: Public : otherUser
+               // Create public study with predefined reference
+               aStudy = TestEntitiesGenerator.getTestStudy(otherUser);
+               aStudy.setVisibility(Visibility.PUBLIC);
+               aStudy.setTitle("1.This is another public study");
+               aStudy.setReference("TEST_REF");
+               Long refStudyId = _studyDAO.create(aStudy);
+               // Add a scenario to the study 2
+               scen = TestEntitiesGenerator.getTestScenario(aStudy);
+               _scenarioDAO.create(scen);
+               ht.flush();
+
+               // Add knowledge elements to the scenario
+               KnowledgeElement kelm21 = TestEntitiesGenerator
+                               .getTestKnowledgeElement(scen, ktype1, "TestKelm21 title");
+               KnowledgeElement kelm22 = TestEntitiesGenerator
+                               .getTestKnowledgeElement(scen, ktype2, "TestKelm22 title");
+               _knowledgeElementDAO.create(kelm21);
+               _knowledgeElementDAO.create(kelm22);
+               ht.flush();
+
+               // ============== STUDY 3: Private : thirdUser : otherUser is contributor
+               // Create private study of third user with otherUser as a reviewer
+               aStudy = TestEntitiesGenerator.getTestStudy(thirdUser);
+               aStudy.setTitle("3.This is a private study of third user");
+               Long reviewStudyId = _studyDAO.create(aStudy);
+
+               // Create validation cycle for the last study
+               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 a scenario to the study 3
+               scen = TestEntitiesGenerator.getTestScenario(aStudy);
+               _scenarioDAO.create(scen);
+               ht.flush();
+
+               // Add knowledge elements to the scenario
+               KnowledgeElement kelm31 = TestEntitiesGenerator
+                               .getTestKnowledgeElement(scen, ktype1, "TestKelm31 title");
+               KnowledgeElement kelm32 = TestEntitiesGenerator
+                               .getTestKnowledgeElement(scen, ktype2, "TestKelm32 title");
+               _knowledgeElementDAO.create(kelm31);
+               _knowledgeElementDAO.create(kelm32);
+               ht.flush();
+               calend = java.util.Calendar.getInstance();
+               calend.roll(Calendar.DATE, true);
+               Date afterCreation = calend.getTime();
+
+               // ============== START OF TEST CALLS
+               // Search by author goodUser as not logged in user
+               KnowledgeSearchFilterDTO filter = new KnowledgeSearchFilterDTO();
+               filter.setAuthor(String.valueOf(goodUser.getIndex()));
+               List<Proxy> res = _searchService.selectKnowledgeElementsWhere(filter);
+               Assert.assertNotNull(res);
+               Assert.assertEquals(res.size(), 2);
+               Assert.assertEquals(res.get(0).getTitle(), kelm11.getTitle());
+               Assert.assertEquals(res.get(1).getTitle(), kelm12.getTitle());
+
+               // Search for other logged in study contributor (what is visible for otherUser by default)
+               filter = new KnowledgeSearchFilterDTO();
+               filter.setConnectedUserId(otherUser.getIndex());
+               res = _searchService.selectKnowledgeElementsWhere(filter);
+               Assert.assertNotNull(res);
+               Assert.assertEquals(res.size(), 6);
+               Assert.assertEquals(res.get(0).getTitle(), kelm11.getTitle());
+               Assert.assertEquals(res.get(1).getTitle(), kelm12.getTitle());
+               Assert.assertEquals(res.get(2).getTitle(), kelm21.getTitle());
+               Assert.assertEquals(res.get(3).getTitle(), kelm22.getTitle());
+               Assert.assertEquals(res.get(4).getTitle(), kelm31.getTitle());
+               Assert.assertEquals(res.get(5).getTitle(), kelm32.getTitle());
+
+               // Search for logged in study contributor
+               filter = new KnowledgeSearchFilterDTO();
+               filter.setConnectedUserId(goodUser.getIndex());
+               res = _searchService.selectKnowledgeElementsWhere(filter);
+               Assert.assertNotNull(res);
+               Assert.assertEquals(res.size(), 6);
+               Assert.assertEquals(res.get(0).getTitle(), kelm01.getTitle());
+               Assert.assertEquals(res.get(1).getTitle(), kelm02.getTitle());
+               Assert.assertEquals(res.get(2).getTitle(), kelm11.getTitle());
+               Assert.assertEquals(res.get(3).getTitle(), kelm12.getTitle());
+               Assert.assertEquals(res.get(4).getTitle(), kelm21.getTitle());
+               Assert.assertEquals(res.get(5).getTitle(), kelm22.getTitle());
+
+               // Search by title contents
+               filter = new KnowledgeSearchFilterDTO();
+               filter.setWords("TestKelm22");
+               res = _searchService.selectKnowledgeElementsWhere(filter);
+               Assert.assertNotNull(res);
+               Assert.assertEquals(res.size(), 1);
+               Assert.assertEquals(res.get(0).getTitle(), kelm22.getTitle());
+
+               // Search by reference
+                filter = new KnowledgeSearchFilterDTO();
+                filter.setReference(kelm22.getReference());
+                res = _searchService.selectKnowledgeElementsWhere(filter);
+                Assert.assertNotNull(res);
+                Assert.assertEquals(res.size(), 1);
+                Assert.assertEquals(res.get(0).getTitle(), kelm22.getTitle());
+
+               // Search by simulation context
+               filter = new KnowledgeSearchFilterDTO();
+               filter.setConnectedUserId(goodUser.getIndex());
+               List<SimulationContext> contexts = new ArrayList<SimulationContext>();
+               contexts.add(ctx);
+               filter.setSimContexts(contexts);
+               res = _searchService.selectKnowledgeElementsWhere(filter);
+               Assert.assertNotNull(res);
+               Assert.assertEquals(res.size(), 2);
+               Assert.assertEquals(res.get(0).getTitle(), kelm01.getTitle());
+               Assert.assertEquals(res.get(1).getTitle(), kelm02.getTitle());
+               // Check simualtion contexts of the found study
+               for (Proxy proxy : res) {
+                       KnowledgeElement kelm = _knowledgeElementDAO.get(proxy.getIndex());
+                       aStudy = _studyService.selectStudy(kelm.getOwnerScenario()
+                                       .getOwnerStudy().getIndex());
+                       Iterator<SimulationContext> ctxIter = aStudy
+                                       .SimulationContextIterator();
+                       contexts.clear();
+                       while (ctxIter.hasNext()) {
+                               contexts.add(ctxIter.next());
+                       }
+                       Assert.assertEquals(contexts.size(), 1);
+                       Assert.assertEquals(contexts.get(0), ctx);
+               }
+
+               // Search for dates
+               filter = new KnowledgeSearchFilterDTO();
+               filter.setCreatedAfter(beforeCreation);
+               filter.setCreatedBefore(afterCreation);
+               res = _searchService.selectKnowledgeElementsWhere(filter);
+               Assert.assertNotNull(res);
+               Assert.assertEquals(res.size(), 4);
+               Assert.assertEquals(res.get(0).getTitle(), kelm11.getTitle());
+               Assert.assertEquals(res.get(1).getTitle(), kelm12.getTitle());
+               Assert.assertEquals(res.get(2).getTitle(), kelm21.getTitle());
+               Assert.assertEquals(res.get(3).getTitle(), kelm22.getTitle());
+               
+               // Search for dates with no result
+               filter = new KnowledgeSearchFilterDTO();
+               filter.setCreatedAfter(afterCreation);
+               res = _searchService.selectKnowledgeElementsWhere(filter);
+               Assert.assertNotNull(res);
+               Assert.assertEquals(res.size(), 0);
+               
                rollbackNestedTransaction();
-               LOG.debug(">>>>> END testGenerateLocalIndex()");
+               LOG.debug(">>>>> END testSelectKnowledgeElementsWhere()");
        }
 }