Salome HOME
Document id generation is fixed. Document index is updated in the database now.
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / dao / TestStudyDAO.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.dao;
10
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.sql.SQLException;
14 import java.util.Date;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.hibernate.criterion.Order;
20 import org.hibernate.criterion.Restrictions;
21 import org.splat.dal.bo.kernel.User;
22 import org.splat.dal.bo.som.Document;
23 import org.splat.dal.bo.som.DocumentType;
24 import org.splat.dal.bo.som.ProgressState;
25 import org.splat.dal.bo.som.ProjectElement;
26 import org.splat.dal.bo.som.Publication;
27 import org.splat.dal.bo.som.Scenario;
28 import org.splat.dal.bo.som.Study;
29 import org.splat.dal.bo.som.Document.Properties;
30 import org.splat.dal.dao.som.Database;
31 import org.splat.dal.dao.som.DocumentDAO;
32 import org.splat.dal.dao.som.StudyDAO;
33 import org.splat.kernel.InvalidPropertyException;
34 import org.splat.kernel.MissedPropertyException;
35 import org.splat.kernel.MultiplyDefinedException;
36 import org.splat.log.AppLogger;
37 import org.splat.service.DocumentTypeService;
38 import org.splat.service.PublicationService;
39 import org.splat.service.StepService;
40 import org.splat.service.StudyService;
41 import org.splat.service.technical.ProjectSettingsService;
42 import org.splat.service.technical.ProjectSettingsService.Step;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.beans.factory.annotation.Qualifier;
45 import org.springframework.orm.hibernate3.HibernateTemplate;
46 import org.testng.Assert;
47 import org.testng.annotations.Test;
48
49 import test.splat.common.BaseTest;
50
51 /**
52  * Test class for StudyDAO.
53  * 
54  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
55  * 
56  */
57 public class TestStudyDAO extends BaseTest {
58
59         /**
60          * Logger for the class.
61          */
62         private static final AppLogger LOG = AppLogger
63                         .getLogger(TestStudyDAO.class);
64
65         /**
66          * The tested StudyDAO. Later injected by Spring.
67          */
68         @Autowired
69         @Qualifier("studyDAO")
70         private transient StudyDAO _studyDAO;
71
72         /**
73          * The PublicationService. Later injected by Spring.
74          */
75         @Autowired
76         @Qualifier("publicationService")
77         private transient PublicationService _publicationService;
78
79         /**
80          * The StepService. Later injected by Spring.
81          */
82         @Autowired
83         @Qualifier("stepService")
84         private transient StepService _stepService;
85
86         /**
87          * The ProjectSettingsService. Later injected by Spring.
88          */
89         @Autowired
90         @Qualifier("projectSettings")
91         private transient ProjectSettingsService _projectSettings;
92
93         /**
94          * The DocumentTypeService. Later injected by Spring.
95          */
96         @Autowired
97         @Qualifier("documentTypeService")
98         private transient DocumentTypeService _documentTypeService;
99
100         /**
101          * The DocumentDAO. Later injected by Spring.
102          */
103         @Autowired
104         @Qualifier("documentDAO")
105         private transient DocumentDAO _documentDAO;
106
107         /**
108          * The StudyService. Later injected by Spring.
109          */
110         @Autowired
111         @Qualifier("studyService")
112         private transient StudyService _studyService;
113
114         /**
115          * Test creation of a study.<BR>
116          * <B>Description :</B> <BR>
117          * <i>Create a study.</i><BR>
118          * <B>Action : </B><BR>
119          * <i>1. call DAO's create method for a good transient study.</i><BR>
120          * <i>2. call DAO's create method for a study with non-zero id.</i><BR>
121          * <B>Test data : </B><BR>
122          * <i>no input parameters</i><BR>
123          * <i>no input parameters</i><BR>
124          * 
125          * <B>Outcome results:</B><BR>
126          * <i>
127          * <ul>
128          * <li>Object is created in the database successfully<BR>
129          * </li>
130          * <li>Another new object is created with new generated id<BR>
131          * </li>
132          * </ul>
133          * </i>
134          * 
135          * @throws InvalidPropertyException
136          *             if an invalid property is used when creating objects
137          * @throws MultiplyDefinedException
138          *             when trying to create an object with already existing id
139          * @throws MissedPropertyException
140          *             if a mandatory property is not defined for an object to be created
141          * @throws SQLException
142          * @throws IOException
143          * 
144          */
145         @Test
146         public void testCreate() throws InvalidPropertyException,
147                         MissedPropertyException, MultiplyDefinedException, IOException,
148                         SQLException {
149                 LOG.debug(">>>>> BEGIN testCreate()");
150                 startNestedTransaction();
151
152                 Study aStudy = createStudy();
153                 // Call DAO's create method for a good transient study.
154                 long id = _studyDAO.create(aStudy);
155                 Assert.assertNotNull(id,
156                                 "Create method returns null instead of a new id.");
157                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
158
159                 LOG.debug("Created id: " + id);
160
161                 Study aStudyFound = getHibernateTemplate().get(Study.class, id);
162                 _studyDAO.flush();
163
164                 // Call DAO's create method for a study with non-zero id.
165                 Study aBadStudy = new Study((new Study.Properties()).setTitle(
166                                 "2.Bad test study").setManager(aStudy.getAuthor()).setDate(
167                                 aStudy.getDate()).setReference("sid"));
168                 aBadStudy.setIndex(aStudyFound.getIndex());
169
170                 _studyDAO.flush();
171                 List<Study> res = _studyDAO.getAll(Order.asc("rid"));
172
173                 Assert.assertNotNull(res,
174                                 "Method getFilteredList must not return null.");
175                 Assert.assertEquals(res.size(), 1, "Number of found objects is wrong.");
176                 Assert.assertEquals(aBadStudy.getIndex(), id, "Id must be equal.");
177
178                 Assert.assertNotNull(aBadStudy.getIndex(),
179                                 "Create method returns null instead of a new id.");
180                 Assert.assertTrue(aBadStudy.getIndex() > 0,
181                                 "The new id is not a positive number.");
182
183                 long id2 = _studyDAO.create(aBadStudy);
184
185                 _studyDAO.flush();
186
187                 LOG.debug("Created second id: " + id2);
188
189                 res = _studyDAO.getAll();
190                 _studyDAO.flush();
191
192                 Assert.assertNotNull(res,
193                                 "Method getFilteredList must not return null.");
194                 Assert.assertNotNull(res.get(0),
195                                 "Method getFilteredList must not return null.");
196                 Assert.assertNotNull(res.get(1),
197                                 "Method getFilteredList must not return null.");
198                 Assert.assertEquals(res.size(), 2, "Number of found objects is wrong.");
199                 Assert.assertEquals(res.get(0).getIndex(), id,
200                                 "Sorting is incorrect or id of the first element is wrong.");
201                 Assert.assertEquals(res.get(1).getIndex(), id2,
202                                 "Sorting is incorrect or id of the second element is wrong.");
203                 id = res.get(0).getIndex();
204                 id2 = res.get(1).getIndex();
205                 Assert.assertFalse(id == id2, "Ids of objects must not be duplicated.");
206
207                 rollbackNestedTransaction();
208                 LOG.debug(">>>>> END testCreate()");
209         }
210
211         /**
212          * Test of getting a study.<BR>
213          * <B>Description :</B> <BR>
214          * <i>Create a study and try to get it from the database.</i><BR>
215          * <B>Action : </B><BR>
216          * <i>1. call DAO's read method for an existing id.</i><BR>
217          * <i>2. call DAO's read method for a not existing id.</i><BR>
218          * <B>Test data : </B><BR>
219          * <i>no input parameters</i><BR>
220          * <i>no input parameters</i><BR>
221          * 
222          * <B>Outcome results:</B><BR>
223          * <i>
224          * <ul>
225          * <li>Object is found in the database successfully<BR>
226          * </li>
227          * <li>Result of search is null<BR>
228          * </li>
229          * </ul>
230          * </i>
231          * 
232          * @throws InvalidPropertyException
233          *             if an invalid property is used when creating objects
234          * @throws MultiplyDefinedException
235          *             when trying to create an object with already existing id
236          * @throws MissedPropertyException
237          *             if a mandatory property is not defined for an object to be created
238          * @throws SQLException
239          * @throws IOException
240          * 
241          */
242         @Test
243         public void testGet() throws InvalidPropertyException,
244                         MissedPropertyException, MultiplyDefinedException, IOException,
245                         SQLException {
246                 LOG.debug(">>>>> BEGIN testGet()");
247                 startNestedTransaction();
248
249                 Study aStudy = createStudy();
250                 // Call DAO's create method for a good transient study.
251                 Long id = aStudy.getIndex();
252                 Assert.assertNotNull(id,
253                                 "Create method returns null instead of a new id.");
254                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
255
256                 // Call DAO's get method for an existing id.
257                 _studyDAO.flush();
258                 getHibernateTemplate().evict(aStudy);
259                 getHibernateTemplate().clear();
260                 Study aStudyFound = _studyDAO.get(id);
261
262                 Assert.assertNotNull(aStudyFound);
263
264                 // Call DAO's get method for a not existing id.
265                 aStudyFound = _studyDAO.get(-1L);
266                 getHibernateTemplate().flush();
267                 Assert.assertNull(aStudyFound,
268                                 "A found object with not existing id must be null.");
269
270                 aStudyFound = _studyDAO.get(0L);
271                 getHibernateTemplate().flush();
272                 Assert.assertNull(aStudyFound,
273                                 "A found object with not existing id must be null.");
274
275                 aStudyFound = _studyDAO.get(id + 1);
276                 getHibernateTemplate().flush();
277                 Assert.assertNull(aStudyFound,
278                                 "A found object with not existing id must be null.");
279
280                 rollbackNestedTransaction();
281                 LOG.debug(">>>>> END testGet()");
282         }
283
284         /**
285          * Test of updating a study.<BR>
286          * <B>Description :</B> <BR>
287          * <i>Create a study and try to update it with another data.</i><BR>
288          * <B>Action : </B><BR>
289          * <i>1. call DAO's update method for an existing id.</i><BR>
290          * <i>2. call DAO's update method for a not existing id.</i><BR>
291          * <i>3. call DAO's update method for wrong data.</i><BR>
292          * <B>Test data : </B><BR>
293          * <i>no input parameters</i><BR>
294          * <i>no input parameters</i><BR>
295          * <i>no input parameters</i><BR>
296          * 
297          * <B>Outcome results:</B><BR>
298          * <i>
299          * <ul>
300          * <li>Object is update in the database successfully<BR>
301          * </li>
302          * <li>Exception is thrown<BR>
303          * </li>
304          * <li>Exception is thrown<BR>
305          * </li>
306          * </ul>
307          * </i>
308          * 
309          * @throws InvalidPropertyException
310          *             if an invalid property is used when creating objects
311          * @throws MultiplyDefinedException
312          *             when trying to create an object with already existing id
313          * @throws MissedPropertyException
314          *             if a mandatory property is not defined for an object to be created
315          * @throws SQLException
316          * @throws IOException
317          * 
318          */
319         @Test
320         public void testUpdate() throws InvalidPropertyException,
321                         MissedPropertyException, MultiplyDefinedException, IOException,
322                         SQLException {
323                 LOG.debug(">>>>> BEGIN testUpdate()");
324                 startNestedTransaction();
325
326                 Study aStudy = createStudy();
327                 // Call DAO's create method for a good transient study.
328                 Long id = _studyDAO.create(aStudy);
329                 Assert.assertNotNull(id,
330                                 "Create method returns null instead of a new id.");
331                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
332
333                 // Call DAO's update method for an existing id.
334                 Assert.assertTrue(aStudy.getProgressState() != ProgressState.APPROVED,
335                                 "The initial state of the study should not be APPROVED.");
336                 aStudy.setProgressState(ProgressState.APPROVED);
337                 aStudy.setTitle(aStudy.getTitle() + " updated");
338                 _studyDAO.update(aStudy);
339
340                 // Check that the object has been updated.
341                 Study aStudyFound = _studyDAO.get(id);
342
343                 // Call DAO's create method for a study with non-zero id.
344                 Study aBadStudy = new Study((new Study.Properties()).setTitle(
345                                 "Test study").setManager(aStudy.getAuthor()).setDate(
346                                 aStudy.getDate()));
347                 // aBadKelm.setIndex(aKelmFound.getIndex());
348                 try {
349                         _studyDAO.update(aBadStudy);
350                         getHibernateTemplate().flush();
351                         Assert.fail("Update with not existing id must be failed.");
352                 } catch (Exception e) {
353                         LOG.debug("Expected exception is thrown: "
354                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
355                 }
356                 // Call update with bad data (null title).
357                 aBadStudy.setIndex(aStudyFound.getIndex());
358                 aBadStudy.setTitle(null);
359                 try {
360                         _studyDAO.update(aBadStudy);
361                         getHibernateTemplate().flush();
362                         Assert.fail("Update with null title must be failed.");
363                 } catch (Exception e) {
364                         LOG.debug("Expected exception is thrown: "
365                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
366                 }
367                 // Call update with bad data (null state).
368                 aBadStudy.setTitle(aStudyFound.getTitle());
369                 aBadStudy.setProgressState(null);
370                 try {
371                         _studyDAO.update(aBadStudy);
372                         getHibernateTemplate().flush();
373                         Assert.fail("Update with null state must be failed.");
374                 } catch (Exception e) {
375                         LOG.debug("Expected exception is thrown: "
376                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
377                 }
378
379                 rollbackNestedTransaction();
380                 LOG.debug(">>>>> END testUpdate()");
381         }
382
383         /**
384          * Test of deleting a study.<BR>
385          * <B>Description :</B> <BR>
386          * <i>Create a study and try to delete it.</i><BR>
387          * <B>Action : </B><BR>
388          * <i>1. call DAO's delete method for an existing id.</i><BR>
389          * <i>2. call DAO's delete method for a not existing id.</i><BR>
390          * <B>Test data : </B><BR>
391          * <i>no input parameters</i><BR>
392          * <i>no input parameters</i><BR>
393          * 
394          * <B>Outcome results:</B><BR>
395          * <i>
396          * <ul>
397          * <li>Object is found in the database successfully<BR>
398          * </li>
399          * <li>Exception is thrown<BR>
400          * </li>
401          * </ul>
402          * </i>
403          * 
404          * @throws InvalidPropertyException
405          *             if an invalid property is used when creating objects
406          * @throws MultiplyDefinedException
407          *             when trying to create an object with already existing id
408          * @throws MissedPropertyException
409          *             if a mandatory property is not defined for an object to be created
410          * @throws SQLException
411          * @throws IOException
412          * 
413          */
414         @Test
415         public void testDelete() throws InvalidPropertyException,
416                         MissedPropertyException, MultiplyDefinedException, IOException,
417                         SQLException {
418                 LOG.debug(">>>>> BEGIN testDelete()");
419                 startNestedTransaction();
420
421                 Study aStudy = createStudy();
422                 // Call DAO's create method for a good transient study.
423                 Long id = aStudy.getIndex();// _studyDAO.create(aStudy);
424                 Assert.assertNotNull(id,
425                                 "Create method returns null instead of a new id.");
426                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
427                 _studyDAO.flush();
428
429                 // Call DAO's delete method for an existing id.
430                 _studyDAO.merge(aStudy);
431                 _studyDAO.delete(aStudy);
432
433                 // Check that the object has been deleted.
434                 Study aStudyFound = _studyDAO.get(id);
435
436                 Assert.assertNull(aStudyFound, "Deleted object must not be found.");
437                 // Call DAO's delete method for a not existing id.
438                 getHibernateTemplate().flush();
439
440                 rollbackNestedTransaction();
441                 LOG.debug(">>>>> END testDelete()");
442         }
443
444         /**
445          * Test of getting a filtered list of studys.<BR>
446          * <B>Description :</B> <BR>
447          * <i>Create two studys and try to get them from the database.</i><BR>
448          * <B>Action : </B><BR>
449          * <i>1. Find objects with the given value of the property of a child object.</i><BR>
450          * <i>2. Get ordered list of objects with the given value of the property of a child object.</i><BR>
451          * <B>Test data : </B><BR>
452          * <i>no input parameters</i><BR>
453          * <i>no input parameters</i><BR>
454          * 
455          * <B>Outcome results:</B><BR>
456          * <i>
457          * <ul>
458          * <li>Objects are found in the database successfully<BR>
459          * </li>
460          * <li>Objects are found in the database and sorted in the right order<BR>
461          * </li>
462          * </ul>
463          * </i>
464          * 
465          * @throws InvalidPropertyException
466          *             if an invalid property is used when creating objects
467          * @throws MultiplyDefinedException
468          *             when trying to create an object with already existing id
469          * @throws MissedPropertyException
470          *             if a mandatory property is not defined for an object to be created
471          * @throws SQLException
472          * @throws IOException
473          * 
474          */
475         @Test
476         public void testGetFilteredList() throws InvalidPropertyException,
477                         MissedPropertyException, MultiplyDefinedException, IOException,
478                         SQLException {
479                 LOG.debug(">>>>> BEGIN testGetFilteredList()");
480                 startNestedTransaction();
481
482                 // Create several objects in the database
483                 Study aStudy = createStudy();
484                 long id = _studyDAO.create(aStudy);
485                 Study aNewStudy = new Study((new Study.Properties()).setTitle(
486                                 "TST_Study N2").setManager(aStudy.getAuthor()).setReference(
487                                 "sid").setDate(aStudy.getDate()));
488                 long id2 = _studyDAO.create(aNewStudy);
489
490                 _studyDAO.flush();
491
492                 // ////////////////////////////////////////////////////
493                 // Call DAO's getFilteredList method without order parameter.
494                 List<Study> res = _studyDAO.getFilteredList("manager", Restrictions.eq(
495                                 "username", aStudy.getAuthor().getUsername()));
496
497                 Assert.assertNotNull(res,
498                                 "Method getFilteredList must not return null.");
499                 Assert.assertEquals(res.size(), 2, "Number of found objects is wrong.");
500
501                 // ////////////////////////////////////////////////////
502                 // Call DAO's getFilteredList method with defined order parameter.
503                 res = _studyDAO.getFilteredList("manager", Restrictions.eq("username",
504                                 aStudy.getAuthor().getUsername()), Order.asc("title"));
505
506                 Assert.assertNotNull(res,
507                                 "Method getFilteredList must not return null.");
508                 Assert.assertEquals(res.size(), 2, "Number of found objects is wrong.");
509                 Assert.assertEquals(res.get(0).getIndex(), id,
510                                 "Sorting of results is not correct.");
511                 Assert.assertEquals(res.get(1).getIndex(), id2,
512                                 "Sorting of results is not correct.");
513
514                 rollbackNestedTransaction();
515                 LOG.debug(">>>>> END testGetFilteredList()");
516         }
517
518         /**
519          * Create a persistent scenario for tests.
520          * 
521          * @return a persistent scenario
522          * @throws InvalidPropertyException
523          *             if an invalid property is used when creating objects
524          * @throws MultiplyDefinedException
525          *             when trying to create an object with already existing id
526          * @throws MissedPropertyException
527          *             if a mandatory property is not defined for an object to be created
528          * @throws IOException
529          *             if document creation is failed
530          * @throws SQLException
531          *             if project settings loading is failed
532          */
533         private Study createStudy() throws InvalidPropertyException,
534                         MissedPropertyException, MultiplyDefinedException, IOException,
535                         SQLException {
536                 // Create a scenario for tests
537                 HibernateTemplate ht = getHibernateTemplate();
538
539                 Database.getInstance().reset();
540                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
541                 // Load workflow customization
542                 try {
543                         _projectSettings.configure(ClassLoader.getSystemResource(
544                                         "test/som.xml").getPath());
545                 } catch (FileNotFoundException e) {
546                         Assert.fail("Can't find som.xml: ", e);
547                 }
548                 List<Step> steps = _projectSettings.getAllSteps();
549                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
550
551                 // Create a test user
552                 User.Properties uprop = new User.Properties();
553                 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
554                                 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
555                                 .addRole("TST_user").setMailAddress(
556                                                 "noreply@salome-platform.org");
557                 uprop.disableCheck();
558                 User anAuthor = new User(uprop);
559                 ht.saveOrUpdate(anAuthor);
560
561                 // Create a test study
562                 Study.Properties stprops = new Study.Properties().setReference(
563                                 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
564                 Study aStudy = new Study(stprops);
565                 ht.saveOrUpdate(aStudy);
566
567                 // Create a test scenario
568                 Scenario.Properties sprops = new Scenario.Properties().setTitle(
569                                 "TST_Scenario").setManager(anAuthor).setOwnerStudy(aStudy);
570                 Scenario aScenario = new Scenario(sprops);
571                 aStudy.getScenariiList().add(aScenario);
572                 ht.saveOrUpdate(anAuthor);
573                 ht.saveOrUpdate(aStudy);
574                 ht.saveOrUpdate(aScenario);
575
576                 // Create documents for each scenario step
577                 Document.Properties dprop = new Document.Properties().setAuthor(
578                                 anAuthor).setDate(new Date());
579                 int i = 0;
580                 Publication usedPub = null;
581                 Map<Long, Long> usedMap = new HashMap<Long, Long>();
582                 for (int stepNum = 1; stepNum <= steps.size(); stepNum++) {
583                         Step step = _projectSettings.getStep(stepNum);
584                         LOG.debug("Create scenario step: " + stepNum);
585                         ProjectElement projElem;
586
587                         if (step.appliesTo(Study.class)) {
588                                 projElem = aStudy;
589                         } else {
590                                 projElem = aScenario;
591                         }
592                         org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
593                         List<DocumentType> dtypes = _documentTypeService
594                                         .selectTypesOf(step);
595                         if (dtypes.size() > 0) {
596                                 DocumentType dtype = dtypes.get(0);
597                                 // Create a document published in the scenario
598                                 // document<i>: document type[0] - first type used on the step
599                                 // <source-file>.brep
600                                 // <attached-file>.med
601                                 i++;
602                                 dprop.setName("document" + stepNum).setType(dtype);
603                                 if (step.getNumber() > 3) {
604                                         dprop.setFormat("med");
605                                 } else {
606                                         dprop.setFormat("py");
607                                 }
608                                 Publication pub = createDoc(projElem, aScStep, dprop, "med",
609                                                 false);
610                                 if (usedPub != null) {
611                                         pub.addDependency(usedPub);
612                                         LOG.debug("Add dependency: " + pub.value().getTitle()
613                                                         + " from " + usedPub.value().getTitle());
614                                         ht.saveOrUpdate(pub.value());
615                                         ht.flush();
616
617                                         usedMap.put(pub.getIndex(), usedPub.getIndex());
618                                 }
619                                 usedPub = pub;
620                         }
621                         if (dtypes.size() <= 0) {
622                                 LOG.debug("No document types are found for scenario step " + i);
623                         }
624                 }
625
626                 // Check that the scenario and its documents have been created correctly.
627
628                 Assert.assertNotNull(ht.find("from Document"),
629                                 "No documents in the database.");
630                 Assert.assertTrue(ht.find("from Document").size() > 0,
631                                 "No documents in the database.");
632
633                 Assert.assertNotNull(ht.find("from Publication where owner="
634                                 + aScenario.getIndex()), "No publications in the database.");
635                 Assert.assertTrue(
636                                 ht.find("from Publication where owner=" + aScenario.getIndex())
637                                                 .size() > 0, "No publications in the database.");
638
639                 for (Publication p : (List<Publication>) ht
640                                 .find("from Publication where owner=" + aScenario.getIndex())) {
641                         LOG.debug("Publication found: [id=" + p.getIndex() + ", owner="
642                                         + p.getOwner().getIndex() + ", doc=" + p.value().getIndex()
643                                         + "]");
644                         Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
645                                         "The publication was not attached to the scenario.");
646                 }
647
648                 // Remove the scenario from the current hibernate session.
649                 ht.evict(aScenario);
650                 // Check that the scenario is created in the database.
651                 Scenario aScen = ht.load(Scenario.class, aScenario.getIndex());
652                 Assert.assertNotNull(aScen, "Scenario was not saved in the database.");
653                 Assert.assertTrue(aScen.getDocums().size() > 0,
654                                 "No publications in the scenario.");
655
656                 Assert.assertTrue(i > 0,
657                                 "More then one document must be in the database");
658
659                 // Check created uses relations
660                 Assert
661                                 .assertTrue(usedMap.size() > 0,
662                                                 "Uses relations must be created.");
663                 boolean foundAny = false;
664                 for (Long usingId : usedMap.keySet()) {
665                         for (Publication pub : aScen.getDocums()) {
666                                 if (pub.getIndex() == usingId) {
667                                         boolean found = false;
668                                         for (Publication used : aScen.getDocums()) {
669                                                 found = (used.getIndex() == usedMap.get(usingId));
670                                                 if (found) {
671                                                         break;
672                                                 }
673                                         }
674                                         if (!found) {
675                                                 for (Publication used : aStudy.getDocums()) {
676                                                         found = (used.getIndex() == usedMap.get(usingId));
677                                                         if (found) {
678                                                                 break;
679                                                         }
680                                                 }
681                                         }
682                                         Assert.assertTrue(found,
683                                                         "Uses relation was not created in the database.");
684                                         foundAny = foundAny || found;
685                                 }
686                         }
687                 }
688                 Assert.assertTrue(foundAny,
689                                 "No Uses relation was created in the database.");
690
691                 return aScenario.getOwnerStudy();
692         }
693
694         /**
695          * Create a document published in the scenario. <BR>
696          * document:<BR>
697          * document type - type used on the step <BR>
698          * &lt;source-file&gt;.brep <BR>
699          * &lt;attached-file&gt;.med
700          * 
701          * @param aScenario
702          *            the scenario to add the document to
703          * @param aScStep
704          *            scenario step where the document to be published
705          * @param dprop
706          *            document properties
707          * @param attachedFileExt
708          *            extension of the secon attached (exported) file
709          * @param isOutdated
710          *            outdated document flag
711          * @return the publication of the created document
712          * @throws IOException
713          * @throws MultiplyDefinedException
714          * @throws InvalidPropertyException
715          * @throws MissedPropertyException
716          */
717         private Publication createDoc(final ProjectElement aScenario,
718                         final org.splat.som.Step aScStep, final Properties dprop,
719                         final String attachedFileExt, final boolean isOutdated)
720                         throws MissedPropertyException, InvalidPropertyException,
721                         MultiplyDefinedException, IOException {
722                 // Create a document published in the scenario
723                 // document<i>: document type - type used on the step
724                 // <source-file>.brep
725                 // <attached-file>.med
726                 Publication pub = _stepService.createDocument(aScStep, dprop);
727                 Assert.assertNotNull(pub.getOwner(),
728                                 "The publication must be attached to the scenario.");
729                 Assert.assertEquals(pub.getOwner().getIndex(), aScenario.getIndex(),
730                                 "The publication was not attached to the scenario.");
731
732                 if (isOutdated) {
733                         pub.setIsnew('O');
734                 }
735                 aScenario.add(pub);
736                 HibernateTemplate ht = getHibernateTemplate();
737                 ht.saveOrUpdate(pub);
738
739                 // Attach a file
740                 ht.save(pub.value());
741                 ht.flush();
742                 ht.saveOrUpdate(_publicationService.attach(pub, attachedFileExt));
743
744                 return pub;
745         }
746
747         /**
748          * Test of getting a study.<BR>
749          * <B>Description :</B> <BR>
750          * <i>Create a study and try to get it from the database.</i><BR>
751          * <B>Action : </B><BR>
752          * <i>1. call DAO's read method for an existing id.</i><BR>
753          * <i>2. call DAO's read method for a not existing id.</i><BR>
754          * <B>Test data : </B><BR>
755          * <i>no input parameters</i><BR>
756          * <i>no input parameters</i><BR>
757          * 
758          * <B>Outcome results:</B><BR>
759          * <i>
760          * <ul>
761          * <li>Object is found in the database successfully<BR>
762          * </li>
763          * <li>Result of search is null<BR>
764          * </li>
765          * </ul>
766          * </i>
767          * 
768          * @throws InvalidPropertyException
769          *             if an invalid property is used when creating objects
770          * @throws MultiplyDefinedException
771          *             when trying to create an object with already existing id
772          * @throws MissedPropertyException
773          *             if a mandatory property is not defined for an object to be created
774          * @throws SQLException
775          * @throws IOException
776          * 
777          */
778         @Test
779         public void testIsReferenced() throws InvalidPropertyException,
780                         MissedPropertyException, MultiplyDefinedException, IOException,
781                         SQLException {
782                 LOG.debug(">>>>> BEGIN testIsReferenced()");
783                 startNestedTransaction();
784
785                 HibernateTemplate ht = getHibernateTemplate();
786                 Study aStudy = createStudy();
787                 // Call DAO's create method for a good transient study.
788                 Long id = aStudy.getIndex();
789                 Assert.assertNotNull(id,
790                                 "Create method returns null instead of a new id.");
791                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
792
793                 // Call DAO's get method for an existing id.
794                 _studyDAO.flush();
795                 getHibernateTemplate().evict(aStudy);
796                 getHibernateTemplate().clear();
797                 Study aStudyFound = _studyDAO.get(id);
798
799                 ProjectElement projElem;
800                 Step step;
801                 long docId;
802                 Document doc;
803                 for (int i = _projectSettings.getAllSteps().size(); i > 0; i--) {
804                         LOG.debug("Remove documents from the step " + i);
805                         step = _projectSettings.getStep(i);
806                         if (step.appliesTo(Study.class)) {
807                                 projElem = aStudyFound;
808                         } else {
809                                 projElem = aStudyFound.getScenarii()[0];
810                         }
811
812                         org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
813
814                         if (aScStep.getDocuments().size() > 0) {
815                                 doc = aScStep.getDocuments().get(0).value();
816                                 docId = doc.getIndex();
817 /*                              Assert.assertEquals(_documentDAO.isReferenced(doc), (ht.find(
818                                                 "from UsesRelation where refer=" + docId).size() + ht
819                                                 .find("from UsedByRelation where refer=" + docId)
820                                                 .size()) > 0);
821 */                      }
822                 }
823
824                 rollbackNestedTransaction();
825                 LOG.debug(">>>>> END testIsReferenced()");
826         }
827 }