+/*****************************************************************************
+ * Company OPEN CASCADE
+ * Application SIMAN
+ * File $Id$
+ * Creation date 12 Oct 2012
+ * @author $Author$
+ * @version $Revision$
+ *****************************************************************************/
+package test.splat.service;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.splat.dal.bo.kernel.User;
+import org.splat.dal.bo.som.ContributorRelation;
+import org.splat.dal.bo.som.DocumentType;
+import org.splat.dal.bo.som.SimulationContext;
+import org.splat.dal.bo.som.Study;
+import org.splat.dal.bo.som.ValidationCycle;
+import org.splat.dal.bo.som.ValidationCycleRelation;
+import org.splat.dal.bo.som.ValidationStep;
+import org.splat.dal.bo.som.Visibility;
+import org.splat.dal.dao.kernel.UserDAO;
+import org.splat.dal.dao.som.Database;
+import org.splat.dal.dao.som.StudyDAO;
+import org.splat.dal.dao.som.ValidationCycleDAO;
+import org.splat.exception.BusinessException;
+import org.splat.log.AppLogger;
+import org.splat.service.DocumentTypeService;
+import org.splat.service.PublicationService;
+import org.splat.service.SearchService;
+import org.splat.service.SimulationContextService;
+import org.splat.service.StepService;
+import org.splat.service.StudyService;
+import org.splat.service.dto.Proxy;
+import org.splat.service.technical.ProjectSettingsService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.orm.hibernate3.HibernateTemplate;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import test.splat.common.BaseTest;
+import test.splat.util.TestEntitiesGenerator;
+
+/**
+ * Test class for StudyService.
+ *
+ * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
+ *
+ */
+public class TestSearchService extends BaseTest {
+
+ /**
+ * Logger for the class.
+ */
+ private static final AppLogger LOG = AppLogger
+ .getLogger(TestSearchService.class);
+
+ /**
+ * The StudyDAO. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("studyDAO")
+ private transient StudyDAO _studyDAO;
+
+ /**
+ * The PublicationService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("publicationService")
+ private transient PublicationService _publicationService;
+
+ /**
+ * The StepService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("stepService")
+ private transient StepService _stepService;
+
+ /**
+ * The ProjectSettingsService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("projectSettings")
+ private transient ProjectSettingsService _projectSettings;
+
+ /**
+ * The DocumentTypeService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("documentTypeService")
+ private transient DocumentTypeService _documentTypeService;
+
+ /**
+ * The StudyService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("studyService")
+ private transient StudyService _studyService;
+
+ /**
+ * The SearchService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("searchService")
+ private transient SearchService _searchService;
+
+ /**
+ * The UserDAO. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("userDAO")
+ private transient UserDAO _userDAO;
+
+ /**
+ * The ValidationCycleDAO. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("validationCycleDAO")
+ private transient ValidationCycleDAO _validationCycleDAO;
+
+ /**
+ * The SimulationContextService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("simulationContextService")
+ private transient SimulationContextService _simulationContextService;
+
+ /**
+ * Test of searching a study.<BR>
+ * <B>Description :</B> <BR>
+ * <i>Create studies and try to find them according next document index.</i><BR>
+ * <B>Action : </B><BR>
+ * <i>
+ * <ol>
+ * <li>Search study by state</li>
+ * <li>Search study by reference</li>
+ * <li>Search study by author</li>
+ * <li>Search study by actor</li>
+ * <li>Search study by title</li>
+ * <li>Search study by simulation contexts</li>
+ * <li>Search study by all criteria</li>
+ * <li>Search study by any criteria</li>
+ * </ol>
+ * </i> <B>Test data : </B><BR>
+ * <i>no input parameters</i><BR>
+ *
+ * <B>Outcome results:</B><BR>
+ * <i>
+ * <ul>
+ * <li>A study must be found according to the given criteria<BR>
+ * </li>
+ * </ul>
+ * </i>
+ *
+ * @throws BusinessException
+ * if can't create test user or study
+ */
+ @Test
+ public void testSelectStudiesWhere() throws BusinessException {
+ LOG.debug(">>>>> BEGIN testGenerateLocalIndex()");
+ startNestedTransaction();
+
+ HibernateTemplate ht = getHibernateTemplate();
+
+ Database.getInstance().reset();
+ _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
+ // Load workflow customization
+ try {
+ _projectSettings.configure(ClassLoader.getSystemResource(
+ "test/som.xml").getPath());
+ } catch (Exception e) {
+ Assert.fail("Can't load som.xml: ", e);
+ }
+
+ User goodUser = TestEntitiesGenerator.getTestUser("goodUser");
+ _userDAO.create(goodUser);
+ User otherUser = TestEntitiesGenerator.getTestUser("otherUser");
+ _userDAO.create(otherUser);
+ User thirdUser = TestEntitiesGenerator.getTestUser("thirdUser");
+ _userDAO.create(thirdUser);
+
+ // Create private study
+ Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
+ aStudy.setTitle("0.This is private study");
+ Long privateStudyId = _studyDAO.create(aStudy);
+
+ // Add simulation context to the study 0
+ SimulationContext.Properties cprop = new SimulationContext.Properties();
+ cprop.setType(_simulationContextService.selectType("product"))
+ .setValue("Test Simulation Context: Product");
+ SimulationContext ctx = _studyService.addProjectContext(aStudy, cprop);
+ ht.flush();
+
+ // Create public study with predefined title
+ aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
+ aStudy.setVisibility(Visibility.PUBLIC);
+ aStudy.setTitle("2.This is a public study");
+ aStudy.addRelation(new ContributorRelation(aStudy, otherUser));
+ Long titleStudyId = _studyDAO.create(aStudy);
+
+ // Add simulation context to the study 2
+ SimulationContext.Properties cprop1 = new SimulationContext.Properties();
+ cprop.setType(_simulationContextService.selectType("product"))
+ .setValue("Test Simulation Context: Product1");
+ SimulationContext ctx1 = _studyService.addProjectContext(aStudy, cprop);
+ ht.flush();
+
+ // Create public study with predefined reference
+ aStudy = TestEntitiesGenerator.getTestStudy(otherUser);
+ aStudy.setVisibility(Visibility.PUBLIC);
+ aStudy.setTitle("1.This is another public study");
+ aStudy.setReference("TEST_REF");
+ Long refStudyId = _studyDAO.create(aStudy);
+
+ // Create private study of third user with otherUser as a reviewer
+ aStudy = TestEntitiesGenerator.getTestStudy(thirdUser);
+ aStudy.setTitle("3.This is a private study of third user");
+ Long reviewStudyId = _studyDAO.create(aStudy);
+
+ // Create validation cycle for the last study
+ ValidationCycle.Properties vprop = new ValidationCycle.Properties();
+ DocumentType type = _documentTypeService.selectType("specification");
+ vprop.setDocumentType(type);
+ vprop.setActor(ValidationStep.REVIEW, otherUser);
+ ValidationCycle cycle = new ValidationCycle(aStudy, vprop);
+ _validationCycleDAO.create(cycle);
+ ValidationCycleRelation link = cycle.getContext();
+ aStudy.addRelation(link);
+ ht.flush();
+
+ // Search by study author
+ Study.Properties sprop = new Study.Properties();
+ sprop.setManager(goodUser);
+ List<Proxy> res = _searchService.selectStudiesWhere(true, true, sprop);
+ Assert.assertNotNull(res);
+ Assert.assertEquals(res.size(), 1);
+ Assert.assertEquals(res.get(0).getIndex(), titleStudyId);
+
+ // Search for other logged in study contributor
+ sprop.clear();
+ sprop.setActor(otherUser);
+ res = _searchService.selectStudiesWhere(true, true, sprop);
+ Assert.assertNotNull(res);
+ Assert.assertEquals(res.size(), 3);
+ Assert.assertEquals(res.get(0).getIndex(), refStudyId); // Public study
+ Assert.assertEquals(res.get(1).getIndex(), titleStudyId); // Public study
+ Assert.assertEquals(res.get(2).getIndex(), reviewStudyId); // OtherUser is reviewer in this study
+
+ // Search for logged in study contributor
+ sprop.clear();
+ sprop.setActor(goodUser);
+ res = _searchService.selectStudiesWhere(true, true, sprop);
+ Assert.assertNotNull(res);
+ Assert.assertEquals(res.size(), 3);
+ Assert.assertEquals(res.get(0).getIndex(), privateStudyId); // goodUser is author
+ Assert.assertEquals(res.get(2).getIndex(), titleStudyId); // Public study
+ Assert.assertEquals(res.get(1).getIndex(), refStudyId); // Public study
+
+ // Search by study title contents
+ sprop.clear();
+ sprop.setTitle("study public");
+ res = _searchService.selectStudiesWhere(true, true, sprop);
+ Assert.assertNotNull(res);
+ Assert.assertEquals(res.size(), 2);
+ Assert.assertEquals(res.get(0).getIndex(), refStudyId);
+ Assert.assertEquals(res.get(1).getIndex(), titleStudyId);
+
+ // Search by study reference
+ sprop.clear();
+ sprop.setReference("TEST_REF");
+ res = _searchService.selectStudiesWhere(true, true, sprop);
+ Assert.assertNotNull(res);
+ Assert.assertEquals(res.size(), 1);
+ Assert.assertEquals(res.get(0).getIndex(), refStudyId);
+
+ // Search a study by simulation context
+ sprop.clear();
+ sprop.setActor(goodUser);
+ List<SimulationContext> contexts = new ArrayList<SimulationContext>();
+ contexts.add(ctx);
+ sprop.setSimulationContexts(contexts);
+ res = _searchService.selectStudiesWhere(true, true, sprop);
+ Assert.assertNotNull(res);
+ Assert.assertEquals(res.size(), 1);
+ Assert.assertEquals(res.get(0).getIndex(), privateStudyId);
+ // Check simualtion contexts of the found study
+ aStudy = _studyService.selectStudy(res.get(0).getIndex());
+ Iterator<SimulationContext> ctxIter = aStudy.SimulationContextIterator();
+ contexts.clear();
+ while (ctxIter.hasNext()) {
+ contexts.add(ctxIter.next());
+ }
+ Assert.assertEquals(contexts.size(), 1);
+ Assert.assertEquals(contexts.get(0), ctx);
+
+ rollbackNestedTransaction();
+ LOG.debug(">>>>> END testGenerateLocalIndex()");
+ }
+}