Salome HOME
a1e4ee2ff178c6fa8cc21f68234a4a0d16eaed6d
[tools/siman.git] / Workspace / DaoGenerator / src / templates / TestKnowledgeElementDAO.java.vm
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   12 Oct 2012
6  * @author         $Author$
7  * @version        $Revision$
8  * Generated by Siman Generator on $date
9  *****************************************************************************/
10 package test.splat.dao;
11
12 import java.util.Date;
13
14 import org.splat.dal.bo.kernel.User;
15 import org.splat.dal.bo.som.${EntityClass};
16 import org.splat.dal.dao.som.${EntityClass}DAO;
17 import org.splat.kernel.InvalidPropertyException;
18 import org.splat.kernel.MissedPropertyException;
19 import org.splat.kernel.MultiplyDefinedException;
20 import org.splat.log.AppLogger;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.beans.factory.annotation.Qualifier;
23 import org.springframework.orm.hibernate3.HibernateTemplate;
24 import org.testng.Assert;
25 import org.testng.annotations.Test;
26
27 import test.splat.common.BaseTest;
28
29 /**
30  * Test class for ${EntityClass}DAO.
31  * 
32  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
33  * 
34  */
35 public class Test${EntityClass}DAO extends BaseTest {
36
37         /**
38          * Logger for the class.
39          */
40         private static final AppLogger LOG = AppLogger
41                         .getLogger(Test${EntityClass}DAO.class);
42
43         /**
44          * The tested ${EntityClass}DAO. Later injected by Spring.
45          */
46         @Autowired
47         @Qualifier("${DAOBean}")
48         private transient ${EntityClass}DAO _${DAOBean};
49
50         /**
51          * Test creation of an object.<BR>
52          * <B>Description :</B> <BR>
53          * <i>Create an object.</i><BR>
54          * <B>Action : </B><BR>
55          * <i>1. call DAO's create method for a good transient object.</i><BR>
56          * <i>2. call DAO's create method for an object with non-zero id.</i><BR>
57          * <B>Test data : </B><BR>
58          * <i>no input parameters</i><BR>
59          * <i>no input parameters</i><BR>
60          * 
61          * <B>Outcome results:</B><BR>
62          * <i>
63          * <ul>
64          * <li>Object is created in the database successfully<BR>
65          * </li>
66          * <li>Exception is thrown<BR>
67          * </li>
68          * </ul>
69          * </i>
70          * 
71          * @throws InvalidPropertyException
72          *             if an invalid property is used when creating objects
73          * @throws MultiplyDefinedException
74          *             when trying to create an object with already existing id
75          * @throws MissedPropertyException
76          *             if a mandatory property is not defined for an object to be created
77          * 
78          */
79         @Test
80         public void testCreate() throws InvalidPropertyException,
81                         MissedPropertyException, MultiplyDefinedException {
82                 LOG.debug(">>>>> BEGIN testCreate()");
83
84                 ${EntityClass} anObject = get${EntityClass}();
85                 // Call DAO's create method for a good transient object.
86                 Long id = _${DAOBean}.create(anObject);
87                 Assert.assertNotNull(id,
88                                 "Create method returns null instead of a new id.");
89                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
90                 ${EntityClass} anObjectFound = getHibernateTemplate().get(
91                                 ${EntityClass}.class, id);
92                 compareObjects(anObjectFound, anObject);
93
94                 // Call DAO's create method for an object with non-zero id.
95                 // TODO: Fill the object's properties
96                 ${EntityClass} aBadObj = new ${EntityClass}(
97                                 (new ${EntityClass}.Properties()));
98                 aBadObj.setIndex(anObjectFound.getIndex());
99                 try {
100                         _${DAOBean}.create(aBadObj);
101                         getHibernateTemplate().flush();
102                         Assert.fail("Creation with existing id must be failed.");
103                 } catch (Exception e) {
104                         LOG.debug("Expected exception is thrown: "
105                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
106                 }
107                 LOG.debug(">>>>> END testCreate()");
108         }
109
110         /**
111          * Test of getting an object.<BR>
112          * <B>Description :</B> <BR>
113          * <i>Create an object and try to get it from the database.</i><BR>
114          * <B>Action : </B><BR>
115          * <i>1. call DAO's read method for an existing id.</i><BR>
116          * <i>2. call DAO's read method for a not existing id.</i><BR>
117          * <B>Test data : </B><BR>
118          * <i>no input parameters</i><BR>
119          * <i>no input parameters</i><BR>
120          * 
121          * <B>Outcome results:</B><BR>
122          * <i>
123          * <ul>
124          * <li>Object is found in the database successfully<BR>
125          * </li>
126          * <li>Exception is thrown<BR>
127          * </li>
128          * </ul>
129          * </i>
130          * 
131          * @throws InvalidPropertyException
132          *             if an invalid property is used when creating objects
133          * @throws MultiplyDefinedException
134          *             when trying to create an object with already existing id
135          * @throws MissedPropertyException
136          *             if a mandatory property is not defined for an object to be created
137          * 
138          */
139         @Test
140         public void testGet() throws InvalidPropertyException,
141                         MissedPropertyException, MultiplyDefinedException {
142                 LOG.debug(">>>>> BEGIN testGet()");
143                 ${EntityClass} anObject = get${EntityClass}();
144                 // Call DAO's create method for a good transient object.
145                 Long id = _${DAOBean}.create(anObject);
146                 Assert.assertNotNull(id,
147                                 "Create method returns null instead of a new id.");
148                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
149
150                 // Call DAO's get method for an existing id.
151                 ${EntityClass} anObjectFound = _${DAOBean}.get(id);
152
153                 compareObjects(anObjectFound, anObject);
154
155                 // Call DAO's get method for a not existing id.
156                 try {
157                         anObjectFound = _${DAOBean}.get(-1L);
158                         getHibernateTemplate().flush();
159                         Assert
160                                         .fail("Getting an object with not existing id must be failed.");
161                 } catch (Exception e) {
162                         LOG.debug("Expected exception is thrown: "
163                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
164                 }
165                 try {
166                         anObjectFound = _${DAOBean}.get(0L);
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                         anObjectFound = _${DAOBean}.get(id + 1);
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                 LOG.debug(">>>>> END testGet()");
184         }
185
186         /**
187          * Test of updating an object.<BR>
188          * <B>Description :</B> <BR>
189          * <i>Create an object and try to update it with another data.</i><BR>
190          * <B>Action : </B><BR>
191          * <i>1. call DAO's update method for an existing id.</i><BR>
192          * <i>2. call DAO's update method for a not existing id.</i><BR>
193          * <i>3. call DAO's update method for wrong data.</i><BR>
194          * <B>Test data : </B><BR>
195          * <i>no input parameters</i><BR>
196          * <i>no input parameters</i><BR>
197          * <i>no input parameters</i><BR>
198          * 
199          * <B>Outcome results:</B><BR>
200          * <i>
201          * <ul>
202          * <li>Object is update in the database successfully<BR>
203          * </li>
204          * <li>Exception is thrown<BR>
205          * </li>
206          * <li>Exception is thrown<BR>
207          * </li>
208          * </ul>
209          * </i>
210          * 
211          * @throws InvalidPropertyException
212          *             if an invalid property is used when creating objects
213          * @throws MultiplyDefinedException
214          *             when trying to create an object with already existing id
215          * @throws MissedPropertyException
216          *             if a mandatory property is not defined for an object to be created
217          * 
218          */
219         @Test
220         public void testUpdate() throws InvalidPropertyException,
221                         MissedPropertyException, MultiplyDefinedException {
222                 LOG.debug(">>>>> BEGIN testUpdate()");
223                 ${EntityClass} anObject = get${EntityClass}();
224                 // Call DAO's create method for a good transient object.
225                 Long id = _${DAOBean}.create(anObject);
226                 Assert.assertNotNull(id,
227                                 "Create method returns null instead of a new id.");
228                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
229
230                 // Call DAO's update method for an existing id.
231                 // TODO: Modify some object's properties to update it later
232 /*              Assert
233                                 .assertTrue(anObject.getProgressState() != ProgressState.APPROVED,
234                                                 "The initial state of the object should not be APPROVED.");
235                 anObject.setProgressState(ProgressState.APPROVED);
236                 anObject.setTitle(anObject.getTitle() + " updated");*/
237                 
238                 _${DAOBean}.update(anObject);
239
240                 // Check that the object has been updated.
241                 ${EntityClass} anObjectFound = _${DAOBean}.get(id);
242
243                 compareObjects(anObjectFound, anObject);
244
245                 // Call DAO's create method for an object with non-zero id.
246         // TODO: Fill the object's properties
247                 ${EntityClass} aBadObj = new ${EntityClass}(
248                                 (new ${EntityClass}.Properties()));
249                 // aBadObj.setIndex(anObjectFound.getIndex());
250                 try {
251                         _${DAOBean}.update(aBadObj);
252                         getHibernateTemplate().flush();
253                         Assert.fail("Update with not existing id must be failed.");
254                 } catch (Exception e) {
255                         LOG.debug("Expected exception is thrown: "
256                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
257                 }
258                 // Call update with bad data (null title).
259                 aBadObj.setIndex(anObjectFound.getIndex());
260                 aBadObj.setTitle(null);
261                 try {
262                         _${DAOBean}.update(aBadObj);
263                         getHibernateTemplate().flush();
264                         Assert.fail("Update with null title must be failed.");
265                 } catch (Exception e) {
266                         LOG.debug("Expected exception is thrown: "
267                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
268                 }
269                 // Call update with bad data (null state).
270                 aBadObj.setTitle(anObjectFound.getTitle());
271                 aBadObj.setProgressState(null);
272                 try {
273                         _${DAOBean}.update(aBadObj);
274                         getHibernateTemplate().flush();
275                         Assert.fail("Update with null state must be failed.");
276                 } catch (Exception e) {
277                         LOG.debug("Expected exception is thrown: "
278                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
279                 }
280                 LOG.debug(">>>>> END testUpdate()");
281         }
282
283         /**
284          * Test of deleting an object.<BR>
285          * <B>Description :</B> <BR>
286          * <i>Create an object and try to delete it.</i><BR>
287          * <B>Action : </B><BR>
288          * <i>1. call DAO's delete method for an existing id.</i><BR>
289          * <i>2. call DAO's delete method for a not existing id.</i><BR>
290          * <B>Test data : </B><BR>
291          * <i>no input parameters</i><BR>
292          * <i>no input parameters</i><BR>
293          * 
294          * <B>Outcome results:</B><BR>
295          * <i>
296          * <ul>
297          * <li>Object is found in the database successfully<BR>
298          * </li>
299          * <li>Exception is thrown<BR>
300          * </li>
301          * </ul>
302          * </i>
303          * 
304          * @throws InvalidPropertyException
305          *             if an invalid property is used when creating objects
306          * @throws MultiplyDefinedException
307          *             when trying to create an object with already existing id
308          * @throws MissedPropertyException
309          *             if a mandatory property is not defined for an object to be created
310          * 
311          */
312         @Test
313         public void testDelete() throws InvalidPropertyException,
314                         MissedPropertyException, MultiplyDefinedException {
315                 LOG.debug(">>>>> BEGIN testDelete()");
316                 ${EntityClass} anObject = get${EntityClass}();
317                 // Call DAO's create method for a good transient object.
318                 Long id = _${DAOBean}.create(anObject);
319                 Assert.assertNotNull(id,
320                                 "Create method returns null instead of a new id.");
321                 Assert.assertTrue(id > 0, "The new id is not a positive number.");
322
323                 // Call DAO's delete method for an existing id.
324                 _${DAOBean}.delete(anObject);
325
326                 // Check that the object has been deleted.
327                 ${EntityClass} anObjectFound = _${DAOBean}.get(id);
328
329                 Assert.assertNull(anObjectFound, "Deleted object must not be found.");
330                 // Call DAO's delete method for a not existing id.
331                 try {
332                         _${DAOBean}.delete(anObject);
333                         getHibernateTemplate().flush();
334                         Assert.fail("Delete with with not existing id must be failed.");
335                 } catch (Exception e) {
336                         LOG.debug("Expected exception is thrown: "
337                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
338                 }
339                 LOG.debug(">>>>> END testDelete()");
340         }
341
342         /**
343          * Create a transient ${EntityClass} for tests.
344          * 
345          * @return a transient ${EntityClass}
346          * @throws InvalidPropertyException
347          *             if an invalid property is used when creating objects
348          * @throws MultiplyDefinedException
349          *             when trying to create an object with already existing id
350          * @throws MissedPropertyException
351          *             if a mandatory property is not defined for an object to be created
352          */
353         private ${EntityClass} get${EntityClass}()
354                         throws InvalidPropertyException, MissedPropertyException,
355                         MultiplyDefinedException {
356                 // Create a test knowledge type
357                 HibernateTemplate ht = getHibernateTemplate();
358
359                 ${EntityClass}.Properties kprops = new ${EntityClass}.Properties();
360                 // Prepare a transient object
361
362                 return new ${EntityClass}(kprops);
363         }
364
365         /**
366          * Check that given objects are equal.
367          * 
368          * @param anActual
369          *            the object to check
370          * @param anExpected
371          *            the expected object
372          */
373         private void compareObjects(${EntityClass} anActual,
374                         ${EntityClass} anExpected) {
375                 Assert.assertNotNull(anActual,
376                                 "Created object is not found in the database.");
377                 Assert.assertEquals(anActual.getAuthor(), anExpected.getAuthor(),
378                                 "object author is not saved.");
379                 Assert.assertEquals(anActual.getDate(), anExpected.getDate(),
380                                 "object date is not saved.");
381                 Assert
382                                 .assertEquals(anActual.getOwnerScenario(), anExpected
383                                                 .getOwnerScenario(),
384                                                 "object scenario is not saved.");
385                 Assert.assertEquals(anActual.getProgressState(), anExpected
386                                 .getProgressState(), "object state is not saved.");
387                 Assert.assertEquals(anActual.getTitle(), anExpected.getTitle(),
388                                 "object title is not saved.");
389                 Assert.assertEquals(anActual.getType(), anExpected.getType(),
390                                 "object type is not saved.");
391                 Assert.assertEquals(anActual.getValue(), anExpected.getValue(),
392                                 "object value is not saved.");
393                 Assert.assertEquals(anActual.getIndex(), anExpected.getIndex(),
394                                 "object index is not saved.");
395         }
396 }