Salome HOME
DAO's read() method is renamed to get(). Unit test methods for KnowledgeElementDAO...
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / dao / TestKnowledgeElementDAO.java
index b2d5222dfbd40ad08fd82c6b96c29224b6a5fe60..965cef0cec2e86b377e6e882bdabd180a4a7f718 100644 (file)
@@ -13,6 +13,7 @@ import java.util.Date;
 import org.splat.dal.bo.kernel.User;
 import org.splat.dal.bo.som.KnowledgeElement;
 import org.splat.dal.bo.som.KnowledgeElementType;
+import org.splat.dal.bo.som.ProgressState;
 import org.splat.dal.bo.som.Scenario;
 import org.splat.dal.bo.som.Study;
 import org.splat.dal.dao.som.KnowledgeElementDAO;
@@ -20,7 +21,6 @@ import org.splat.kernel.InvalidPropertyException;
 import org.splat.kernel.MissedPropertyException;
 import org.splat.kernel.MultiplyDefinedException;
 import org.splat.log.AppLogger;
-import org.splat.service.StudyService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.orm.hibernate3.HibernateTemplate;
@@ -50,13 +50,6 @@ public class TestKnowledgeElementDAO extends BaseTest {
        @Qualifier("knowledgeElementDAO")
        private transient KnowledgeElementDAO _knowledgeElementDAO;
 
-       /**
-        * The StudyService injected bean.
-        */
-       @Autowired
-       @Qualifier("studyService")
-       private StudyService _studyService;
-
        /**
         * Test creation of a knowledge element.<BR>
         * <B>Description :</B> <BR>
@@ -89,7 +82,7 @@ public class TestKnowledgeElementDAO extends BaseTest {
        @Test
        public void testCreate() throws InvalidPropertyException,
                        MissedPropertyException, MultiplyDefinedException {
-               LOG.debug(">>>>> RUN testCreate()");
+               LOG.debug(">>>>> BEGIN testCreate()");
 
                KnowledgeElement aKelm = getKnowledgeElement();
                // Call DAO's create method for a good transient knowledge element.
@@ -99,26 +92,7 @@ public class TestKnowledgeElementDAO extends BaseTest {
                Assert.assertTrue(id > 0, "The new id is not a positive number.");
                KnowledgeElement aKelmFound = getHibernateTemplate().get(
                                KnowledgeElement.class, id);
-               Assert.assertNotNull(aKelmFound,
-                               "Created object is not found in the database.");
-               Assert.assertEquals(aKelmFound.getAuthor(), aKelm.getAuthor(),
-                               "Knowledge Element author is not saved.");
-               Assert.assertEquals(aKelmFound.getDate(), aKelm.getDate(),
-                               "Knowledge Element date is not saved.");
-               Assert
-                               .assertEquals(aKelmFound.getOwnerScenario(), aKelm
-                                               .getOwnerScenario(),
-                                               "Knowledge Element scenario is not saved.");
-               Assert.assertEquals(aKelmFound.getProgressState(), aKelm
-                               .getProgressState(), "Knowledge Element state is not saved.");
-               Assert.assertEquals(aKelmFound.getTitle(), aKelm.getTitle(),
-                               "Knowledge Element title is not saved.");
-               Assert.assertEquals(aKelmFound.getType(), aKelm.getType(),
-                               "Knowledge Element type is not saved.");
-               Assert.assertEquals(aKelmFound.getValue(), aKelm.getValue(),
-                               "Knowledge Element value is not saved.");
-               Assert.assertEquals(aKelmFound.getIndex(), aKelm.getIndex(),
-                               "Knowledge Element index is not saved.");
+               compareObjects(aKelmFound, aKelm);
 
                // Call DAO's create method for a knowledge element with non-zero id.
                KnowledgeElement aBadKelm = new KnowledgeElement(
@@ -139,6 +113,150 @@ public class TestKnowledgeElementDAO extends BaseTest {
                        LOG.debug("Expected exception is thrown: "
                                        + e.getClass().getSimpleName() + ": " + e.getMessage());
                }
+               LOG.debug(">>>>> END testCreate()");
+       }
+
+       /**
+        * Test of getting a knowledge element.<BR>
+        * <B>Description :</B> <BR>
+        * <i>Create a knowledge element and try to get it from the database.</i><BR>
+        * <B>Action : </B><BR>
+        * <i>1. call DAO's read method for an existing id.</i><BR>
+        * <i>2. call DAO's read method for a not existing id.</i><BR>
+        * <B>Test data : </B><BR>
+        * <i>no input parameters</i><BR>
+        * <i>no input parameters</i><BR>
+        * 
+        * <B>Outcome results:</B><BR>
+        * <i>
+        * <ul>
+        * <li>Object is found in the database successfully<BR>
+        * </li>
+        * <li>Exception is thrown<BR>
+        * </li>
+        * </ul>
+        * </i>
+        * 
+        * @throws InvalidPropertyException
+        *             if an invalid property is used when creating objects
+        * @throws MultiplyDefinedException
+        *             when trying to create an object with already existing id
+        * @throws MissedPropertyException
+        *             if a mandatory property is not defined for an object to be created
+        * 
+        */
+       @Test
+       public void testGet() throws InvalidPropertyException,
+                       MissedPropertyException, MultiplyDefinedException {
+               LOG.debug(">>>>> BEGIN testGet()");
+               KnowledgeElement aKelm = getKnowledgeElement();
+               // Call DAO's create method for a good transient knowledge element.
+               Long id = _knowledgeElementDAO.create(aKelm);
+               Assert.assertNotNull(id,
+                               "Create method returns null instead of a new id.");
+               Assert.assertTrue(id > 0, "The new id is not a positive number.");
+
+               // Call DAO's get method for an existing id.
+               KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
+
+               compareObjects(aKelmFound, aKelm);
+
+               // Call DAO's get method for a not existing id.
+               try {
+                       aKelmFound = _knowledgeElementDAO.get(-1L);
+                       getHibernateTemplate().flush();
+                       Assert
+                                       .fail("Getting an object with not existing id must be failed.");
+               } catch (Exception e) {
+                       LOG.debug("Expected exception is thrown: "
+                                       + e.getClass().getSimpleName() + ": " + e.getMessage());
+               }
+               try {
+                       aKelmFound = _knowledgeElementDAO.get(0L);
+                       getHibernateTemplate().flush();
+                       Assert
+                                       .fail("Getting an object with not existing id must be failed.");
+               } catch (Exception e) {
+                       LOG.debug("Expected exception is thrown: "
+                                       + e.getClass().getSimpleName() + ": " + e.getMessage());
+               }
+               try {
+                       aKelmFound = _knowledgeElementDAO.get(id + 1);
+                       getHibernateTemplate().flush();
+                       Assert
+                                       .fail("Getting an object with not existing id must be failed.");
+               } catch (Exception e) {
+                       LOG.debug("Expected exception is thrown: "
+                                       + e.getClass().getSimpleName() + ": " + e.getMessage());
+               }
+               LOG.debug(">>>>> END testGet()");
+       }
+
+       /**
+        * Test of updating a knowledge element.<BR>
+        * <B>Description :</B> <BR>
+        * <i>Create a knowledge element and try to update it with another data.</i><BR>
+        * <B>Action : </B><BR>
+        * <i>1. call DAO's update method for an existing id.</i><BR>
+        * <i>2. call DAO's update method for a not existing id.</i><BR>
+        * <i>3. call DAO's update method for wrong data.</i><BR>
+        * <B>Test data : </B><BR>
+        * <i>no input parameters</i><BR>
+        * <i>no input parameters</i><BR>
+        * <i>no input parameters</i><BR>
+        * 
+        * <B>Outcome results:</B><BR>
+        * <i>
+        * <ul>
+        * <li>Object is found in the database successfully<BR>
+        * </li>
+        * <li>Exception is thrown<BR>
+        * </li>
+        * <li>Exception is thrown<BR>
+        * </li>
+        * </ul>
+        * </i>
+        * 
+        * @throws InvalidPropertyException
+        *             if an invalid property is used when creating objects
+        * @throws MultiplyDefinedException
+        *             when trying to create an object with already existing id
+        * @throws MissedPropertyException
+        *             if a mandatory property is not defined for an object to be created
+        * 
+        */
+       @Test
+       public void testUpdate() throws InvalidPropertyException,
+                       MissedPropertyException, MultiplyDefinedException {
+               LOG.debug(">>>>> BEGIN testUpdate()");
+               LOG.debug(">>>>> BEGIN testGet()");
+               KnowledgeElement aKelm = getKnowledgeElement();
+               // Call DAO's create method for a good transient knowledge element.
+               Long id = _knowledgeElementDAO.create(aKelm);
+               Assert.assertNotNull(id,
+                               "Create method returns null instead of a new id.");
+               Assert.assertTrue(id > 0, "The new id is not a positive number.");
+
+               // Call DAO's update method for an existing id.
+               Assert
+                               .assertTrue(aKelm.getProgressState() != ProgressState.APPROVED,
+                                               "The initial state of the knowledge element should not be APPROVED.");
+               aKelm.setProgressState(ProgressState.APPROVED);
+               aKelm.setTitle(aKelm.getTitle() + " updated");
+               _knowledgeElementDAO.update(aKelm);
+
+               // Check that the object has been updated.
+               KnowledgeElement aKelmFound = _knowledgeElementDAO.get(id);
+
+               compareObjects(aKelmFound, aKelm);
+
+               LOG.debug(">>>>> END testUpdate()");
+       }
+
+       @Test
+       public void testDelete() {
+               LOG.debug(">>>>> BEGIN testDelete()");
+               LOG.debug(">>>>> END testDelete()");
        }
 
        /**
@@ -199,4 +317,36 @@ public class TestKnowledgeElementDAO extends BaseTest {
 
                return new KnowledgeElement(kprops);
        }
+
+       /**
+        * Check that given objects are equal.
+        * 
+        * @param anActual
+        *            the object to check
+        * @param anExpected
+        *            the expected object
+        */
+       private void compareObjects(KnowledgeElement anActual,
+                       KnowledgeElement anExpected) {
+               Assert.assertNotNull(anActual,
+                               "Created object is not found in the database.");
+               Assert.assertEquals(anActual.getAuthor(), anExpected.getAuthor(),
+                               "Knowledge Element author is not saved.");
+               Assert.assertEquals(anActual.getDate(), anExpected.getDate(),
+                               "Knowledge Element date is not saved.");
+               Assert
+                               .assertEquals(anActual.getOwnerScenario(), anExpected
+                                               .getOwnerScenario(),
+                                               "Knowledge Element scenario is not saved.");
+               Assert.assertEquals(anActual.getProgressState(), anExpected
+                               .getProgressState(), "Knowledge Element state is not saved.");
+               Assert.assertEquals(anActual.getTitle(), anExpected.getTitle(),
+                               "Knowledge Element title is not saved.");
+               Assert.assertEquals(anActual.getType(), anExpected.getType(),
+                               "Knowledge Element type is not saved.");
+               Assert.assertEquals(anActual.getValue(), anExpected.getValue(),
+                               "Knowledge Element value is not saved.");
+               Assert.assertEquals(anActual.getIndex(), anExpected.getIndex(),
+                               "Knowledge Element index is not saved.");
+       }
 }