]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java
Salome HOME
Unit test is created for ScenarioService.getScenarioInfo method.
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / service / TestScenarioService.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   12 Oct 2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9 package test.splat.service;
10
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.sql.SQLException;
14 import java.util.Date;
15 import java.util.List;
16
17 import org.splat.dal.bo.kernel.User;
18 import org.splat.dal.bo.som.Document;
19 import org.splat.dal.bo.som.DocumentType;
20 import org.splat.dal.bo.som.Publication;
21 import org.splat.dal.bo.som.Scenario;
22 import org.splat.dal.bo.som.Study;
23 import org.splat.kernel.InvalidPropertyException;
24 import org.splat.kernel.MissedPropertyException;
25 import org.splat.kernel.MultiplyDefinedException;
26 import org.splat.log.AppLogger;
27 import org.splat.service.DocumentService;
28 import org.splat.service.DocumentTypeService;
29 import org.splat.service.ProjectElementService;
30 import org.splat.service.PublicationService;
31 import org.splat.service.ScenarioService;
32 import org.splat.service.StepService;
33 import org.splat.service.dto.DocumentDTO;
34 import org.splat.service.dto.FileDTO;
35 import org.splat.service.dto.StepDTO;
36 import org.splat.service.technical.ProjectSettingsService;
37 import org.splat.service.technical.ProjectSettingsService.Step;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.beans.factory.annotation.Qualifier;
40 import org.springframework.orm.hibernate3.HibernateTemplate;
41 import org.testng.Assert;
42 import org.testng.annotations.Test;
43
44 import test.splat.common.BaseTest;
45
46 /**
47  * Test class for KnowledgeElementDAO.
48  * 
49  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
50  * 
51  */
52 @Test(sequential = true, groups = { "service", "functional", "business" })
53 public class TestScenarioService extends BaseTest {
54
55         /**
56          * Logger for the class.
57          */
58         private static final AppLogger LOG = AppLogger
59                         .getLogger(TestScenarioService.class);
60
61         /**
62          * The tested ScenarioService. Later injected by Spring.
63          */
64         @Autowired
65         @Qualifier("scenarioService")
66         private transient ScenarioService _scenarioService;
67
68         /**
69          * The DocumentService. Later injected by Spring.
70          */
71         @Autowired
72         @Qualifier("documentService")
73         private transient DocumentService _documentService;
74
75         /**
76          * The PublicationService. Later injected by Spring.
77          */
78         @Autowired
79         @Qualifier("publicationService")
80         private transient PublicationService _publicationService;
81
82         /**
83          * The StepService. Later injected by Spring.
84          */
85         @Autowired
86         @Qualifier("stepService")
87         private transient StepService _stepService;
88
89         /**
90          * The ProjectElementService. Later injected by Spring.
91          */
92         @Autowired
93         @Qualifier("projectElementService")
94         private transient ProjectElementService _projectElementService;
95
96         /**
97          * The ProjectSettingsService. Later injected by Spring.
98          */
99         @Autowired
100         @Qualifier("projectSettings")
101         private transient ProjectSettingsService _projectSettings;
102
103         /**
104          * The DocumentTypeService. Later injected by Spring.
105          */
106         @Autowired
107         @Qualifier("documentTypeService")
108         private transient DocumentTypeService _documentTypeService;
109
110         /**
111          * Test of getting a scenario content for building siman-salome.conf.<BR>
112          * <B>Description :</B> <BR>
113          * <i>Create a scenario try to get an info for it.</i><BR>
114          * <B>Action : </B><BR>
115          * <i>1. call the method for an existing scenario id.</i><BR>
116          * <i>2. call the method for a not existing scenario id.</i><BR>
117          * <B>Test data : </B><BR>
118          * <i>no input parameters</i><BR>
119          * <i>no input parameters</i><BR>
120          * 
121          * <B>Outcome results:</B><BR>
122          * <i>
123          * <ul>
124          * <li>result DTO must contain list of all documents and files<BR>
125          * </li>
126          * <li>Exception is thrown<BR>
127          * </li>
128          * </ul>
129          * </i>
130          * 
131          * @throws InvalidPropertyException
132          *             if an invalid property is used when creating objects
133          * @throws MultiplyDefinedException
134          *             when trying to create an object with already existing id
135          * @throws MissedPropertyException
136          *             if a mandatory property is not defined for an object to be created
137          * @throws IOException
138          *             if scenario creation is failed
139          * @throws SQLException
140          *             if scenario creation is failed
141          */
142         @Test
143         public void testGetScenarioInfo() throws InvalidPropertyException,
144                         MissedPropertyException, MultiplyDefinedException, IOException,
145                         SQLException {
146                 LOG.debug(">>>>> BEGIN testGetScenarioInfo()");
147                 long scenarioId = createScenario();
148                 // Call DAO's create method for a good transient knowledge element.
149                 List<StepDTO> steps = _scenarioService.getScenarioInfo(scenarioId);
150                 Assert.assertNotNull(steps, "List of steps must not be null.");
151                 Assert.assertTrue(steps.size() > 0, "No steps are read.");
152
153                 List<Step> projSteps = _projectSettings.getStepsOf(Scenario.class);
154                 Assert.assertEquals(steps.size(), projSteps.size(),
155                                 "Not all steps are listed.");
156                 int docIndex = 0;
157                 for (StepDTO step : steps) {
158                         LOG.debug("check the step " + step.getNumber() + ":\n" + step);
159                         Assert.assertNotNull(step, "Step DTO must not be null.");
160                         Assert.assertNotNull(step.getKey(), "Step name must not be null.");
161                         Assert.assertFalse(step.getKey().isEmpty(),
162                                         "Step name must not empty.");
163                         Assert.assertTrue(step.getNumber() > 0,
164                                         "Step number must be positive integer.");
165                         Assert.assertNotNull(step.getDocs(),
166                                         "Step documents list must not be null.");
167
168                         Step aProjStep = null;
169                         for (Step projStep : projSteps) {
170                                 if (projStep.getNumber() == step.getNumber()) {
171                                         aProjStep = projStep;
172                                         break;
173                                 }
174                         }
175
176                         List<DocumentType> dtypes = _documentTypeService
177                                         .selectTypesOf(aProjStep);
178                         for (DocumentType dtype : dtypes) {
179                                 Assert.assertTrue(step.getDocs().size() > 0,
180                                                 "Step documents list must not be empty.");
181                                 String docName = "document" + docIndex;
182                                 docIndex++;
183                                 for (DocumentDTO doc : step.getDocs()) {
184                                         if (docName.equals(doc.getTitle())) {
185                                                 Assert.assertTrue(doc.getId() > 0,
186                                                                 "Document id must be positive integer.");
187                                                 Assert.assertEquals(doc.getTitle(), docName);
188                                                 Assert.assertNotNull(doc.getFiles(),
189                                                                 "Document files list must not be null.");
190                                                 Assert.assertTrue(doc.getFiles().size() > 1,
191                                                                 "Document must have more then 1 attached file.");
192         
193                                                 for (FileDTO file : doc.getFiles()) {
194                                                         Assert.assertNotNull(file.getPath(),
195                                                                         "File path must not be null.");
196                                                         Assert.assertFalse(file.getPath().isEmpty(),
197                                                                         "File path must not be empty.");
198                                                         Assert.assertEquals(file.getState(), 'Y',
199                                                                         "File state must be actual ('Y').");
200                                                 }
201                                         }
202                                 }
203                         }
204                 }
205
206                 // Call DAO's get method for a not existing id.
207                 try {
208                         steps = _scenarioService.getScenarioInfo(-1L);
209                         // getHibernateTemplate().flush();
210                         Assert
211                                         .fail("Getting an object with not existing id must be failed.");
212                 } catch (Exception e) {
213                         LOG.debug("Expected exception is thrown: "
214                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
215                 }
216                 LOG.debug(">>>>> END testGetScenarioInfo()");
217         }
218
219         /**
220          * Create a transient KnowledgeElement for tests.
221          * 
222          * @return a transient KnowledgeElement
223          * @throws InvalidPropertyException
224          *             if an invalid property is used when creating objects
225          * @throws MultiplyDefinedException
226          *             when trying to create an object with already existing id
227          * @throws MissedPropertyException
228          *             if a mandatory property is not defined for an object to be created
229          * @throws IOException
230          *             if document creation is failed
231          * @throws SQLException
232          *             if project settings loading is failed
233          */
234         private long createScenario() throws InvalidPropertyException,
235                         MissedPropertyException, MultiplyDefinedException, IOException,
236                         SQLException {
237                 // Create a scenario for tests
238                 HibernateTemplate ht = getHibernateTemplate();
239
240                 // Load workflow customization
241                 try {
242                         _projectSettings.configure("src/test/som.xml");
243                 } catch (FileNotFoundException e) {
244                         Assert.fail("Can't find som.xml: ", e);
245                 }
246                 List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
247                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
248
249                 // Create a test user
250                 User.Properties uprop = new User.Properties();
251                 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
252                                 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
253                                 .addRole("TST_user").setMailAddress(
254                                                 "noreply@salome-platform.org");
255                 uprop.disableCheck();
256                 User anAuthor = new User(uprop);
257                 ht.save(anAuthor);
258
259                 // Create a test study
260                 Study.Properties stprops = new Study.Properties().setReference(
261                                 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
262                 Study aStudy = new Study(stprops);
263                 ht.saveOrUpdate(aStudy);
264
265                 // Create a test scenario
266                 Scenario.Properties sprops = new Scenario.Properties().setTitle(
267                                 "TST_Study").setManager(anAuthor).setOwnerStudy(aStudy);
268                 Scenario aScenario = new Scenario(sprops);
269                 aStudy.getScenariiList().add(aScenario);
270                 ht.saveOrUpdate(anAuthor);
271                 ht.saveOrUpdate(aStudy);
272                 ht.saveOrUpdate(aScenario);
273
274                 Document.Properties dprop = new Document.Properties().setAuthor(
275                                 anAuthor).setDate(new Date());
276                 // Create documents
277                 int i = 0;
278                 for (Step step : steps) {
279                         LOG.debug("Create scenario step: " + i);
280
281                         org.splat.som.Step aScStep = new org.splat.som.Step(step, aScenario);
282                         List<DocumentType> dtypes = _documentTypeService
283                                         .selectTypesOf(step);
284                         for (DocumentType dtype : dtypes) {
285                                 dprop.setName("document" + i++).setType(dtype).setFormat(
286                                                 dtype.getName());
287                                 // Create a document published in the scenario
288                                 Publication pub = _stepService.createDocument(aScStep, dprop);
289                                 Assert.assertNotNull(pub.getOwner(),
290                                                 "The publication must be attached to the scenario.");
291                                 Assert.assertEquals(pub.getOwner().getIndex(), aScenario
292                                                 .getIndex(),
293                                                 "The publication was not attached to the scenario.");
294
295                                 aScenario.add(pub);
296                                 ht.saveOrUpdate(pub);
297
298                                 // Attach a file
299                                 ht.saveOrUpdate(pub.value());
300                                 ht.saveOrUpdate(_publicationService.attach(pub, "med"));
301                         }
302                         if (dtypes.size() <= 0) {
303                                 LOG.debug("No document types are found for scenario step " + i);
304                         }
305                 }
306
307                 Assert.assertNotNull(ht.find("from Document"),
308                                 "No documents in the database.");
309                 Assert.assertTrue(ht.find("from Document").size() > 0,
310                                 "No documents in the database.");
311
312                 Assert.assertNotNull(ht.find("from Publication where owner="
313                                 + aScenario.getIndex()), "No publications in the database.");
314                 Assert.assertTrue(
315                                 ht.find("from Publication where owner=" + aScenario.getIndex())
316                                                 .size() > 0, "No publications in the database.");
317
318                 for (Publication p : (List<Publication>) ht
319                                 .find("from Publication where owner=" + aScenario.getIndex())) {
320                         LOG.debug("Publication found: [id=" + p.getIndex() + ", owner="
321                                         + p.getOwner().getIndex() + ", doc=" + p.value().getIndex()
322                                         + "]");
323                         Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
324                                         "The publication was not attached to the scenario.");
325                 }
326
327                 ht.evict(aScenario);
328                 Scenario aScen = ht.load(Scenario.class, aScenario.getIndex());
329                 Assert.assertNotNull(aScen, "Scenario was not saved in the database.");
330                 Assert.assertTrue(aScen.getDocums().size() > 0,
331                                 "No publications in the scenario.");
332
333                 Assert.assertTrue(i > 0,
334                                 "More then one document must be in the database");
335
336                 return aScenario.getIndex();
337         }
338 }