]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/test/splat/service/TestStepService.java
Salome HOME
dd9b728c38187497fe9b7c90aae25a16f77ef96e
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / service / TestStepService.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.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.splat.dal.bo.kernel.User;
20 import org.splat.dal.bo.som.Document;
21 import org.splat.dal.bo.som.DocumentType;
22 import org.splat.dal.bo.som.ProjectElement;
23 import org.splat.dal.bo.som.Publication;
24 import org.splat.dal.bo.som.Scenario;
25 import org.splat.dal.bo.som.Study;
26 import org.splat.dal.bo.som.UsedByRelation;
27 import org.splat.dal.bo.som.UsesRelation;
28 import org.splat.dal.bo.som.Document.Properties;
29 import org.splat.dal.dao.som.Database;
30 import org.splat.dal.dao.som.ScenarioDAO;
31 import org.splat.kernel.InvalidPropertyException;
32 import org.splat.kernel.MismatchException;
33 import org.splat.kernel.MissedPropertyException;
34 import org.splat.kernel.MultiplyDefinedException;
35 import org.splat.kernel.NotApplicableException;
36 import org.splat.log.AppLogger;
37 import org.splat.service.DocumentTypeService;
38 import org.splat.service.KnowledgeElementTypeService;
39 import org.splat.service.ProjectElementService;
40 import org.splat.service.PublicationService;
41 import org.splat.service.ScenarioService;
42 import org.splat.service.SimulationContextService;
43 import org.splat.service.StepService;
44 import org.splat.service.StudyService;
45 import org.splat.service.technical.ProjectSettingsService;
46 import org.splat.service.technical.RepositoryService;
47 import org.splat.service.technical.ProjectSettingsService.Step;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.beans.factory.annotation.Qualifier;
50 import org.springframework.orm.hibernate3.HibernateTemplate;
51 import org.testng.Assert;
52 import org.testng.annotations.Test;
53
54 import test.splat.common.BaseTest;
55
56 /**
57  * Test class for StepService.
58  * 
59  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
60  * 
61  */
62 public class TestStepService extends BaseTest {
63
64         /**
65          * Logger for the class.
66          */
67         private static final AppLogger LOG = AppLogger
68                         .getLogger(TestStepService.class);
69
70         /**
71          * The StudyService. Later injected by Spring.
72          */
73         @Autowired
74         @Qualifier("studyService")
75         private transient StudyService _studyService;
76
77         /**
78          * The ScenarioService. Later injected by Spring.
79          */
80         @Autowired
81         @Qualifier("scenarioService")
82         private transient ScenarioService _scenarioService;
83
84         /**
85          * The ProjectElementService. Later injected by Spring.
86          */
87         @Autowired
88         @Qualifier("projectElementService")
89         private transient ProjectElementService _projectElementService;
90
91         /**
92          * The RepositoryService. Later injected by Spring.
93          */
94         @Autowired
95         @Qualifier("repositoryService")
96         private transient RepositoryService _repositoryService;
97
98         /**
99          * The Scenario DAO. Later injected by Spring.
100          */
101         @Autowired
102         @Qualifier("scenarioDAO")
103         private transient ScenarioDAO _scenarioDAO;
104
105         /**
106          * The PublicationService. Later injected by Spring.
107          */
108         @Autowired
109         @Qualifier("publicationService")
110         private transient PublicationService _publicationService;
111
112         /**
113          * The StepService. Later injected by Spring.
114          */
115         @Autowired
116         @Qualifier("stepService")
117         private transient StepService _stepService;
118
119         /**
120          * The SimulationContextService. Later injected by Spring.
121          */
122         @Autowired
123         @Qualifier("simulationContextService")
124         private transient SimulationContextService _simulationContextService;
125
126         /**
127          * The ProjectSettingsService. Later injected by Spring.
128          */
129         @Autowired
130         @Qualifier("projectSettings")
131         private transient ProjectSettingsService _projectSettings;
132
133         /**
134          * The DocumentTypeService. Later injected by Spring.
135          */
136         @Autowired
137         @Qualifier("documentTypeService")
138         private transient DocumentTypeService _documentTypeService;
139
140         /**
141          * The KnowledgeElementTypeService. Later injected by Spring.
142          */
143         @Autowired
144         @Qualifier("knowledgeElementTypeService")
145         private transient KnowledgeElementTypeService _knowledgeElementTypeService;
146
147         /**
148          * Test check-in scenario operation to be performed after SALOME session.<BR>
149          * <B>Description :</B> <BR>
150          * <i>Create a scenario and try to check-in it with some simulated SALOME results data.<BR>
151          * After check-in verify following points:
152          * <ul>
153          * <li>scenario is no more marked as checked out</li>
154          * <li>new document versions are created for checked in documents</li>
155          * <li>presentation of the previous version is removed</li>
156          * <li>uses relations are copied correctly</li>
157          * <li>files are moved correctly</li>
158          * <li>formats of files are new if they are according to the document's type on the study step</li>
159          * <li>new documents are created for new data</li>
160          * <li>new documents have correctly generated names</li>
161          * <li>uses relations are created correctly</li>
162          * <li>files are moved correctly</li>
163          * </ul>
164          * </i><BR>
165          * <B>Action : </B><BR>
166          * <i>1. call the method for an existing scenario id.</i><BR>
167          * <i>2. call the method for a not existing scenario id.</i><BR>
168          * <B>Test data : </B><BR>
169          * <i>no input parameters</i><BR>
170          * <i>no input parameters</i><BR>
171          * 
172          * <B>Outcome results:</B><BR>
173          * <i>
174          * <ul>
175          * <li>New version of existing documents must be created and new documents must be imported for documents with zero id. Correct
176          * relations must be created.<BR>
177          * </li>
178          * <li>Exception is thrown<BR>
179          * </li>
180          * </ul>
181          * </i>
182          * 
183          * @throws InvalidPropertyException
184          *             if an invalid property is used when creating objects
185          * @throws MultiplyDefinedException
186          *             when trying to create an object with already existing id
187          * @throws MissedPropertyException
188          *             if a mandatory property is not defined for an object to be created
189          * @throws IOException
190          *             if scenario creation is failed
191          * @throws SQLException
192          *             if scenario creation is failed
193          * @throws NotApplicableException
194          *             if checkin failed
195          * @throws MismatchException
196          *             if checkin failed
197          */
198         @Test
199         public void testRemoveDocument() throws InvalidPropertyException,
200                         MissedPropertyException, MultiplyDefinedException, IOException,
201                         SQLException, MismatchException, NotApplicableException {
202                 LOG.debug(">>>>> BEGIN testRemoveDocument()");
203                 startNestedTransaction();
204
205                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
206                 _projectSettings.configure(ClassLoader
207                                 .getSystemResource("test/som.xml").getPath());
208                 HibernateTemplate ht = getHibernateTemplate();
209                 ht.flush();
210                 long scenarioId = createScenario();
211                 ht.flush();
212                 _scenarioDAO.flush();
213                 Scenario aScen = _scenarioDAO.get(scenarioId);
214
215                 Assert.assertTrue(ht.find("from UsesRelation").size() > 0,
216                                 "Uses relations were not created in the database.");
217
218                 Assert.assertTrue(ht.find("from UsedByRelation").size() > 0,
219                                 "UsedBy relations were not created in the database.");
220
221                 // ////////////////////////////////////////////////////////
222                 // Call removeDocument method for each document of the
223                 // study/scenario starting from the last document.
224
225                 ProjectElement projElem;
226                 boolean ok;
227                 long docId;
228                 Step step;
229                 for (int i = _projectSettings.getAllSteps().size(); i > 0; i--) {
230                         LOG.debug("Remove documents from the step " + i);
231                         step = _projectSettings.getStep(i);
232                         if (step.appliesTo(Study.class)) {
233                                 projElem = _studyService.selectStudy(aScen.getOwnerStudy()
234                                                 .getIndex());
235                         } else {
236                                 projElem = aScen;
237                         }
238
239                         org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
240
241                         if (aScStep.getDocuments().size() > 0) {
242                                 docId = aScStep.getDocuments().get(0).value().getIndex();
243                                 LOG.debug("Remove document "
244                                                 + aScStep.getDocument(docId).value().getTitle());
245                                 ht.flush();
246                                 List<UsesRelation> uses = ht
247                                                 .find("from UsesRelation where owner=" + docId);
248                                 List<UsedByRelation> usedBy = ht
249                                                 .find("from UsedByRelation where owner=" + docId);
250                                 LOG.debug("From db: It uses following " + uses.size()
251                                                 + " documents: ");
252                                 for (UsesRelation rel : uses) {
253                                         LOG.debug(rel.getTo().getTitle());
254                                 }
255                                 LOG.debug("From step: It uses following "
256                                                 + aScStep.getDocument(docId).getRelations(
257                                                                 UsesRelation.class).size() + " documents: ");
258                                 for (Publication rel : aScStep.getDocument(docId).getRelations(
259                                                 UsesRelation.class)) {
260                                         LOG.debug(rel.value().getTitle());
261                                 }
262                                 LOG.debug("From db: It is used by following " + usedBy.size()
263                                                 + " documents: ");
264                                 for (UsedByRelation rel : usedBy) {
265                                         LOG.debug(rel.getTo().getTitle());
266                                 }
267                                 LOG.debug("From step: It is used by following "
268                                                 + aScStep.getDocument(docId).getRelations(
269                                                                 UsedByRelation.class).size() + " documents: ");
270                                 for (Publication rel : aScStep.getDocument(docId).getRelations(
271                                                 UsedByRelation.class)) {
272                                         LOG.debug(rel.value().getTitle());
273                                 }
274                                 ok = _stepService.removeDocument(aScStep, docId);
275
276                                 Assert.assertTrue(ok, "Removing was failed.");
277                                 _scenarioDAO.flush();
278                                 ht.flush();
279                                 Assert.assertEquals(ht.find(
280                                                 "from UsesRelation where owner=" + docId).size(), 0,
281                                                 "UsesRelation(s) were not removed from the database.");
282                                 Assert
283                                                 .assertEquals(ht.find(
284                                                                 "from UsedByRelation where owner=" + docId)
285                                                                 .size(), 0,
286                                                                 "UsedByRelation(s) were not removed from the database.");
287                                 Assert.assertEquals(ht.find(
288                                                 "from UsesRelation where refer=" + docId).size(), 0,
289                                                 "Referencing UsesRelation(s) were not removed from the database.");
290                                 Assert
291                                                 .assertEquals(ht.find(
292                                                                 "from UsedByRelation where refer=" + docId)
293                                                                 .size(), 0,
294                                                                 "Referencing UsedByRelation(s) were not removed from the database.");
295                                 Assert
296                                 .assertEquals(ht.find(
297                                                 "from ConvertsRelation where owner=" + docId)
298                                                 .size(), 0,
299                                                 "ConvertsRelation(s) were not removed from the database.");
300                         }
301                 }
302
303                 Assert.assertEquals(ht.find("from Document").size(), 0,
304                                 "Documents were not removed from the database.");
305
306                 Assert.assertEquals(ht.find(
307                                 "from Publication where owner=" + aScen.getIndex()).size(), 0,
308                                 "Publications were not removed from the database.");
309
310                 Assert.assertEquals(ht.find("from UsesRelation").size(), 0,
311                                 "Uses relations were not removed from the database.");
312
313                 Assert.assertEquals(ht.find("from UsedByRelation").size(), 0,
314                                 "UsedBy relations were not removed from the database.");
315
316                 rollbackNestedTransaction();
317                 LOG.debug(">>>>> END testRemoveDocument()");
318         }
319
320         /**
321          * Create a persistent scenario for tests.
322          * 
323          * @return a persistent scenario
324          * @throws InvalidPropertyException
325          *             if an invalid property is used when creating objects
326          * @throws MultiplyDefinedException
327          *             when trying to create an object with already existing id
328          * @throws MissedPropertyException
329          *             if a mandatory property is not defined for an object to be created
330          * @throws IOException
331          *             if document creation is failed
332          * @throws SQLException
333          *             if project settings loading is failed
334          */
335         private long createScenario() throws InvalidPropertyException,
336                         MissedPropertyException, MultiplyDefinedException, IOException,
337                         SQLException {
338                 // Create a scenario for tests
339                 HibernateTemplate ht = getHibernateTemplate();
340
341                 Database.getInstance().reset();
342                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
343                 // Load workflow customization
344                 try {
345                         _projectSettings.configure(ClassLoader.getSystemResource(
346                                         "test/som.xml").getPath());
347                 } catch (FileNotFoundException e) {
348                         Assert.fail("Can't find som.xml: ", e);
349                 }
350                 List<Step> steps = _projectSettings.getAllSteps();
351                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
352
353                 // Create a test user
354                 User.Properties uprop = new User.Properties();
355                 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
356                                 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
357                                 .addRole("TST_user").setMailAddress(
358                                                 "noreply@salome-platform.org");
359                 uprop.disableCheck();
360                 User anAuthor = new User(uprop);
361                 ht.saveOrUpdate(anAuthor);
362
363                 // Create a test study
364                 Study.Properties stprops = new Study.Properties().setReference(
365                                 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
366                 Study aStudy = new Study(stprops);
367                 ht.saveOrUpdate(aStudy);
368
369                 // Create a test scenario
370                 Scenario.Properties sprops = new Scenario.Properties().setTitle(
371                                 "TST_Scenario").setManager(anAuthor).setOwnerStudy(aStudy);
372                 Scenario aScenario = new Scenario(sprops);
373                 aStudy.getScenariiList().add(aScenario);
374                 ht.saveOrUpdate(anAuthor);
375                 ht.saveOrUpdate(aStudy);
376                 ht.saveOrUpdate(aScenario);
377
378                 // Create documents for each scenario step
379                 Document.Properties dprop = new Document.Properties().setAuthor(
380                                 anAuthor).setDate(new Date());
381                 int i = 0;
382                 Publication usedPub = null;
383                 Map<Long, Long> usedMap = new HashMap<Long, Long>();
384                 for (int stepNum = 1; stepNum <= steps.size(); stepNum++) {
385                         Step step = _projectSettings.getStep(stepNum);
386                         LOG.debug("Create scenario step: " + stepNum);
387                         ProjectElement projElem;
388
389                         if (step.appliesTo(Study.class)) {
390                                 projElem = aStudy;
391                         } else {
392                                 projElem = aScenario;
393                         }
394                         org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
395                         List<DocumentType> dtypes = _documentTypeService
396                                         .selectTypesOf(step);
397                         if (dtypes.size() > 0) {
398                                 DocumentType dtype = dtypes.get(0);
399                                 // Create a document published in the scenario
400                                 // document<i>: document type[0] - first type used on the step
401                                 // <source-file>.brep
402                                 // <attached-file>.med
403                                 i++;
404                                 dprop.setName("document" + stepNum).setType(dtype);
405                                 if (step.getNumber() > 3) {
406                                         dprop.setFormat("med");
407                                 } else {
408                                         dprop.setFormat("py");
409                                 }
410                                 Publication pub = createDoc(projElem, aScStep, dprop, "med",
411                                                 false);
412                                 if (usedPub != null) {
413                                         pub.addDependency(usedPub);
414                                         LOG.debug("Add dependency: " + pub.value().getTitle()
415                                                         + " from " + usedPub.value().getTitle());
416                                         ht.saveOrUpdate(pub.value());
417                                         ht.flush();
418
419                                         usedMap.put(pub.getIndex(), usedPub.getIndex());
420                                 }
421                                 usedPub = pub;
422                         }
423                         if (dtypes.size() <= 0) {
424                                 LOG.debug("No document types are found for scenario step " + i);
425                         }
426                 }
427
428                 // Check that the scenario and its documents have been created correctly.
429
430                 Assert.assertNotNull(ht.find("from Document"),
431                                 "No documents in the database.");
432                 Assert.assertTrue(ht.find("from Document").size() > 0,
433                                 "No documents in the database.");
434
435                 Assert.assertNotNull(ht.find("from Publication where owner="
436                                 + aScenario.getIndex()), "No publications in the database.");
437                 Assert.assertTrue(
438                                 ht.find("from Publication where owner=" + aScenario.getIndex())
439                                                 .size() > 0, "No publications in the database.");
440
441                 for (Publication p : (List<Publication>) ht
442                                 .find("from Publication where owner=" + aScenario.getIndex())) {
443                         LOG.debug("Publication found: [id=" + p.getIndex() + ", owner="
444                                         + p.getOwner().getIndex() + ", doc=" + p.value().getIndex()
445                                         + "]");
446                         Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
447                                         "The publication was not attached to the scenario.");
448                 }
449
450                 // Remove the scenario from the current hibernate session.
451                 ht.evict(aScenario);
452                 // Check that the scenario is created in the database.
453                 Scenario aScen = ht.load(Scenario.class, aScenario.getIndex());
454                 Assert.assertNotNull(aScen, "Scenario was not saved in the database.");
455                 Assert.assertTrue(aScen.getDocums().size() > 0,
456                                 "No publications in the scenario.");
457
458                 Assert.assertTrue(i > 0,
459                                 "More then one document must be in the database");
460
461                 // Check created uses relations
462                 Assert
463                                 .assertTrue(usedMap.size() > 0,
464                                                 "Uses relations must be created.");
465                 boolean foundAny = false;
466                 for (Long usingId : usedMap.keySet()) {
467                         for (Publication pub : aScen.getDocums()) {
468                                 if (pub.getIndex() == usingId) {
469                                         boolean found = false;
470                                         for (Publication used : aScen.getDocums()) {
471                                                 found = (used.getIndex() == usedMap.get(usingId));
472                                                 if (found) {
473                                                         break;
474                                                 }
475                                         }
476                                         if (!found) {
477                                                 for (Publication used : aStudy.getDocums()) {
478                                                         found = (used.getIndex() == usedMap.get(usingId));
479                                                         if (found) {
480                                                                 break;
481                                                         }
482                                                 }
483                                         }
484                                         Assert.assertTrue(found,
485                                                         "Uses relation was not created in the database.");
486                                         foundAny = foundAny || found;
487                                 }
488                         }
489                 }
490                 Assert.assertTrue(foundAny,
491                                 "No Uses relation was created in the database.");
492
493                 return aScenario.getIndex();
494         }
495
496         /**
497          * Create a document published in the scenario. <BR>
498          * document:<BR>
499          * document type - type used on the step <BR>
500          * &lt;source-file&gt;.brep <BR>
501          * &lt;attached-file&gt;.med
502          * 
503          * @param aScenario
504          *            the scenario to add the document to
505          * @param aScStep
506          *            scenario step where the document to be published
507          * @param dprop
508          *            document properties
509          * @param attachedFileExt
510          *            extension of the secon attached (exported) file
511          * @param isOutdated
512          *            outdated document flag
513          * @return the publication of the created document
514          * @throws IOException
515          * @throws MultiplyDefinedException
516          * @throws InvalidPropertyException
517          * @throws MissedPropertyException
518          */
519         private Publication createDoc(final ProjectElement aScenario,
520                         final org.splat.som.Step aScStep, final Properties dprop,
521                         final String attachedFileExt, final boolean isOutdated)
522                         throws MissedPropertyException, InvalidPropertyException,
523                         MultiplyDefinedException, IOException {
524                 // Create a document published in the scenario
525                 // document<i>: document type - type used on the step
526                 // <source-file>.brep
527                 // <attached-file>.med
528                 Publication pub = _stepService.createDocument(aScStep, dprop);
529                 Assert.assertNotNull(pub.getOwner(),
530                                 "The publication must be attached to the scenario.");
531                 Assert.assertEquals(pub.getOwner().getIndex(), aScenario.getIndex(),
532                                 "The publication was not attached to the scenario.");
533
534                 if (isOutdated) {
535                         pub.setIsnew('O');
536                 }
537                 aScenario.add(pub);
538                 HibernateTemplate ht = getHibernateTemplate();
539                 ht.saveOrUpdate(pub);
540
541                 // Attach a file
542                 ht.save(pub.value());
543                 ht.flush();
544                 ht.saveOrUpdate(_publicationService.attach(pub, attachedFileExt));
545
546                 return pub;
547         }
548 }