]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/test/splat/service/TestProjectSettingsService.java
Salome HOME
Test data are not saved now in the test database due to transactions rollbacks. User...
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / service / TestProjectSettingsService.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.List;
15
16 import org.splat.dal.bo.som.Scenario;
17 import org.splat.dal.dao.som.Database;
18 import org.splat.log.AppLogger;
19 import org.splat.service.DocumentTypeService;
20 import org.splat.service.technical.ProjectSettingsService;
21 import org.splat.service.technical.ProjectSettingsService.Step;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.beans.factory.annotation.Qualifier;
24 import org.springframework.dao.DuplicateKeyException;
25 import org.testng.Assert;
26 import org.testng.annotations.Test;
27
28 import test.splat.common.BaseTest;
29
30 /**
31  * Test class for KnowledgeElementDAO.
32  * 
33  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
34  * 
35  */
36 public class TestProjectSettingsService extends BaseTest {
37
38         /**
39          * Logger for the class.
40          */
41         private static final AppLogger LOG = AppLogger
42                         .getLogger(TestProjectSettingsService.class);
43
44         /**
45          * The ProjectSettingsService. Later injected by Spring.
46          */
47         @Autowired
48         @Qualifier("projectSettings")
49         private transient ProjectSettingsService _projectSettings;
50
51         /**
52          * The DocumentTypeService. Later injected by Spring.
53          */
54         @Autowired
55         @Qualifier("documentTypeService")
56         private transient DocumentTypeService _documentTypeService;
57
58         /**
59          * Test of loading document mappings to file formats from customization XML file.<BR>
60          * <B>Description :</B> <BR>
61          * <i>Load customization and check the result.</i><BR>
62          * <B>Action : </B><BR>
63          * <i>1. call the method for som.xml</i><BR>
64          * <i>2. call the method for a xml without mappings.</i><BR>
65          * <i>3. call the method for a not existing file.</i><BR>
66          * <B>Test data : </B><BR>
67          * <i>test/som.xml</i><BR>
68          * <i>test/som-without-mappings.xml</i><BR>
69          * <i>not existing xxx.xml</i><BR>
70          * 
71          * <B>Outcome results:</B><BR>
72          * <i>
73          * <ul>
74          * <li>doImport() must return true for mapped formats<BR>
75          * </li>
76          * <li>doImport() must always return false<BR>
77          * </li>
78          * <li>Exception is thrown<BR>
79          * </li>
80          * </ul>
81          * </i>
82          * 
83          * @throws IOException
84          *             if configuration loading is failed
85          * @throws SQLException
86          *             if configuration loading is failed
87          */
88         @Test
89         public void testLoadMappings() throws IOException, SQLException {
90                 LOG.debug(">>>>> BEGIN testLoadMappings()()");
91                 startNestedTransaction();
92                 // ////// Load good workflow customization
93                 /*
94                  * geometry: brep model: med loads: c3m results: med
95                  */
96                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
97                 try {
98                         _projectSettings.configure(ClassLoader.getSystemResource(
99                                         "test/som.xml").getPath());
100                 } catch (FileNotFoundException e) {
101                         Assert.fail("Can't find configuration file: ", e);
102                 }
103                 List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
104                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
105                 Assert.assertTrue(_projectSettings.doImport("geometry", "brep"));
106                 Assert.assertTrue(_projectSettings.doImport("model", "med"));
107                 Assert.assertTrue(_projectSettings.doImport("loads", "c3m"));
108                 Assert.assertTrue(_projectSettings.doImport("results", "med"));
109
110                 // ////// Load workflow customization with empty mappings
111                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
112                 try {
113                         _projectSettings.configure(ClassLoader.getSystemResource(
114                                         "test/som-without-mappings.xml").getPath());
115                 } catch (FileNotFoundException e) {
116                         Assert.fail("Can't find configuration file: ", e);
117                 }
118                 steps = _projectSettings.getStepsOf(Scenario.class);
119                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
120                 Assert.assertFalse(_projectSettings.doImport("geometry", "brep"));
121                 Assert.assertFalse(_projectSettings.doImport("model", "med"));
122                 Assert.assertFalse(_projectSettings.doImport("loads", "c3m"));
123                 Assert.assertFalse(_projectSettings.doImport("results", "med"));
124
125                 // ////// Load workflow customization from not existing file
126                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
127                 try {
128                         _projectSettings.configure(ClassLoader.getSystemResource("/")
129                                         .getPath()
130                                         + "test/xxx.xml");
131                         Assert
132                                         .fail("Customization loading must fail for not existing configuration file.");
133                 } catch (FileNotFoundException e) {
134                         LOG.debug("Configuration file must not be found.", e);
135                 }
136
137                 rollbackNestedTransaction();
138                 LOG.debug(">>>>> END testLoadMappings()()");
139         }
140
141         /**
142          * Test of loading file formats mappings to document types from customization XML file.<BR>
143          * <B>Description :</B> <BR>
144          * <i>Load customization and check the result.</i><BR>
145          * <B>Action : </B><BR>
146          * <i>1. call the method for som.xml</i><BR>
147          * <i>2. call the method for a xml without mappings.</i><BR>
148          * <i>3. call the method for a not existing file.</i><BR>
149          * <B>Test data : </B><BR>
150          * <i>test/som.xml</i><BR>
151          * <i>test/som-without-mappings.xml</i><BR>
152          * <i>not existing xxx.xml</i><BR>
153          * 
154          * <B>Outcome results:</B><BR>
155          * <i>
156          * <ul>
157          * <li>Following mappings must be loaded:<BR/>
158          * <ul>
159          * <li>step 1
160          * <ul>
161          * <li>pdf: requirements</li>
162          * <li>doc: specification</li>
163          * <!-- Microsoft Word 2003 and earlier -->
164          * <li>docx: specification</li>
165          * <!-- Microsoft Word 2007 and later -->
166          * <li>xml: specification</li>
167          * <!-- Microsoft Word 2007 Open XML -->
168          * </ul>
169          * </li>
170          * <li>step 2
171          * <ul>
172          * <li>doc: design</li>
173          * <li>docx: design</li>
174          * <li>xml: design</li>
175          * </ul>
176          * </li>
177          * <li>step 3
178          * <ul>
179          * <li>doc: memorandum</li>
180          * <li>docx: memorandum</li>
181          * <li>xml: memorandum</li>
182          * <li>sldprt: geometry</li>
183          * <!-- SolidWorks Part -->
184          * <li>sldasm: geometry</li>
185          * <!-- SolidWorks Assembly -->
186          * <li>part: geometry</li>
187          * <!-- GEOM Part -->
188          * <li>py: geometry</li>
189          * <!-- GEOM Python script -->
190          * </ul>
191          * </li>
192          * <li>step 4
193          * <ul>
194          * <li>doc: memorandum</li>
195          * <li>docx: memorandum</li>
196          * <li>xml: memorandum</li>
197          * <li>med: model</li>
198          * <li>py: model</li>
199          * <!-- SMESH Python script -->
200          * </ul>
201          * </li>
202          * </ul>
203          * </li>
204          * <li>getDefaultDocumentType must always return null<BR>
205          * </li>
206          * <li>Exception is thrown<BR>
207          * </li>
208          * </ul>
209          * </i>
210          * 
211          * @throws IOException
212          *             if configuration loading is failed
213          * @throws SQLException
214          *             if configuration loading is failed
215          */
216         @Test
217         public void testLoadDefaultDocTypes() throws IOException, SQLException {
218                 LOG.debug(">>>>> BEGIN testLoadDefaultDocTypes()");
219                 startNestedTransaction();
220                 // ////// Load good workflow customization
221                 /*
222                  * geometry: brep model: med loads: c3m results: med
223                  */
224                 Database.getInstance().reset();
225                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
226                 try {
227                         _projectSettings.configure(ClassLoader.getSystemResource(
228                                         "test/som.xml").getPath());
229                 } catch (FileNotFoundException e) {
230                         Assert.fail("Can't find configuration file: ", e);
231                 }
232                 Assert.assertTrue(_documentTypeService.selectAllTypes().size() > 0,
233                                 "No document types are created.");
234                 List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
235                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
236
237                 for (Step step : steps) {
238                         List<String> defTypes = _projectSettings.getDefaultFormats(step);
239                         Assert.assertNotNull(defTypes,
240                                         "getDefaultFormats must always return not null list:");
241                         switch (step.getNumber()) {
242                                 case 1:
243                                         Assert.assertEquals(defTypes.size(), 4);
244                                         Assert.assertNotNull(_projectSettings
245                                                         .getDefaultDocumentType(step, "pdf"));
246                                         Assert.assertEquals(_projectSettings
247                                                         .getDefaultDocumentType(step, "pdf").getName(),
248                                                         "requirements");
249                                         Assert.assertNotNull(_projectSettings
250                                                         .getDefaultDocumentType(step, "doc"));
251                                         Assert.assertEquals(_projectSettings
252                                                         .getDefaultDocumentType(step, "doc").getName(),
253                                                         "specification");
254                                         Assert.assertNotNull(_projectSettings
255                                                         .getDefaultDocumentType(step, "docx"));
256                                         Assert.assertEquals(_projectSettings
257                                                         .getDefaultDocumentType(step, "docx").getName(),
258                                                         "specification");
259                                         Assert.assertNotNull(_projectSettings
260                                                         .getDefaultDocumentType(step, "xml"));
261                                         Assert.assertEquals(_projectSettings
262                                                         .getDefaultDocumentType(step, "xml").getName(),
263                                                         "specification");
264                                         break;
265                                 case 2:
266                                         Assert.assertEquals(defTypes.size(), 3);
267                                         Assert.assertNull(_projectSettings.getDefaultDocumentType(
268                                                         step, "pdf"));
269                                         Assert.assertNotNull(_projectSettings
270                                                         .getDefaultDocumentType(step, "doc"));
271                                         Assert.assertEquals(_projectSettings
272                                                         .getDefaultDocumentType(step, "doc").getName(),
273                                                         "design");
274                                         Assert.assertNotNull(_projectSettings
275                                                         .getDefaultDocumentType(step, "docx"));
276                                         Assert.assertEquals(_projectSettings
277                                                         .getDefaultDocumentType(step, "docx").getName(),
278                                                         "design");
279                                         Assert.assertNotNull(_projectSettings
280                                                         .getDefaultDocumentType(step, "xml"));
281                                         Assert.assertEquals(_projectSettings
282                                                         .getDefaultDocumentType(step, "xml").getName(),
283                                                         "design");
284                                         break;
285                                 case 3:
286                                         Assert.assertEquals(defTypes.size(), 7);
287                                         Assert.assertNull(_projectSettings.getDefaultDocumentType(
288                                                         step, "pdf"));
289                                         Assert.assertNotNull(_projectSettings
290                                                         .getDefaultDocumentType(step, "sldprt"));
291                                         Assert.assertEquals(_projectSettings
292                                                         .getDefaultDocumentType(step, "sldprt").getName(),
293                                                         "geometry");
294                                         Assert.assertNotNull(_projectSettings
295                                                         .getDefaultDocumentType(step, "sldasm"));
296                                         Assert.assertEquals(_projectSettings
297                                                         .getDefaultDocumentType(step, "sldasm").getName(),
298                                                         "geometry");
299                                         Assert.assertNotNull(_projectSettings
300                                                         .getDefaultDocumentType(step, "part"));
301                                         Assert.assertEquals(_projectSettings
302                                                         .getDefaultDocumentType(step, "part").getName(),
303                                                         "geometry");
304                                         Assert.assertNotNull(_projectSettings
305                                                         .getDefaultDocumentType(step, "py"));
306                                         Assert.assertEquals(_projectSettings
307                                                         .getDefaultDocumentType(step, "py").getName(),
308                                                         "geometry");
309                                         Assert.assertNotNull(_projectSettings
310                                                         .getDefaultDocumentType(step, "doc"));
311                                         Assert.assertEquals(_projectSettings
312                                                         .getDefaultDocumentType(step, "doc").getName(),
313                                                         "memorandum");
314                                         Assert.assertNotNull(_projectSettings
315                                                         .getDefaultDocumentType(step, "docx"));
316                                         Assert.assertEquals(_projectSettings
317                                                         .getDefaultDocumentType(step, "docx").getName(),
318                                                         "memorandum");
319                                         Assert.assertNotNull(_projectSettings
320                                                         .getDefaultDocumentType(step, "xml"));
321                                         Assert.assertEquals(_projectSettings
322                                                         .getDefaultDocumentType(step, "xml").getName(),
323                                                         "memorandum");
324                                         break;
325                                 case 4:
326                                         Assert.assertEquals(defTypes.size(), 5);
327                                         Assert.assertNull(_projectSettings.getDefaultDocumentType(
328                                                         step, "pdf"));
329                                         Assert.assertNotNull(_projectSettings
330                                                         .getDefaultDocumentType(step, "med"));
331                                         Assert.assertEquals(_projectSettings
332                                                         .getDefaultDocumentType(step, "med").getName(),
333                                                         "model");
334                                         Assert.assertNotNull(_projectSettings
335                                                         .getDefaultDocumentType(step, "py"));
336                                         Assert.assertEquals(_projectSettings
337                                                         .getDefaultDocumentType(step, "py").getName(),
338                                                         "model");
339                                         Assert.assertNotNull(_projectSettings
340                                                         .getDefaultDocumentType(step, "doc"));
341                                         Assert.assertEquals(_projectSettings
342                                                         .getDefaultDocumentType(step, "doc").getName(),
343                                                         "memorandum");
344                                         Assert.assertNotNull(_projectSettings
345                                                         .getDefaultDocumentType(step, "docx"));
346                                         Assert.assertEquals(_projectSettings
347                                                         .getDefaultDocumentType(step, "docx").getName(),
348                                                         "memorandum");
349                                         Assert.assertNotNull(_projectSettings
350                                                         .getDefaultDocumentType(step, "xml"));
351                                         Assert.assertEquals(_projectSettings
352                                                         .getDefaultDocumentType(step, "xml").getName(),
353                                                         "memorandum");
354                                         break;
355                                 default:
356                                         Assert.assertEquals(defTypes.size(), 0);
357                                         Assert.assertNull(_projectSettings.getDefaultDocumentType(
358                                                         step, "pdf"));
359                                         Assert.assertNull(_projectSettings.getDefaultDocumentType(
360                                                         step, "doc"));
361                                         Assert.assertNull(_projectSettings.getDefaultDocumentType(
362                                                         step, "docx"));
363                                         Assert.assertNull(_projectSettings.getDefaultDocumentType(
364                                                         step, "xml"));
365                                         Assert.assertNull(_projectSettings.getDefaultDocumentType(
366                                                         step, "sldprt"));
367                                         Assert.assertNull(_projectSettings.getDefaultDocumentType(
368                                                         step, "sldasm"));
369                                         Assert.assertNull(_projectSettings.getDefaultDocumentType(
370                                                         step, "part"));
371                                         Assert.assertNull(_projectSettings.getDefaultDocumentType(
372                                                         step, "py"));
373                                         Assert.assertNull(_projectSettings.getDefaultDocumentType(
374                                                         step, "med"));
375                         }
376                 }
377
378                 // ////// Load workflow customization with empty mappings
379                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
380                 try {
381                         _projectSettings.configure(ClassLoader.getSystemResource(
382                                         "test/som-without-mappings.xml").getPath());
383                 } catch (FileNotFoundException e) {
384                         Assert.fail("Can't find configuration file: ", e);
385                 }
386                 steps = _projectSettings.getStepsOf(Scenario.class);
387                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
388                 for (Step step : steps) {
389                         List<String> defTypes = _projectSettings.getDefaultFormats(step);
390                         Assert.assertNotNull(defTypes,
391                                         "getDefaultFormats must always return not null list:");
392                         Assert.assertEquals(defTypes.size(), 0);
393                         Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
394                                         "pdf"));
395                         Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
396                                         "doc"));
397                         Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
398                                         "docx"));
399                         Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
400                                         "xml"));
401                         Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
402                                         "sldprt"));
403                         Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
404                                         "sldasm"));
405                         Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
406                                         "part"));
407                         Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
408                                         "py"));
409                         Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
410                                         "med"));
411                 }
412
413                 // ////// Load workflow customization from not existing file
414                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
415                 try {
416                         _projectSettings.configure(ClassLoader.getSystemResource("/")
417                                         .getPath()
418                                         + "test/xxx.xml");
419                         Assert
420                                         .fail("Customization loading must fail for not existing configuration file.");
421                 } catch (FileNotFoundException e) {
422                         LOG.debug("Configuration file must not be found.", e);
423                 }
424
425                 rollbackNestedTransaction();
426                 LOG.debug(">>>>> END testLoadDefaultDocTypes()");
427         }
428
429         /**
430          * Test of repeated database configuration method (dynamic reconfiguration).<BR>
431          * The problem - duplication of the simer user admin role. <B>Description :</B> <BR>
432          * <i>Load customization twice and check the result.</i><BR>
433          * <B>Action : </B><BR>
434          * <i>1. call the method twice for som.xml</i><BR>
435          * <B>Test data : </B><BR>
436          * <i>test/som.xml</i><BR>
437          * 
438          * <B>Outcome results:</B><BR>
439          * <i>
440          * <ul>
441          * <li>step must be configured<BR>
442          * </li>
443          * </ul>
444          * </i>
445          * 
446          * @throws IOException
447          *             if configuration loading is failed
448          * @throws SQLException
449          *             if configuration loading is failed
450          */
451         @Test
452         public void testConfigure() throws IOException, SQLException {
453                 LOG.debug(">>>>> BEGIN testConfigure()");
454                 startNestedTransaction();
455                 // ///////////////////////////////////////////////////
456                 // ////// Load good workflow customization
457                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
458                 try {
459                         _projectSettings.configure(ClassLoader.getSystemResource(
460                                         "test/som.xml").getPath());
461                 } catch (FileNotFoundException e) {
462                         Assert.fail("Can't find configuration file: ", e);
463                 }
464
465                 List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
466                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
467
468                 // /////////////////////////////////////////////////////////
469                 // ////// Test reconfiguration attempt
470                 Database.getInstance().reset();
471                 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
472                 try {
473                         _projectSettings.configure(ClassLoader.getSystemResource(
474                                         "test/som.xml").getPath());
475                 } catch (FileNotFoundException e) {
476                         Assert.fail("Can't find configuration file: ", e);
477                 }
478                 steps = _projectSettings.getStepsOf(Scenario.class);
479                 Assert.assertTrue(steps.size() > 0, "No steps are created.");
480
481                 try {
482                         /*
483                          * The next call to flush() must not throw the following exception: org.springframework.dao.DuplicateKeyException: a different
484                          * object with the same identifier value was already associated with the session: [org.splat.dal.bo.kernel.Role#simer]; nested
485                          * exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated
486                          * with the session: [org.splat.dal.bo.kernel.Role#simer] at
487                          * org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:662) at
488                          * org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at
489                          * org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) at
490                          * org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at
491                          * org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:881)
492                          */
493                         getHibernateTemplate().flush();
494                 } catch (DuplicateKeyException dke) {
495                         Assert.fail(
496                                         "User creation failed during the database reconfiguration: "
497                                                         + dke.getMessage(), dke);
498                 }
499
500                 rollbackNestedTransaction();
501                 LOG.debug(">>>>> END testConfigure()");
502         }
503 }