import java.util.Map;
import org.splat.dal.bo.kernel.User;
+import org.splat.dal.bo.som.ContributorRelation;
import org.splat.dal.bo.som.DescriptionAttribute;
import org.splat.dal.bo.som.Document;
import org.splat.dal.bo.som.DocumentType;
+import org.splat.dal.bo.som.KnowledgeElement;
+import org.splat.dal.bo.som.KnowledgeElementType;
+import org.splat.dal.bo.som.ProgressState;
import org.splat.dal.bo.som.ProjectElement;
import org.splat.dal.bo.som.Publication;
import org.splat.dal.bo.som.Scenario;
+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.Document.Properties;
import org.splat.dal.dao.kernel.UserDAO;
import org.splat.dal.dao.som.Database;
+import org.splat.dal.dao.som.KnowledgeElementDAO;
+import org.splat.dal.dao.som.KnowledgeElementTypeDAO;
+import org.splat.dal.dao.som.ScenarioDAO;
import org.splat.dal.dao.som.StudyDAO;
+import org.splat.dal.dao.som.ValidationCycleDAO;
import org.splat.exception.BusinessException;
import org.splat.exception.InvalidParameterException;
import org.splat.kernel.InvalidPropertyException;
import org.splat.log.AppLogger;
import org.splat.service.DocumentTypeService;
import org.splat.service.PublicationService;
+import org.splat.service.SimulationContextService;
import org.splat.service.StepService;
import org.splat.service.StudyService;
import org.splat.service.technical.ProjectSettingsService;
@Autowired
@Qualifier("studyService")
private transient StudyService _studyService;
-
+
/**
* The UserDAO. Later injected by Spring.
*/
@Qualifier("userDAO")
private transient UserDAO _userDAO;
+ /**
+ * The KnowledgeElementDAO. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("knowledgeElementDAO")
+ private transient KnowledgeElementDAO _knowledgeElementDAO;
+
+ /**
+ * The KnowledgeElementTypeDAO. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("knowledgeElementTypeDAO")
+ private transient KnowledgeElementTypeDAO _knowledgeElementTypeDAO;
+
+ /**
+ * The ScenarioDAO. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("scenarioDAO")
+ private transient ScenarioDAO _scenarioDAO;
+
+ /**
+ * The SimulationContextService. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("simulationContextService")
+ private transient SimulationContextService _simulationContextService;
+
+ /**
+ * The ValidationCycleDAO. Later injected by Spring.
+ */
+ @Autowired
+ @Qualifier("validationCycleDAO")
+ private transient ValidationCycleDAO _validationCycleDAO;
+
/**
* Create a persistent scenario for tests.
*
Assert.assertEquals(ind, ht.get(Study.class, aStudyFound.getIndex())
.getLastLocalIndex(),
"Incremented index must be saved in the database.");
- aStudy = (Study)ht.find(
+ aStudy = (Study) ht.find(
"from Study where rid = " + aStudyFound.getIndex()).get(0);
Assert.assertEquals(ind, aStudy.getLastLocalIndex(),
"Incremented index must be saved in the database.");
rollbackNestedTransaction();
LOG.debug(">>>>> END testGenerateLocalIndex()");
}
-
+
/**
* Test of retrieval of a study description.
- *
- * @throws BusinessException if there is something wrong likely unrelated to the tested method
+ *
+ * @throws BusinessException
+ * if there is something wrong likely unrelated to the tested method
*/
@Test
public void testGetDescription() throws BusinessException {
LOG.debug(">>>>> BEGIN testGetDescription()");
startNestedTransaction();
-
+
User user = TestEntitiesGenerator.getTestUser("GoodUser");
_userDAO.create(user);
Study study = TestEntitiesGenerator.getTestStudy(user);
_studyDAO.create(study);
_studyDAO.flush();
Long studyId = Long.valueOf(study.getIndex());
-
- //Empty description:
- Assert.assertNull(_studyService.getDescription(studyId),
+
+ // Empty description:
+ Assert.assertNull(_studyService.getDescription(studyId),
"returned value for study without description must be null");
-
- //Not empty description:
+
+ // Not empty description:
study.setAttribute(new DescriptionAttribute(study, "description"));
_studyDAO.update(study);
_studyDAO.flush();
- Assert.assertEquals("description", _studyService.getDescription(studyId));
-
- //null id
+ Assert.assertEquals("description", _studyService
+ .getDescription(studyId));
+
+ // null id
try {
_studyService.getDescription(null);
Assert.fail("retrieval with null study id must fail");
- } catch(InvalidParameterException e){
+ } catch (InvalidParameterException e) {
LOG.debug("Expected exception is thrown: "
- + e.getClass().getSimpleName() + ": " + e.getMessage());
+ + e.getClass().getSimpleName() + ": " + e.getMessage());
}
-
- //Non-existing id
+
+ // Non-existing id
Study tmpStudy = TestEntitiesGenerator.getTestStudy(user);
_studyDAO.create(tmpStudy);
Long nonExistingId = tmpStudy.getIndex();
try {
_studyService.getDescription(nonExistingId);
Assert.fail("retrieval with non-existing study id must fail");
- } catch(InvalidParameterException e){
+ } catch (InvalidParameterException e) {
LOG.debug("Expected exception is thrown: "
- + e.getClass().getSimpleName() + ": " + e.getMessage());
+ + e.getClass().getSimpleName() + ": " + e.getMessage());
}
rollbackNestedTransaction();
LOG.debug(">>>>> END testGetDescription()");
}
-
+
/**
* Test of setting of a study description.
- *
- * @throws BusinessException if there is something wrong likely unrelated to the tested method
+ *
+ * @throws BusinessException
+ * if there is something wrong likely unrelated to the tested method
*/
@Test
public void testSetDescription() throws BusinessException {
LOG.debug(">>>>> BEGIN testSetDescription()");
startNestedTransaction();
-
+
User user = TestEntitiesGenerator.getTestUser("GoodUser");
_userDAO.create(user);
Study study = TestEntitiesGenerator.getTestStudy(user);
_studyDAO.create(study);
_studyDAO.flush();
Long studyId = Long.valueOf(study.getIndex());
-
- //Setting description for study without any
+
+ // Setting description for study without any
_studyService.setDescription(studyId, "description");
- //Resetting description
+ // Resetting description
_studyService.setDescription(studyId, "replaced description");
-
- //null id
+
+ // null id
try {
_studyService.setDescription(null, "description");
Assert.fail("setting with null study id must fail");
- } catch(InvalidParameterException e){
+ } catch (InvalidParameterException e) {
LOG.debug("Expected exception is thrown: "
- + e.getClass().getSimpleName() + ": " + e.getMessage());
+ + e.getClass().getSimpleName() + ": " + e.getMessage());
}
-
- //Non-existing id
+
+ // Non-existing id
Study tmpStudy = TestEntitiesGenerator.getTestStudy(user);
_studyDAO.create(tmpStudy);
Long nonExistingId = tmpStudy.getIndex();
_studyDAO.delete(tmpStudy);
-
+
try {
_studyService.setDescription(nonExistingId, "description");
Assert.fail("retrieval with non-existing study id must fail");
- } catch(InvalidParameterException e){
+ } catch (InvalidParameterException e) {
LOG.debug("Expected exception is thrown: "
- + e.getClass().getSimpleName() + ": " + e.getMessage());
+ + e.getClass().getSimpleName() + ": " + e.getMessage());
}
rollbackNestedTransaction();
LOG.debug(">>>>> END testSetDescription()");
}
-
+
/**
* Test of removal of a study description.
- *
- * @throws BusinessException if there is something wrong likely unrelated to the tested method
+ *
+ * @throws BusinessException
+ * if there is something wrong likely unrelated to the tested method
*/
@Test
public void testRemoveDescription() throws BusinessException {
LOG.debug(">>>>> BEGIN testRemoveDescription()");
startNestedTransaction();
-
+
User user = TestEntitiesGenerator.getTestUser("GoodUser");
_userDAO.create(user);
Study study = TestEntitiesGenerator.getTestStudy(user);
_studyDAO.create(study);
_studyDAO.flush();
Long studyId = Long.valueOf(study.getIndex());
-
- //Empty description:
- Assert.assertFalse(_studyService.removeDescription(studyId),
+
+ // Empty description:
+ Assert.assertFalse(_studyService.removeDescription(studyId),
"returned value for study without description must be null");
-
- //Not empty description:
+
+ // Not empty description:
study.setAttribute(new DescriptionAttribute(study, "description"));
_studyDAO.update(study);
_studyDAO.flush();
- Assert.assertTrue(_studyService.removeDescription(studyId), "existing description removal must return true");
-
-
- Assert.assertNull(_studyService.getDescription(studyId), "description hasn't been successfully removed");
-
- //null id
+ Assert.assertTrue(_studyService.removeDescription(studyId),
+ "existing description removal must return true");
+
+ Assert.assertNull(_studyService.getDescription(studyId),
+ "description hasn't been successfully removed");
+
+ // null id
try {
_studyService.removeDescription(null);
Assert.fail("removal with null study id must fail");
- } catch(InvalidParameterException e){
+ } catch (InvalidParameterException e) {
LOG.debug("Expected exception is thrown: "
- + e.getClass().getSimpleName() + ": " + e.getMessage());
+ + e.getClass().getSimpleName() + ": " + e.getMessage());
}
-
- //Non-existing id
+
+ // Non-existing id
Study tmpStudy = TestEntitiesGenerator.getTestStudy(user);
_studyDAO.create(tmpStudy);
Long nonExistingId = tmpStudy.getIndex();
try {
_studyService.removeDescription(nonExistingId);
Assert.fail("removal with non-existing study id must fail");
- } catch(InvalidParameterException e){
+ } catch (InvalidParameterException e) {
LOG.debug("Expected exception is thrown: "
- + e.getClass().getSimpleName() + ": " + e.getMessage());
+ + e.getClass().getSimpleName() + ": " + e.getMessage());
}
rollbackNestedTransaction();
LOG.debug(">>>>> END testGetDescription()");
}
+
+ /**
+ * Test study removal.<BR>
+ * <B>Description :</B> <BR>
+ * <i>Delete a study.</i><BR>
+ * <B>Action : </B><BR>
+ * <i>1. call the method for a not existing study id expecting an exception.</i><BR>
+ * <i>2. call the method for an existing study id.</i><BR>
+ * <B>Test data : </B><BR>
+ * <i>no input parameters</i><BR>
+ *
+ * <B>Outcome results:</B><BR>
+ * <i>
+ * <ul>
+ * <li>1: Exception must be thrown.</li>
+ * <li>2: The study and all its objects must be removed.</li>
+ * </ul>
+ * </i>
+ *
+ * @throws BusinessException
+ * if test data creation is failed
+ */
+ @Test
+ public void testRemoveStudy() throws BusinessException {
+ LOG.debug(">>>>> BEGIN testRemoveStudy()");
+ 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("classpath:test/som.xml");
+ } 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);
+ KnowledgeElementType ktype1 = new KnowledgeElementType("testKType1");
+ KnowledgeElementType ktype2 = new KnowledgeElementType("testKType2");
+ ktype2.setState(ProgressState.inWORK);
+ _knowledgeElementTypeDAO.create(ktype1);
+ _knowledgeElementTypeDAO.create(ktype2);
+
+ // Create private study
+ Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
+ aStudy.setTitle("0.This is private study");
+ Long studyId = _studyDAO.create(aStudy);
+
+ // Add simulation context to the study
+ SimulationContext.Properties cprop = new SimulationContext.Properties();
+ cprop.setType(_simulationContextService.selectType("product"))
+ .setValue("Test Simulation Context: Product");
+ SimulationContext ctx = _studyService.addProjectContext(aStudy, cprop);
+ ht.flush();
+
+ // Add a scenario to the study
+ Scenario scen = TestEntitiesGenerator.getTestScenario(aStudy);
+ _scenarioDAO.create(scen);
+ ht.flush();
+
+ // Add knowledge elements to the scenario
+ KnowledgeElement kelm01 = TestEntitiesGenerator
+ .getTestKnowledgeElement(scen, ktype1, "TestKelm01 title");
+ KnowledgeElement kelm02 = TestEntitiesGenerator
+ .getTestKnowledgeElement(scen, ktype2, "TestKelm02 title");
+ _knowledgeElementDAO.create(kelm01);
+ _knowledgeElementDAO.create(kelm02);
+ ht.flush();
+
+ // Add contributor relation
+ aStudy.addRelation(new ContributorRelation(aStudy, otherUser));
+ ht.flush();
+
+ // Add a validation cycle with otherUser as a reviewer
+ 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();
+
+ // Add documents to the first study activity
+ // Add a version relation
+ // Add a converts relation
+ // Add documents to the first scenario activity
+ // Add uses relations
+
+ ht.clear();
+ _studyService.removeStudy(studyId);
+ ht.flush();
+
+ // Check removal of the study
+ Study foundStudy = ht.get(Study.class, studyId);
+ Assert.assertNull(foundStudy);
+ // Check removal of relations to validation cycle
+ List found = ht.find("from ValidationCycleRelation where owner="
+ + studyId);
+ Assert.assertEquals(found.size(), 0,
+ "ValidationCycleRelation is not removed");
+ // Check removal of validation cycles
+ found = ht.find("from ValidationCycle where rid=" + cycle.getIndex());
+ Assert.assertEquals(found.size(), 0, "ValidationCycle is not removed");
+ // Check removal of contributors relation
+ found = ht.find("from ContributorRelation where owner=" + studyId);
+ Assert.assertEquals(found.size(), 0,
+ "ContributorRelation is not removed");
+ // Check that contributors are not removed
+ User foundUser = ht.get(User.class, otherUser.getIndex());
+ Assert.assertNotNull(foundUser);
+ // Check removal of the scenario
+ found = ht.find("from Scenario where owner=" + studyId);
+ Assert.assertEquals(found.size(), 0, "Scenario is not removed");
+ // Check removal of publications
+ found = ht
+ .find("from Publication pub left join pub.owner as own where own.rid="
+ + studyId + " or own.rid=" + scen.getIndex());
+ Assert.assertEquals(found.size(), 0, "Publication is not removed");
+ // Check removal of documents
+ // Check removal of version relations
+ // Check removal of converts relations
+ // Check removal of files
+ // Check removal of uses relations
+ // Check removal of usedBy relations
+
+ rollbackNestedTransaction();
+ LOG.debug(">>>>> END testRemoveStudy()");
+ }
}