Salome HOME
Loading of mappings of document types to lists of file formats is implemented. Projec...
[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                                 docIndex++;
182                                 for (DocumentDTO doc : step.getDocs()) {
183                                         if (docName.equals(doc.getTitle())) {
184                                                 Assert.assertTrue(doc.getId() > 0,
185                                                                 "Document id must be positive integer.");
186                                                 Assert.assertEquals(doc.getTitle(), docName);
187                                                 Assert.assertNotNull(doc.getFiles(),
188                                                                 "Document files list must not be null.");
189                                                 Assert.assertTrue(doc.getFiles().size() > 1,
190                                                                 "Document must have more then 1 attached file.");
191         
192                                                 for (FileDTO file : doc.getFiles()) {
193                                                         Assert.assertNotNull(file.getPath(),
194                                                                         "File path must not be null.");
195                                                         Assert.assertFalse(file.getPath().isEmpty(),
196                                                                         "File path must not be empty.");
197                                                         Assert.assertEquals(file.getState(), 'Y',
198                                                                         "File state must be actual ('Y').");
199                                                 }
200                                         }
201                                 }
202                         }
203                 }
204
205                 // Call DAO's get method for a not existing id.
206                 try {
207                         steps = _scenarioService.getScenarioInfo(-1L);
208                         // getHibernateTemplate().flush();
209                         Assert
210                                         .fail("Getting an object with not existing id must be failed.");
211                 } catch (Exception e) {
212                         LOG.debug("Expected exception is thrown: "
213                                         + e.getClass().getSimpleName() + ": " + e.getMessage());
214                 }
215                 LOG.debug(">>>>> END testGetScenarioInfo()");
216         }
217
218         /**
219          * Create a transient KnowledgeElement for tests.
220          * 
221          * @return a transient KnowledgeElement
222          * @throws InvalidPropertyException
223          *             if an invalid property is used when creating objects
224          * @throws MultiplyDefinedException
225          *             when trying to create an object with already existing id
226          * @throws MissedPropertyException
227          *             if a mandatory property is not defined for an object to be created
228          * @throws IOException
229          *             if document creation is failed
230          * @throws SQLException
231          *             if project settings loading is failed
232          */
233         private long createScenario() throws InvalidPropertyException,
234                         MissedPropertyException, MultiplyDefinedException, IOException,
235                         SQLException {
236                 // Create a scenario for tests
237                 HibernateTemplate ht = getHibernateTemplate();
238
239                 // Load workflow customization
240                 try {
241                         _projectSettings.configure(ClassLoader.getSystemResource("test/som.xml").getPath());
242                 } catch (FileNotFoundException e) {
243                         Assert.fail("Can't find som.xml: ", e);
244                 }
245                 List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
246                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
247
248                 // Create a test user
249                 User.Properties uprop = new User.Properties();
250                 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
251                                 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
252                                 .addRole("TST_user").setMailAddress(
253                                                 "noreply@salome-platform.org");
254                 uprop.disableCheck();
255                 User anAuthor = new User(uprop);
256                 ht.save(anAuthor);
257
258                 // Create a test study
259                 Study.Properties stprops = new Study.Properties().setReference(
260                                 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
261                 Study aStudy = new Study(stprops);
262                 ht.saveOrUpdate(aStudy);
263
264                 // Create a test scenario
265                 Scenario.Properties sprops = new Scenario.Properties().setTitle(
266                                 "TST_Study").setManager(anAuthor).setOwnerStudy(aStudy);
267                 Scenario aScenario = new Scenario(sprops);
268                 aStudy.getScenariiList().add(aScenario);
269                 ht.saveOrUpdate(anAuthor);
270                 ht.saveOrUpdate(aStudy);
271                 ht.saveOrUpdate(aScenario);
272
273                 Document.Properties dprop = new Document.Properties().setAuthor(
274                                 anAuthor).setDate(new Date());
275                 // Create documents
276                 int i = 0;
277                 for (Step step : steps) {
278                         LOG.debug("Create scenario step: " + i);
279
280                         org.splat.som.Step aScStep = new org.splat.som.Step(step, aScenario);
281                         List<DocumentType> dtypes = _documentTypeService
282                                         .selectTypesOf(step);
283                         for (DocumentType dtype : dtypes) {
284                                 dprop.setName("document" + i++).setType(dtype).setFormat(
285                                                 dtype.getName());
286                                 // Create a document published in the scenario
287                                 Publication pub = _stepService.createDocument(aScStep, dprop);
288                                 Assert.assertNotNull(pub.getOwner(),
289                                                 "The publication must be attached to the scenario.");
290                                 Assert.assertEquals(pub.getOwner().getIndex(), aScenario
291                                                 .getIndex(),
292                                                 "The publication was not attached to the scenario.");
293
294                                 aScenario.add(pub);
295                                 ht.saveOrUpdate(pub);
296
297                                 // Attach a file
298                                 ht.saveOrUpdate(pub.value());
299                                 ht.saveOrUpdate(_publicationService.attach(pub, "med"));
300                         }
301                         if (dtypes.size() <= 0) {
302                                 LOG.debug("No document types are found for scenario step " + i);
303                         }
304                 }
305
306                 Assert.assertNotNull(ht.find("from Document"),
307                                 "No documents in the database.");
308                 Assert.assertTrue(ht.find("from Document").size() > 0,
309                                 "No documents in the database.");
310
311                 Assert.assertNotNull(ht.find("from Publication where owner="
312                                 + aScenario.getIndex()), "No publications in the database.");
313                 Assert.assertTrue(
314                                 ht.find("from Publication where owner=" + aScenario.getIndex())
315                                                 .size() > 0, "No publications in the database.");
316
317                 for (Publication p : (List<Publication>) ht
318                                 .find("from Publication where owner=" + aScenario.getIndex())) {
319                         LOG.debug("Publication found: [id=" + p.getIndex() + ", owner="
320                                         + p.getOwner().getIndex() + ", doc=" + p.value().getIndex()
321                                         + "]");
322                         Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
323                                         "The publication was not attached to the scenario.");
324                 }
325
326                 ht.evict(aScenario);
327                 Scenario aScen = ht.load(Scenario.class, aScenario.getIndex());
328                 Assert.assertNotNull(aScen, "Scenario was not saved in the database.");
329                 Assert.assertTrue(aScen.getDocums().size() > 0,
330                                 "No publications in the scenario.");
331
332                 Assert.assertTrue(i > 0,
333                                 "More then one document must be in the database");
334
335                 return aScenario.getIndex();
336         }
337 }