]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/test/splat/dao/TestKnowledgeElementDAO.java
Salome HOME
Id now is Long instead of Integer. First unit test is created. hibernate-3.5.jar...
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / dao / TestKnowledgeElementDAO.java
1 /*****************************************************************************
2  * Company         EURIWARE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   12 Oct 2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9 package test.splat.dao;
10
11 import java.util.Date;
12
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;
30
31 import test.splat.common.BaseTest;
32
33 /**
34  * Test class for KnowledgeElementDAO.
35  * 
36  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
37  * 
38  */
39 public class TestKnowledgeElementDAO extends BaseTest {
40
41         /**
42          * Logger for the class.
43          */
44         private static final AppLogger LOG = AppLogger
45                         .getLogger(TestKnowledgeElementDAO.class);
46
47         /**
48          * The tested KnowledgeElementDAO. Later injected by Spring.
49          */
50         @Autowired
51         @Qualifier("knowledgeElementDAO")
52         private transient KnowledgeElementDAO _knowledgeElementDAO;
53
54         /**
55          * The StudyService injected bean.
56          */
57         @Autowired
58         @Qualifier("studyService")
59         private StudyService _studyService;
60
61         /**
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>
71          * 
72          * <B>Outcome results:</B><BR>
73          * <i>
74          * <ul>
75          * <li>Object is created in the database successfully<BR>
76          * </li>
77          * <li>Exception is thrown<BR>
78          * </li>
79          * </ul>
80          * </i>
81          * 
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
88          * 
89          */
90         @Test
91         public void testCreate() throws InvalidPropertyException,
92                         MissedPropertyException, MultiplyDefinedException {
93                 LOG.debug(">>>>> RUN testCreate()");
94
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.");
109                 Assert
110                                 .assertEquals(aKelmFound.getOwnerScenario(), aKelm
111                                                 .getOwnerScenario(),
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.");
123
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())
132                                                 .setValue(
133                                                                 "This is another test knowledge element.\nIt is created by the unit test."));
134                 aBadKelm.setIndex(aKelmFound.getIndex());
135                 try {
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());
142                 }
143         }
144
145         /**
146          * Create a transient KnowledgeElement for tests.
147          * 
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
155          */
156         private KnowledgeElement getKnowledgeElement()
157                         throws InvalidPropertyException, MissedPropertyException,
158                         MultiplyDefinedException {
159                 // Create a test knowledge type
160                 HibernateTemplate ht = getHibernateTemplate();
161
162                 KnowledgeElement.Properties kprops = new KnowledgeElement.Properties();
163                 KnowledgeElementType aKType = new KnowledgeElementType("TST_kelmtype");
164                 ht.save(aKType);
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);
173                 ht.update(anAuthor);
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);
186                 ht.update(aStudy);
187                 ht.saveOrUpdate(aScenario);
188
189                 Date aDate = new Date();
190
191                 // Prepare a knowledge element transient object
192                 kprops
193                                 .setTitle("Test knowledge element")
194                                 .setAuthor(anAuthor)
195                                 .setOwnerScenario(aScenario)
196                                 .setType(aKType)
197                                 .setDate(aDate)
198                                 .setValue(
199                                                 "This is the test knowledge element.\nIt is created by the unit test.");
200
201                 return new KnowledgeElement(kprops);
202         }
203 }