1 /*****************************************************************************
5 * Creation date 12 Oct 2012
8 *****************************************************************************/
9 package test.splat.service;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
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;
45 import test.splat.common.BaseTest;
46 import test.splat.util.TestEntitiesGenerator;
49 * Test class for StudyService.
51 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
54 public class TestSearchService extends BaseTest {
57 * Logger for the class.
59 private static final AppLogger LOG = AppLogger
60 .getLogger(TestSearchService.class);
63 * The StudyDAO. Later injected by Spring.
66 @Qualifier("studyDAO")
67 private transient StudyDAO _studyDAO;
70 * The PublicationService. Later injected by Spring.
73 @Qualifier("publicationService")
74 private transient PublicationService _publicationService;
77 * The StepService. Later injected by Spring.
80 @Qualifier("stepService")
81 private transient StepService _stepService;
84 * The ProjectSettingsService. Later injected by Spring.
87 @Qualifier("projectSettings")
88 private transient ProjectSettingsService _projectSettings;
91 * The DocumentTypeService. Later injected by Spring.
94 @Qualifier("documentTypeService")
95 private transient DocumentTypeService _documentTypeService;
98 * The StudyService. Later injected by Spring.
101 @Qualifier("studyService")
102 private transient StudyService _studyService;
105 * The SearchService. Later injected by Spring.
108 @Qualifier("searchService")
109 private transient SearchService _searchService;
112 * The UserDAO. Later injected by Spring.
115 @Qualifier("userDAO")
116 private transient UserDAO _userDAO;
119 * The ValidationCycleDAO. Later injected by Spring.
122 @Qualifier("validationCycleDAO")
123 private transient ValidationCycleDAO _validationCycleDAO;
126 * The SimulationContextService. Later injected by Spring.
129 @Qualifier("simulationContextService")
130 private transient SimulationContextService _simulationContextService;
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>
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>
148 * </i> <B>Test data : </B><BR>
149 * <i>no input parameters</i><BR>
151 * <B>Outcome results:</B><BR>
154 * <li>A study must be found according to the given criteria<BR>
159 * @throws BusinessException
160 * if can't create test user or study
163 public void testSelectStudiesWhere() throws BusinessException {
164 LOG.debug(">>>>> BEGIN testGenerateLocalIndex()");
165 startNestedTransaction();
167 HibernateTemplate ht = getHibernateTemplate();
169 Database.getInstance().reset();
170 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
171 // Load workflow customization
173 _projectSettings.configure(ClassLoader.getSystemResource(
174 "test/som.xml").getPath());
175 } catch (Exception e) {
176 Assert.fail("Can't load som.xml: ", e);
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);
186 // Create private study
187 Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
188 aStudy.setTitle("0.This is private study");
189 Long privateStudyId = _studyDAO.create(aStudy);
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);
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);
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);
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);
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);
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);
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);
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
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
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);
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);
280 // Search a study by simulation context
281 filter = new StudySearchFilterDTO();
282 filter.setConnectedUserId(goodUser.getIndex());
283 List<SimulationContext> contexts = new ArrayList<SimulationContext>();
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();
294 while (ctxIter.hasNext()) {
295 contexts.add(ctxIter.next());
297 Assert.assertEquals(contexts.size(), 1);
298 Assert.assertEquals(contexts.get(0), ctx);
300 rollbackNestedTransaction();
301 LOG.debug(">>>>> END testGenerateLocalIndex()");