]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/test/splat/dao/TestKnowledgeElementDAO.java
Salome HOME
Search knowledge is implemented. Unit tests are improved. Lucene index is not used...
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / dao / TestKnowledgeElementDAO.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.util.Date;
12 import java.util.List;
13
14 import org.hibernate.criterion.Order;
15 import org.hibernate.criterion.Restrictions;
16 import org.splat.dal.bo.kernel.User;
17 import org.splat.dal.bo.som.KnowledgeElement;
18 import org.splat.dal.bo.som.KnowledgeElementType;
19 import org.splat.dal.bo.som.ProgressState;
20 import org.splat.dal.bo.som.Scenario;
21 import org.splat.dal.bo.som.Study;
22 import org.splat.dal.dao.som.KnowledgeElementDAO;
23 import org.splat.kernel.InvalidPropertyException;
24 import org.splat.kernel.MissedPropertyException;
25 import org.splat.kernel.MultiplyDefinedException;
26 import org.splat.log.AppLogger;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Qualifier;
29 import org.springframework.orm.hibernate3.HibernateTemplate;
30 import org.testng.Assert;
31 import org.testng.annotations.Test;
32
33 import test.splat.common.BaseTest;
34
35 /**
36  * Test class for KnowledgeElementDAO.
37  * 
38  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
39  * 
40  */
41 public class TestKnowledgeElementDAO extends BaseTest {
42
43         /**
44          * Logger for the class.
45          */
46         private static final AppLogger LOG = AppLogger
47                         .getLogger(TestKnowledgeElementDAO.class);
48
49         /**
50          * The tested KnowledgeElementDAO. Later injected by Spring.
51          */
52         @Autowired
53         @Qualifier("knowledgeElementDAO")
54         private transient KnowledgeElementDAO _knowledgeElementDAO;
55
56         /**
57          * Test creation of a knowledge element.<BR>
58          * <B>Description :</B> <BR>
59          * <i>Create a knowledge element.</i><BR>
60          * <B>Action : </B><BR>
61          * <i>1. call DAO's create method for a good transient knowledge element.</i><BR>
62          * <i>2. call DAO's create method for a knowledge element with non-zero id.</i><BR>
63          * <B>Test data : </B><BR>
64          * <i>no input parameters</i><BR>
65          * <i>no input parameters</i><BR>
66          * 
67          * <B>Outcome results:</B><BR>
68          * <i>
69          * <ul>
70          * <li>Object is created in the database successfully<BR>
71          * </li>
72          * <li>Another new object is created with new generated id<BR>
73          * </li>
74          * </ul>
75          * </i>
76          * 
77          * @throws InvalidPropertyException
78          *             if an invalid property is used when creating objects
79          * @throws MultiplyDefinedException
80          *             when trying to create an object with already existing id
81          * @throws MissedPropertyException
82          *             if a mandatory property is not defined for an object to be created
83          * 
84          */
85         @Test
86         public void testCreate() throws InvalidPropertyException,
87                         MissedPropertyException, MultiplyDefinedException {
88                 LOG.debug(">>>>> BEGIN testCreate()");
89                 startNestedTransaction();
90
91                 KnowledgeElement aKelm = getKnowledgeElement();
92                 // Call DAO's create method for a good transient knowledge element.
93                 long id = _knowledgeElementDAO.create(aKelm);
94                 Assert.assertNotNull(id,
95                                 "Create method returns null instead of a new id.");
96                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
97
98                 LOG.debug("Created id: " + id);
99
100                 KnowledgeElement aKelmFound = getHibernateTemplate().get(
101                                 KnowledgeElement.class, id);
102                 compareObjects(aKelmFound, aKelm);
103                 _knowledgeElementDAO.flush();
104
105                 // Call DAO's create method for a knowledge element with non-zero id.
106                 KnowledgeElement aBadKelm = new KnowledgeElement(
107                                 (new KnowledgeElement.Properties())
108                                                 .setTitle("2.Bad test knowledge element")
109                                                 .setAuthor(aKelm.getAuthor())
110                                                 .setOwnerScenario(aKelm.getOwnerScenario())
111                                                 .setType(aKelm.getType())
112                                                 .setDate(aKelm.getDate())
113                                                 .setValue(
114                                                                 "This is another test knowledge element.\nIt is created by the unit test."));
115                 aBadKelm.setIndex(aKelmFound.getIndex());
116
117                 _knowledgeElementDAO.flush();
118                 List<KnowledgeElement> res = _knowledgeElementDAO.getAll(Order
119                                 .asc("rid"));
120
121                 Assert.assertNotNull(res,
122                                 "Method getFilteredList must not return null.");
123                 Assert.assertEquals(res.size(), 1, "Number of found objects is wrong.");
124                 Assert.assertEquals(aBadKelm.getIndex(), id, "Id must be equal.");
125
126                 Assert.assertNotNull(aBadKelm.getIndex(),
127                                 "Create method returns null instead of a new id.");
128                 Assert.assertTrue(aBadKelm.getIndex() > 0,
129                                 "The new id is not a positive number.");
130
131                 long id2 = _knowledgeElementDAO.create(aBadKelm);
132
133                 _knowledgeElementDAO.flush();
134
135                 LOG.debug("Created second id: " + id2);
136
137                 res = _knowledgeElementDAO.getAll();
138                 _knowledgeElementDAO.flush();
139
140                 Assert.assertNotNull(res,
141                                 "Method getFilteredList must not return null.");
142                 Assert.assertNotNull(res.get(0),
143                                 "Method getFilteredList must not return null.");
144                 Assert.assertNotNull(res.get(1),
145                                 "Method getFilteredList must not return null.");
146                 Assert.assertEquals(res.size(), 2, "Number of found objects is wrong.");
147                 Assert.assertEquals(res.get(0).getIndex(), id,
148                                 "Sorting is incorrect or id of the first element is wrong.");
149                 Assert.assertEquals(res.get(1).getIndex(), id2,
150                                 "Sorting is incorrect or id of the second element is wrong.");
151                 id = res.get(0).getIndex();
152                 id2 = res.get(1).getIndex();
153                 Assert.assertFalse(id == id2, "Ids of objects must not be duplicated.");
154
155                 rollbackNestedTransaction();
156                 LOG.debug(">>>>> END testCreate()");
157         }
158
159         /**
160          * Test of getting a knowledge element.<BR>
161          * <B>Description :</B> <BR>
162          * <i>Create a knowledge element and try to get it from the database.</i><BR>
163          * <B>Action : </B><BR>
164          * <i>1. call DAO's read method for an existing id.</i><BR>
165          * <i>2. call DAO's read method for a not existing id.</i><BR>
166          * <B>Test data : </B><BR>
167          * <i>no input parameters</i><BR>
168          * <i>no input parameters</i><BR>
169          * 
170          * <B>Outcome results:</B><BR>
171          * <i>
172          * <ul>
173          * <li>Object is found in the database successfully<BR>
174          * </li>
175          * <li>Result of search is null<BR>
176          * </li>
177          * </ul>
178          * </i>
179          * 
180          * @throws InvalidPropertyException
181          *             if an invalid property is used when creating objects
182          * @throws MultiplyDefinedException
183          *             when trying to create an object with already existing id
184          * @throws MissedPropertyException
185          *             if a mandatory property is not defined for an object to be created
186          * 
187          */
188         @Test
189         public void testGet() throws InvalidPropertyException,
190                         MissedPropertyException, MultiplyDefinedException {
191                 LOG.debug(">>>>> BEGIN testGet()");
192                 startNestedTransaction();
193
194                 KnowledgeElement aKelm = getKnowledgeElement();
195                 // Call DAO's create method for a good transient knowledge element.
196                 Long id = _knowledgeElementDAO.create(aKelm);
197                 Assert.assertNotNull(id,
198                                 "Create method returns null instead of a new id.");
199                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
200
201                 // Call DAO's get method for an existing id.
202                 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
203
204                 compareObjects(aKelmFound, aKelm);
205
206                 // Call DAO's get method for a not existing id.
207                 aKelmFound = _knowledgeElementDAO.get(-1L);
208                 getHibernateTemplate().flush();
209                 Assert.assertNull(aKelmFound,
210                                 "A found object with not existing id must be null.");
211
212                 aKelmFound = _knowledgeElementDAO.get(0L);
213                 getHibernateTemplate().flush();
214                 Assert.assertNull(aKelmFound,
215                                 "A found object with not existing id must be null.");
216
217                 aKelmFound = _knowledgeElementDAO.get(id + 1);
218                 getHibernateTemplate().flush();
219                 Assert.assertNull(aKelmFound,
220                                 "A found object with not existing id must be null.");
221
222                 rollbackNestedTransaction();
223                 LOG.debug(">>>>> END testGet()");
224         }
225
226         /**
227          * Test of updating a knowledge element.<BR>
228          * <B>Description :</B> <BR>
229          * <i>Create a knowledge element and try to update it with another data.</i><BR>
230          * <B>Action : </B><BR>
231          * <i>1. call DAO's update method for an existing id.</i><BR>
232          * <i>2. call DAO's update method for a not existing id.</i><BR>
233          * <i>3. call DAO's update method for wrong data.</i><BR>
234          * <B>Test data : </B><BR>
235          * <i>no input parameters</i><BR>
236          * <i>no input parameters</i><BR>
237          * <i>no input parameters</i><BR>
238          * 
239          * <B>Outcome results:</B><BR>
240          * <i>
241          * <ul>
242          * <li>Object is update in the database successfully<BR>
243          * </li>
244          * <li>Exception is thrown<BR>
245          * </li>
246          * <li>Exception is thrown<BR>
247          * </li>
248          * </ul>
249          * </i>
250          * 
251          * @throws InvalidPropertyException
252          *             if an invalid property is used when creating objects
253          * @throws MultiplyDefinedException
254          *             when trying to create an object with already existing id
255          * @throws MissedPropertyException
256          *             if a mandatory property is not defined for an object to be created
257          * 
258          */
259         @Test
260         public void testUpdate() throws InvalidPropertyException,
261                         MissedPropertyException, MultiplyDefinedException {
262                 LOG.debug(">>>>> BEGIN testUpdate()");
263                 startNestedTransaction();
264
265                 KnowledgeElement aKelm = getKnowledgeElement();
266                 // Call DAO's create method for a good transient knowledge element.
267                 Long id = _knowledgeElementDAO.create(aKelm);
268                 Assert.assertNotNull(id,
269                                 "Create method returns null instead of a new id.");
270                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
271
272                 // Call DAO's update method for an existing id.
273                 Assert
274                                 .assertTrue(aKelm.getProgressState() != ProgressState.APPROVED,
275                                                 "The initial state of the knowledge element should not be APPROVED.");
276                 aKelm.setProgressState(ProgressState.APPROVED);
277                 aKelm.setTitle(aKelm.getTitle() + " updated");
278                 _knowledgeElementDAO.update(aKelm);
279
280                 // Check that the object has been updated.
281                 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
282
283                 compareObjects(aKelmFound, aKelm);
284
285                 // Call DAO's update method for a bad knowledge element with zero id.
286                 KnowledgeElement aBadKelm = new KnowledgeElement(
287                                 (new KnowledgeElement.Properties())
288                                                 .setTitle("Test knowledge element")
289                                                 .setAuthor(aKelm.getAuthor())
290                                                 .setOwnerScenario(aKelm.getOwnerScenario())
291                                                 .setType(aKelm.getType())
292                                                 .setDate(aKelm.getDate())
293                                                 .setValue(
294                                                                 "This is another test knowledge element.\nIt is created by the unit test."));
295                 // aBadKelm.setIndex(aKelmFound.getIndex());
296                 try {
297                         _knowledgeElementDAO.update(aBadKelm);
298                         getHibernateTemplate().flush();
299                         Assert.fail("Update with not existing id must be failed.");
300                 } catch (Exception e) {
301                         LOG.debug("Expected exception is thrown: "
302                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
303                 }
304                 // Call update with bad data (null title).
305                 aBadKelm.setIndex(aKelmFound.getIndex());
306                 aBadKelm.setTitle(null);
307                 try {
308                         _knowledgeElementDAO.update(aBadKelm);
309                         getHibernateTemplate().flush();
310                         Assert.fail("Update with null title must be failed.");
311                 } catch (Exception e) {
312                         LOG.debug("Expected exception is thrown: "
313                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
314                 }
315                 // Call update with bad data (null state).
316                 aBadKelm.setTitle(aKelmFound.getTitle());
317                 aBadKelm.setProgressState(null);
318                 try {
319                         _knowledgeElementDAO.update(aBadKelm);
320                         getHibernateTemplate().flush();
321                         Assert.fail("Update with null state must be failed.");
322                 } catch (Exception e) {
323                         LOG.debug("Expected exception is thrown: "
324                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
325                 }
326
327                 rollbackNestedTransaction();
328                 LOG.debug(">>>>> END testUpdate()");
329         }
330
331         /**
332          * Test of deleting a knowledge element.<BR>
333          * <B>Description :</B> <BR>
334          * <i>Create a knowledge element and try to delete it.</i><BR>
335          * <B>Action : </B><BR>
336          * <i>1. call DAO's delete method for an existing id.</i><BR>
337          * <i>2. call DAO's delete method for a not existing id.</i><BR>
338          * <B>Test data : </B><BR>
339          * <i>no input parameters</i><BR>
340          * <i>no input parameters</i><BR>
341          * 
342          * <B>Outcome results:</B><BR>
343          * <i>
344          * <ul>
345          * <li>Object is found in the database successfully<BR>
346          * </li>
347          * <li>Exception is thrown<BR>
348          * </li>
349          * </ul>
350          * </i>
351          * 
352          * @throws InvalidPropertyException
353          *             if an invalid property is used when creating objects
354          * @throws MultiplyDefinedException
355          *             when trying to create an object with already existing id
356          * @throws MissedPropertyException
357          *             if a mandatory property is not defined for an object to be created
358          * 
359          */
360         @Test
361         public void testDelete() throws InvalidPropertyException,
362                         MissedPropertyException, MultiplyDefinedException {
363                 LOG.debug(">>>>> BEGIN testDelete()");
364                 startNestedTransaction();
365
366                 KnowledgeElement aKelm = getKnowledgeElement();
367                 // Call DAO's create method for a good transient knowledge element.
368                 Long id = _knowledgeElementDAO.create(aKelm);
369                 Assert.assertNotNull(id,
370                                 "Create method returns null instead of a new id.");
371                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
372
373                 // Call DAO's delete method for an existing id.
374                 _knowledgeElementDAO.delete(aKelm);
375
376                 // Check that the object has been deleted.
377                 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
378
379                 Assert.assertNull(aKelmFound, "Deleted object must not be found.");
380                 // Call DAO's delete method for a not existing id.
381                 getHibernateTemplate().flush();
382
383                 rollbackNestedTransaction();
384                 LOG.debug(">>>>> END testDelete()");
385         }
386
387         /**
388          * Create a transient KnowledgeElement for tests.
389          * 
390          * @return a transient KnowledgeElement
391          * @throws InvalidPropertyException
392          *             if an invalid property is used when creating objects
393          * @throws MultiplyDefinedException
394          *             when trying to create an object with already existing id
395          * @throws MissedPropertyException
396          *             if a mandatory property is not defined for an object to be created
397          */
398         private KnowledgeElement getKnowledgeElement()
399                         throws InvalidPropertyException, MissedPropertyException,
400                         MultiplyDefinedException {
401                 // Create a test knowledge type
402                 HibernateTemplate ht = getHibernateTemplate();
403
404                 KnowledgeElement.Properties kprops = new KnowledgeElement.Properties();
405                 KnowledgeElementType aKType = new KnowledgeElementType("TST_kelmtype");
406                 ht.save(aKType);
407                 // Create a test user
408                 User.Properties uprop = new User.Properties();
409                 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
410                                 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
411                                 .addRole("TST_user").setMailAddress(
412                                                 "noreply@salome-platform.org");
413                 uprop.disableCheck();
414                 User anAuthor = new User(uprop);
415                 ht.saveOrUpdate(anAuthor);
416                 // Create a test study
417                 Study.Properties stprops = new Study.Properties().setReference(
418                                 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
419                 Study aStudy = new Study(stprops);
420                 ht.saveOrUpdate(aStudy);
421                 // Create a test scenario
422                 Scenario.Properties sprops = new Scenario.Properties().setTitle(
423                                 "TST_Study").setManager(anAuthor).setOwnerStudy(aStudy);
424                 Scenario aScenario = new Scenario(sprops);
425                 aStudy.getScenariiList().add(aScenario);
426                 ht.saveOrUpdate(anAuthor);
427                 ht.saveOrUpdate(aKType);
428                 ht.update(aStudy);
429                 ht.saveOrUpdate(aScenario);
430
431                 Date aDate = new Date();
432
433                 // Prepare a knowledge element transient object
434                 kprops
435                                 .setTitle("1. Test knowledge element")
436                                 .setAuthor(anAuthor)
437                                 .setOwnerScenario(aScenario)
438                                 .setType(aKType)
439                                 .setDate(aDate)
440                                 .setValue(
441                                                 "This is the test knowledge element.\nIt is created by the unit test.");
442
443                 return new KnowledgeElement(kprops);
444         }
445
446         /**
447          * Check that given objects are equal.
448          * 
449          * @param anActual
450          *            the object to check
451          * @param anExpected
452          *            the expected object
453          */
454         private void compareObjects(final KnowledgeElement anActual,
455                         final KnowledgeElement anExpected) {
456                 Assert.assertNotNull(anActual,
457                                 "Created object is not found in the database.");
458                 Assert.assertEquals(anActual.getAuthor(), anExpected.getAuthor(),
459                                 "Knowledge Element author is not saved.");
460                 Assert.assertEquals(anActual.getDate(), anExpected.getDate(),
461                                 "Knowledge Element date is not saved.");
462                 Assert
463                                 .assertEquals(anActual.getOwnerScenario(), anExpected
464                                                 .getOwnerScenario(),
465                                                 "Knowledge Element scenario is not saved.");
466                 Assert.assertEquals(anActual.getProgressState(), anExpected
467                                 .getProgressState(), "Knowledge Element state is not saved.");
468                 Assert.assertEquals(anActual.getTitle(), anExpected.getTitle(),
469                                 "Knowledge Element title is not saved.");
470                 Assert.assertEquals(anActual.getType(), anExpected.getType(),
471                                 "Knowledge Element type is not saved.");
472                 Assert.assertEquals(anActual.getValue(), anExpected.getValue(),
473                                 "Knowledge Element value is not saved.");
474                 Assert.assertEquals(anActual.getIndex(), anExpected.getIndex(),
475                                 "Knowledge Element index is not saved.");
476         }
477
478         /**
479          * Test of getting a filtered list of knowledge elements.<BR>
480          * <B>Description :</B> <BR>
481          * <i>Create two knowledge elements and try to get them from the database.</i><BR>
482          * <B>Action : </B><BR>
483          * <i>1. Find objects with the given value of the property of a child object.</i><BR>
484          * <i>2. Get ordered list of objects with the given value of the property of a child object.</i><BR>
485          * <B>Test data : </B><BR>
486          * <i>no input parameters</i><BR>
487          * <i>no input parameters</i><BR>
488          * 
489          * <B>Outcome results:</B><BR>
490          * <i>
491          * <ul>
492          * <li>Objects are found in the database successfully<BR>
493          * </li>
494          * <li>Objects are found in the database and sorted in the right order<BR>
495          * </li>
496          * </ul>
497          * </i>
498          * 
499          * @throws InvalidPropertyException
500          *             if an invalid property is used when creating objects
501          * @throws MultiplyDefinedException
502          *             when trying to create an object with already existing id
503          * @throws MissedPropertyException
504          *             if a mandatory property is not defined for an object to be created
505          * 
506          */
507         @Test
508         public void testGetFilteredList() throws InvalidPropertyException,
509                         MissedPropertyException, MultiplyDefinedException {
510                 LOG.debug(">>>>> BEGIN testGetFilteredList()");
511                 startNestedTransaction();
512
513                 // Create several objects in the database
514                 KnowledgeElement aKelm = getKnowledgeElement();
515                 long id = _knowledgeElementDAO.create(aKelm);
516                 KnowledgeElement aNewKelm = new KnowledgeElement(
517                                 (new KnowledgeElement.Properties())
518                                                 .setTitle("Test knowledge element N2")
519                                                 .setAuthor(aKelm.getAuthor())
520                                                 .setOwnerScenario(aKelm.getOwnerScenario())
521                                                 .setType(aKelm.getType())
522                                                 .setDate(aKelm.getDate())
523                                                 .setValue(
524                                                                 "This is another test knowledge element.\nIt is created by the unit test."));
525                 long id2 = _knowledgeElementDAO.create(aNewKelm);
526
527                 _knowledgeElementDAO.flush();
528
529                 // ////////////////////////////////////////////////////
530                 // Call DAO's getFilteredList method without order parameter.
531                 List<KnowledgeElement> res = _knowledgeElementDAO.getFilteredList(
532                                 "type", Restrictions.eq("name", "TST_kelmtype"));
533
534                 Assert.assertNotNull(res,
535                                 "Method getFilteredList must not return null.");
536                 Assert.assertEquals(res.size(), 2, "Number of found objects is wrong.");
537
538                 // ////////////////////////////////////////////////////
539                 // Call DAO's getFilteredList method with defined order parameter.
540                 res = _knowledgeElementDAO.getFilteredList("type", Restrictions.eq(
541                                 "name", "TST_kelmtype"), Order.asc("title"));
542
543                 Assert.assertNotNull(res,
544                                 "Method getFilteredList must not return null.");
545                 Assert.assertEquals(res.size(), 2, "Number of found objects is wrong.");
546                 Assert.assertEquals(res.get(0).getIndex(), id,
547                                 "Sorting of results is not correct.");
548                 Assert.assertEquals(res.get(1).getIndex(), id2,
549                                 "Sorting of results is not correct.");
550
551                 rollbackNestedTransaction();
552                 LOG.debug(">>>>> END testGetFilteredList()");
553         }
554 }