]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/test/splat/service/TestPublicationService.java
Salome HOME
Fix for some problems with definition uses.
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / service / TestPublicationService.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   12 Oct 2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9 package test.splat.service;
10
11 import java.io.BufferedReader;
12 import java.io.DataInputStream;
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.FileNotFoundException;
16 import java.io.FileWriter;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19 import java.sql.SQLException;
20 import java.text.DecimalFormat;
21 import java.text.ParseException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.splat.dal.bo.kernel.User;
31 import org.splat.dal.bo.som.ConvertsRelation;
32 import org.splat.dal.bo.som.Document;
33 import org.splat.dal.bo.som.DocumentType;
34 import org.splat.dal.bo.som.ProgressState;
35 import org.splat.dal.bo.som.ProjectElement;
36 import org.splat.dal.bo.som.Publication;
37 import org.splat.dal.bo.som.Scenario;
38 import org.splat.dal.bo.som.Study;
39 import org.splat.dal.bo.som.Document.Properties;
40 import org.splat.dal.bo.som.UsedByRelation;
41 import org.splat.dal.bo.som.UsesRelation;
42 import org.splat.dal.dao.kernel.UserDAO;
43 import org.splat.dal.dao.som.Database;
44 import org.splat.dal.dao.som.ScenarioDAO;
45 import org.splat.dal.dao.som.StudyDAO;
46 import org.splat.exception.BusinessException;
47 import org.splat.exception.IncompatibleDataException;
48 import org.splat.kernel.InvalidPropertyException;
49 import org.splat.kernel.MissedPropertyException;
50 import org.splat.kernel.MultiplyDefinedException;
51 import org.splat.kernel.NotApplicableException;
52 import org.splat.log.AppLogger;
53 import org.splat.service.DocumentTypeService;
54 import org.splat.service.ProjectElementService;
55 import org.splat.service.PublicationService;
56 import org.splat.service.StepService;
57 import org.splat.service.StudyService;
58 import org.splat.service.technical.ProjectSettingsService;
59 import org.splat.service.technical.RepositoryService;
60 import org.splat.service.technical.ProjectSettingsService.Step;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.beans.factory.annotation.Qualifier;
63 import org.springframework.orm.hibernate3.HibernateTemplate;
64 import org.testng.Assert;
65 import org.testng.annotations.Test;
66
67 import test.splat.common.BaseTest;
68 import test.splat.util.TestEntitiesGenerator;
69
70 /**
71  * Test class for PublicationService.
72  * 
73  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
74  * 
75  */
76 public class TestPublicationService extends BaseTest {
77
78         /**
79          * Logger for the class.
80          */
81         private static final AppLogger LOG = AppLogger
82                         .getLogger(TestPublicationService.class);
83
84         /**
85          * The StudyDAO. Later injected by Spring.
86          */
87         @Autowired
88         @Qualifier("studyDAO")
89         private transient StudyDAO _studyDAO;
90
91         /**
92          * The PublicationService. Later injected by Spring.
93          */
94         @Autowired
95         @Qualifier("publicationService")
96         private transient PublicationService _publicationService;
97
98         /**
99          * The ProjectElementService. Later injected by Spring.
100          */
101         @Autowired
102         @Qualifier("projectElementService")
103         private transient ProjectElementService _projectElementService;
104
105         /**
106          * The StepService. Later injected by Spring.
107          */
108         @Autowired
109         @Qualifier("stepService")
110         private transient StepService _stepService;
111
112         /**
113          * The ProjectSettingsService. Later injected by Spring.
114          */
115         @Autowired
116         @Qualifier("projectSettings")
117         private transient ProjectSettingsService _projectSettings;
118
119         /**
120          * The DocumentTypeService. Later injected by Spring.
121          */
122         @Autowired
123         @Qualifier("documentTypeService")
124         private transient DocumentTypeService _documentTypeService;
125
126         /**
127          * The StudyService. Later injected by Spring.
128          */
129         @Autowired
130         @Qualifier("studyService")
131         private transient StudyService _studyService;
132
133         /**
134          * The RepositoryService. Later injected by Spring.
135          */
136         @Autowired
137         @Qualifier("repositoryService")
138         private transient RepositoryService _repositoryService;
139
140         /**
141          * The UserDAO. Later injected by Spring.
142          */
143         @Autowired
144         @Qualifier("userDAO")
145         private transient UserDAO _userDAO;
146
147         /**
148          * The Scenario DAO. Later injected by Spring.
149          */
150         @Autowired
151         @Qualifier("scenarioDAO")
152         private transient ScenarioDAO _scenarioDAO;
153
154         /**
155          * Create a persistent scenario for tests.
156          * 
157          * @return a persistent scenario
158          * @throws InvalidPropertyException
159          *             if an invalid property is used when creating objects
160          * @throws MultiplyDefinedException
161          *             when trying to create an object with already existing id
162          * @throws MissedPropertyException
163          *             if a mandatory property is not defined for an object to be created
164          * @throws IOException
165          *             if document creation is failed
166          * @throws SQLException
167          *             if project settings loading is failed
168          */
169         private Study createStudy() throws InvalidPropertyException,
170                         MissedPropertyException, MultiplyDefinedException, IOException,
171                         SQLException {
172                 // Create a scenario for tests
173                 HibernateTemplate ht = getHibernateTemplate();
174
175                 Database.getInstance().reset();
176                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
177                 // Load workflow customization
178                 try {
179                         _projectSettings.configure("classpath:test/som.xml");
180                 } catch (FileNotFoundException e) {
181                         Assert.fail("Can't find som.xml: ", e);
182                 }
183                 List<Step> steps = _projectSettings.getAllSteps();
184                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
185
186                 // Create a test user
187                 User.Properties uprop = new User.Properties();
188                 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
189                                 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
190                                 .addRole("TST_user").setMailAddress(
191                                                 "noreply@salome-platform.org");
192                 uprop.disableCheck();
193                 User anAuthor = new User(uprop);
194                 ht.saveOrUpdate(anAuthor);
195
196                 // Create a test study
197                 Study.Properties stprops = new Study.Properties().setReference(
198                                 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
199                 Study aStudy = new Study(stprops);
200                 ht.saveOrUpdate(aStudy);
201
202                 // Create a test scenario
203                 Scenario.Properties sprops = new Scenario.Properties().setTitle(
204                                 "TST_Scenario").setManager(anAuthor).setOwnerStudy(aStudy);
205                 Scenario aScenario = new Scenario(sprops);
206                 aStudy.getScenariiList().add(aScenario);
207                 ht.saveOrUpdate(anAuthor);
208                 ht.saveOrUpdate(aStudy);
209                 ht.saveOrUpdate(aScenario);
210
211                 // Create documents for each scenario step
212                 Document.Properties dprop = new Document.Properties().setAuthor(
213                                 anAuthor).setDate(new Date());
214                 int i = 0;
215                 Publication usedPub = null;
216                 Map<Long, Long> usedMap = new HashMap<Long, Long>();
217                 for (int stepNum = 1; stepNum <= steps.size(); stepNum++) {
218                         Step step = _projectSettings.getStep(stepNum);
219                         LOG.debug("Create scenario step: " + stepNum);
220                         ProjectElement projElem;
221
222                         if (step.appliesTo(Study.class)) {
223                                 projElem = aStudy;
224                         } else {
225                                 projElem = aScenario;
226                         }
227                         org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
228                         List<DocumentType> dtypes = _documentTypeService
229                                         .selectTypesOf(step);
230                         if (dtypes.size() > 0) {
231                                 DocumentType dtype = dtypes.get(0);
232                                 // Create a document published in the scenario
233                                 // document<i>: document type[0] - first type used on the step
234                                 // <source-file>.brep
235                                 // <attached-file>.med
236                                 i++;
237                                 dprop.setName("document" + stepNum).setType(dtype);
238                                 if (step.getNumber() > 3) {
239                                         dprop.setFormat("med");
240                                 } else {
241                                         dprop.setFormat("py");
242                                 }
243                                 Publication pub = createDoc(projElem, aScStep, dprop, "med",
244                                                 false);
245                                 if (usedPub != null) {
246                                         pub.addDependency(usedPub);
247                                         LOG.debug("Add dependency: " + pub.value().getTitle()
248                                                         + " from " + usedPub.value().getTitle());
249                                         ht.saveOrUpdate(pub.value());
250                                         ht.flush();
251
252                                         usedMap.put(pub.getIndex(), usedPub.getIndex());
253                                 }
254                                 usedPub = pub;
255                         }
256                         if (dtypes.size() <= 0) {
257                                 LOG.debug("No document types are found for scenario step " + i);
258                         }
259                 }
260
261                 // Check that the scenario and its documents have been created correctly.
262
263                 Assert.assertNotNull(ht.find("from Document"),
264                                 "No documents in the database.");
265                 Assert.assertTrue(ht.find("from Document").size() > 0,
266                                 "No documents in the database.");
267
268                 Assert.assertNotNull(ht.find("from Publication where owner="
269                                 + aScenario.getIndex()), "No publications in the database.");
270                 Assert.assertTrue(
271                                 ht.find("from Publication where owner=" + aScenario.getIndex())
272                                                 .size() > 0, "No publications in the database.");
273
274                 for (Publication p : (List<Publication>) ht
275                                 .find("from Publication where owner=" + aScenario.getIndex())) {
276                         LOG.debug("Publication found: [id=" + p.getIndex() + ", owner="
277                                         + p.getOwner().getIndex() + ", doc=" + p.value().getIndex()
278                                         + "]");
279                         Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
280                                         "The publication was not attached to the scenario.");
281                 }
282
283                 // Remove the scenario from the current hibernate session.
284                 ht.evict(aScenario);
285                 // Check that the scenario is created in the database.
286                 Scenario aScen = ht.load(Scenario.class, aScenario.getIndex());
287                 Assert.assertNotNull(aScen, "Scenario was not saved in the database.");
288                 Assert.assertTrue(aScen.getDocums().size() > 0,
289                                 "No publications in the scenario.");
290
291                 Assert.assertTrue(i > 0,
292                                 "More then one document must be in the database");
293
294                 // Check created uses relations
295                 Assert
296                                 .assertTrue(usedMap.size() > 0,
297                                                 "Uses relations must be created.");
298                 boolean foundAny = false;
299                 for (Long usingId : usedMap.keySet()) {
300                         for (Publication pub : aScen.getDocums()) {
301                                 if (pub.getIndex() == usingId) {
302                                         boolean found = false;
303                                         for (Publication used : aScen.getDocums()) {
304                                                 found = (used.getIndex() == usedMap.get(usingId));
305                                                 if (found) {
306                                                         break;
307                                                 }
308                                         }
309                                         if (!found) {
310                                                 for (Publication used : aStudy.getDocums()) {
311                                                         found = (used.getIndex() == usedMap.get(usingId));
312                                                         if (found) {
313                                                                 break;
314                                                         }
315                                                 }
316                                         }
317                                         Assert.assertTrue(found,
318                                                         "Uses relation was not created in the database.");
319                                         foundAny = foundAny || found;
320                                 }
321                         }
322                 }
323                 Assert.assertTrue(foundAny,
324                                 "No Uses relation was created in the database.");
325
326                 return aScenario.getOwnerStudy();
327         }
328
329         /**
330          * Create a document published in the scenario. <BR>
331          * document:<BR>
332          * document type - type used on the step <BR>
333          * &lt;source-file&gt;.brep <BR>
334          * &lt;attached-file&gt;.med
335          * 
336          * @param aScenario
337          *            the scenario to add the document to
338          * @param aScStep
339          *            scenario step where the document to be published
340          * @param dprop
341          *            document properties
342          * @param attachedFileExt
343          *            extension of the secon attached (exported) file
344          * @param isOutdated
345          *            outdated document flag
346          * @return the publication of the created document
347          * @throws IOException
348          * @throws MultiplyDefinedException
349          * @throws InvalidPropertyException
350          * @throws MissedPropertyException
351          */
352         private Publication createDoc(final ProjectElement aScenario,
353                         final org.splat.som.Step aScStep, final Properties dprop,
354                         final String attachedFileExt, final boolean isOutdated)
355                         throws MissedPropertyException, InvalidPropertyException,
356                         MultiplyDefinedException, IOException {
357                 // Create a document published in the scenario
358                 // document<i>: document type - type used on the step
359                 // <source-file>.brep
360                 // <attached-file>.med
361                 Publication pub = _stepService.createDocument(aScStep, dprop);
362                 Assert.assertNotNull(pub.getOwner(),
363                                 "The publication must be attached to the scenario.");
364                 Assert.assertEquals(pub.getOwner().getIndex(), aScenario.getIndex(),
365                                 "The publication was not attached to the scenario.");
366
367                 if (isOutdated) {
368                         pub.setIsnew('O');
369                 }
370                 aScenario.add(pub);
371                 HibernateTemplate ht = getHibernateTemplate();
372                 ht.saveOrUpdate(pub);
373
374                 // Attach a file
375                 ht.save(pub.value());
376                 ht.flush();
377                 ht.saveOrUpdate(_publicationService.attach(pub, attachedFileExt));
378
379                 return pub;
380         }
381
382         /**
383      * Test of generating a study document index.<BR>
384      * <B>Description :</B> <BR>
385      * <i>Create a study and try to generate the next document index.</i><BR>
386      * <B>Action : </B><BR>
387      * <i>1. call DAO's read method for an existing id.</i><BR>
388      * <B>Test data : </B><BR>
389      * <i>no input parameters</i><BR>
390      * 
391      * <B>Outcome results:</B><BR>
392      * <i>
393      * <ul>
394      * <li>The new index must be equal to the incremented old one and saved into the database<BR>
395      * </li>
396      * </ul>
397      * </i>
398      * 
399      * @throws InvalidPropertyException
400      *             if an invalid property is used when creating objects
401      * @throws MultiplyDefinedException
402      *             when trying to create an object with already existing id
403      * @throws MissedPropertyException
404      *             if a mandatory property is not defined for an object to be created
405      * @throws SQLException
406      *             if test study creation is failed
407      * @throws IOException
408      *             if test study creation is failed
409      * @throws ParseException
410      * @throws InterruptedException
411      * @throws NotApplicableException
412      * 
413      */
414         @Test
415     public void testCreateDoc() throws InvalidPropertyException,
416             MissedPropertyException, MultiplyDefinedException, IOException,
417             SQLException, NotApplicableException, InterruptedException,
418             ParseException {
419         LOG.debug(">>>>> BEGIN testCreateDoc()");
420         startNestedTransaction();
421
422         HibernateTemplate ht = getHibernateTemplate();
423         Study aStudy = createStudy();
424         // Call DAO's create method for a good transient study.
425         Long id = aStudy.getIndex();
426         Assert.assertNotNull(id,
427                 "Create method returns null instead of a new id.");
428         Assert.assertTrue(id > 0, "The new id is not a positive number.");
429
430         // Call DAO's get method for an existing id.
431         _studyDAO.flush();
432         getHibernateTemplate().evict(aStudy);
433         getHibernateTemplate().clear();
434         Study aStudyFound = _studyDAO.get(id);
435         ht.evict(aStudyFound);
436
437         long userId = aStudyFound.getAuthor().getIndex();
438         org.splat.som.Step[] studySteps = _projectElementService
439                 .getSteps(aStudyFound);
440         org.splat.som.Step[] scSteps = _projectElementService
441                 .getSteps(aStudyFound.getScenarii()[0]);
442
443         List<org.splat.som.Step> steps = new ArrayList<org.splat.som.Step>();
444         steps.addAll(Arrays.asList(studySteps));
445         steps.addAll(Arrays.asList(scSteps));
446         steps.addAll(Arrays.asList(studySteps));
447         DecimalFormat tostring = new DecimalFormat(Document.suformat);
448         for (org.splat.som.Step step : steps) {
449             LOG.debug("Create a document for the step " + step.getNumber());
450
451             List<DocumentType> dtypes = _documentTypeService.selectTypesOf(step
452                     .getStep());
453             if (dtypes.size() > 0) {
454                 DocumentType dtype = dtypes.get(0);
455                 int oldInd = ht.get(Study.class, aStudyFound.getIndex())
456                         .getLastLocalIndex();
457
458                 // Create a file in the download directory
459                 String fname = "filename" + step.getNumber() + ".py";
460                 String filePath = getDownloadPath(aStudyFound.getAuthor())
461                         + fname;
462                 LOG.debug(step.getStep().getKey() + ": "
463                         + step.getOwner().getClass().getSimpleName()
464                         + ": Create test file: " + filePath);
465                 FileWriter fw = new FileWriter(filePath);
466                 fw
467                         .write("Simulation of filename.py file for creating a new document at "
468                                 + new Date());
469                 fw.close();
470
471                 Publication addoc = _publicationService.createDoc(aStudyFound
472                         .getIndex(), step, dtype.getIndex(), userId, fname,
473                         "tstdoc", ProgressState.inWORK, "", "", new Date(),
474                         null);
475
476                 _studyDAO.flush();
477                 ht.flush();
478
479                 int ind = ht.get(Study.class, aStudyFound.getIndex())
480                         .getLastLocalIndex();
481
482                 LOG.debug("Previous index: " + oldInd + ", new index: " + ind);
483                 Assert.assertEquals(ht.get(Study.class, aStudyFound.getIndex())
484                         .getLastLocalIndex(), oldInd + 1,
485                         "Incremented index must be saved in the database.");
486                 aStudy = (Study) ht.find(
487                         "from Study where rid = " + aStudyFound.getIndex())
488                         .get(0);
489                 Assert.assertEquals(aStudy.getLastLocalIndex(), oldInd + 1,
490                         "Incremented index must be saved in the database.");
491                 Assert.assertEquals(addoc.getSourceFile().getName(), tostring
492                         .format(aStudy.getLastLocalIndex())
493                         + "_" + fname, "The generated file name is incorrect.");
494             }
495         }
496
497         rollbackNestedTransaction();
498         LOG.debug(">>>>> END testCreateDoc()");
499     }
500
501         /**
502          * Test of file attaching to a study document.<BR>
503          * <B>Description :</B> <BR>
504          * <i>Create a study and try to attach a new file to its each document.</i><BR>
505          * <B>Action : </B><BR>
506          * <i>1. call attach method for each document publication of the study.</i><BR>
507          * <B>Test data : </B><BR>
508          * <i>no input parameters</i><BR>
509          * 
510          * <B>Outcome results:</B><BR>
511          * <i>
512          * <ul>
513          * <li>The new ConvertsRelation and the new File object must be created in the database.<BR>
514          * </li>
515          * </ul>
516          * </i>
517          * 
518          * @throws InvalidPropertyException
519          *             if an invalid property is used when creating objects
520          * @throws MultiplyDefinedException
521          *             when trying to create an object with already existing id
522          * @throws MissedPropertyException
523          *             if a mandatory property is not defined for an object to be created
524          * @throws SQLException
525          *             if test study creation is failed
526          * @throws IOException
527          *             if test study creation is failed
528          * @throws ParseException
529          * @throws InterruptedException
530          * @throws NotApplicableException
531          * 
532          */
533         @Test
534         public void testAttach() throws InvalidPropertyException,
535                         MissedPropertyException, MultiplyDefinedException, IOException,
536                         SQLException, NotApplicableException, InterruptedException,
537                         ParseException {
538                 LOG.debug(">>>>> BEGIN testAttach()");
539                 startNestedTransaction();
540
541                 HibernateTemplate ht = getHibernateTemplate();
542                 Study aStudy = createStudy();
543                 // Call DAO's create method for a good transient study.
544                 Long id = aStudy.getIndex();
545                 Assert.assertNotNull(id,
546                                 "Create method returns null instead of a new id.");
547                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
548
549                 // Call DAO's get method for an existing id.
550                 _studyDAO.flush();
551                 getHibernateTemplate().evict(aStudy);
552                 getHibernateTemplate().clear();
553                 Study aStudyFound = _studyDAO.get(id);
554                 ht.clear();
555                 ht.evict(aStudyFound);
556
557                 long userId = aStudyFound.getAuthor().getIndex();
558                 org.splat.som.Step[] studySteps = _projectElementService
559                                 .getSteps(aStudyFound);
560                 org.splat.som.Step[] scSteps = _projectElementService
561                                 .getSteps(aStudyFound.getScenarii()[0]);
562
563                 List<org.splat.som.Step> steps = new ArrayList<org.splat.som.Step>();
564                 steps.addAll(Arrays.asList(studySteps));
565                 steps.addAll(Arrays.asList(scSteps));
566                 steps.addAll(Arrays.asList(studySteps));
567                 for (org.splat.som.Step step : steps) {
568                         LOG.debug("Attach a new .py file to documents of the step "
569                                         + step.getNumber() + ": " + step.getStep().getKey() + ": "
570                                         + step.getOwner().getClass().getSimpleName());
571
572                         for (Publication pub : step.getAllDocuments()) {
573
574                                 ht.evict(step.getOwner());
575                                 ht.evict(pub);
576                                 ht.evict(pub.value());
577                                 int nbConverts = ht.find("from ConvertsRelation").size();
578                                 int nbDocConverts = ht.find(
579                                                 "from ConvertsRelation where owner="
580                                                                 + pub.value().getIndex()).size();
581                                 int nbFiles = ht.find("from File").size();
582
583                                 ConvertsRelation rel = _publicationService.attach(pub, "py");
584
585                                 _studyDAO.flush();
586                                 ht.flush();
587                                 Assert.assertNotNull(rel,
588                                                 "Returned ConvertsRelation must not be null.");
589                                 Assert
590                                                 .assertTrue(rel.isSaved(),
591                                                                 "Returned ConvertsRelation must be saved in the database.");
592                                 Assert.assertEquals(ht.find(
593                                                 "from ConvertsRelation where owner="
594                                                                 + pub.value().getIndex()).size(),
595                                                 nbDocConverts + 1,
596                                                 "Number of created ConvertsRelations must be 1.");
597                                 Assert.assertEquals(ht.find("from ConvertsRelation").size(),
598                                                 nbConverts + 1,
599                                                 "Number of created ConvertsRelations must be 1.");
600                                 Assert.assertNotNull(rel.getTo(),
601                                                 "Attached File must not be null.");
602                                 Assert.assertTrue(rel.isSaved(),
603                                                 "Attached File must be saved in the database.");
604                                 Assert
605                                                 .assertEquals(ht
606                                                                 .find(
607                                                                                 "from File where rid="
608                                                                                                 + rel.getTo().getIndex())
609                                                                 .size(), 1, "Number of created File must be 1.");
610                                 Assert.assertEquals(ht.find("from File").size(), nbFiles + 1,
611                                                 "Number of created Files must be 1.");
612                         }
613                 }
614
615                 rollbackNestedTransaction();
616                 LOG.debug(">>>>> END testAttach()");
617         }
618
619         /**
620          * Get path to the user's downloads directory. The directory is created if it is not exist yet.
621          * 
622          * @param user
623          *            user
624          * @return absolute path to downloads directory followed by slash
625          */
626         private String getDownloadPath(final User user) {
627                 // Prepare download directory
628                 File tmpDir = _repositoryService.getDownloadDirectory(user);
629                 if (!tmpDir.exists()) {
630                         Assert.assertTrue(tmpDir.mkdir(),
631                                         "Can't create temporary directory: "
632                                                         + tmpDir.getAbsolutePath());
633                 }
634
635                 return tmpDir.getAbsolutePath() + "/";
636         }
637
638         /**
639          * Test testReplace method for a published document.<BR>
640          * <B>Description :</B> <BR>
641          * <i>Create a study and a scenario with documents and try to replace source files.<BR>
642          * </i><BR>
643          * <B>Action : </B><BR>
644          * <i>1. call the method for a document in the status inDRAFT.</i><BR>
645          * <i>2. call the method for a document in the status inCHECK.</i><BR>
646          * <i>3. call the method for a document in the status APPROVED.</i><BR>
647          * <i>4. call the method for an Extern document published in the study step.</i><BR>
648          * <i>5. call the method for an inWork document published in the scenario step.</i><BR>
649          * <B>Test data : </B><BR>
650          * <i>no input parameters</i><BR>
651          * 
652          * <B>Outcome results:</B><BR>
653          * <i>
654          * <ul>
655          * <li>1-3: Exception must be thrown. The document source file content must not be replaced.<BR>
656          * </li>
657          * <li>4: The document source file content in the study must be replaced.<BR>
658          * </li>
659          * <li>5: The document source file content in the scenario must be replaced.<BR>
660          * </li>
661          * </ul>
662          * </i>
663          * 
664          * @throws BusinessException
665          *             if test data creation is failed
666          * @throws IOException
667          *             if application configuration loading or test data creation is failed
668          * @throws SQLException
669          *             if application configuration loading or test data creation is failed
670          */
671         @Test
672         public void testReplace() throws BusinessException, IOException,
673                         SQLException {
674                 LOG.debug(">>>>> BEGIN testReplace()");
675                 startNestedTransaction();
676
677                 HibernateTemplate ht = getHibernateTemplate();
678
679                 Database.getInstance().reset();
680                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
681                 _projectSettings.configure("classpath:test/som.xml");
682
683                 User goodUser = TestEntitiesGenerator.getTestUser("GoodUser");
684                 _userDAO.create(goodUser);
685
686                 // Create private study
687                 Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
688                 aStudy.setTitle("0.This is private study");
689                 Long studyId = _studyDAO.create(aStudy);
690
691                 // Add a scenario to the study
692                 Scenario scen = TestEntitiesGenerator.getTestScenario(aStudy);
693                 Long scenId = _scenarioDAO.create(scen);
694                 ht.flush();
695
696                 DocumentType dtype = _documentTypeService.selectType("minutes");
697
698                 // Add documents to the first study activity
699                 // Add a converts relations
700                 Map<Integer, org.splat.som.Step> stSteps = _projectElementService
701                                 .getStepsMap(aStudy);
702                 org.splat.som.Step aStep = stSteps.get(1);
703                 Publication pub0inDraft = addDoc(aStudy, aStep, "document0indraft",
704                                 dtype);
705                 pub0inDraft.value().setProgressState(ProgressState.inDRAFT);
706                 Publication pub0inCheck = addDoc(aStudy, aStep, "document0incheck",
707                                 dtype);
708                 pub0inCheck.value().setProgressState(ProgressState.inCHECK);
709                 Publication pub0approved = addDoc(aStudy, aStep, "document0approved",
710                                 dtype);
711                 pub0approved.value().setProgressState(ProgressState.APPROVED);
712                 Publication pub1 = addDoc(aStudy, aStep, "document1", dtype);
713                 String format1 = pub1.value().getFormat();
714                 ht.flush();
715                 ht.update(aStudy);
716                 ht.flush();
717
718                 // Add documents to the first scenario activity
719                 scen = aStudy.getScenariiList().get(0);
720                 Map<Integer, org.splat.som.Step> scSteps = _projectElementService
721                                 .getStepsMap(scen);
722                 aStep = scSteps.get(2);
723                 Publication spub1 = addDoc(scen, aStep, "sdocument1", dtype);
724                 String format2 = spub1.value().getFormat();
725                 ht.flush();
726
727                 // Remember documents ids
728                 Long id0inDraft = pub0inDraft.value().getIndex(), id0inCheck = pub0inCheck
729                                 .value().getIndex(), id0approved = pub0approved.value()
730                                 .getIndex(), id1 = pub1.value().getIndex(), id2 = spub1.value()
731                                 .getIndex();
732
733                 ht.evict(scen);
734                 ht.evict(scen.getOwnerStudy());
735                 ht.clear();
736
737                 Assert.assertTrue(ht.find("from File").size() >= 3,
738                                 "Files were not created in the database.");
739
740                 Date modifTime = Calendar.getInstance().getTime();
741
742                 aStudy = _studyService.selectStudy(studyId);
743                 File newFile = new File(getDownloadPath(goodUser) + "replaced0.ddd");
744                 createFile(newFile.getAbsolutePath());
745
746                 // TEST CALL: Try to replace a file for inDraft document
747                 Publication pub = getPublicationByDocId(aStudy, id0inDraft);
748                 try {
749                         _publicationService.replace(pub, newFile, modifTime);
750                         Assert
751                                         .fail("IncompatibleDataException is expected for a doucment in the status "
752                                                         + pub.getProgressState().name());
753                 } catch (IncompatibleDataException e) {
754                         LOG.debug("Expected exception: ", e);
755                 }
756                 ht.flush();
757                 // TEST CALL: Try to replace a file for inCheck document
758                 pub = getPublicationByDocId(aStudy, id0inCheck);
759                 try {
760                         _publicationService.replace(pub, newFile, modifTime);
761                         Assert
762                                         .fail("IncompatibleDataException is expected for a doucment in the status "
763                                                         + pub.getProgressState().name());
764                 } catch (IncompatibleDataException e) {
765                         LOG.debug("Expected exception: ", e);
766                 }
767                 ht.flush();
768                 // TEST CALL: Try to replace a file for Approved document
769                 pub = getPublicationByDocId(aStudy, id0approved);
770                 try {
771                         _publicationService.replace(pub, newFile, modifTime);
772                         Assert
773                                         .fail("IncompatibleDataException is expected for a doucment in the status "
774                                                         + pub.getProgressState().name());
775                 } catch (IncompatibleDataException e) {
776                         LOG.debug("Expected exception: ", e);
777                 }
778                 ht.flush();
779                 newFile.delete();
780
781                 // TEST CALL: Replace a file in the study step
782                 pub = getPublicationByDocId(aStudy, id1);
783                 newFile = new File(getDownloadPath(goodUser) + "replaced1.ddd");
784                 createFile(newFile.getAbsolutePath());
785                 Assert.assertTrue(_publicationService.replace(pub, newFile, modifTime));
786                 ht.flush();
787
788                 // TEST CALL: Replace a file in the scenario step
789                 scen = aStudy.getScenariiList().get(0);
790                 pub = getPublicationByDocId(scen, id2);
791                 newFile = new File(getDownloadPath(goodUser) + "replaced2.ddd");
792                 createFile(newFile.getAbsolutePath());
793                 Assert.assertTrue(_publicationService.replace(pub, newFile, modifTime));
794                 ht.flush();
795
796                 ht.clear();
797                 // Remove milliseconds because they are lost after saving in the database
798                 modifTime.setTime((modifTime.getTime() / 1000) * 1000);
799
800                 Document doc = ht.get(Document.class, id1);
801                 String txt = readFile(doc.getFile().asFile());
802                 Assert.assertTrue(doc.getFile().getName().contains("_document1"));
803                 Assert.assertEquals(doc.getFile().getFormat(), format1);
804                 Assert.assertTrue(txt.contains("replaced1.ddd"));
805                 Assert.assertEquals(doc.getLastModificationDate(), modifTime);
806
807                 doc = ht.get(Document.class, id2);
808                 txt = readFile(doc.getFile().asFile());
809                 Assert.assertTrue(doc.getFile().getName().contains("_sdocument1"));
810                 Assert.assertEquals(doc.getFile().getFormat(), format2);
811                 Assert.assertTrue(txt.contains("replaced2.ddd"));
812                 Assert.assertEquals(doc.getLastModificationDate(), modifTime);
813
814                 rollbackNestedTransaction();
815                 LOG.debug(">>>>> END testReplace()");
816         }
817
818     /**
819      * Test testUpdateRelations method for updating relations at versioning
820      * document.<BR>
821      * <B>Description :</B> <BR>
822      * <i>Create a study and a scenario with documents and try to update
823      * relations for every document in Study or Scenario.<BR>
824      * </i><BR>
825      * <B>Action : </B><BR>
826      * <i>1. call the method for the every document in the Study or
827      * Scenario.</i><BR>
828      * <B>Test data : </B><BR>
829      * <i>no input parameters</i><BR>
830      *
831      * <B>Outcome results:</B><BR>
832      * <i>
833      * <ul>
834      * <li>1: Update relations for every document: <BR>
835      * Delete old UsedBy relation from old version and create new UsedBy
836      * relation to new version document. Create new Uses relation from new
837      * version document</li>
838      * </ul>
839      * </i>
840      *
841      * @throws BusinessException
842      *             if test data creation is failed
843      * @throws IOException
844      *             if application configuration loading or test data creation is
845      *             failed
846      * @throws SQLException
847      *             if application configuration loading or test data creation is
848      *             failed
849      */
850     @Test
851     public final void testUpdateRelations() throws
852             BusinessException, IOException, SQLException {
853         LOG.debug(">>>>> BEGIN testUpdateRelations()");
854         startNestedTransaction();
855
856         HibernateTemplate ht = getHibernateTemplate();
857
858         Database.getInstance().reset();
859         _projectSettings.getAllSteps().clear(); // Clear config to be able to
860                                                 // load it again
861         _projectSettings.configure("classpath:test/som.xml");
862
863         User goodUser = TestEntitiesGenerator.getTestUser("GoodUser");
864         _userDAO.create(goodUser);
865
866         // Create private study
867         Study aStudy = TestEntitiesGenerator.getTestStudy(goodUser);
868         aStudy.setTitle("0.This is private study");
869         Long studyId = _studyDAO.create(aStudy);
870
871         // Add a scenario to the study
872         Scenario scen = TestEntitiesGenerator.getTestScenario(aStudy);
873         Long scenId = _scenarioDAO.create(scen);
874         ht.flush();
875
876         org.splat.som.Step[] studySteps = _projectElementService
877                 .getSteps(aStudy);
878         org.splat.som.Step[] scSteps = _projectElementService.getSteps(scen);
879
880         List<org.splat.som.Step> steps = new ArrayList<org.splat.som.Step>();
881         steps.addAll(Arrays.asList(studySteps));
882         steps.addAll(Arrays.asList(scSteps));
883         Publication[] currentArray = new Publication[steps.size() * 2 - 2];
884         Publication[] nextArray = new Publication[steps.size() * 2 - 2];
885         Map<Integer, String[]> docusesMap = new HashMap<Integer, String[]>();
886         Map<Integer, long[]> docusedMap = new HashMap<Integer, long[]>();
887         int pubNumber = 0;
888         for (org.splat.som.Step step : steps) {
889             List<DocumentType> dtypes = _documentTypeService.selectTypesOf(step
890                     .getStep());
891             if (dtypes.size() > 0) {
892                 DocumentType dtype = dtypes.get(0);
893                 if (step.getOwner() instanceof Study) {
894                     currentArray[pubNumber] = addDoc(aStudy, step, "current"
895                             + pubNumber, dtype);
896                     currentArray[pubNumber + 1] = addDoc(aStudy, step,
897                             "current" + (pubNumber + 1), dtype);
898
899                 } else {
900                     currentArray[pubNumber] = addDoc(scen, step, "current"
901                             + pubNumber, dtype);
902                     currentArray[pubNumber + 1] = addDoc(scen, step, "current"
903                             + (pubNumber + 1), dtype);
904                 }
905
906                 pubNumber += 2;
907             }
908         }
909
910         ht.flush();
911         ht.update(aStudy);
912         ht.flush();
913
914         for (Integer numPub = currentArray.length - 1; numPub >= 0; numPub--) {           
915             long[] docusedby = new long[1];
916             if (numPub != 0) {
917                 currentArray[numPub].addDependency(currentArray[numPub - 1]);
918             }
919             if (numPub != currentArray.length - 1) {
920                 docusedby[0] = currentArray[numPub + 1].getIndex();
921                 docusedMap.put(new Integer(numPub), docusedby);
922             }
923         }
924
925         for (int numPub = 0; numPub < currentArray.length; numPub++) {
926             String[] docuses = new String[1];
927             nextArray[numPub] = addDoc(aStudy, currentArray[numPub].getStep(),
928                     "next" + numPub, currentArray[numPub].value().getType());
929             ht.flush();
930             if (numPub != 0) {
931                 docuses[0] = String.valueOf(nextArray[numPub - 1].value()
932                         .getIndex());
933                 docusesMap.put(numPub, docuses);
934             }
935             _publicationService.updateRelations(currentArray[numPub].getStep(),
936                     currentArray[numPub], nextArray[numPub],
937                     docusesMap.get(numPub), docusedMap.get(numPub));
938         }
939
940         //Action
941         for (int numPub = 0; numPub < currentArray.length; numPub++) {
942             if (numPub != currentArray.length - 1) {
943
944                 Assert.assertEquals(
945                         currentArray[numPub].getRelations(UsedByRelation.class),
946                         new ArrayList<Publication>());
947                 Assert.assertTrue(nextArray[numPub].getRelations(
948                         UsedByRelation.class)
949                         .contains(currentArray[numPub + 1]));
950                 Assert.assertTrue(nextArray[numPub].getRelations(
951                         UsedByRelation.class).contains(nextArray[numPub + 1]));
952             }
953
954             if (numPub != 0) {
955                 Assert.assertEquals(
956                         currentArray[numPub].getRelations(UsesRelation.class)
957                                 .get(0), nextArray[numPub - 1]);
958                 Assert.assertEquals(
959                         nextArray[numPub].getRelations(UsesRelation.class).get(
960                                 0), nextArray[numPub - 1]);
961             }
962         }
963         rollbackNestedTransaction();
964         LOG.debug(">>>>> END testUpdateRelations()");
965     }
966
967
968         /**
969          * Find a document publication in the project element.
970          * 
971          * @param projElem
972          *            the project element to scan
973          * @param docId
974          *            the document id
975          * @return the document publication if found, otherwise return null
976          */
977         private Publication getPublicationByDocId(final ProjectElement projElem,
978                         final Long docId) {
979                 Publication res = null;
980                 for (Publication pub : projElem.getDocums()) {
981                         if (docId.equals(pub.value().getIndex())) {
982                                 res = pub;
983                                 break;
984                         }
985                 }
986                 return res;
987         }
988
989         /**
990          * Get file content as a string.
991          * 
992          * @param f
993          *            the file to read
994          * @return the file content
995          * @throws IOException
996          *             if reading is failed
997          */
998         private String readFile(final File f) throws IOException {
999                 FileInputStream fstream = new FileInputStream(f);
1000                 // Get the object of DataInputStream
1001                 DataInputStream in = new DataInputStream(fstream);
1002                 BufferedReader br = new BufferedReader(new InputStreamReader(in));
1003                 StringBuffer res = new StringBuffer();
1004                 String str;
1005                 // Read File Line By Line
1006                 while ((str = br.readLine()) != null) {
1007                         res.append(str);
1008                 }
1009                 // Close the input stream
1010                 in.close();
1011                 return res.toString();
1012         }
1013 }