Salome HOME
Use class loader to get som.xml.
[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
13 import org.splat.dal.bo.kernel.User;
14 import org.splat.dal.bo.som.KnowledgeElement;
15 import org.splat.dal.bo.som.KnowledgeElementType;
16 import org.splat.dal.bo.som.ProgressState;
17 import org.splat.dal.bo.som.Scenario;
18 import org.splat.dal.bo.som.Study;
19 import org.splat.dal.dao.som.KnowledgeElementDAO;
20 import org.splat.kernel.InvalidPropertyException;
21 import org.splat.kernel.MissedPropertyException;
22 import org.splat.kernel.MultiplyDefinedException;
23 import org.splat.log.AppLogger;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.beans.factory.annotation.Qualifier;
26 import org.springframework.orm.hibernate3.HibernateTemplate;
27 import org.testng.Assert;
28 import org.testng.annotations.Test;
29
30 import test.splat.common.BaseTest;
31
32 /**
33  * Test class for KnowledgeElementDAO.
34  * 
35  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
36  * 
37  */
38 public class TestKnowledgeElementDAO extends BaseTest {
39
40         /**
41          * Logger for the class.
42          */
43         private static final AppLogger LOG = AppLogger
44                         .getLogger(TestKnowledgeElementDAO.class);
45
46         /**
47          * The tested KnowledgeElementDAO. Later injected by Spring.
48          */
49         @Autowired
50         @Qualifier("knowledgeElementDAO")
51         private transient KnowledgeElementDAO _knowledgeElementDAO;
52
53         /**
54          * Test creation of a knowledge element.<BR>
55          * <B>Description :</B> <BR>
56          * <i>Create a knowledge element.</i><BR>
57          * <B>Action : </B><BR>
58          * <i>1. call DAO's create method for a good transient knowledge element.</i><BR>
59          * <i>2. call DAO's create method for a knowledge element with non-zero id.</i><BR>
60          * <B>Test data : </B><BR>
61          * <i>no input parameters</i><BR>
62          * <i>no input parameters</i><BR>
63          * 
64          * <B>Outcome results:</B><BR>
65          * <i>
66          * <ul>
67          * <li>Object is created in the database successfully<BR>
68          * </li>
69          * <li>Exception is thrown<BR>
70          * </li>
71          * </ul>
72          * </i>
73          * 
74          * @throws InvalidPropertyException
75          *             if an invalid property is used when creating objects
76          * @throws MultiplyDefinedException
77          *             when trying to create an object with already existing id
78          * @throws MissedPropertyException
79          *             if a mandatory property is not defined for an object to be created
80          * 
81          */
82         @Test
83         public void testCreate() throws InvalidPropertyException,
84                         MissedPropertyException, MultiplyDefinedException {
85                 LOG.debug(">>>>> BEGIN testCreate()");
86
87                 KnowledgeElement aKelm = getKnowledgeElement();
88                 // Call DAO's create method for a good transient knowledge element.
89                 Long id = _knowledgeElementDAO.create(aKelm);
90                 Assert.assertNotNull(id,
91                                 "Create method returns null instead of a new id.");
92                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
93                 KnowledgeElement aKelmFound = getHibernateTemplate().get(
94                                 KnowledgeElement.class, id);
95                 compareObjects(aKelmFound, aKelm);
96
97                 // Call DAO's create method for a knowledge element with non-zero id.
98                 KnowledgeElement aBadKelm = new KnowledgeElement(
99                                 (new KnowledgeElement.Properties())
100                                                 .setTitle("Test knowledge element")
101                                                 .setAuthor(aKelm.getAuthor())
102                                                 .setOwnerScenario(aKelm.getOwnerScenario())
103                                                 .setType(aKelm.getType())
104                                                 .setDate(aKelm.getDate())
105                                                 .setValue(
106                                                                 "This is another test knowledge element.\nIt is created by the unit test."));
107                 aBadKelm.setIndex(aKelmFound.getIndex());
108                 try {
109                         _knowledgeElementDAO.create(aBadKelm);
110                         getHibernateTemplate().flush();
111                         Assert.fail("Creation with existing id must be failed.");
112                 } catch (Exception e) {
113                         LOG.debug("Expected exception is thrown: "
114                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
115                 }
116                 LOG.debug(">>>>> END testCreate()");
117         }
118
119         /**
120          * Test of getting a knowledge element.<BR>
121          * <B>Description :</B> <BR>
122          * <i>Create a knowledge element and try to get it from the database.</i><BR>
123          * <B>Action : </B><BR>
124          * <i>1. call DAO's read method for an existing id.</i><BR>
125          * <i>2. call DAO's read method for a not existing id.</i><BR>
126          * <B>Test data : </B><BR>
127          * <i>no input parameters</i><BR>
128          * <i>no input parameters</i><BR>
129          * 
130          * <B>Outcome results:</B><BR>
131          * <i>
132          * <ul>
133          * <li>Object is found in the database successfully<BR>
134          * </li>
135          * <li>Exception is thrown<BR>
136          * </li>
137          * </ul>
138          * </i>
139          * 
140          * @throws InvalidPropertyException
141          *             if an invalid property is used when creating objects
142          * @throws MultiplyDefinedException
143          *             when trying to create an object with already existing id
144          * @throws MissedPropertyException
145          *             if a mandatory property is not defined for an object to be created
146          * 
147          */
148         @Test
149         public void testGet() throws InvalidPropertyException,
150                         MissedPropertyException, MultiplyDefinedException {
151                 LOG.debug(">>>>> BEGIN testGet()");
152                 KnowledgeElement aKelm = getKnowledgeElement();
153                 // Call DAO's create method for a good transient knowledge element.
154                 Long id = _knowledgeElementDAO.create(aKelm);
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                 // Call DAO's get method for an existing id.
160                 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
161
162                 compareObjects(aKelmFound, aKelm);
163
164                 // Call DAO's get method for a not existing id.
165                 try {
166                         aKelmFound = _knowledgeElementDAO.get(-1L);
167                         getHibernateTemplate().flush();
168                         Assert
169                                         .fail("Getting an object with not existing id must be failed.");
170                 } catch (Exception e) {
171                         LOG.debug("Expected exception is thrown: "
172                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
173                 }
174                 try {
175                         aKelmFound = _knowledgeElementDAO.get(0L);
176                         getHibernateTemplate().flush();
177                         Assert
178                                         .fail("Getting an object with not existing id must be failed.");
179                 } catch (Exception e) {
180                         LOG.debug("Expected exception is thrown: "
181                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
182                 }
183                 try {
184                         aKelmFound = _knowledgeElementDAO.get(id + 1);
185                         getHibernateTemplate().flush();
186                         Assert
187                                         .fail("Getting an object with not existing id must be failed.");
188                 } catch (Exception e) {
189                         LOG.debug("Expected exception is thrown: "
190                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
191                 }
192                 LOG.debug(">>>>> END testGet()");
193         }
194
195         /**
196          * Test of updating a knowledge element.<BR>
197          * <B>Description :</B> <BR>
198          * <i>Create a knowledge element and try to update it with another data.</i><BR>
199          * <B>Action : </B><BR>
200          * <i>1. call DAO's update method for an existing id.</i><BR>
201          * <i>2. call DAO's update method for a not existing id.</i><BR>
202          * <i>3. call DAO's update method for wrong data.</i><BR>
203          * <B>Test data : </B><BR>
204          * <i>no input parameters</i><BR>
205          * <i>no input parameters</i><BR>
206          * <i>no input parameters</i><BR>
207          * 
208          * <B>Outcome results:</B><BR>
209          * <i>
210          * <ul>
211          * <li>Object is update in the database successfully<BR>
212          * </li>
213          * <li>Exception is thrown<BR>
214          * </li>
215          * <li>Exception is thrown<BR>
216          * </li>
217          * </ul>
218          * </i>
219          * 
220          * @throws InvalidPropertyException
221          *             if an invalid property is used when creating objects
222          * @throws MultiplyDefinedException
223          *             when trying to create an object with already existing id
224          * @throws MissedPropertyException
225          *             if a mandatory property is not defined for an object to be created
226          * 
227          */
228         @Test
229         public void testUpdate() throws InvalidPropertyException,
230                         MissedPropertyException, MultiplyDefinedException {
231                 LOG.debug(">>>>> BEGIN testUpdate()");
232                 KnowledgeElement aKelm = getKnowledgeElement();
233                 // Call DAO's create method for a good transient knowledge element.
234                 Long id = _knowledgeElementDAO.create(aKelm);
235                 Assert.assertNotNull(id,
236                                 "Create method returns null instead of a new id.");
237                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
238
239                 // Call DAO's update method for an existing id.
240                 Assert
241                                 .assertTrue(aKelm.getProgressState() != ProgressState.APPROVED,
242                                                 "The initial state of the knowledge element should not be APPROVED.");
243                 aKelm.setProgressState(ProgressState.APPROVED);
244                 aKelm.setTitle(aKelm.getTitle() + " updated");
245                 _knowledgeElementDAO.update(aKelm);
246
247                 // Check that the object has been updated.
248                 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
249
250                 compareObjects(aKelmFound, aKelm);
251
252                 // Call DAO's create method for a knowledge element with non-zero id.
253                 KnowledgeElement aBadKelm = new KnowledgeElement(
254                                 (new KnowledgeElement.Properties())
255                                                 .setTitle("Test knowledge element")
256                                                 .setAuthor(aKelm.getAuthor())
257                                                 .setOwnerScenario(aKelm.getOwnerScenario())
258                                                 .setType(aKelm.getType())
259                                                 .setDate(aKelm.getDate())
260                                                 .setValue(
261                                                                 "This is another test knowledge element.\nIt is created by the unit test."));
262                 // aBadKelm.setIndex(aKelmFound.getIndex());
263                 try {
264                         _knowledgeElementDAO.update(aBadKelm);
265                         getHibernateTemplate().flush();
266                         Assert.fail("Update with not existing id must be failed.");
267                 } catch (Exception e) {
268                         LOG.debug("Expected exception is thrown: "
269                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
270                 }
271                 // Call update with bad data (null title).
272                 aBadKelm.setIndex(aKelmFound.getIndex());
273                 aBadKelm.setTitle(null);
274                 try {
275                         _knowledgeElementDAO.update(aBadKelm);
276                         getHibernateTemplate().flush();
277                         Assert.fail("Update with null title must be failed.");
278                 } catch (Exception e) {
279                         LOG.debug("Expected exception is thrown: "
280                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
281                 }
282                 // Call update with bad data (null state).
283                 aBadKelm.setTitle(aKelmFound.getTitle());
284                 aBadKelm.setProgressState(null);
285                 try {
286                         _knowledgeElementDAO.update(aBadKelm);
287                         getHibernateTemplate().flush();
288                         Assert.fail("Update with null state must be failed.");
289                 } catch (Exception e) {
290                         LOG.debug("Expected exception is thrown: "
291                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
292                 }
293                 LOG.debug(">>>>> END testUpdate()");
294         }
295
296         /**
297          * Test of deleting a knowledge element.<BR>
298          * <B>Description :</B> <BR>
299          * <i>Create a knowledge element and try to delete it.</i><BR>
300          * <B>Action : </B><BR>
301          * <i>1. call DAO's delete method for an existing id.</i><BR>
302          * <i>2. call DAO's delete method for a not existing id.</i><BR>
303          * <B>Test data : </B><BR>
304          * <i>no input parameters</i><BR>
305          * <i>no input parameters</i><BR>
306          * 
307          * <B>Outcome results:</B><BR>
308          * <i>
309          * <ul>
310          * <li>Object is found in the database successfully<BR>
311          * </li>
312          * <li>Exception is thrown<BR>
313          * </li>
314          * </ul>
315          * </i>
316          * 
317          * @throws InvalidPropertyException
318          *             if an invalid property is used when creating objects
319          * @throws MultiplyDefinedException
320          *             when trying to create an object with already existing id
321          * @throws MissedPropertyException
322          *             if a mandatory property is not defined for an object to be created
323          * 
324          */
325         @Test
326         public void testDelete() throws InvalidPropertyException,
327                         MissedPropertyException, MultiplyDefinedException {
328                 LOG.debug(">>>>> BEGIN testDelete()");
329                 KnowledgeElement aKelm = getKnowledgeElement();
330                 // Call DAO's create method for a good transient knowledge element.
331                 Long id = _knowledgeElementDAO.create(aKelm);
332                 Assert.assertNotNull(id,
333                                 "Create method returns null instead of a new id.");
334                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
335
336                 // Call DAO's delete method for an existing id.
337                 _knowledgeElementDAO.delete(aKelm);
338
339                 // Check that the object has been deleted.
340                 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
341
342                 Assert.assertNull(aKelmFound, "Deleted object must not be found.");
343                 // Call DAO's delete method for a not existing id.
344                 try {
345                         _knowledgeElementDAO.delete(aKelm);
346                         getHibernateTemplate().flush();
347                         Assert.fail("Delete with with not existing id must be failed.");
348                 } catch (Exception e) {
349                         LOG.debug("Expected exception is thrown: "
350                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
351                 }
352                 LOG.debug(">>>>> END testDelete()");
353         }
354
355         /**
356          * Create a transient KnowledgeElement for tests.
357          * 
358          * @return a transient KnowledgeElement
359          * @throws InvalidPropertyException
360          *             if an invalid property is used when creating objects
361          * @throws MultiplyDefinedException
362          *             when trying to create an object with already existing id
363          * @throws MissedPropertyException
364          *             if a mandatory property is not defined for an object to be created
365          */
366         private KnowledgeElement getKnowledgeElement()
367                         throws InvalidPropertyException, MissedPropertyException,
368                         MultiplyDefinedException {
369                 // Create a test knowledge type
370                 HibernateTemplate ht = getHibernateTemplate();
371
372                 KnowledgeElement.Properties kprops = new KnowledgeElement.Properties();
373                 KnowledgeElementType aKType = new KnowledgeElementType("TST_kelmtype");
374                 ht.save(aKType);
375                 // Create a test user
376                 User.Properties uprop = new User.Properties();
377                 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
378                                 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
379                                 .addRole("TST_user").setMailAddress(
380                                                 "noreply@salome-platform.org");
381                 uprop.disableCheck();
382                 User anAuthor = new User(uprop);
383                 ht.update(anAuthor);
384                 // Create a test study
385                 Study.Properties stprops = new Study.Properties().setReference(
386                                 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
387                 Study aStudy = new Study(stprops);
388                 ht.saveOrUpdate(aStudy);
389                 // Create a test scenario
390                 Scenario.Properties sprops = new Scenario.Properties().setTitle(
391                                 "TST_Study").setManager(anAuthor).setOwnerStudy(aStudy);
392                 Scenario aScenario = new Scenario(sprops);
393                 aStudy.getScenariiList().add(aScenario);
394                 ht.saveOrUpdate(anAuthor);
395                 ht.saveOrUpdate(aKType);
396                 ht.update(aStudy);
397                 ht.saveOrUpdate(aScenario);
398
399                 Date aDate = new Date();
400
401                 // Prepare a knowledge element transient object
402                 kprops
403                                 .setTitle("Test knowledge element")
404                                 .setAuthor(anAuthor)
405                                 .setOwnerScenario(aScenario)
406                                 .setType(aKType)
407                                 .setDate(aDate)
408                                 .setValue(
409                                                 "This is the test knowledge element.\nIt is created by the unit test.");
410
411                 return new KnowledgeElement(kprops);
412         }
413
414         /**
415          * Check that given objects are equal.
416          * 
417          * @param anActual
418          *            the object to check
419          * @param anExpected
420          *            the expected object
421          */
422         private void compareObjects(KnowledgeElement anActual,
423                         KnowledgeElement anExpected) {
424                 Assert.assertNotNull(anActual,
425                                 "Created object is not found in the database.");
426                 Assert.assertEquals(anActual.getAuthor(), anExpected.getAuthor(),
427                                 "Knowledge Element author is not saved.");
428                 Assert.assertEquals(anActual.getDate(), anExpected.getDate(),
429                                 "Knowledge Element date is not saved.");
430                 Assert
431                                 .assertEquals(anActual.getOwnerScenario(), anExpected
432                                                 .getOwnerScenario(),
433                                                 "Knowledge Element scenario is not saved.");
434                 Assert.assertEquals(anActual.getProgressState(), anExpected
435                                 .getProgressState(), "Knowledge Element state is not saved.");
436                 Assert.assertEquals(anActual.getTitle(), anExpected.getTitle(),
437                                 "Knowledge Element title is not saved.");
438                 Assert.assertEquals(anActual.getType(), anExpected.getType(),
439                                 "Knowledge Element type is not saved.");
440                 Assert.assertEquals(anActual.getValue(), anExpected.getValue(),
441                                 "Knowledge Element value is not saved.");
442                 Assert.assertEquals(anActual.getIndex(), anExpected.getIndex(),
443                                 "Knowledge Element index is not saved.");
444         }
445 }