import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.splat.dal.bo.som.Document.Properties;
import org.splat.dal.dao.som.Database;
import org.splat.dal.dao.som.ScenarioDAO;
+import org.splat.exception.BusinessException;
+import org.splat.exception.DocumentIsUsedException;
import org.splat.kernel.InvalidPropertyException;
-import org.splat.kernel.MismatchException;
import org.splat.kernel.MissedPropertyException;
import org.splat.kernel.MultiplyDefinedException;
-import org.splat.kernel.NotApplicableException;
import org.splat.log.AppLogger;
import org.splat.service.DocumentTypeService;
-import org.splat.service.KnowledgeElementTypeService;
-import org.splat.service.ProjectElementService;
import org.splat.service.PublicationService;
-import org.splat.service.ScenarioService;
-import org.splat.service.SimulationContextService;
import org.splat.service.StepService;
import org.splat.service.StudyService;
import org.splat.service.technical.ProjectSettingsService;
-import org.splat.service.technical.RepositoryService;
import org.splat.service.technical.ProjectSettingsService.Step;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@Qualifier("studyService")
private transient StudyService _studyService;
- /**
- * The ScenarioService. Later injected by Spring.
- */
- @Autowired
- @Qualifier("scenarioService")
- private transient ScenarioService _scenarioService;
-
- /**
- * The ProjectElementService. Later injected by Spring.
- */
- @Autowired
- @Qualifier("projectElementService")
- private transient ProjectElementService _projectElementService;
-
- /**
- * The RepositoryService. Later injected by Spring.
- */
- @Autowired
- @Qualifier("repositoryService")
- private transient RepositoryService _repositoryService;
-
/**
* The Scenario DAO. Later injected by Spring.
*/
@Qualifier("stepService")
private transient StepService _stepService;
- /**
- * The SimulationContextService. Later injected by Spring.
- */
- @Autowired
- @Qualifier("simulationContextService")
- private transient SimulationContextService _simulationContextService;
-
/**
* The ProjectSettingsService. Later injected by Spring.
*/
private transient DocumentTypeService _documentTypeService;
/**
- * The KnowledgeElementTypeService. Later injected by Spring.
- */
- @Autowired
- @Qualifier("knowledgeElementTypeService")
- private transient KnowledgeElementTypeService _knowledgeElementTypeService;
-
- /**
- * Test check-in scenario operation to be performed after SALOME session.<BR>
+ * Test removeDocument method.<BR>
* <B>Description :</B> <BR>
- * <i>Create a scenario and try to check-in it with some simulated SALOME results data.<BR>
- * After check-in verify following points:
- * <ul>
- * <li>scenario is no more marked as checked out</li>
- * <li>new document versions are created for checked in documents</li>
- * <li>presentation of the previous version is removed</li>
- * <li>uses relations are copied correctly</li>
- * <li>files are moved correctly</li>
- * <li>formats of files are new if they are according to the document's type on the study step</li>
- * <li>new documents are created for new data</li>
- * <li>new documents have correctly generated names</li>
- * <li>uses relations are created correctly</li>
- * <li>files are moved correctly</li>
- * </ul>
+ * <i>Create a scenario and try to remove documents from it.<BR>
* </i><BR>
* <B>Action : </B><BR>
- * <i>1. call the method for an existing scenario id.</i><BR>
- * <i>2. call the method for a not existing scenario id.</i><BR>
+ * <i>1. call the method for all documents used by other document(s).</i><BR>
+ * <i>2. call the method for all documents starting from the last activity.</i><BR>
* <B>Test data : </B><BR>
* <i>no input parameters</i><BR>
* <i>no input parameters</i><BR>
* <B>Outcome results:</B><BR>
* <i>
* <ul>
- * <li>New version of existing documents must be created and new documents must be imported for documents with zero id. Correct
- * relations must be created.<BR>
+ * <li>Exception DocumentIsUsedException is expected<BR>
* </li>
- * <li>Exception is thrown<BR>
+ * <li>All documents and their files and relations must be removed<BR>
* </li>
* </ul>
* </i>
*
- * @throws InvalidPropertyException
- * if an invalid property is used when creating objects
- * @throws MultiplyDefinedException
- * when trying to create an object with already existing id
- * @throws MissedPropertyException
- * if a mandatory property is not defined for an object to be created
+ * @throws BusinessException
+ * if scenario creation or checkin is failed
* @throws IOException
* if scenario creation is failed
* @throws SQLException
* if scenario creation is failed
- * @throws NotApplicableException
- * if checkin failed
- * @throws MismatchException
- * if checkin failed
*/
@Test
- public void testRemoveDocument() throws InvalidPropertyException,
- MissedPropertyException, MultiplyDefinedException, IOException,
- SQLException, MismatchException, NotApplicableException {
+ public void testRemoveDocument() throws BusinessException, IOException,
+ SQLException {
LOG.debug(">>>>> BEGIN testRemoveDocument()");
startNestedTransaction();
_scenarioDAO.flush();
Scenario aScen = _scenarioDAO.get(scenarioId);
+ ht.evict(aScen);
+ ht.evict(aScen.getOwnerStudy());
+
Assert.assertTrue(ht.find("from UsesRelation").size() > 0,
"Uses relations were not created in the database.");
Assert.assertTrue(ht.find("from UsedByRelation").size() > 0,
"UsedBy relations were not created in the database.");
- // ////////////////////////////////////////////////////////
- // Call removeDocument method for each document of the
- // study/scenario starting from the last document.
-
ProjectElement projElem;
- boolean ok;
+ boolean ok = false;
long docId;
Step step;
+ int nbDoc = ht.find("from Document").size();
+ int nbRemovedDoc = 0;
+ List<Long> removedDocs = new ArrayList<Long>();
+
+ // ////////////////////////////////////////////////////////
+ // Call removeDocument method for each document
+ // used by other document(s).
+ // Exception DocumentIsUsedException is expected
+ for (int i = 1; i <= _projectSettings.getAllSteps().size(); i++) {
+ LOG.debug("Remove used documents from the step " + i);
+ step = _projectSettings.getStep(i);
+ if (step.appliesTo(Study.class)) {
+ projElem = _studyService.selectStudy(aScen.getOwnerStudy()
+ .getIndex());
+ } else {
+ projElem = aScen;
+ }
+
+ org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
+
+ if (aScStep.getDocuments().size() > 0) {
+ docId = aScStep.getDocuments().get(0).value().getIndex();
+ ht.flush();
+ List<UsesRelation> uses = ht
+ .find("from UsesRelation where owner=" + docId);
+ List<UsedByRelation> usedBy = ht
+ .find("from UsedByRelation where owner=" + docId);
+ if (!usedBy.isEmpty()) {
+ int usesRefer = ht.find(
+ "from UsesRelation where refer=" + docId).size();
+ int usedByRefer = ht.find(
+ "from UsedByRelation where refer=" + docId).size();
+ int convertsNb = ht.find(
+ "from ConvertsRelation where owner=" + docId)
+ .size();
+ LOG.debug("Remove used document "
+ + aScStep.getDocument(docId).value().getTitle());
+ LOG.debug("From db: It uses following " + uses.size()
+ + " documents: ");
+ for (UsesRelation rel : uses) {
+ LOG.debug(rel.getTo().getTitle());
+ }
+ LOG
+ .debug("From step: It uses following "
+ + aScStep.getDocument(docId).value()
+ .getRelations(UsesRelation.class)
+ .size() + " documents: ");
+ for (Publication rel : aScStep.getDocument(docId)
+ .getRelations(UsesRelation.class)) {
+ LOG.debug(rel.value().getTitle());
+ }
+ LOG.debug("From db: It is used by following "
+ + usedBy.size() + " documents: ");
+ for (UsedByRelation rel : usedBy) {
+ LOG.debug(rel.getTo().getTitle());
+ }
+ LOG.debug("From step: It is used by following "
+ + aScStep.getDocument(docId).value().getRelations(
+ UsedByRelation.class).size()
+ + " documents: ");
+ for (Publication rel : aScStep.getDocument(docId)
+ .getRelations(UsedByRelation.class)) {
+ LOG.debug(rel.value().getTitle());
+ }
+ try {
+ ok = _stepService.removeDocument(aScStep, docId);
+ Assert.fail("DocumentIsUsedException must be thrown.");
+ } catch (DocumentIsUsedException e) {
+ LOG.debug("Expected exception is thrown: "
+ + e.getLocalizedMessage());
+ }
+ _scenarioDAO.flush();
+ ht.flush();
+ Assert
+ .assertEquals(ht.find(
+ "from UsesRelation where owner=" + docId)
+ .size(), uses.size(),
+ "UsesRelation(s) must not be changed.");
+ Assert.assertEquals(ht.find(
+ "from UsedByRelation where owner=" + docId).size(),
+ usedBy.size(),
+ "UsedByRelation(s) must not be changed.");
+ Assert.assertEquals(ht.find(
+ "from UsesRelation where refer=" + docId).size(),
+ usesRefer,
+ "Referencing UsesRelation(s) must not be changed.");
+ Assert
+ .assertEquals(ht.find(
+ "from UsedByRelation where refer=" + docId)
+ .size(), usedByRefer,
+ "Referencing UsedByRelation(s) must not be changed.");
+ Assert.assertEquals(ht.find(
+ "from ConvertsRelation where owner=" + docId)
+ .size(), convertsNb,
+ "ConvertsRelation(s) must not be changed.");
+ }
+ }
+ }
+
+ // ////////////////////////////////////////////////////////
+ // Call removeDocument method for each document
+ // starting from the last activity.
for (int i = _projectSettings.getAllSteps().size(); i > 0; i--) {
LOG.debug("Remove documents from the step " + i);
step = _projectSettings.getStep(i);
if (aScStep.getDocuments().size() > 0) {
docId = aScStep.getDocuments().get(0).value().getIndex();
- LOG.debug("Remove document "
+ LOG.debug("Remove document#" + docId + " "
+ aScStep.getDocument(docId).value().getTitle());
ht.flush();
List<UsesRelation> uses = ht
UsedByRelation.class)) {
LOG.debug(rel.value().getTitle());
}
+ Assert.assertEquals(ht.find("from Document where rid=" + docId)
+ .size(), 1, "Nothing to delete.");
+
ok = _stepService.removeDocument(aScStep, docId);
+ nbRemovedDoc++;
+ removedDocs.add(docId);
+
Assert.assertTrue(ok, "Removing was failed.");
_scenarioDAO.flush();
ht.flush();
+
+ for (Long id : removedDocs) {
+ String title = "";
+ if (ht.get(Document.class, id) != null) {
+ title = ht.get(Document.class, id).getTitle();
+ }
+ Assert.assertEquals(ht
+ .find("from Document where rid=" + id).size(), 0,
+ "Document#" + id + "(" + title
+ + ") was not removed from the database.");
+ }
+
+ Assert.assertEquals(ht.find("from Document").size(), nbDoc
+ - nbRemovedDoc,
+ "Documents were not removed from the database.");
Assert.assertEquals(ht.find(
"from UsesRelation where owner=" + docId).size(), 0,
"UsesRelation(s) were not removed from the database.");
"from UsedByRelation where owner=" + docId)
.size(), 0,
"UsedByRelation(s) were not removed from the database.");
- Assert.assertEquals(ht.find(
- "from UsesRelation where refer=" + docId).size(), 0,
- "Referencing UsesRelation(s) were not removed from the database.");
+ Assert
+ .assertEquals(ht.find(
+ "from UsesRelation where refer=" + docId)
+ .size(), 0,
+ "Referencing UsesRelation(s) were not removed from the database.");
Assert
.assertEquals(ht.find(
"from UsedByRelation where refer=" + docId)
.size(), 0,
"Referencing UsedByRelation(s) were not removed from the database.");
Assert
- .assertEquals(ht.find(
- "from ConvertsRelation where owner=" + docId)
- .size(), 0,
- "ConvertsRelation(s) were not removed from the database.");
+ .assertEquals(ht.find(
+ "from ConvertsRelation where owner=" + docId)
+ .size(), 0,
+ "ConvertsRelation(s) were not removed from the database.");
}
}
- Assert.assertEquals(ht.find("from Document").size(), 0,
- "Documents were not removed from the database.");
+ Assert
+ .assertEquals(ht.find("from Document").size(), nbDoc
+ - nbRemovedDoc,
+ "Documents were not removed from the database.");
+ Assert
+ .assertEquals(ht.find("from File").size(), 0,
+ "Files were not removed from the database.");
+
Assert.assertEquals(ht.find(
"from Publication where owner=" + aScen.getIndex()).size(), 0,
"Publications were not removed from the database.");