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.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;
44 import test.splat.common.BaseTest;
45 import test.splat.util.TestEntitiesGenerator;
48 * Test class for StudyService.
50 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
53 public class TestSearchService extends BaseTest {
56 * Logger for the class.
58 private static final AppLogger LOG = AppLogger
59 .getLogger(TestSearchService.class);
62 * The StudyDAO. Later injected by Spring.
65 @Qualifier("studyDAO")
66 private transient StudyDAO _studyDAO;
69 * The PublicationService. Later injected by Spring.
72 @Qualifier("publicationService")
73 private transient PublicationService _publicationService;
76 * The StepService. Later injected by Spring.
79 @Qualifier("stepService")
80 private transient StepService _stepService;
83 * The ProjectSettingsService. Later injected by Spring.
86 @Qualifier("projectSettings")
87 private transient ProjectSettingsService _projectSettings;
90 * The DocumentTypeService. Later injected by Spring.
93 @Qualifier("documentTypeService")
94 private transient DocumentTypeService _documentTypeService;
97 * The StudyService. Later injected by Spring.
100 @Qualifier("studyService")
101 private transient StudyService _studyService;
104 * The SearchService. Later injected by Spring.
107 @Qualifier("searchService")
108 private transient SearchService _searchService;
111 * The UserDAO. Later injected by Spring.
114 @Qualifier("userDAO")
115 private transient UserDAO _userDAO;
118 * The ValidationCycleDAO. Later injected by Spring.
121 @Qualifier("validationCycleDAO")
122 private transient ValidationCycleDAO _validationCycleDAO;
125 * The SimulationContextService. Later injected by Spring.
128 @Qualifier("simulationContextService")
129 private transient SimulationContextService _simulationContextService;
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>
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>
147 * </i> <B>Test data : </B><BR>
148 * <i>no input parameters</i><BR>
150 * <B>Outcome results:</B><BR>
153 * <li>A study must be found according to the given criteria<BR>
158 * @throws BusinessException
159 * if can't create test user or study
162 public void testSelectStudiesWhere() throws BusinessException {
163 LOG.debug(">>>>> BEGIN testGenerateLocalIndex()");
164 startNestedTransaction();
166 HibernateTemplate ht = getHibernateTemplate();
168 Database.getInstance().reset();
169 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
170 // Load workflow customization
172 _projectSettings.configure(ClassLoader.getSystemResource(
173 "test/som.xml").getPath());
174 } catch (Exception e) {
175 Assert.fail("Can't load som.xml: ", e);
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);
185 // Create private study
186 Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
187 aStudy.setTitle("0.This is private study");
188 Long privateStudyId = _studyDAO.create(aStudy);
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);
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);
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);
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);
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);
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);
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);
242 // Search for other logged in study contributor
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
252 // Search for logged in study contributor
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
262 // Search by study title contents
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);
271 // Search by study reference
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);
279 // Search a study by simulation context
281 sprop.setActor(goodUser);
282 List<SimulationContext> contexts = new ArrayList<SimulationContext>();
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();
293 while (ctxIter.hasNext()) {
294 contexts.add(ctxIter.next());
296 Assert.assertEquals(contexts.size(), 1);
297 Assert.assertEquals(contexts.get(0), ctx);
299 rollbackNestedTransaction();
300 LOG.debug(">>>>> END testGenerateLocalIndex()");