Salome HOME
Processing instruction is defined now in getScenarioInfo using document types mappings.
[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 public class TestScenarioService extends BaseTest {
53
54         /**
55          * Logger for the class.
56          */
57         private static final AppLogger LOG = AppLogger
58                         .getLogger(TestScenarioService.class);
59
60         /**
61          * The tested ScenarioService. Later injected by Spring.
62          */
63         @Autowired
64         @Qualifier("scenarioService")
65         private transient ScenarioService _scenarioService;
66
67         /**
68          * The DocumentService. Later injected by Spring.
69          */
70         @Autowired
71         @Qualifier("documentService")
72         private transient DocumentService _documentService;
73
74         /**
75          * The PublicationService. Later injected by Spring.
76          */
77         @Autowired
78         @Qualifier("publicationService")
79         private transient PublicationService _publicationService;
80
81         /**
82          * The StepService. Later injected by Spring.
83          */
84         @Autowired
85         @Qualifier("stepService")
86         private transient StepService _stepService;
87
88         /**
89          * The ProjectElementService. Later injected by Spring.
90          */
91         @Autowired
92         @Qualifier("projectElementService")
93         private transient ProjectElementService _projectElementService;
94
95         /**
96          * The ProjectSettingsService. Later injected by Spring.
97          */
98         @Autowired
99         @Qualifier("projectSettings")
100         private transient ProjectSettingsService _projectSettings;
101
102         /**
103          * The DocumentTypeService. Later injected by Spring.
104          */
105         @Autowired
106         @Qualifier("documentTypeService")
107         private transient DocumentTypeService _documentTypeService;
108
109         /**
110          * Test of getting a scenario content for building siman-salome.conf.<BR>
111          * <B>Description :</B> <BR>
112          * <i>Create a scenario try to get an info for it.</i><BR>
113          * <B>Action : </B><BR>
114          * <i>1. call the method for an existing scenario id.</i><BR>
115          * <i>2. call the method for a not existing scenario id.</i><BR>
116          * <B>Test data : </B><BR>
117          * <i>no input parameters</i><BR>
118          * <i>no input parameters</i><BR>
119          * 
120          * <B>Outcome results:</B><BR>
121          * <i>
122          * <ul>
123          * <li>result DTO must contain list of all documents and files<BR>
124          * </li>
125          * <li>Exception is thrown<BR>
126          * </li>
127          * </ul>
128          * </i>
129          * 
130          * @throws InvalidPropertyException
131          *             if an invalid property is used when creating objects
132          * @throws MultiplyDefinedException
133          *             when trying to create an object with already existing id
134          * @throws MissedPropertyException
135          *             if a mandatory property is not defined for an object to be created
136          * @throws IOException
137          *             if scenario creation is failed
138          * @throws SQLException
139          *             if scenario creation is failed
140          */
141         @Test
142         public void testGetScenarioInfo() throws InvalidPropertyException,
143                         MissedPropertyException, MultiplyDefinedException, IOException,
144                         SQLException {
145                 LOG.debug(">>>>> BEGIN testGetScenarioInfo()");
146                 long scenarioId = createScenario();
147                 // Call DAO's create method for a good transient knowledge element.
148                 List<StepDTO> steps = _scenarioService.getScenarioInfo(scenarioId);
149                 Assert.assertNotNull(steps, "List of steps must not be null.");
150                 Assert.assertTrue(steps.size() > 0, "No steps are read.");
151
152                 List<Step> projSteps = _projectSettings.getStepsOf(Scenario.class);
153                 Assert.assertEquals(steps.size(), projSteps.size(),
154                                 "Not all steps are listed.");
155                 int docIndex = 0;
156                 for (StepDTO step : steps) {
157                         LOG.debug("check the step " + step.getNumber() + ":\n" + step);
158                         Assert.assertNotNull(step, "Step DTO must not be null.");
159                         Assert.assertNotNull(step.getKey(), "Step name must not be null.");
160                         Assert.assertFalse(step.getKey().isEmpty(),
161                                         "Step name must not empty.");
162                         Assert.assertTrue(step.getNumber() > 0,
163                                         "Step number must be positive integer.");
164                         Assert.assertNotNull(step.getDocs(),
165                                         "Step documents list must not be null.");
166
167                         Step aProjStep = null;
168                         for (Step projStep : projSteps) {
169                                 if (projStep.getNumber() == step.getNumber()) {
170                                         aProjStep = projStep;
171                                         break;
172                                 }
173                         }
174
175                         List<DocumentType> dtypes = _documentTypeService
176                                         .selectTypesOf(aProjStep);
177                         for (DocumentType dtype : dtypes) {
178                                 Assert.assertTrue(step.getDocs().size() > 0,
179                                                 "Step documents list must not be empty.");
180                                 String docName = "document" + docIndex;
181                                 for (DocumentDTO doc : step.getDocs()) {
182                                         if (docName.equals(doc.getTitle())) {
183                                                 Assert.assertTrue(doc.getId() > 0,
184                                                                 "Document id must be positive integer.");
185                                                 Assert.assertEquals(doc.getTitle(), docName);
186                                                 Assert.assertNotNull(doc.getFiles(),
187                                                                 "Document files list must not be null.");
188                                                 Assert.assertTrue(doc.getFiles().size() > 1,
189                                                                 "Document must have more then 1 attached file.");
190         
191                                                 for (FileDTO file : doc.getFiles()) {
192                                                         Assert.assertNotNull(file.getPath(),
193                                                                         "File path must not be null.");
194                                                         Assert.assertFalse(file.getPath().isEmpty(),
195                                                                         "File path must not be empty.");
196                                                         /*                                                      
197                                                     <mappings>
198                                                         <document type="geometry">
199                                                             <import format="brep"/>     <!-- Result Shape                             -->
200                                                         </document>
201                                                         <document type="model">
202                                                             <import format="med"/>      <!-- Result mesh without input parameters     -->
203                                                         </document>
204                                                         <document type="loads">
205                                                             <import format="c3m"/>      <!-- Input data created interactively         -->
206                                                         </document>
207                                                         <document type="results">
208                                                             <import format="med"/>      <!-- Calculation results source file          -->
209                                                         </document>
210                                                 </mappings>
211                                                          */
212                                                         // Check state and processing instruction
213                                                         String fileFormat = file.getPath().substring(
214                                                                         file.getPath().lastIndexOf('.') + 1);
215 /*                                                      if (_projectSettings.doImport(dtype.getName(),
216                                                                         fileFormat)) {
217                                                                 Assert.assertTrue(file.isResult(),
218                                                                                 "The file must be a result file.");
219                                                         } else {
220                                                                 Assert.assertFalse(file.isResult(),
221                                                                                 "The file must be a source file.");
222                                                         }
223 */                                                      if ((docIndex % 2) == 0) { // New
224                                                                 Assert.assertEquals(file.getState(), 'Y',
225                                                                                 "File state must be actual ('Y').");
226                                                                 if (_projectSettings.doImport(dtype.getName(),
227                                                                                 fileFormat)) {
228                                                                         Assert.assertEquals(file.getProcessing(),
229                                                                                         "file-import",
230                                                                                         "File must be imported.");
231                                                                 } else {
232                                                                         Assert.assertEquals(file.getProcessing(),
233                                                                                         "file-download",
234                                                                                         "File must be downloaded.");
235                                                                 }
236                                                         } else { // Outdated
237                                                                 Assert.assertEquals(file.getState(), 'O',
238                                                                                 "File state must be actual ('O').");
239                                                                 Assert
240                                                                                 .assertEquals(file.getProcessing(),
241                                                                                                 "file-download",
242                                                                                                 "Outdated document should not be imported but downloaded.");
243                                                         }
244                                                 }
245                                         }
246                                 }
247                                 docIndex++;
248                         }
249                 }
250
251                 // Call DAO's get method for a not existing id.
252                 try {
253                         steps = _scenarioService.getScenarioInfo(-1L);
254                         // getHibernateTemplate().flush();
255                         Assert
256                                         .fail("Getting an object with not existing id must be failed.");
257                 } catch (Exception e) {
258                         LOG.debug("Expected exception is thrown: "
259                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
260                 }
261                 LOG.debug(">>>>> END testGetScenarioInfo()");
262         }
263
264         /**
265          * Create a transient KnowledgeElement for tests.
266          * 
267          * @return a transient KnowledgeElement
268          * @throws InvalidPropertyException
269          *             if an invalid property is used when creating objects
270          * @throws MultiplyDefinedException
271          *             when trying to create an object with already existing id
272          * @throws MissedPropertyException
273          *             if a mandatory property is not defined for an object to be created
274          * @throws IOException
275          *             if document creation is failed
276          * @throws SQLException
277          *             if project settings loading is failed
278          */
279         private long createScenario() throws InvalidPropertyException,
280                         MissedPropertyException, MultiplyDefinedException, IOException,
281                         SQLException {
282                 // Create a scenario for tests
283                 HibernateTemplate ht = getHibernateTemplate();
284
285                 // Load workflow customization
286                 try {
287                         _projectSettings.configure(ClassLoader.getSystemResource("test/som.xml").getPath());
288                 } catch (FileNotFoundException e) {
289                         Assert.fail("Can't find som.xml: ", e);
290                 }
291                 List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
292                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
293
294                 // Create a test user
295                 User.Properties uprop = new User.Properties();
296                 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
297                                 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
298                                 .addRole("TST_user").setMailAddress(
299                                                 "noreply@salome-platform.org");
300                 uprop.disableCheck();
301                 User anAuthor = new User(uprop);
302                 ht.save(anAuthor);
303
304                 // Create a test study
305                 Study.Properties stprops = new Study.Properties().setReference(
306                                 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
307                 Study aStudy = new Study(stprops);
308                 ht.saveOrUpdate(aStudy);
309
310                 // Create a test scenario
311                 Scenario.Properties sprops = new Scenario.Properties().setTitle(
312                                 "TST_Study").setManager(anAuthor).setOwnerStudy(aStudy);
313                 Scenario aScenario = new Scenario(sprops);
314                 aStudy.getScenariiList().add(aScenario);
315                 ht.saveOrUpdate(anAuthor);
316                 ht.saveOrUpdate(aStudy);
317                 ht.saveOrUpdate(aScenario);
318
319                 Document.Properties dprop = new Document.Properties().setAuthor(
320                                 anAuthor).setDate(new Date());
321                 // Create documents
322                 int i = 0;
323                 for (Step step : steps) {
324                         LOG.debug("Create scenario step: " + i);
325
326                         org.splat.som.Step aScStep = new org.splat.som.Step(step, aScenario);
327                         List<DocumentType> dtypes = _documentTypeService
328                                         .selectTypesOf(step);
329                         for (DocumentType dtype : dtypes) {
330                                 dprop.setName("document" + i++).setType(dtype).setFormat(
331                                                 "brep");
332                                 // Create a document published in the scenario
333                                 // document<i>: document type[0] - first type used on the step
334                                 //      <source-file>.brep
335                                 //      <attached-file>.med
336                                 Publication pub = _stepService.createDocument(aScStep, dprop);
337                                 Assert.assertNotNull(pub.getOwner(),
338                                                 "The publication must be attached to the scenario.");
339                                 Assert.assertEquals(pub.getOwner().getIndex(), aScenario
340                                                 .getIndex(),
341                                                 "The publication was not attached to the scenario.");
342
343                                 aScenario.add(pub);
344                                 ht.saveOrUpdate(pub);
345
346                                 // Attach a file
347                                 ht.saveOrUpdate(pub.value());
348                                 ht.saveOrUpdate(_publicationService.attach(pub, "med"));
349                                 
350                                 // Create a document with outdated publication
351                                 dprop.setName("document" + i++).setType(dtype).setFormat(
352                                 "brep");
353                                 // Create a document published in the scenario
354                                 // document<i>: document type[0] - first type used on the step
355                                 //      <source-file>.brep
356                                 //      <attached-file>.med
357                                 pub = _stepService.createDocument(aScStep, dprop);
358                                 Assert.assertNotNull(pub.getOwner(),
359                                                 "The publication must be attached to the scenario.");
360                                 Assert.assertEquals(pub.getOwner().getIndex(), aScenario
361                                                 .getIndex(),
362                                                 "The publication was not attached to the scenario.");
363                 
364                                 pub.setIsnew('O');
365                                 aScenario.add(pub);
366                                 ht.saveOrUpdate(pub);
367                 
368                                 // Attach a file
369                                 ht.saveOrUpdate(pub.value());
370                                 ht.saveOrUpdate(_publicationService.attach(pub, "med"));
371                                                 
372                         }
373                         if (dtypes.size() <= 0) {
374                                 LOG.debug("No document types are found for scenario step " + i);
375                         }
376                 }
377
378                 Assert.assertNotNull(ht.find("from Document"),
379                                 "No documents in the database.");
380                 Assert.assertTrue(ht.find("from Document").size() > 0,
381                                 "No documents in the database.");
382
383                 Assert.assertNotNull(ht.find("from Publication where owner="
384                                 + aScenario.getIndex()), "No publications in the database.");
385                 Assert.assertTrue(
386                                 ht.find("from Publication where owner=" + aScenario.getIndex())
387                                                 .size() > 0, "No publications in the database.");
388
389                 for (Publication p : (List<Publication>) ht
390                                 .find("from Publication where owner=" + aScenario.getIndex())) {
391                         LOG.debug("Publication found: [id=" + p.getIndex() + ", owner="
392                                         + p.getOwner().getIndex() + ", doc=" + p.value().getIndex()
393                                         + "]");
394                         Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
395                                         "The publication was not attached to the scenario.");
396                 }
397
398                 ht.evict(aScenario);
399                 Scenario aScen = ht.load(Scenario.class, aScenario.getIndex());
400                 Assert.assertNotNull(aScen, "Scenario was not saved in the database.");
401                 Assert.assertTrue(aScen.getDocums().size() > 0,
402                                 "No publications in the scenario.");
403
404                 Assert.assertTrue(i > 0,
405                                 "More then one document must be in the database");
406
407                 return aScenario.getIndex();
408         }
409 }