From 5f91ad46ebaeb56c63b31360a63f35a1c63b25c2 Mon Sep 17 00:00:00 2001 From: mka Date: Thu, 12 Dec 2013 13:29:59 +0000 Subject: [PATCH] Fix for some problems with definition uses. --- .../splat/service/TestPublicationService.java | 386 ++++++++++++------ 1 file changed, 269 insertions(+), 117 deletions(-) diff --git a/Workspace/Siman-Common/src/test/splat/service/TestPublicationService.java b/Workspace/Siman-Common/src/test/splat/service/TestPublicationService.java index 04f2b07..0df8754 100644 --- a/Workspace/Siman-Common/src/test/splat/service/TestPublicationService.java +++ b/Workspace/Siman-Common/src/test/splat/service/TestPublicationService.java @@ -37,6 +37,8 @@ import org.splat.dal.bo.som.Publication; import org.splat.dal.bo.som.Scenario; import org.splat.dal.bo.som.Study; import org.splat.dal.bo.som.Document.Properties; +import org.splat.dal.bo.som.UsedByRelation; +import org.splat.dal.bo.som.UsesRelation; import org.splat.dal.dao.kernel.UserDAO; import org.splat.dal.dao.som.Database; import org.splat.dal.dao.som.ScenarioDAO; @@ -378,123 +380,123 @@ public class TestPublicationService extends BaseTest { } /** - * Test of generating a study document index.
- * Description :
- * Create a study and try to generate the next document index.
- * Action :
- * 1. call DAO's read method for an existing id.
- * Test data :
- * no input parameters
- * - * Outcome results:
- * - * - * - * - * @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 SQLException - * if test study creation is failed - * @throws IOException - * if test study creation is failed - * @throws ParseException - * @throws InterruptedException - * @throws NotApplicableException - * - */ + * Test of generating a study document index.
+ * Description :
+ * Create a study and try to generate the next document index.
+ * Action :
+ * 1. call DAO's read method for an existing id.
+ * Test data :
+ * no input parameters
+ * + * Outcome results:
+ * + * + * + * + * @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 SQLException + * if test study creation is failed + * @throws IOException + * if test study creation is failed + * @throws ParseException + * @throws InterruptedException + * @throws NotApplicableException + * + */ @Test - public void testCreateDoc() throws InvalidPropertyException, - MissedPropertyException, MultiplyDefinedException, IOException, - SQLException, NotApplicableException, InterruptedException, - ParseException { - LOG.debug(">>>>> BEGIN testCreateDoc()"); - startNestedTransaction(); - - HibernateTemplate ht = getHibernateTemplate(); - Study aStudy = createStudy(); - // Call DAO's create method for a good transient study. - Long id = aStudy.getIndex(); - Assert.assertNotNull(id, - "Create method returns null instead of a new id."); - Assert.assertTrue(id > 0, "The new id is not a positive number."); - - // Call DAO's get method for an existing id. - _studyDAO.flush(); - getHibernateTemplate().evict(aStudy); - getHibernateTemplate().clear(); - Study aStudyFound = _studyDAO.get(id); - ht.evict(aStudyFound); - - long userId = aStudyFound.getAuthor().getIndex(); - org.splat.som.Step[] studySteps = _projectElementService - .getSteps(aStudyFound); - org.splat.som.Step[] scSteps = _projectElementService - .getSteps(aStudyFound.getScenarii()[0]); - - List steps = new ArrayList(); - steps.addAll(Arrays.asList(studySteps)); - steps.addAll(Arrays.asList(scSteps)); - steps.addAll(Arrays.asList(studySteps)); - DecimalFormat tostring = new DecimalFormat(Document.suformat); - for (org.splat.som.Step step : steps) { - LOG.debug("Create a document for the step " + step.getNumber()); - - List dtypes = _documentTypeService.selectTypesOf(step - .getStep()); - if (dtypes.size() > 0) { - DocumentType dtype = dtypes.get(0); - int oldInd = ht.get(Study.class, aStudyFound.getIndex()) - .getLastLocalIndex(); - - // Create a file in the download directory - String fname = "filename" + step.getNumber() + ".py"; - String filePath = getDownloadPath(aStudyFound.getAuthor()) - + fname; - LOG.debug(step.getStep().getKey() + ": " - + step.getOwner().getClass().getSimpleName() - + ": Create test file: " + filePath); - FileWriter fw = new FileWriter(filePath); - fw - .write("Simulation of filename.py file for creating a new document at " - + new Date()); - fw.close(); - - Publication addoc = _publicationService.createDoc(aStudyFound - .getIndex(), step, dtype.getIndex(), userId, fname, - "tstdoc", ProgressState.inWORK, "", "", new Date(), - null); - - _studyDAO.flush(); - ht.flush(); - - int ind = ht.get(Study.class, aStudyFound.getIndex()) - .getLastLocalIndex(); - - LOG.debug("Previous index: " + oldInd + ", new index: " + ind); - Assert.assertEquals(ht.get(Study.class, aStudyFound.getIndex()) - .getLastLocalIndex(), oldInd + 1, - "Incremented index must be saved in the database."); - aStudy = (Study) ht.find( - "from Study where rid = " + aStudyFound.getIndex()) - .get(0); - Assert.assertEquals(aStudy.getLastLocalIndex(), oldInd + 1, - "Incremented index must be saved in the database."); - Assert.assertEquals(addoc.getSourceFile().getName(), tostring - .format(aStudy.getLastLocalIndex()) - + "_" + fname, "The generated file name is incorrect."); - } - } - - rollbackNestedTransaction(); - LOG.debug(">>>>> END testCreateDoc()"); - } + public void testCreateDoc() throws InvalidPropertyException, + MissedPropertyException, MultiplyDefinedException, IOException, + SQLException, NotApplicableException, InterruptedException, + ParseException { + LOG.debug(">>>>> BEGIN testCreateDoc()"); + startNestedTransaction(); + + HibernateTemplate ht = getHibernateTemplate(); + Study aStudy = createStudy(); + // Call DAO's create method for a good transient study. + Long id = aStudy.getIndex(); + Assert.assertNotNull(id, + "Create method returns null instead of a new id."); + Assert.assertTrue(id > 0, "The new id is not a positive number."); + + // Call DAO's get method for an existing id. + _studyDAO.flush(); + getHibernateTemplate().evict(aStudy); + getHibernateTemplate().clear(); + Study aStudyFound = _studyDAO.get(id); + ht.evict(aStudyFound); + + long userId = aStudyFound.getAuthor().getIndex(); + org.splat.som.Step[] studySteps = _projectElementService + .getSteps(aStudyFound); + org.splat.som.Step[] scSteps = _projectElementService + .getSteps(aStudyFound.getScenarii()[0]); + + List steps = new ArrayList(); + steps.addAll(Arrays.asList(studySteps)); + steps.addAll(Arrays.asList(scSteps)); + steps.addAll(Arrays.asList(studySteps)); + DecimalFormat tostring = new DecimalFormat(Document.suformat); + for (org.splat.som.Step step : steps) { + LOG.debug("Create a document for the step " + step.getNumber()); + + List dtypes = _documentTypeService.selectTypesOf(step + .getStep()); + if (dtypes.size() > 0) { + DocumentType dtype = dtypes.get(0); + int oldInd = ht.get(Study.class, aStudyFound.getIndex()) + .getLastLocalIndex(); + + // Create a file in the download directory + String fname = "filename" + step.getNumber() + ".py"; + String filePath = getDownloadPath(aStudyFound.getAuthor()) + + fname; + LOG.debug(step.getStep().getKey() + ": " + + step.getOwner().getClass().getSimpleName() + + ": Create test file: " + filePath); + FileWriter fw = new FileWriter(filePath); + fw + .write("Simulation of filename.py file for creating a new document at " + + new Date()); + fw.close(); + + Publication addoc = _publicationService.createDoc(aStudyFound + .getIndex(), step, dtype.getIndex(), userId, fname, + "tstdoc", ProgressState.inWORK, "", "", new Date(), + null); + + _studyDAO.flush(); + ht.flush(); + + int ind = ht.get(Study.class, aStudyFound.getIndex()) + .getLastLocalIndex(); + + LOG.debug("Previous index: " + oldInd + ", new index: " + ind); + Assert.assertEquals(ht.get(Study.class, aStudyFound.getIndex()) + .getLastLocalIndex(), oldInd + 1, + "Incremented index must be saved in the database."); + aStudy = (Study) ht.find( + "from Study where rid = " + aStudyFound.getIndex()) + .get(0); + Assert.assertEquals(aStudy.getLastLocalIndex(), oldInd + 1, + "Incremented index must be saved in the database."); + Assert.assertEquals(addoc.getSourceFile().getName(), tostring + .format(aStudy.getLastLocalIndex()) + + "_" + fname, "The generated file name is incorrect."); + } + } + + rollbackNestedTransaction(); + LOG.debug(">>>>> END testCreateDoc()"); + } /** * Test of file attaching to a study document.
@@ -813,6 +815,156 @@ public class TestPublicationService extends BaseTest { LOG.debug(">>>>> END testReplace()"); } + /** + * Test testUpdateRelations method for updating relations at versioning + * document.
+ * Description :
+ * Create a study and a scenario with documents and try to update + * relations for every document in Study or Scenario.
+ *

+ * Action :
+ * 1. call the method for the every document in the Study or + * Scenario.
+ * Test data :
+ * no input parameters
+ * + * Outcome results:
+ * + *
    + *
  • 1: Update relations for every document:
    + * Delete old UsedBy relation from old version and create new UsedBy + * relation to new version document. Create new Uses relation from new + * version document
  • + *
+ *
+ * + * @throws BusinessException + * if test data creation is failed + * @throws IOException + * if application configuration loading or test data creation is + * failed + * @throws SQLException + * if application configuration loading or test data creation is + * failed + */ + @Test + public final void testUpdateRelations() throws + BusinessException, IOException, SQLException { + LOG.debug(">>>>> BEGIN testUpdateRelations()"); + startNestedTransaction(); + + HibernateTemplate ht = getHibernateTemplate(); + + Database.getInstance().reset(); + _projectSettings.getAllSteps().clear(); // Clear config to be able to + // load it again + _projectSettings.configure("classpath:test/som.xml"); + + User goodUser = TestEntitiesGenerator.getTestUser("GoodUser"); + _userDAO.create(goodUser); + + // Create private study + Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser); + aStudy.setTitle("0.This is private study"); + Long studyId = _studyDAO.create(aStudy); + + // Add a scenario to the study + Scenario scen = TestEntitiesGenerator.getTestScenario(aStudy); + Long scenId = _scenarioDAO.create(scen); + ht.flush(); + + org.splat.som.Step[] studySteps = _projectElementService + .getSteps(aStudy); + org.splat.som.Step[] scSteps = _projectElementService.getSteps(scen); + + List steps = new ArrayList(); + steps.addAll(Arrays.asList(studySteps)); + steps.addAll(Arrays.asList(scSteps)); + Publication[] currentArray = new Publication[steps.size() * 2 - 2]; + Publication[] nextArray = new Publication[steps.size() * 2 - 2]; + Map docusesMap = new HashMap(); + Map docusedMap = new HashMap(); + int pubNumber = 0; + for (org.splat.som.Step step : steps) { + List dtypes = _documentTypeService.selectTypesOf(step + .getStep()); + if (dtypes.size() > 0) { + DocumentType dtype = dtypes.get(0); + if (step.getOwner() instanceof Study) { + currentArray[pubNumber] = addDoc(aStudy, step, "current" + + pubNumber, dtype); + currentArray[pubNumber + 1] = addDoc(aStudy, step, + "current" + (pubNumber + 1), dtype); + + } else { + currentArray[pubNumber] = addDoc(scen, step, "current" + + pubNumber, dtype); + currentArray[pubNumber + 1] = addDoc(scen, step, "current" + + (pubNumber + 1), dtype); + } + + pubNumber += 2; + } + } + + ht.flush(); + ht.update(aStudy); + ht.flush(); + + for (Integer numPub = currentArray.length - 1; numPub >= 0; numPub--) { + long[] docusedby = new long[1]; + if (numPub != 0) { + currentArray[numPub].addDependency(currentArray[numPub - 1]); + } + if (numPub != currentArray.length - 1) { + docusedby[0] = currentArray[numPub + 1].getIndex(); + docusedMap.put(new Integer(numPub), docusedby); + } + } + + for (int numPub = 0; numPub < currentArray.length; numPub++) { + String[] docuses = new String[1]; + nextArray[numPub] = addDoc(aStudy, currentArray[numPub].getStep(), + "next" + numPub, currentArray[numPub].value().getType()); + ht.flush(); + if (numPub != 0) { + docuses[0] = String.valueOf(nextArray[numPub - 1].value() + .getIndex()); + docusesMap.put(numPub, docuses); + } + _publicationService.updateRelations(currentArray[numPub].getStep(), + currentArray[numPub], nextArray[numPub], + docusesMap.get(numPub), docusedMap.get(numPub)); + } + + //Action + for (int numPub = 0; numPub < currentArray.length; numPub++) { + if (numPub != currentArray.length - 1) { + + Assert.assertEquals( + currentArray[numPub].getRelations(UsedByRelation.class), + new ArrayList()); + Assert.assertTrue(nextArray[numPub].getRelations( + UsedByRelation.class) + .contains(currentArray[numPub + 1])); + Assert.assertTrue(nextArray[numPub].getRelations( + UsedByRelation.class).contains(nextArray[numPub + 1])); + } + + if (numPub != 0) { + Assert.assertEquals( + currentArray[numPub].getRelations(UsesRelation.class) + .get(0), nextArray[numPub - 1]); + Assert.assertEquals( + nextArray[numPub].getRelations(UsesRelation.class).get( + 0), nextArray[numPub - 1]); + } + } + rollbackNestedTransaction(); + LOG.debug(">>>>> END testUpdateRelations()"); + } + + /** * Find a document publication in the project element. * @@ -858,4 +1010,4 @@ public class TestPublicationService extends BaseTest { in.close(); return res.toString(); } -} +} \ No newline at end of file -- 2.39.2