1 /*****************************************************************************
5 * Creation date 12 Oct 2012
8 *****************************************************************************/
9 package test.splat.dao;
11 import java.util.Date;
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;
30 import test.splat.common.BaseTest;
33 * Test class for KnowledgeElementDAO.
35 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
38 public class TestKnowledgeElementDAO extends BaseTest {
41 * Logger for the class.
43 private static final AppLogger LOG = AppLogger
44 .getLogger(TestKnowledgeElementDAO.class);
47 * The tested KnowledgeElementDAO. Later injected by Spring.
50 @Qualifier("knowledgeElementDAO")
51 private transient KnowledgeElementDAO _knowledgeElementDAO;
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>
64 * <B>Outcome results:</B><BR>
67 * <li>Object is created in the database successfully<BR>
69 * <li>Exception is thrown<BR>
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
83 public void testCreate() throws InvalidPropertyException,
84 MissedPropertyException, MultiplyDefinedException {
85 LOG.debug(">>>>> BEGIN testCreate()");
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);
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())
106 "This is another test knowledge element.\nIt is created by the unit test."));
107 aBadKelm.setIndex(aKelmFound.getIndex());
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());
116 LOG.debug(">>>>> END testCreate()");
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>
130 * <B>Outcome results:</B><BR>
133 * <li>Object is found in the database successfully<BR>
135 * <li>Exception is thrown<BR>
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
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.");
159 // Call DAO's get method for an existing id.
160 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
162 compareObjects(aKelmFound, aKelm);
164 // Call DAO's get method for a not existing id.
166 aKelmFound = _knowledgeElementDAO.get(-1L);
167 getHibernateTemplate().flush();
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());
175 aKelmFound = _knowledgeElementDAO.get(0L);
176 getHibernateTemplate().flush();
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());
184 aKelmFound = _knowledgeElementDAO.get(id + 1);
185 getHibernateTemplate().flush();
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());
192 LOG.debug(">>>>> END testGet()");
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>
208 * <B>Outcome results:</B><BR>
211 * <li>Object is update in the database successfully<BR>
213 * <li>Exception is thrown<BR>
215 * <li>Exception is thrown<BR>
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
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.");
239 // Call DAO's update method for an existing id.
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);
247 // Check that the object has been updated.
248 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
250 compareObjects(aKelmFound, aKelm);
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())
261 "This is another test knowledge element.\nIt is created by the unit test."));
262 // aBadKelm.setIndex(aKelmFound.getIndex());
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());
271 // Call update with bad data (null title).
272 aBadKelm.setIndex(aKelmFound.getIndex());
273 aBadKelm.setTitle(null);
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());
282 // Call update with bad data (null state).
283 aBadKelm.setTitle(aKelmFound.getTitle());
284 aBadKelm.setProgressState(null);
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());
293 LOG.debug(">>>>> END testUpdate()");
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>
307 * <B>Outcome results:</B><BR>
310 * <li>Object is found in the database successfully<BR>
312 * <li>Exception is thrown<BR>
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
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.");
336 // Call DAO's delete method for an existing id.
337 _knowledgeElementDAO.delete(aKelm);
339 // Check that the object has been deleted.
340 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
342 Assert.assertNull(aKelmFound, "Deleted object must not be found.");
343 // Call DAO's delete method for a not existing id.
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());
352 LOG.debug(">>>>> END testDelete()");
356 * Create a transient KnowledgeElement for tests.
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
366 private KnowledgeElement getKnowledgeElement()
367 throws InvalidPropertyException, MissedPropertyException,
368 MultiplyDefinedException {
369 // Create a test knowledge type
370 HibernateTemplate ht = getHibernateTemplate();
372 KnowledgeElement.Properties kprops = new KnowledgeElement.Properties();
373 KnowledgeElementType aKType = new KnowledgeElementType("TST_kelmtype");
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);
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);
397 ht.saveOrUpdate(aScenario);
399 Date aDate = new Date();
401 // Prepare a knowledge element transient object
403 .setTitle("Test knowledge element")
405 .setOwnerScenario(aScenario)
409 "This is the test knowledge element.\nIt is created by the unit test.");
411 return new KnowledgeElement(kprops);
415 * Check that given objects are equal.
418 * the object to check
420 * the expected object
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.");
431 .assertEquals(anActual.getOwnerScenario(), anExpected
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.");