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