Salome HOME
ProjectSettings are now configured when the bean is created (without call to a struts...
[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("classpath:test/som.xml");
544                 } catch (FileNotFoundException e) {
545                         Assert.fail("Can't find som.xml: ", e);
546                 }
547                 List<Step> steps = _projectSettings.getAllSteps();
548                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
549
550                 // Create a test user
551                 User.Properties uprop = new User.Properties();
552                 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
553                                 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
554                                 .addRole("TST_user").setMailAddress(
555                                                 "noreply@salome-platform.org");
556                 uprop.disableCheck();
557                 User anAuthor = new User(uprop);
558                 ht.saveOrUpdate(anAuthor);
559
560                 // Create a test study
561                 Study.Properties stprops = new Study.Properties().setReference(
562                                 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
563                 Study aStudy = new Study(stprops);
564                 ht.saveOrUpdate(aStudy);
565
566                 // Create a test scenario
567                 Scenario.Properties sprops = new Scenario.Properties().setTitle(
568                                 "TST_Scenario").setManager(anAuthor).setOwnerStudy(aStudy);
569                 Scenario aScenario = new Scenario(sprops);
570                 aStudy.getScenariiList().add(aScenario);
571                 ht.saveOrUpdate(anAuthor);
572                 ht.saveOrUpdate(aStudy);
573                 ht.saveOrUpdate(aScenario);
574
575                 // Create documents for each scenario step
576                 Document.Properties dprop = new Document.Properties().setAuthor(
577                                 anAuthor).setDate(new Date());
578                 int i = 0;
579                 Publication usedPub = null;
580                 Map<Long, Long> usedMap = new HashMap<Long, Long>();
581                 for (int stepNum = 1; stepNum <= steps.size(); stepNum++) {
582                         Step step = _projectSettings.getStep(stepNum);
583                         LOG.debug("Create scenario step: " + stepNum);
584                         ProjectElement projElem;
585
586                         if (step.appliesTo(Study.class)) {
587                                 projElem = aStudy;
588                         } else {
589                                 projElem = aScenario;
590                         }
591                         org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
592                         List<DocumentType> dtypes = _documentTypeService
593                                         .selectTypesOf(step);
594                         if (dtypes.size() > 0) {
595                                 DocumentType dtype = dtypes.get(0);
596                                 // Create a document published in the scenario
597                                 // document<i>: document type[0] - first type used on the step
598                                 // <source-file>.brep
599                                 // <attached-file>.med
600                                 i++;
601                                 dprop.setName("document" + stepNum).setType(dtype);
602                                 if (step.getNumber() > 3) {
603                                         dprop.setFormat("med");
604                                 } else {
605                                         dprop.setFormat("py");
606                                 }
607                                 Publication pub = createDoc(projElem, aScStep, dprop, "med",
608                                                 false);
609                                 if (usedPub != null) {
610                                         pub.addDependency(usedPub);
611                                         LOG.debug("Add dependency: " + pub.value().getTitle()
612                                                         + " from " + usedPub.value().getTitle());
613                                         ht.saveOrUpdate(pub.value());
614                                         ht.flush();
615
616                                         usedMap.put(pub.getIndex(), usedPub.getIndex());
617                                 }
618                                 usedPub = pub;
619                         }
620                         if (dtypes.size() <= 0) {
621                                 LOG.debug("No document types are found for scenario step " + i);
622                         }
623                 }
624
625                 // Check that the scenario and its documents have been created correctly.
626
627                 Assert.assertNotNull(ht.find("from Document"),
628                                 "No documents in the database.");
629                 Assert.assertTrue(ht.find("from Document").size() > 0,
630                                 "No documents in the database.");
631
632                 Assert.assertNotNull(ht.find("from Publication where owner="
633                                 + aScenario.getIndex()), "No publications in the database.");
634                 Assert.assertTrue(
635                                 ht.find("from Publication where owner=" + aScenario.getIndex())
636                                                 .size() > 0, "No publications in the database.");
637
638                 for (Publication p : (List<Publication>) ht
639                                 .find("from Publication where owner=" + aScenario.getIndex())) {
640                         LOG.debug("Publication found: [id=" + p.getIndex() + ", owner="
641                                         + p.getOwner().getIndex() + ", doc=" + p.value().getIndex()
642                                         + "]");
643                         Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
644                                         "The publication was not attached to the scenario.");
645                 }
646
647                 // Remove the scenario from the current hibernate session.
648                 ht.evict(aScenario);
649                 // Check that the scenario is created in the database.
650                 Scenario aScen = ht.load(Scenario.class, aScenario.getIndex());
651                 Assert.assertNotNull(aScen, "Scenario was not saved in the database.");
652                 Assert.assertTrue(aScen.getDocums().size() > 0,
653                                 "No publications in the scenario.");
654
655                 Assert.assertTrue(i > 0,
656                                 "More then one document must be in the database");
657
658                 // Check created uses relations
659                 Assert
660                                 .assertTrue(usedMap.size() > 0,
661                                                 "Uses relations must be created.");
662                 boolean foundAny = false;
663                 for (Long usingId : usedMap.keySet()) {
664                         for (Publication pub : aScen.getDocums()) {
665                                 if (pub.getIndex() == usingId) {
666                                         boolean found = false;
667                                         for (Publication used : aScen.getDocums()) {
668                                                 found = (used.getIndex() == usedMap.get(usingId));
669                                                 if (found) {
670                                                         break;
671                                                 }
672                                         }
673                                         if (!found) {
674                                                 for (Publication used : aStudy.getDocums()) {
675                                                         found = (used.getIndex() == usedMap.get(usingId));
676                                                         if (found) {
677                                                                 break;
678                                                         }
679                                                 }
680                                         }
681                                         Assert.assertTrue(found,
682                                                         "Uses relation was not created in the database.");
683                                         foundAny = foundAny || found;
684                                 }
685                         }
686                 }
687                 Assert.assertTrue(foundAny,
688                                 "No Uses relation was created in the database.");
689
690                 return aScenario.getOwnerStudy();
691         }
692
693         /**
694          * Create a document published in the scenario. <BR>
695          * document:<BR>
696          * document type - type used on the step <BR>
697          * &lt;source-file&gt;.brep <BR>
698          * &lt;attached-file&gt;.med
699          * 
700          * @param aScenario
701          *            the scenario to add the document to
702          * @param aScStep
703          *            scenario step where the document to be published
704          * @param dprop
705          *            document properties
706          * @param attachedFileExt
707          *            extension of the secon attached (exported) file
708          * @param isOutdated
709          *            outdated document flag
710          * @return the publication of the created document
711          * @throws IOException
712          * @throws MultiplyDefinedException
713          * @throws InvalidPropertyException
714          * @throws MissedPropertyException
715          */
716         private Publication createDoc(final ProjectElement aScenario,
717                         final org.splat.som.Step aScStep, final Properties dprop,
718                         final String attachedFileExt, final boolean isOutdated)
719                         throws MissedPropertyException, InvalidPropertyException,
720                         MultiplyDefinedException, IOException {
721                 // Create a document published in the scenario
722                 // document<i>: document type - type used on the step
723                 // <source-file>.brep
724                 // <attached-file>.med
725                 Publication pub = _stepService.createDocument(aScStep, dprop);
726                 Assert.assertNotNull(pub.getOwner(),
727                                 "The publication must be attached to the scenario.");
728                 Assert.assertEquals(pub.getOwner().getIndex(), aScenario.getIndex(),
729                                 "The publication was not attached to the scenario.");
730
731                 if (isOutdated) {
732                         pub.setIsnew('O');
733                 }
734                 aScenario.add(pub);
735                 HibernateTemplate ht = getHibernateTemplate();
736                 ht.saveOrUpdate(pub);
737
738                 // Attach a file
739                 ht.save(pub.value());
740                 ht.flush();
741                 ht.saveOrUpdate(_publicationService.attach(pub, attachedFileExt));
742
743                 return pub;
744         }
745
746         /**
747          * Test of getting a study.<BR>
748          * <B>Description :</B> <BR>
749          * <i>Create a study and try to get it from the database.</i><BR>
750          * <B>Action : </B><BR>
751          * <i>1. call DAO's read method for an existing id.</i><BR>
752          * <i>2. call DAO's read method for a not existing id.</i><BR>
753          * <B>Test data : </B><BR>
754          * <i>no input parameters</i><BR>
755          * <i>no input parameters</i><BR>
756          * 
757          * <B>Outcome results:</B><BR>
758          * <i>
759          * <ul>
760          * <li>Object is found in the database successfully<BR>
761          * </li>
762          * <li>Result of search is null<BR>
763          * </li>
764          * </ul>
765          * </i>
766          * 
767          * @throws InvalidPropertyException
768          *             if an invalid property is used when creating objects
769          * @throws MultiplyDefinedException
770          *             when trying to create an object with already existing id
771          * @throws MissedPropertyException
772          *             if a mandatory property is not defined for an object to be created
773          * @throws SQLException
774          * @throws IOException
775          * 
776          */
777         @Test
778         public void testIsReferenced() throws InvalidPropertyException,
779                         MissedPropertyException, MultiplyDefinedException, IOException,
780                         SQLException {
781                 LOG.debug(">>>>> BEGIN testIsReferenced()");
782                 startNestedTransaction();
783
784                 HibernateTemplate ht = getHibernateTemplate();
785                 Study aStudy = createStudy();
786                 // Call DAO's create method for a good transient study.
787                 Long id = aStudy.getIndex();
788                 Assert.assertNotNull(id,
789                                 "Create method returns null instead of a new id.");
790                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
791
792                 // Call DAO's get method for an existing id.
793                 _studyDAO.flush();
794                 getHibernateTemplate().evict(aStudy);
795                 getHibernateTemplate().clear();
796                 Study aStudyFound = _studyDAO.get(id);
797
798                 ProjectElement projElem;
799                 Step step;
800                 long docId;
801                 Document doc;
802                 for (int i = _projectSettings.getAllSteps().size(); i > 0; i--) {
803                         LOG.debug("Remove documents from the step " + i);
804                         step = _projectSettings.getStep(i);
805                         if (step.appliesTo(Study.class)) {
806                                 projElem = aStudyFound;
807                         } else {
808                                 projElem = aStudyFound.getScenarii()[0];
809                         }
810
811                         org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
812
813                         if (aScStep.getDocuments().size() > 0) {
814                                 doc = aScStep.getDocuments().get(0).value();
815                                 docId = doc.getIndex();
816 /*                              Assert.assertEquals(_documentDAO.isReferenced(doc), (ht.find(
817                                                 "from UsesRelation where refer=" + docId).size() + ht
818                                                 .find("from UsedByRelation where refer=" + docId)
819                                                 .size()) > 0);
820 */                      }
821                 }
822
823                 rollbackNestedTransaction();
824                 LOG.debug(">>>>> END testIsReferenced()");
825         }
826 }