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 found 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 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.");
240 // Call DAO's update method for an existing id.
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);
248 // Check that the object has been updated.
249 KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
251 compareObjects(aKelmFound, aKelm);
253 LOG.debug(">>>>> END testUpdate()");
257 public void testDelete() {
258 LOG.debug(">>>>> BEGIN testDelete()");
259 LOG.debug(">>>>> END testDelete()");
263 * Create a transient KnowledgeElement for tests.
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
273 private KnowledgeElement getKnowledgeElement()
274 throws InvalidPropertyException, MissedPropertyException,
275 MultiplyDefinedException {
276 // Create a test knowledge type
277 HibernateTemplate ht = getHibernateTemplate();
279 KnowledgeElement.Properties kprops = new KnowledgeElement.Properties();
280 KnowledgeElementType aKType = new KnowledgeElementType("TST_kelmtype");
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);
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);
304 ht.saveOrUpdate(aScenario);
306 Date aDate = new Date();
308 // Prepare a knowledge element transient object
310 .setTitle("Test knowledge element")
312 .setOwnerScenario(aScenario)
316 "This is the test knowledge element.\nIt is created by the unit test.");
318 return new KnowledgeElement(kprops);
322 * Check that given objects are equal.
325 * the object to check
327 * the expected object
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.");
338 .assertEquals(anActual.getOwnerScenario(), anExpected
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.");