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.Scenario;
17 import org.splat.dal.bo.som.Study;
18 import org.splat.dal.dao.som.KnowledgeElementDAO;
19 import org.splat.kernel.InvalidPropertyException;
20 import org.splat.kernel.MissedPropertyException;
21 import org.splat.kernel.MultiplyDefinedException;
22 import org.splat.log.AppLogger;
23 import org.splat.service.StudyService;
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.springframework.test.annotation.Rollback;
28 import org.testng.Assert;
29 import org.testng.annotations.Test;
31 import test.splat.common.BaseTest;
34 * Test class for KnowledgeElementDAO.
36 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
39 public class TestKnowledgeElementDAO extends BaseTest {
42 * Logger for the class.
44 private static final AppLogger LOG = AppLogger
45 .getLogger(TestKnowledgeElementDAO.class);
48 * The tested KnowledgeElementDAO. Later injected by Spring.
51 @Qualifier("knowledgeElementDAO")
52 private transient KnowledgeElementDAO _knowledgeElementDAO;
55 * The StudyService injected bean.
58 @Qualifier("studyService")
59 private StudyService _studyService;
62 * Test creation of a knowledge element.<BR>
63 * <B>Description :</B> <BR>
64 * <i>Create a knowledge element.</i><BR>
65 * <B>Action : </B><BR>
66 * <i>1. call DAO's create method for a good transient knowledge element.</i><BR>
67 * <i>2. call DAO's create method for a knowledge element with non-zero id.</i><BR>
68 * <B>Test data : </B><BR>
69 * <i>no input parameters</i><BR>
70 * <i>no input parameters</i><BR>
72 * <B>Outcome results:</B><BR>
75 * <li>Object is created in the database successfully<BR>
77 * <li>Exception is thrown<BR>
82 * @throws InvalidPropertyException
83 * if an invalid property is used when creating objects
84 * @throws MultiplyDefinedException
85 * when trying to create an object with already existing id
86 * @throws MissedPropertyException
87 * if a mandatory property is not defined for an object to be created
91 public void testCreate() throws InvalidPropertyException,
92 MissedPropertyException, MultiplyDefinedException {
93 LOG.debug(">>>>> RUN testCreate()");
95 KnowledgeElement aKelm = getKnowledgeElement();
96 // Call DAO's create method for a good transient knowledge element.
97 Long id = _knowledgeElementDAO.create(aKelm);
98 Assert.assertNotNull(id,
99 "Create method returns null instead of a new id.");
100 Assert.assertTrue(id > 0, "The new id is not a positive number.");
101 KnowledgeElement aKelmFound = getHibernateTemplate().get(
102 KnowledgeElement.class, id);
103 Assert.assertNotNull(aKelmFound,
104 "Created object is not found in the database.");
105 Assert.assertEquals(aKelmFound.getAuthor(), aKelm.getAuthor(),
106 "Knowledge Element author is not saved.");
107 Assert.assertEquals(aKelmFound.getDate(), aKelm.getDate(),
108 "Knowledge Element date is not saved.");
110 .assertEquals(aKelmFound.getOwnerScenario(), aKelm
112 "Knowledge Element scenario is not saved.");
113 Assert.assertEquals(aKelmFound.getProgressState(), aKelm
114 .getProgressState(), "Knowledge Element state is not saved.");
115 Assert.assertEquals(aKelmFound.getTitle(), aKelm.getTitle(),
116 "Knowledge Element title is not saved.");
117 Assert.assertEquals(aKelmFound.getType(), aKelm.getType(),
118 "Knowledge Element type is not saved.");
119 Assert.assertEquals(aKelmFound.getValue(), aKelm.getValue(),
120 "Knowledge Element value is not saved.");
121 Assert.assertEquals(aKelmFound.getIndex(), aKelm.getIndex(),
122 "Knowledge Element index is not saved.");
124 // Call DAO's create method for a knowledge element with non-zero id.
125 KnowledgeElement aBadKelm = new KnowledgeElement(
126 (new KnowledgeElement.Properties())
127 .setTitle("Test knowledge element")
128 .setAuthor(aKelm.getAuthor())
129 .setOwnerScenario(aKelm.getOwnerScenario())
130 .setType(aKelm.getType())
131 .setDate(aKelm.getDate())
133 "This is another test knowledge element.\nIt is created by the unit test."));
134 aBadKelm.setIndex(aKelmFound.getIndex());
136 _knowledgeElementDAO.create(aBadKelm);
137 getHibernateTemplate().flush();
138 Assert.fail("Creation with existing id must be failed.");
139 } catch (Exception e) {
140 LOG.debug("Expected exception is thrown: "
141 + e.getClass().getSimpleName() + ": " + e.getMessage());
146 * Create a transient KnowledgeElement for tests.
148 * @return a transient KnowledgeElement
149 * @throws InvalidPropertyException
150 * if an invalid property is used when creating objects
151 * @throws MultiplyDefinedException
152 * when trying to create an object with already existing id
153 * @throws MissedPropertyException
154 * if a mandatory property is not defined for an object to be created
156 private KnowledgeElement getKnowledgeElement()
157 throws InvalidPropertyException, MissedPropertyException,
158 MultiplyDefinedException {
159 // Create a test knowledge type
160 HibernateTemplate ht = getHibernateTemplate();
162 KnowledgeElement.Properties kprops = new KnowledgeElement.Properties();
163 KnowledgeElementType aKType = new KnowledgeElementType("TST_kelmtype");
165 // Create a test user
166 User.Properties uprop = new User.Properties();
167 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
168 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
169 .addRole("TST_user").setMailAddress(
170 "noreply@salome-platform.org");
171 uprop.disableCheck();
172 User anAuthor = new User(uprop);
174 // Create a test study
175 Study.Properties stprops = new Study.Properties().setReference(
176 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
177 Study aStudy = new Study(stprops);
178 ht.saveOrUpdate(aStudy);
179 // Create a test scenario
180 Scenario.Properties sprops = new Scenario.Properties().setTitle(
181 "TST_Study").setManager(anAuthor).setOwnerStudy(aStudy);
182 Scenario aScenario = new Scenario(sprops);
183 aStudy.getScenariiList().add(aScenario);
184 ht.saveOrUpdate(anAuthor);
185 ht.saveOrUpdate(aKType);
187 ht.saveOrUpdate(aScenario);
189 Date aDate = new Date();
191 // Prepare a knowledge element transient object
193 .setTitle("Test knowledge element")
195 .setOwnerScenario(aScenario)
199 "This is the test knowledge element.\nIt is created by the unit test.");
201 return new KnowledgeElement(kprops);