]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/test/splat/dao/TestKnowledgeElementDAO.java
Salome HOME
965cef0cec2e86b377e6e882bdabd180a4a7f718
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / dao / TestKnowledgeElementDAO.java
1 /*****************************************************************************
2  * Company         EURIWARE
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 found 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                 LOG.debug(">>>>> BEGIN testGet()");
233                 KnowledgeElement aKelm = getKnowledgeElement();
234                 // Call DAO's create method for a good transient knowledge element.
235                 Long id = _knowledgeElementDAO.create(aKelm);
236                 Assert.assertNotNull(id,
237                                 "Create method returns null instead of a new id.");
238                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
239
240                 // Call DAO's update method for an existing id.
241                 Assert
242                                 .assertTrue(aKelm.getProgressState() != ProgressState.APPROVED,
243                                                 "The initial state of the knowledge element should not be APPROVED.");
244                 aKelm.setProgressState(ProgressState.APPROVED);
245                 aKelm.setTitle(aKelm.getTitle() + " updated");
246                 _knowledgeElementDAO.update(aKelm);
247
248                 // Check that the object has been updated.
249                 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
250
251                 compareObjects(aKelmFound, aKelm);
252
253                 LOG.debug(">>>>> END testUpdate()");
254         }
255
256         @Test
257         public void testDelete() {
258                 LOG.debug(">>>>> BEGIN testDelete()");
259                 LOG.debug(">>>>> END testDelete()");
260         }
261
262         /**
263          * Create a transient KnowledgeElement for tests.
264          * 
265          * @return a transient KnowledgeElement
266          * @throws InvalidPropertyException
267          *             if an invalid property is used when creating objects
268          * @throws MultiplyDefinedException
269          *             when trying to create an object with already existing id
270          * @throws MissedPropertyException
271          *             if a mandatory property is not defined for an object to be created
272          */
273         private KnowledgeElement getKnowledgeElement()
274                         throws InvalidPropertyException, MissedPropertyException,
275                         MultiplyDefinedException {
276                 // Create a test knowledge type
277                 HibernateTemplate ht = getHibernateTemplate();
278
279                 KnowledgeElement.Properties kprops = new KnowledgeElement.Properties();
280                 KnowledgeElementType aKType = new KnowledgeElementType("TST_kelmtype");
281                 ht.save(aKType);
282                 // Create a test user
283                 User.Properties uprop = new User.Properties();
284                 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
285                                 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
286                                 .addRole("TST_user").setMailAddress(
287                                                 "noreply@salome-platform.org");
288                 uprop.disableCheck();
289                 User anAuthor = new User(uprop);
290                 ht.update(anAuthor);
291                 // Create a test study
292                 Study.Properties stprops = new Study.Properties().setReference(
293                                 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
294                 Study aStudy = new Study(stprops);
295                 ht.saveOrUpdate(aStudy);
296                 // Create a test scenario
297                 Scenario.Properties sprops = new Scenario.Properties().setTitle(
298                                 "TST_Study").setManager(anAuthor).setOwnerStudy(aStudy);
299                 Scenario aScenario = new Scenario(sprops);
300                 aStudy.getScenariiList().add(aScenario);
301                 ht.saveOrUpdate(anAuthor);
302                 ht.saveOrUpdate(aKType);
303                 ht.update(aStudy);
304                 ht.saveOrUpdate(aScenario);
305
306                 Date aDate = new Date();
307
308                 // Prepare a knowledge element transient object
309                 kprops
310                                 .setTitle("Test knowledge element")
311                                 .setAuthor(anAuthor)
312                                 .setOwnerScenario(aScenario)
313                                 .setType(aKType)
314                                 .setDate(aDate)
315                                 .setValue(
316                                                 "This is the test knowledge element.\nIt is created by the unit test.");
317
318                 return new KnowledgeElement(kprops);
319         }
320
321         /**
322          * Check that given objects are equal.
323          * 
324          * @param anActual
325          *            the object to check
326          * @param anExpected
327          *            the expected object
328          */
329         private void compareObjects(KnowledgeElement anActual,
330                         KnowledgeElement anExpected) {
331                 Assert.assertNotNull(anActual,
332                                 "Created object is not found in the database.");
333                 Assert.assertEquals(anActual.getAuthor(), anExpected.getAuthor(),
334                                 "Knowledge Element author is not saved.");
335                 Assert.assertEquals(anActual.getDate(), anExpected.getDate(),
336                                 "Knowledge Element date is not saved.");
337                 Assert
338                                 .assertEquals(anActual.getOwnerScenario(), anExpected
339                                                 .getOwnerScenario(),
340                                                 "Knowledge Element scenario is not saved.");
341                 Assert.assertEquals(anActual.getProgressState(), anExpected
342                                 .getProgressState(), "Knowledge Element state is not saved.");
343                 Assert.assertEquals(anActual.getTitle(), anExpected.getTitle(),
344                                 "Knowledge Element title is not saved.");
345                 Assert.assertEquals(anActual.getType(), anExpected.getType(),
346                                 "Knowledge Element type is not saved.");
347                 Assert.assertEquals(anActual.getValue(), anExpected.getValue(),
348                                 "Knowledge Element value is not saved.");
349                 Assert.assertEquals(anActual.getIndex(), anExpected.getIndex(),
350                                 "Knowledge Element index is not saved.");
351         }
352 }