Salome HOME
458a1769aca252fa3357545d43ef528f7ab85396
[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.Calendar;
13 import java.util.Date;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import org.splat.dal.bo.kernel.User;
18 import org.splat.dal.bo.som.ContributorRelation;
19 import org.splat.dal.bo.som.DocumentType;
20 import org.splat.dal.bo.som.KnowledgeElement;
21 import org.splat.dal.bo.som.KnowledgeElementType;
22 import org.splat.dal.bo.som.ProgressState;
23 import org.splat.dal.bo.som.Scenario;
24 import org.splat.dal.bo.som.SimulationContext;
25 import org.splat.dal.bo.som.Study;
26 import org.splat.dal.bo.som.ValidationCycle;
27 import org.splat.dal.bo.som.ValidationCycleRelation;
28 import org.splat.dal.bo.som.ValidationStep;
29 import org.splat.dal.bo.som.Visibility;
30 import org.splat.dal.dao.kernel.UserDAO;
31 import org.splat.dal.dao.som.Database;
32 import org.splat.dal.dao.som.KnowledgeElementDAO;
33 import org.splat.dal.dao.som.KnowledgeElementTypeDAO;
34 import org.splat.dal.dao.som.ScenarioDAO;
35 import org.splat.dal.dao.som.StudyDAO;
36 import org.splat.dal.dao.som.ValidationCycleDAO;
37 import org.splat.exception.BusinessException;
38 import org.splat.log.AppLogger;
39 import org.splat.service.DocumentTypeService;
40 import org.splat.service.PublicationService;
41 import org.splat.service.SearchService;
42 import org.splat.service.SimulationContextService;
43 import org.splat.service.StepService;
44 import org.splat.service.StudyService;
45 import org.splat.service.dto.KnowledgeSearchFilterDTO;
46 import org.splat.service.dto.Proxy;
47 import org.splat.service.dto.StudySearchFilterDTO;
48 import org.splat.service.technical.ProjectSettingsService;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.beans.factory.annotation.Qualifier;
51 import org.springframework.orm.hibernate3.HibernateTemplate;
52 import org.testng.Assert;
53 import org.testng.annotations.Test;
54
55 import test.splat.common.BaseTest;
56 import test.splat.util.TestEntitiesGenerator;
57
58 /**
59  * Test class for StudyService.
60  * 
61  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
62  * 
63  */
64 public class TestSearchService extends BaseTest {
65
66         /**
67          * Logger for the class.
68          */
69         private static final AppLogger LOG = AppLogger
70                         .getLogger(TestSearchService.class);
71
72         /**
73          * The StudyDAO. Later injected by Spring.
74          */
75         @Autowired
76         @Qualifier("studyDAO")
77         private transient StudyDAO _studyDAO;
78
79         /**
80          * The ScenarioDAO. Later injected by Spring.
81          */
82         @Autowired
83         @Qualifier("scenarioDAO")
84         private transient ScenarioDAO _scenarioDAO;
85
86         /**
87          * The KnowledgeElementDAO. Later injected by Spring.
88          */
89         @Autowired
90         @Qualifier("knowledgeElementDAO")
91         private transient KnowledgeElementDAO _knowledgeElementDAO;
92
93         /**
94          * The KnowledgeElementTypeDAO. Later injected by Spring.
95          */
96         @Autowired
97         @Qualifier("knowledgeElementTypeDAO")
98         private transient KnowledgeElementTypeDAO _knowledgeElementTypeDAO;
99
100         /**
101          * The PublicationService. Later injected by Spring.
102          */
103         @Autowired
104         @Qualifier("publicationService")
105         private transient PublicationService _publicationService;
106
107         /**
108          * The StepService. Later injected by Spring.
109          */
110         @Autowired
111         @Qualifier("stepService")
112         private transient StepService _stepService;
113
114         /**
115          * The ProjectSettingsService. Later injected by Spring.
116          */
117         @Autowired
118         @Qualifier("projectSettings")
119         private transient ProjectSettingsService _projectSettings;
120
121         /**
122          * The DocumentTypeService. Later injected by Spring.
123          */
124         @Autowired
125         @Qualifier("documentTypeService")
126         private transient DocumentTypeService _documentTypeService;
127
128         /**
129          * The StudyService. Later injected by Spring.
130          */
131         @Autowired
132         @Qualifier("studyService")
133         private transient StudyService _studyService;
134
135         /**
136          * The SearchService. Later injected by Spring.
137          */
138         @Autowired
139         @Qualifier("searchService")
140         private transient SearchService _searchService;
141
142         /**
143          * The UserDAO. Later injected by Spring.
144          */
145         @Autowired
146         @Qualifier("userDAO")
147         private transient UserDAO _userDAO;
148
149         /**
150          * The ValidationCycleDAO. Later injected by Spring.
151          */
152         @Autowired
153         @Qualifier("validationCycleDAO")
154         private transient ValidationCycleDAO _validationCycleDAO;
155
156         /**
157          * The SimulationContextService. Later injected by Spring.
158          */
159         @Autowired
160         @Qualifier("simulationContextService")
161         private transient SimulationContextService _simulationContextService;
162
163         /**
164          * Test of searching a study.<BR>
165          * <B>Description :</B> <BR>
166          * <i>Create studies and try to find them according next document index.</i><BR>
167          * <B>Action : </B><BR>
168          * <i>
169          * <ol>
170          * <li>Search study by state</li>
171          * <li>Search study by reference</li>
172          * <li>Search study by author</li>
173          * <li>Search study by actor</li>
174          * <li>Search study by title</li>
175          * <li>Search study by simulation contexts</li>
176          * <li>Search study by all criteria</li>
177          * <li>Search study by any criteria</li>
178          * </ol>
179          * </i> <B>Test data : </B><BR>
180          * <i>no input parameters</i><BR>
181          * 
182          * <B>Outcome results:</B><BR>
183          * <i>
184          * <ul>
185          * <li>A study must be found according to the given criteria<BR>
186          * </li>
187          * </ul>
188          * </i>
189          * 
190          * @throws BusinessException
191          *             if can't create test user or study
192          */
193         @Test
194         public void testSelectStudiesWhere() throws BusinessException {
195                 LOG.debug(">>>>> BEGIN testSelectStudiesWhere()");
196                 startNestedTransaction();
197
198                 HibernateTemplate ht = getHibernateTemplate();
199                 Calendar calend = java.util.Calendar.getInstance();
200                 calend.roll(Calendar.DATE, false);
201                 Date beforeCreation = calend.getTime();
202
203                 Database.getInstance().reset();
204                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
205                 // Load workflow customization
206                 try {
207                         _projectSettings.configure("classpath:test/som.xml");
208                 } catch (Exception e) {
209                         Assert.fail("Can't load som.xml: ", e);
210                 }
211
212                 User goodUser = TestEntitiesGenerator.getTestUser("goodUser");
213                 _userDAO.create(goodUser);
214                 User otherUser = TestEntitiesGenerator.getTestUser("otherUser");
215                 _userDAO.create(otherUser);
216                 User thirdUser = TestEntitiesGenerator.getTestUser("thirdUser");
217                 _userDAO.create(thirdUser);
218
219                 // ============== STUDY 0: Private : goodUser
220                 // Create private study
221                 Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
222                 aStudy.setTitle("0.This is private study");
223                 Long privateStudyId = _studyDAO.create(aStudy);
224
225                 // Add simulation context to the study 0
226                 SimulationContext.Properties cprop = new SimulationContext.Properties();
227                 cprop.setType(_simulationContextService.selectType("product"))
228                                 .setValue("Test Simulation Context: Product");
229                 SimulationContext ctx = _studyService.addProjectContext(aStudy, cprop);
230                 ht.flush();
231
232                 // ============== STUDY 1: Public : goodUser : otherUser is contributor
233                 // Create public study with predefined title
234                 aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
235                 aStudy.setVisibility(Visibility.PUBLIC);
236                 aStudy.setTitle("2.This is a public study");
237                 aStudy.addRelation(new ContributorRelation(aStudy, otherUser));
238                 Long titleStudyId = _studyDAO.create(aStudy);
239
240                 // Add simulation context to the study 1
241                 cprop.setType(_simulationContextService.selectType("product"))
242                                 .setValue("Test Simulation Context: Product1");
243                 _studyService.addProjectContext(aStudy, cprop);
244                 ht.flush();
245
246                 // ============== STUDY 2: Public : otherUser
247                 // Create public study with predefined reference
248                 aStudy = TestEntitiesGenerator.getTestStudy(otherUser);
249                 aStudy.setVisibility(Visibility.PUBLIC);
250                 aStudy.setTitle("1.This is another public study");
251                 aStudy.setReference("TEST_REF");
252                 Long refStudyId = _studyDAO.create(aStudy);
253
254                 // ============== STUDY 3: Private : inDraft : thirdUser : otherUser is contributor
255                 // Create private study of third user with otherUser as a reviewer
256                 aStudy = TestEntitiesGenerator.getTestStudy(thirdUser);
257                 aStudy.setTitle("3.This is a private study of third user");
258                 aStudy.setProgressState(ProgressState.inDRAFT);
259                 Long reviewStudyId = _studyDAO.create(aStudy);
260
261                 // Create validation cycle for the last study
262                 ValidationCycle.Properties vprop = new ValidationCycle.Properties();
263                 DocumentType type = _documentTypeService.selectType("specification");
264                 vprop.setDocumentType(type);
265                 vprop.setActor(ValidationStep.REVIEW, otherUser);
266                 ValidationCycle cycle = new ValidationCycle(aStudy, vprop);
267                 _validationCycleDAO.create(cycle);
268                 ValidationCycleRelation link = cycle.getContext();
269                 aStudy.addRelation(link);
270                 ht.flush();
271                 calend = java.util.Calendar.getInstance();
272                 calend.roll(Calendar.DATE, true);
273                 Date afterCreation = calend.getTime();
274
275                 // ============== START OF TEST CALLS
276                 // Search by study author
277                 StudySearchFilterDTO filter = new StudySearchFilterDTO();
278                 filter.setAuthor(String.valueOf(goodUser.getIndex()));
279                 List<Proxy> res = _searchService.selectStudiesWhere(filter);
280                 Assert.assertNotNull(res);
281                 Assert.assertEquals(res.size(), 1);
282                 Assert.assertEquals(res.get(0).getIndex(), titleStudyId);
283
284                 // Search for other logged in study contributor
285                 filter = new StudySearchFilterDTO();
286                 filter.setConnectedUserId(otherUser.getIndex());
287                 res = _searchService.selectStudiesWhere(filter);
288                 Assert.assertNotNull(res);
289                 Assert.assertEquals(res.size(), 3);
290                 Assert.assertEquals(res.get(0).getIndex(), refStudyId); // Public study
291                 Assert.assertEquals(res.get(1).getIndex(), titleStudyId); // Public study
292                 Assert.assertEquals(res.get(2).getIndex(), reviewStudyId); // OtherUser is reviewer in this study
293
294                 // Search for logged in study contributor
295                 filter = new StudySearchFilterDTO();
296                 filter.setConnectedUserId(goodUser.getIndex());
297                 res = _searchService.selectStudiesWhere(filter);
298                 Assert.assertNotNull(res);
299                 Assert.assertEquals(res.size(), 3);
300                 Assert.assertEquals(res.get(0).getIndex(), privateStudyId); // goodUser is author
301                 Assert.assertEquals(res.get(1).getIndex(), refStudyId); // Public study
302                 Assert.assertEquals(res.get(2).getIndex(), titleStudyId); // Public study
303
304                 // Search for logged in study contributor
305                 // filtered by state
306                 filter = new StudySearchFilterDTO();
307                 filter.setConnectedUserId(otherUser.getIndex());
308                 filter.setState(ProgressState.inDRAFT);
309                 res = _searchService.selectStudiesWhere(filter);
310                 Assert.assertNotNull(res);
311                 Assert.assertEquals(res.size(), 1);
312                 Assert.assertEquals(res.get(0).getIndex(), reviewStudyId); // OtherUser is reviewer in this study
313
314                 // Search by study title contents
315                 filter = new StudySearchFilterDTO();
316                 filter.setWords("study public");
317                 res = _searchService.selectStudiesWhere(filter);
318                 Assert.assertNotNull(res);
319                 Assert.assertEquals(res.size(), 2);
320                 Assert.assertEquals(res.get(0).getIndex(), refStudyId);
321                 Assert.assertEquals(res.get(1).getIndex(), titleStudyId);
322
323                 // Search by study reference
324                 filter = new StudySearchFilterDTO();
325                 filter.setReference("TEST_REF");
326                 res = _searchService.selectStudiesWhere(filter);
327                 Assert.assertNotNull(res);
328                 Assert.assertEquals(res.size(), 1);
329                 Assert.assertEquals(res.get(0).getIndex(), refStudyId);
330
331                 // Search a study by simulation context
332                 filter = new StudySearchFilterDTO();
333                 filter.setConnectedUserId(goodUser.getIndex());
334                 List<SimulationContext> contexts = new ArrayList<SimulationContext>();
335                 contexts.add(ctx);
336                 filter.setSimContexts(contexts);
337                 res = _searchService.selectStudiesWhere(filter);
338                 Assert.assertNotNull(res);
339                 Assert.assertEquals(res.size(), 1);
340                 Assert.assertEquals(res.get(0).getIndex(), privateStudyId);
341                 // Check simualtion contexts of the found study
342                 for (Proxy proxy: res) {
343                 aStudy = _studyService.selectStudy(proxy.getIndex());
344                 Iterator<SimulationContext> ctxIter = aStudy
345                                 .SimulationContextIterator();
346                 contexts.clear();
347                 while (ctxIter.hasNext()) {
348                         contexts.add(ctxIter.next());
349                 }
350                 Assert.assertEquals(contexts.size(), 1);
351                 Assert.assertEquals(contexts.get(0), ctx);
352                 }
353
354                 // Search for dates
355                 filter = new StudySearchFilterDTO();
356                 filter.setCreatedAfter(beforeCreation);
357                 filter.setCreatedBefore(afterCreation);
358                 res = _searchService.selectStudiesWhere(filter);
359                 Assert.assertNotNull(res);
360                 Assert.assertEquals(res.size(), 2);
361                 Assert.assertEquals(res.get(0).getIndex(), refStudyId);
362                 Assert.assertEquals(res.get(1).getIndex(), titleStudyId);
363                 
364                 // Search for dates with no result
365                 filter = new StudySearchFilterDTO();
366                 filter.setCreatedAfter(afterCreation);
367                 res = _searchService.selectStudiesWhere(filter);
368                 Assert.assertNotNull(res);
369                 Assert.assertEquals(res.size(), 0);
370                 
371                 rollbackNestedTransaction();
372                 LOG.debug(">>>>> END testSelectStudiesWhere()");
373         }
374
375         /**
376          * Test of searching a study.<BR>
377          * <B>Description :</B> <BR>
378          * <i>Create studies and try to find them according next document index.</i><BR>
379          * <B>Action : </B><BR>
380          * <i>
381          * <ol>
382          * <li>Search study by state</li>
383          * <li>Search study by reference</li>
384          * <li>Search study by author</li>
385          * <li>Search study by actor</li>
386          * <li>Search study by title</li>
387          * <li>Search study by simulation contexts</li>
388          * <li>Search study by all criteria</li>
389          * <li>Search study by any criteria</li>
390          * </ol>
391          * </i> <B>Test data : </B><BR>
392          * <i>no input parameters</i><BR>
393          * 
394          * <B>Outcome results:</B><BR>
395          * <i>
396          * <ul>
397          * <li>A study must be found according to the given criteria<BR>
398          * </li>
399          * </ul>
400          * </i>
401          * 
402          * @throws BusinessException
403          *             if can't create test user or study
404          */
405         @Test
406         public void testSelectKnowledgeElementsWhere() throws BusinessException {
407                 LOG.debug(">>>>> BEGIN testSelectKnowledgeElementsWhere()");
408                 startNestedTransaction();
409
410                 HibernateTemplate ht = getHibernateTemplate();
411                 
412                 Calendar calend = java.util.Calendar.getInstance();
413                 calend.roll(Calendar.DATE, false);
414                 Date beforeCreation = calend.getTime();
415
416                 Database.getInstance().reset();
417                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
418                 // Load workflow customization
419                 try {
420                         _projectSettings.configure("classpath:test/som.xml");
421                 } catch (Exception e) {
422                         Assert.fail("Can't load som.xml: ", e);
423                 }
424
425                 User goodUser = TestEntitiesGenerator.getTestUser("goodUser");
426                 _userDAO.create(goodUser);
427                 User otherUser = TestEntitiesGenerator.getTestUser("otherUser");
428                 _userDAO.create(otherUser);
429                 User thirdUser = TestEntitiesGenerator.getTestUser("thirdUser");
430                 _userDAO.create(thirdUser);
431                 KnowledgeElementType ktype1 = new KnowledgeElementType("testKType1");
432                 KnowledgeElementType ktype2 = new KnowledgeElementType("testKType2");
433                 ktype2.setState(ProgressState.inWORK);
434                 _knowledgeElementTypeDAO.create(ktype1);
435                 _knowledgeElementTypeDAO.create(ktype2);
436
437                 // ============== STUDY 0: Private : goodUser
438                 // Create private study
439                 Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
440                 aStudy.setTitle("0.This is private study");
441                 Long privateStudyId = _studyDAO.create(aStudy);
442
443                 // Add simulation context to the study 0
444                 SimulationContext.Properties cprop = new SimulationContext.Properties();
445                 cprop.setType(_simulationContextService.selectType("product"))
446                                 .setValue("Test Simulation Context: Product");
447                 SimulationContext ctx = _studyService.addProjectContext(aStudy, cprop);
448                 ht.flush();
449
450                 // Add a scenario to the study 0
451                 Scenario scen = TestEntitiesGenerator.getTestScenario(aStudy);
452                 _scenarioDAO.create(scen);
453                 ht.flush();
454
455                 // Add knowledge elements to the scenario
456                 KnowledgeElement kelm01 = TestEntitiesGenerator
457                                 .getTestKnowledgeElement(scen, ktype1, "TestKelm01 title");
458                 KnowledgeElement kelm02 = TestEntitiesGenerator
459                                 .getTestKnowledgeElement(scen, ktype2, "TestKelm02 title");
460                 _knowledgeElementDAO.create(kelm01);
461                 _knowledgeElementDAO.create(kelm02);
462                 ht.flush();
463
464                 // ============== STUDY 1: Public : goodUser : otherUser is contributor
465                 // Create public study with predefined title
466                 aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
467                 aStudy.setVisibility(Visibility.PUBLIC);
468                 aStudy.setTitle("2.This is a public study");
469                 aStudy.addRelation(new ContributorRelation(aStudy, otherUser));
470                 Long titleStudyId = _studyDAO.create(aStudy);
471
472                 // Add a scenario to the study 1
473                 scen = TestEntitiesGenerator.getTestScenario(aStudy);
474                 _scenarioDAO.create(scen);
475                 ht.flush();
476
477                 // Add knowledge elements to the scenario
478                 KnowledgeElement kelm11 = TestEntitiesGenerator
479                                 .getTestKnowledgeElement(scen, ktype1, "TestKelm11 title");
480                 KnowledgeElement kelm12 = TestEntitiesGenerator
481                                 .getTestKnowledgeElement(scen, ktype2, "TestKelm12 title");
482                 _knowledgeElementDAO.create(kelm11);
483                 _knowledgeElementDAO.create(kelm12);
484
485                 // Add simulation context to the study 1
486                 cprop.setType(_simulationContextService.selectType("product"))
487                                 .setValue("Test Simulation Context: Product1");
488                 _studyService.addProjectContext(aStudy, cprop);
489                 ht.flush();
490
491                 // ============== STUDY 2: Public : otherUser
492                 // Create public study with predefined reference
493                 aStudy = TestEntitiesGenerator.getTestStudy(otherUser);
494                 aStudy.setVisibility(Visibility.PUBLIC);
495                 aStudy.setTitle("1.This is another public study");
496                 aStudy.setReference("TEST_REF");
497                 Long refStudyId = _studyDAO.create(aStudy);
498                 // Add a scenario to the study 2
499                 scen = TestEntitiesGenerator.getTestScenario(aStudy);
500                 _scenarioDAO.create(scen);
501                 ht.flush();
502
503                 // Add knowledge elements to the scenario
504                 KnowledgeElement kelm21 = TestEntitiesGenerator
505                                 .getTestKnowledgeElement(scen, ktype1, "TestKelm21 title");
506                 KnowledgeElement kelm22 = TestEntitiesGenerator
507                                 .getTestKnowledgeElement(scen, ktype2, "TestKelm22 title");
508                 _knowledgeElementDAO.create(kelm21);
509                 _knowledgeElementDAO.create(kelm22);
510                 ht.flush();
511
512                 // ============== STUDY 3: Private : thirdUser : otherUser is contributor
513                 // Create private study of third user with otherUser as a reviewer
514                 aStudy = TestEntitiesGenerator.getTestStudy(thirdUser);
515                 aStudy.setTitle("3.This is a private study of third user");
516                 Long reviewStudyId = _studyDAO.create(aStudy);
517
518                 // Create validation cycle for the last study
519                 ValidationCycle.Properties vprop = new ValidationCycle.Properties();
520                 DocumentType type = _documentTypeService.selectType("specification");
521                 vprop.setDocumentType(type);
522                 vprop.setActor(ValidationStep.REVIEW, otherUser);
523                 ValidationCycle cycle = new ValidationCycle(aStudy, vprop);
524                 _validationCycleDAO.create(cycle);
525                 ValidationCycleRelation link = cycle.getContext();
526                 aStudy.addRelation(link);
527                 ht.flush();
528                 // Add a scenario to the study 3
529                 scen = TestEntitiesGenerator.getTestScenario(aStudy);
530                 _scenarioDAO.create(scen);
531                 ht.flush();
532
533                 // Add knowledge elements to the scenario
534                 KnowledgeElement kelm31 = TestEntitiesGenerator
535                                 .getTestKnowledgeElement(scen, ktype1, "TestKelm31 title");
536                 KnowledgeElement kelm32 = TestEntitiesGenerator
537                                 .getTestKnowledgeElement(scen, ktype2, "TestKelm32 title");
538                 _knowledgeElementDAO.create(kelm31);
539                 _knowledgeElementDAO.create(kelm32);
540                 ht.flush();
541                 calend = java.util.Calendar.getInstance();
542                 calend.roll(Calendar.DATE, true);
543                 Date afterCreation = calend.getTime();
544
545                 // ============== START OF TEST CALLS
546                 // Search by author goodUser as not logged in user
547                 KnowledgeSearchFilterDTO filter = new KnowledgeSearchFilterDTO();
548                 filter.setAuthor(String.valueOf(goodUser.getIndex()));
549                 List<Proxy> res = _searchService.selectKnowledgeElementsWhere(filter);
550                 Assert.assertNotNull(res);
551                 Assert.assertEquals(res.size(), 2);
552                 Assert.assertEquals(res.get(0).getTitle(), kelm11.getTitle());
553                 Assert.assertEquals(res.get(1).getTitle(), kelm12.getTitle());
554
555                 // Search by author goodUser as not logged in user and filter by state
556                 filter = new KnowledgeSearchFilterDTO();
557                 filter.setAuthor(String.valueOf(goodUser.getIndex()));
558                 filter.setState(ProgressState.inWORK);
559                 res = _searchService.selectKnowledgeElementsWhere(filter);
560                 Assert.assertNotNull(res);
561                 Assert.assertEquals(res.size(), 1);
562                 Assert.assertEquals(res.get(0).getTitle(), kelm12.getTitle());
563
564                 // Search for other logged in study contributor (what is visible for otherUser by default)
565                 filter = new KnowledgeSearchFilterDTO();
566                 filter.setConnectedUserId(otherUser.getIndex());
567                 res = _searchService.selectKnowledgeElementsWhere(filter);
568                 Assert.assertNotNull(res);
569                 Assert.assertEquals(res.size(), 6);
570                 Assert.assertEquals(res.get(0).getTitle(), kelm11.getTitle());
571                 Assert.assertEquals(res.get(1).getTitle(), kelm12.getTitle());
572                 Assert.assertEquals(res.get(2).getTitle(), kelm21.getTitle());
573                 Assert.assertEquals(res.get(3).getTitle(), kelm22.getTitle());
574                 Assert.assertEquals(res.get(4).getTitle(), kelm31.getTitle());
575                 Assert.assertEquals(res.get(5).getTitle(), kelm32.getTitle());
576
577                 // Search for logged in study contributor
578                 filter = new KnowledgeSearchFilterDTO();
579                 filter.setConnectedUserId(goodUser.getIndex());
580                 res = _searchService.selectKnowledgeElementsWhere(filter);
581                 Assert.assertNotNull(res);
582                 Assert.assertEquals(res.size(), 6);
583                 Assert.assertEquals(res.get(0).getTitle(), kelm01.getTitle());
584                 Assert.assertEquals(res.get(1).getTitle(), kelm02.getTitle());
585                 Assert.assertEquals(res.get(2).getTitle(), kelm11.getTitle());
586                 Assert.assertEquals(res.get(3).getTitle(), kelm12.getTitle());
587                 Assert.assertEquals(res.get(4).getTitle(), kelm21.getTitle());
588                 Assert.assertEquals(res.get(5).getTitle(), kelm22.getTitle());
589
590                 // Search by title contents
591                 filter = new KnowledgeSearchFilterDTO();
592                 filter.setWords("TestKelm22");
593                 res = _searchService.selectKnowledgeElementsWhere(filter);
594                 Assert.assertNotNull(res);
595                 Assert.assertEquals(res.size(), 1);
596                 Assert.assertEquals(res.get(0).getTitle(), kelm22.getTitle());
597
598                 // Search by reference
599                  filter = new KnowledgeSearchFilterDTO();
600                  filter.setReference(kelm22.getReference());
601                  res = _searchService.selectKnowledgeElementsWhere(filter);
602                  Assert.assertNotNull(res);
603                  Assert.assertEquals(res.size(), 1);
604                  Assert.assertEquals(res.get(0).getTitle(), kelm22.getTitle());
605
606                 // Search by simulation context
607                 filter = new KnowledgeSearchFilterDTO();
608                 filter.setConnectedUserId(goodUser.getIndex());
609                 List<SimulationContext> contexts = new ArrayList<SimulationContext>();
610                 contexts.add(ctx);
611                 filter.setSimContexts(contexts);
612                 res = _searchService.selectKnowledgeElementsWhere(filter);
613                 Assert.assertNotNull(res);
614                 Assert.assertEquals(res.size(), 2);
615                 Assert.assertEquals(res.get(0).getTitle(), kelm01.getTitle());
616                 Assert.assertEquals(res.get(1).getTitle(), kelm02.getTitle());
617                 // Check simualtion contexts of the found study
618                 for (Proxy proxy : res) {
619                         KnowledgeElement kelm = _knowledgeElementDAO.get(proxy.getIndex());
620                         aStudy = _studyService.selectStudy(kelm.getOwnerScenario()
621                                         .getOwnerStudy().getIndex());
622                         Iterator<SimulationContext> ctxIter = aStudy
623                                         .SimulationContextIterator();
624                         contexts.clear();
625                         while (ctxIter.hasNext()) {
626                                 contexts.add(ctxIter.next());
627                         }
628                         Assert.assertEquals(contexts.size(), 1);
629                         Assert.assertEquals(contexts.get(0), ctx);
630                 }
631
632                 // Search for dates
633                 filter = new KnowledgeSearchFilterDTO();
634                 filter.setCreatedAfter(beforeCreation);
635                 filter.setCreatedBefore(afterCreation);
636                 res = _searchService.selectKnowledgeElementsWhere(filter);
637                 Assert.assertNotNull(res);
638                 Assert.assertEquals(res.size(), 4);
639                 Assert.assertEquals(res.get(0).getTitle(), kelm11.getTitle());
640                 Assert.assertEquals(res.get(1).getTitle(), kelm12.getTitle());
641                 Assert.assertEquals(res.get(2).getTitle(), kelm21.getTitle());
642                 Assert.assertEquals(res.get(3).getTitle(), kelm22.getTitle());
643                 
644                 // Search for dates with no result
645                 filter = new KnowledgeSearchFilterDTO();
646                 filter.setCreatedAfter(afterCreation);
647                 res = _searchService.selectKnowledgeElementsWhere(filter);
648                 Assert.assertNotNull(res);
649                 Assert.assertEquals(res.size(), 0);
650                 
651                 rollbackNestedTransaction();
652                 LOG.debug(">>>>> END testSelectKnowledgeElementsWhere()");
653         }
654 }