1 /*****************************************************************************
5 * Creation date 12 Oct 2012
8 *****************************************************************************/
9 package test.splat.dao;
11 import java.util.Date;
12 import java.util.List;
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;
33 import test.splat.common.BaseTest;
36 * Test class for KnowledgeElementDAO.
38 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
41 public class TestKnowledgeElementDAO extends BaseTest {
44 * Logger for the class.
46 private static final AppLogger LOG = AppLogger
47 .getLogger(TestKnowledgeElementDAO.class);
50 * The tested KnowledgeElementDAO. Later injected by Spring.
53 @Qualifier("knowledgeElementDAO")
54 private transient KnowledgeElementDAO _knowledgeElementDAO;
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>
67 * <B>Outcome results:</B><BR>
70 * <li>Object is created in the database successfully<BR>
72 * <li>Another new object is created with new generated id<BR>
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
86 public void testCreate() throws InvalidPropertyException,
87 MissedPropertyException, MultiplyDefinedException {
88 LOG.debug(">>>>> BEGIN testCreate()");
89 startNestedTransaction();
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.");
98 LOG.debug("Created id: " + id);
100 KnowledgeElement aKelmFound = getHibernateTemplate().get(
101 KnowledgeElement.class, id);
102 compareObjects(aKelmFound, aKelm);
103 _knowledgeElementDAO.flush();
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())
114 "This is another test knowledge element.\nIt is created by the unit test."));
115 aBadKelm.setIndex(aKelmFound.getIndex());
117 _knowledgeElementDAO.flush();
118 List<KnowledgeElement> res = _knowledgeElementDAO.getAll(Order
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.");
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.");
131 long id2 = _knowledgeElementDAO.create(aBadKelm);
133 _knowledgeElementDAO.flush();
135 LOG.debug("Created second id: " + id2);
137 res = _knowledgeElementDAO.getAll(Order.asc("rid"));
138 _knowledgeElementDAO.flush();
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.");
155 rollbackNestedTransaction();
156 LOG.debug(">>>>> END testCreate()");
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>
170 * <B>Outcome results:</B><BR>
173 * <li>Object is found in the database successfully<BR>
175 * <li>Result of search is null<BR>
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
189 public void testGet() throws InvalidPropertyException,
190 MissedPropertyException, MultiplyDefinedException {
191 LOG.debug(">>>>> BEGIN testGet()");
192 startNestedTransaction();
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.");
201 // Call DAO's get method for an existing id.
202 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
204 compareObjects(aKelmFound, aKelm);
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.");
212 aKelmFound = _knowledgeElementDAO.get(0L);
213 getHibernateTemplate().flush();
214 Assert.assertNull(aKelmFound,
215 "A found object with not existing id must be null.");
217 aKelmFound = _knowledgeElementDAO.get(id + 1);
218 getHibernateTemplate().flush();
219 Assert.assertNull(aKelmFound,
220 "A found object with not existing id must be null.");
222 rollbackNestedTransaction();
223 LOG.debug(">>>>> END testGet()");
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>
239 * <B>Outcome results:</B><BR>
242 * <li>Object is update in the database successfully<BR>
244 * <li>Exception is thrown<BR>
246 * <li>Exception is thrown<BR>
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
260 public void testUpdate() throws InvalidPropertyException,
261 MissedPropertyException, MultiplyDefinedException {
262 LOG.debug(">>>>> BEGIN testUpdate()");
263 startNestedTransaction();
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.");
272 // Call DAO's update method for an existing id.
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);
280 // Check that the object has been updated.
281 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
283 compareObjects(aKelmFound, aKelm);
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())
294 "This is another test knowledge element.\nIt is created by the unit test."));
295 // aBadKelm.setIndex(aKelmFound.getIndex());
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());
304 // Call update with bad data (null title).
305 aBadKelm.setIndex(aKelmFound.getIndex());
306 aBadKelm.setTitle(null);
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());
315 // Call update with bad data (null state).
316 aBadKelm.setTitle(aKelmFound.getTitle());
317 aBadKelm.setProgressState(null);
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());
327 rollbackNestedTransaction();
328 LOG.debug(">>>>> END testUpdate()");
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>
342 * <B>Outcome results:</B><BR>
345 * <li>Object is found in the database successfully<BR>
347 * <li>Exception is thrown<BR>
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
361 public void testDelete() throws InvalidPropertyException,
362 MissedPropertyException, MultiplyDefinedException {
363 LOG.debug(">>>>> BEGIN testDelete()");
364 startNestedTransaction();
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.");
373 // Call DAO's delete method for an existing id.
374 _knowledgeElementDAO.delete(aKelm);
376 // Check that the object has been deleted.
377 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
379 Assert.assertNull(aKelmFound, "Deleted object must not be found.");
380 // Call DAO's delete method for a not existing id.
381 getHibernateTemplate().flush();
383 rollbackNestedTransaction();
384 LOG.debug(">>>>> END testDelete()");
388 * Create a transient KnowledgeElement for tests.
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
398 private KnowledgeElement getKnowledgeElement()
399 throws InvalidPropertyException, MissedPropertyException,
400 MultiplyDefinedException {
401 // Create a test knowledge type
402 HibernateTemplate ht = getHibernateTemplate();
404 KnowledgeElement.Properties kprops = new KnowledgeElement.Properties();
405 KnowledgeElementType aKType = new KnowledgeElementType("TST_kelmtype");
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);
429 ht.saveOrUpdate(aScenario);
431 Date aDate = new Date();
433 // Prepare a knowledge element transient object
435 .setTitle("1. Test knowledge element")
437 .setOwnerScenario(aScenario)
441 "This is the test knowledge element.\nIt is created by the unit test.");
443 return new KnowledgeElement(kprops);
447 * Check that given objects are equal.
450 * the object to check
452 * the expected object
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.");
463 .assertEquals(anActual.getOwnerScenario(), anExpected
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.");
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>
489 * <B>Outcome results:</B><BR>
492 * <li>Objects are found in the database successfully<BR>
494 * <li>Objects are found in the database and sorted in the right order<BR>
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
508 public void testGetFilteredList() throws InvalidPropertyException,
509 MissedPropertyException, MultiplyDefinedException {
510 LOG.debug(">>>>> BEGIN testGetFilteredList()");
511 startNestedTransaction();
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())
524 "This is another test knowledge element.\nIt is created by the unit test."));
525 long id2 = _knowledgeElementDAO.create(aNewKelm);
527 _knowledgeElementDAO.flush();
529 // ////////////////////////////////////////////////////
530 // Call DAO's getFilteredList method without order parameter.
531 List<KnowledgeElement> res = _knowledgeElementDAO.getFilteredList(
532 "type", Restrictions.eq("name", "TST_kelmtype"));
534 Assert.assertNotNull(res,
535 "Method getFilteredList must not return null.");
536 Assert.assertEquals(res.size(), 2, "Number of found objects is wrong.");
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"));
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.");
551 rollbackNestedTransaction();
552 LOG.debug(">>>>> END testGetFilteredList()");