]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/test/splat/service/TestSearchService.java
Salome HOME
Simulation context type selection is fixed in study search. Lucene is no more used...
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / service / TestSearchService.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   12 Oct 2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9 package test.splat.service;
10
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14
15 import org.splat.dal.bo.kernel.User;
16 import org.splat.dal.bo.som.ContributorRelation;
17 import org.splat.dal.bo.som.DocumentType;
18 import org.splat.dal.bo.som.SimulationContext;
19 import org.splat.dal.bo.som.Study;
20 import org.splat.dal.bo.som.ValidationCycle;
21 import org.splat.dal.bo.som.ValidationCycleRelation;
22 import org.splat.dal.bo.som.ValidationStep;
23 import org.splat.dal.bo.som.Visibility;
24 import org.splat.dal.dao.kernel.UserDAO;
25 import org.splat.dal.dao.som.Database;
26 import org.splat.dal.dao.som.StudyDAO;
27 import org.splat.dal.dao.som.ValidationCycleDAO;
28 import org.splat.exception.BusinessException;
29 import org.splat.log.AppLogger;
30 import org.splat.service.DocumentTypeService;
31 import org.splat.service.PublicationService;
32 import org.splat.service.SearchService;
33 import org.splat.service.SimulationContextService;
34 import org.splat.service.StepService;
35 import org.splat.service.StudyService;
36 import org.splat.service.dto.Proxy;
37 import org.splat.service.technical.ProjectSettingsService;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.beans.factory.annotation.Qualifier;
40 import org.springframework.orm.hibernate3.HibernateTemplate;
41 import org.testng.Assert;
42 import org.testng.annotations.Test;
43
44 import test.splat.common.BaseTest;
45 import test.splat.util.TestEntitiesGenerator;
46
47 /**
48  * Test class for StudyService.
49  * 
50  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
51  * 
52  */
53 public class TestSearchService extends BaseTest {
54
55         /**
56          * Logger for the class.
57          */
58         private static final AppLogger LOG = AppLogger
59                         .getLogger(TestSearchService.class);
60
61         /**
62          * The StudyDAO. Later injected by Spring.
63          */
64         @Autowired
65         @Qualifier("studyDAO")
66         private transient StudyDAO _studyDAO;
67
68         /**
69          * The PublicationService. Later injected by Spring.
70          */
71         @Autowired
72         @Qualifier("publicationService")
73         private transient PublicationService _publicationService;
74
75         /**
76          * The StepService. Later injected by Spring.
77          */
78         @Autowired
79         @Qualifier("stepService")
80         private transient StepService _stepService;
81
82         /**
83          * The ProjectSettingsService. Later injected by Spring.
84          */
85         @Autowired
86         @Qualifier("projectSettings")
87         private transient ProjectSettingsService _projectSettings;
88
89         /**
90          * The DocumentTypeService. Later injected by Spring.
91          */
92         @Autowired
93         @Qualifier("documentTypeService")
94         private transient DocumentTypeService _documentTypeService;
95
96         /**
97          * The StudyService. Later injected by Spring.
98          */
99         @Autowired
100         @Qualifier("studyService")
101         private transient StudyService _studyService;
102
103         /**
104          * The SearchService. Later injected by Spring.
105          */
106         @Autowired
107         @Qualifier("searchService")
108         private transient SearchService _searchService;
109
110         /**
111          * The UserDAO. Later injected by Spring.
112          */
113         @Autowired
114         @Qualifier("userDAO")
115         private transient UserDAO _userDAO;
116
117         /**
118          * The ValidationCycleDAO. Later injected by Spring.
119          */
120         @Autowired
121         @Qualifier("validationCycleDAO")
122         private transient ValidationCycleDAO _validationCycleDAO;
123
124         /**
125          * The SimulationContextService. Later injected by Spring.
126          */
127         @Autowired
128         @Qualifier("simulationContextService")
129         private transient SimulationContextService _simulationContextService;
130
131         /**
132          * Test of searching a study.<BR>
133          * <B>Description :</B> <BR>
134          * <i>Create studies and try to find them according next document index.</i><BR>
135          * <B>Action : </B><BR>
136          * <i>
137          * <ol>
138          * <li>Search study by state</li>
139          * <li>Search study by reference</li>
140          * <li>Search study by author</li>
141          * <li>Search study by actor</li>
142          * <li>Search study by title</li>
143          * <li>Search study by simulation contexts</li>
144          * <li>Search study by all criteria</li>
145          * <li>Search study by any criteria</li>
146          * </ol>
147          * </i> <B>Test data : </B><BR>
148          * <i>no input parameters</i><BR>
149          * 
150          * <B>Outcome results:</B><BR>
151          * <i>
152          * <ul>
153          * <li>A study must be found according to the given criteria<BR>
154          * </li>
155          * </ul>
156          * </i>
157          * 
158          * @throws BusinessException
159          *             if can't create test user or study
160          */
161         @Test
162         public void testSelectStudiesWhere() throws BusinessException {
163                 LOG.debug(">>>>> BEGIN testGenerateLocalIndex()");
164                 startNestedTransaction();
165
166                 HibernateTemplate ht = getHibernateTemplate();
167
168                 Database.getInstance().reset();
169                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
170                 // Load workflow customization
171                 try {
172                         _projectSettings.configure(ClassLoader.getSystemResource(
173                                         "test/som.xml").getPath());
174                 } catch (Exception e) {
175                         Assert.fail("Can't load som.xml: ", e);
176                 }
177
178                 User goodUser = TestEntitiesGenerator.getTestUser("goodUser");
179                 _userDAO.create(goodUser);
180                 User otherUser = TestEntitiesGenerator.getTestUser("otherUser");
181                 _userDAO.create(otherUser);
182                 User thirdUser = TestEntitiesGenerator.getTestUser("thirdUser");
183                 _userDAO.create(thirdUser);
184
185                 // Create private study
186                 Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
187                 aStudy.setTitle("0.This is private study");
188                 Long privateStudyId = _studyDAO.create(aStudy);
189
190                 // Add simulation context to the study 0
191                 SimulationContext.Properties cprop = new SimulationContext.Properties();
192                 cprop.setType(_simulationContextService.selectType("product"))
193                                 .setValue("Test Simulation Context: Product");
194                 SimulationContext ctx = _studyService.addProjectContext(aStudy, cprop);
195                 ht.flush();
196
197                 // Create public study with predefined title
198                 aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
199                 aStudy.setVisibility(Visibility.PUBLIC);
200                 aStudy.setTitle("2.This is a public study");
201                 aStudy.addRelation(new ContributorRelation(aStudy, otherUser));
202                 Long titleStudyId = _studyDAO.create(aStudy);
203
204                 // Add simulation context to the study 2
205                 SimulationContext.Properties cprop1 = new SimulationContext.Properties();
206                 cprop.setType(_simulationContextService.selectType("product"))
207                                 .setValue("Test Simulation Context: Product1");
208                 SimulationContext ctx1 = _studyService.addProjectContext(aStudy, cprop);
209                 ht.flush();
210
211                 // Create public study with predefined reference
212                 aStudy = TestEntitiesGenerator.getTestStudy(otherUser);
213                 aStudy.setVisibility(Visibility.PUBLIC);
214                 aStudy.setTitle("1.This is another public study");
215                 aStudy.setReference("TEST_REF");
216                 Long refStudyId = _studyDAO.create(aStudy);
217
218                 // Create private study of third user with otherUser as a reviewer
219                 aStudy = TestEntitiesGenerator.getTestStudy(thirdUser);
220                 aStudy.setTitle("3.This is a private study of third user");
221                 Long reviewStudyId = _studyDAO.create(aStudy);
222
223                 // Create validation cycle for the last study
224                 ValidationCycle.Properties vprop = new ValidationCycle.Properties();
225                 DocumentType type = _documentTypeService.selectType("specification");
226                 vprop.setDocumentType(type);
227                 vprop.setActor(ValidationStep.REVIEW, otherUser);
228                 ValidationCycle cycle = new ValidationCycle(aStudy, vprop);
229                 _validationCycleDAO.create(cycle);
230                 ValidationCycleRelation link = cycle.getContext();
231                 aStudy.addRelation(link);
232                 ht.flush();
233
234                 // Search by study author
235                 Study.Properties sprop = new Study.Properties();
236                 sprop.setManager(goodUser);
237                 List<Proxy> res = _searchService.selectStudiesWhere(true, true, sprop);
238                 Assert.assertNotNull(res);
239                 Assert.assertEquals(res.size(), 1);
240                 Assert.assertEquals(res.get(0).getIndex(), titleStudyId);
241
242                 // Search for other logged in study contributor
243                 sprop.clear();
244                 sprop.setActor(otherUser);
245                 res = _searchService.selectStudiesWhere(true, true, sprop);
246                 Assert.assertNotNull(res);
247                 Assert.assertEquals(res.size(), 3);
248                 Assert.assertEquals(res.get(0).getIndex(), refStudyId); // Public study
249                 Assert.assertEquals(res.get(1).getIndex(), titleStudyId); // Public study
250                 Assert.assertEquals(res.get(2).getIndex(), reviewStudyId); // OtherUser is reviewer in this study
251
252                 // Search for logged in study contributor
253                 sprop.clear();
254                 sprop.setActor(goodUser);
255                 res = _searchService.selectStudiesWhere(true, true, sprop);
256                 Assert.assertNotNull(res);
257                 Assert.assertEquals(res.size(), 3);
258                 Assert.assertEquals(res.get(0).getIndex(), privateStudyId); // goodUser is author
259                 Assert.assertEquals(res.get(2).getIndex(), titleStudyId); // Public study
260                 Assert.assertEquals(res.get(1).getIndex(), refStudyId); // Public study
261
262                 // Search by study title contents
263                 sprop.clear();
264                 sprop.setTitle("study public");
265                 res = _searchService.selectStudiesWhere(true, true, sprop);
266                 Assert.assertNotNull(res);
267                 Assert.assertEquals(res.size(), 2);
268                 Assert.assertEquals(res.get(0).getIndex(), refStudyId);
269                 Assert.assertEquals(res.get(1).getIndex(), titleStudyId);
270
271                 // Search by study reference
272                 sprop.clear();
273                 sprop.setReference("TEST_REF");
274                 res = _searchService.selectStudiesWhere(true, true, sprop);
275                 Assert.assertNotNull(res);
276                 Assert.assertEquals(res.size(), 1);
277                 Assert.assertEquals(res.get(0).getIndex(), refStudyId);
278
279                 // Search a study by simulation context
280                 sprop.clear();
281                 sprop.setActor(goodUser);
282                 List<SimulationContext> contexts = new ArrayList<SimulationContext>();
283                 contexts.add(ctx);
284                 sprop.setSimulationContexts(contexts);
285                 res = _searchService.selectStudiesWhere(true, true, sprop);
286                 Assert.assertNotNull(res);
287                 Assert.assertEquals(res.size(), 1);
288                 Assert.assertEquals(res.get(0).getIndex(), privateStudyId);
289                 // Check simualtion contexts of the found study
290                 aStudy = _studyService.selectStudy(res.get(0).getIndex());
291                 Iterator<SimulationContext> ctxIter = aStudy.SimulationContextIterator();
292                 contexts.clear();
293                 while (ctxIter.hasNext()) {
294                         contexts.add(ctxIter.next());
295                 }
296                 Assert.assertEquals(contexts.size(), 1);
297                 Assert.assertEquals(contexts.get(0), ctx);
298
299                 rollbackNestedTransaction();
300                 LOG.debug(">>>>> END testGenerateLocalIndex()");
301         }
302 }