1 /*****************************************************************************
5 * Creation date 12 Oct 2012
8 *****************************************************************************/
9 package test.splat.service;
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.sql.SQLException;
14 import java.util.List;
16 import org.splat.dal.bo.som.KnowledgeElementType;
17 import org.splat.dal.bo.som.Scenario;
18 import org.splat.dal.bo.som.SimulationContextType;
19 import org.splat.dal.dao.som.Database;
20 import org.splat.log.AppLogger;
21 import org.splat.service.DocumentTypeService;
22 import org.splat.service.KnowledgeElementTypeService;
23 import org.splat.service.SimulationContextService;
24 import org.splat.service.technical.ProjectSettingsService;
25 import org.splat.service.technical.ProjectSettingsService.Step;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.beans.factory.annotation.Qualifier;
28 import org.springframework.dao.DuplicateKeyException;
29 import org.testng.Assert;
30 import org.testng.annotations.Test;
32 import test.splat.common.BaseTest;
35 * Test class for ProjectService.
37 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
40 public class TestProjectSettingsService extends BaseTest {
43 * Logger for the class.
45 private static final AppLogger LOG = AppLogger
46 .getLogger(TestProjectSettingsService.class);
49 * The ProjectSettingsService. Later injected by Spring.
52 @Qualifier("projectSettings")
53 private transient ProjectSettingsService _projectSettings;
56 * The DocumentTypeService. Later injected by Spring.
59 @Qualifier("documentTypeService")
60 private transient DocumentTypeService _documentTypeService;
63 * The KnowledgeElementTypeService. Later injected by Spring.
66 @Qualifier("knowledgeElementTypeService")
67 private transient KnowledgeElementTypeService _knowledgeElementTypeService;
70 * The DocumentTypeService. Later injected by Spring.
73 @Qualifier("simulationContextService")
74 private transient SimulationContextService _simulationContextService;
77 * Test of loading document mappings to file formats from customization XML file.<BR>
78 * <B>Description :</B> <BR>
79 * <i>Load customization and check the result.</i><BR>
80 * <B>Action : </B><BR>
81 * <i>1. call the method for som.xml</i><BR>
82 * <i>2. call the method for a xml without mappings.</i><BR>
83 * <i>3. call the method for a not existing file.</i><BR>
84 * <B>Test data : </B><BR>
85 * <i>test/som.xml</i><BR>
86 * <i>test/som-without-mappings.xml</i><BR>
87 * <i>not existing xxx.xml</i><BR>
89 * <B>Outcome results:</B><BR>
92 * <li>doImport() must return true for mapped formats<BR>
94 * <li>doImport() must always return false<BR>
96 * <li>Exception is thrown<BR>
101 * @throws IOException
102 * if configuration loading is failed
103 * @throws SQLException
104 * if configuration loading is failed
107 public void testLoadMappings() throws IOException, SQLException {
108 LOG.debug(">>>>> BEGIN testLoadMappings()()");
109 startNestedTransaction();
110 // ////// Load good workflow customization
112 * geometry: brep model: med loads: c3m results: med
114 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
116 _projectSettings.configure(ClassLoader.getSystemResource(
117 "test/som.xml").getPath());
118 } catch (FileNotFoundException e) {
119 Assert.fail("Can't find configuration file: ", e);
121 List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
122 Assert.assertTrue(steps.size() > 0, "No steps are created.");
123 Assert.assertTrue(_projectSettings.doImport("geometry", "brep"));
124 Assert.assertTrue(_projectSettings.doImport("model", "med"));
125 Assert.assertTrue(_projectSettings.doImport("loads", "c3m"));
126 Assert.assertTrue(_projectSettings.doImport("results", "med"));
128 // ////// Load workflow customization with empty mappings
129 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
131 _projectSettings.configure(ClassLoader.getSystemResource(
132 "test/som-without-mappings.xml").getPath());
133 } catch (FileNotFoundException e) {
134 Assert.fail("Can't find configuration file: ", e);
136 steps = _projectSettings.getStepsOf(Scenario.class);
137 Assert.assertTrue(steps.size() > 0, "No steps are created.");
138 Assert.assertFalse(_projectSettings.doImport("geometry", "brep"));
139 Assert.assertFalse(_projectSettings.doImport("model", "med"));
140 Assert.assertFalse(_projectSettings.doImport("loads", "c3m"));
141 Assert.assertFalse(_projectSettings.doImport("results", "med"));
143 // ////// Load workflow customization from not existing file
144 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
146 _projectSettings.configure(ClassLoader.getSystemResource("/")
150 .fail("Customization loading must fail for not existing configuration file.");
151 } catch (FileNotFoundException e) {
152 LOG.debug("Configuration file must not be found.", e);
155 rollbackNestedTransaction();
156 LOG.debug(">>>>> END testLoadMappings()()");
160 * Test of loading file formats mappings to document types from customization XML file.<BR>
161 * <B>Description :</B> <BR>
162 * <i>Load customization and check the result.</i><BR>
163 * <B>Action : </B><BR>
164 * <i>1. call the method for som.xml</i><BR>
165 * <i>2. call the method for a xml without mappings.</i><BR>
166 * <i>3. call the method for a not existing file.</i><BR>
167 * <B>Test data : </B><BR>
168 * <i>test/som.xml</i><BR>
169 * <i>test/som-without-mappings.xml</i><BR>
170 * <i>not existing xxx.xml</i><BR>
172 * <B>Outcome results:</B><BR>
175 * <li>Following mappings must be loaded:<BR/>
179 * <li>pdf: requirements</li>
180 * <li>doc: specification</li>
181 * <!-- Microsoft Word 2003 and earlier -->
182 * <li>docx: specification</li>
183 * <!-- Microsoft Word 2007 and later -->
184 * <li>xml: specification</li>
185 * <!-- Microsoft Word 2007 Open XML -->
190 * <li>doc: design</li>
191 * <li>docx: design</li>
192 * <li>xml: design</li>
197 * <li>doc: memorandum</li>
198 * <li>docx: memorandum</li>
199 * <li>xml: memorandum</li>
200 * <li>sldprt: geometry</li>
201 * <!-- SolidWorks Part -->
202 * <li>sldasm: geometry</li>
203 * <!-- SolidWorks Assembly -->
204 * <li>part: geometry</li>
206 * <li>py: geometry</li>
207 * <!-- GEOM Python script -->
212 * <li>doc: memorandum</li>
213 * <li>docx: memorandum</li>
214 * <li>xml: memorandum</li>
215 * <li>med: model</li>
217 * <!-- SMESH Python script -->
222 * <li>getDefaultDocumentType must always return null<BR>
224 * <li>Exception is thrown<BR>
229 * @throws IOException
230 * if configuration loading is failed
231 * @throws SQLException
232 * if configuration loading is failed
235 public void testLoadDefaultDocTypes() throws IOException, SQLException {
236 LOG.debug(">>>>> BEGIN testLoadDefaultDocTypes()");
237 startNestedTransaction();
238 // ////// Load good workflow customization
240 * geometry: brep model: med loads: c3m results: med
242 Database.getInstance().reset();
243 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
245 _projectSettings.configure(ClassLoader.getSystemResource(
246 "test/som.xml").getPath());
247 } catch (FileNotFoundException e) {
248 Assert.fail("Can't find configuration file: ", e);
250 Assert.assertTrue(_documentTypeService.selectAllTypes().size() > 0,
251 "No document types are created.");
252 List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
253 Assert.assertTrue(steps.size() > 0, "No steps are created.");
255 for (Step step : steps) {
256 List<String> defTypes = _projectSettings.getDefaultFormats(step);
257 Assert.assertNotNull(defTypes,
258 "getDefaultFormats must always return not null list:");
259 switch (step.getNumber()) {
261 Assert.assertEquals(defTypes.size(), 4);
262 Assert.assertNotNull(_projectSettings
263 .getDefaultDocumentType(step, "pdf"));
264 Assert.assertEquals(_projectSettings
265 .getDefaultDocumentType(step, "pdf").getName(),
267 Assert.assertNotNull(_projectSettings
268 .getDefaultDocumentType(step, "doc"));
269 Assert.assertEquals(_projectSettings
270 .getDefaultDocumentType(step, "doc").getName(),
272 Assert.assertNotNull(_projectSettings
273 .getDefaultDocumentType(step, "docx"));
274 Assert.assertEquals(_projectSettings
275 .getDefaultDocumentType(step, "docx").getName(),
277 Assert.assertNotNull(_projectSettings
278 .getDefaultDocumentType(step, "xml"));
279 Assert.assertEquals(_projectSettings
280 .getDefaultDocumentType(step, "xml").getName(),
284 Assert.assertEquals(defTypes.size(), 3);
285 Assert.assertNull(_projectSettings.getDefaultDocumentType(
287 Assert.assertNotNull(_projectSettings
288 .getDefaultDocumentType(step, "doc"));
289 Assert.assertEquals(_projectSettings
290 .getDefaultDocumentType(step, "doc").getName(),
292 Assert.assertNotNull(_projectSettings
293 .getDefaultDocumentType(step, "docx"));
294 Assert.assertEquals(_projectSettings
295 .getDefaultDocumentType(step, "docx").getName(),
297 Assert.assertNotNull(_projectSettings
298 .getDefaultDocumentType(step, "xml"));
299 Assert.assertEquals(_projectSettings
300 .getDefaultDocumentType(step, "xml").getName(),
304 Assert.assertEquals(defTypes.size(), 8);
305 Assert.assertNull(_projectSettings.getDefaultDocumentType(
307 Assert.assertNotNull(_projectSettings
308 .getDefaultDocumentType(step, "sldprt"));
309 Assert.assertEquals(_projectSettings
310 .getDefaultDocumentType(step, "sldprt").getName(),
312 Assert.assertNotNull(_projectSettings
313 .getDefaultDocumentType(step, "sldasm"));
314 Assert.assertEquals(_projectSettings
315 .getDefaultDocumentType(step, "sldasm").getName(),
317 Assert.assertNotNull(_projectSettings
318 .getDefaultDocumentType(step, "part"));
319 Assert.assertEquals(_projectSettings
320 .getDefaultDocumentType(step, "part").getName(),
322 Assert.assertNotNull(_projectSettings
323 .getDefaultDocumentType(step, "py"));
324 Assert.assertEquals(_projectSettings
325 .getDefaultDocumentType(step, "py").getName(),
327 Assert.assertNotNull(_projectSettings
328 .getDefaultDocumentType(step, "brep"));
329 Assert.assertEquals(_projectSettings
330 .getDefaultDocumentType(step, "brep").getName(),
332 Assert.assertNotNull(_projectSettings
333 .getDefaultDocumentType(step, "doc"));
334 Assert.assertEquals(_projectSettings
335 .getDefaultDocumentType(step, "doc").getName(),
337 Assert.assertNotNull(_projectSettings
338 .getDefaultDocumentType(step, "docx"));
339 Assert.assertEquals(_projectSettings
340 .getDefaultDocumentType(step, "docx").getName(),
342 Assert.assertNotNull(_projectSettings
343 .getDefaultDocumentType(step, "xml"));
344 Assert.assertEquals(_projectSettings
345 .getDefaultDocumentType(step, "xml").getName(),
349 Assert.assertEquals(defTypes.size(), 5);
350 Assert.assertNull(_projectSettings.getDefaultDocumentType(
352 Assert.assertNotNull(_projectSettings
353 .getDefaultDocumentType(step, "med"));
354 Assert.assertEquals(_projectSettings
355 .getDefaultDocumentType(step, "med").getName(),
357 Assert.assertNotNull(_projectSettings
358 .getDefaultDocumentType(step, "py"));
359 Assert.assertEquals(_projectSettings
360 .getDefaultDocumentType(step, "py").getName(),
362 Assert.assertNotNull(_projectSettings
363 .getDefaultDocumentType(step, "doc"));
364 Assert.assertEquals(_projectSettings
365 .getDefaultDocumentType(step, "doc").getName(),
367 Assert.assertNotNull(_projectSettings
368 .getDefaultDocumentType(step, "docx"));
369 Assert.assertEquals(_projectSettings
370 .getDefaultDocumentType(step, "docx").getName(),
372 Assert.assertNotNull(_projectSettings
373 .getDefaultDocumentType(step, "xml"));
374 Assert.assertEquals(_projectSettings
375 .getDefaultDocumentType(step, "xml").getName(),
379 Assert.assertEquals(defTypes.size(), 0);
380 Assert.assertNull(_projectSettings.getDefaultDocumentType(
382 Assert.assertNull(_projectSettings.getDefaultDocumentType(
384 Assert.assertNull(_projectSettings.getDefaultDocumentType(
386 Assert.assertNull(_projectSettings.getDefaultDocumentType(
388 Assert.assertNull(_projectSettings.getDefaultDocumentType(
390 Assert.assertNull(_projectSettings.getDefaultDocumentType(
392 Assert.assertNull(_projectSettings.getDefaultDocumentType(
394 Assert.assertNull(_projectSettings.getDefaultDocumentType(
396 Assert.assertNull(_projectSettings.getDefaultDocumentType(
401 // ////// Load workflow customization with empty mappings
402 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
404 _projectSettings.configure(ClassLoader.getSystemResource(
405 "test/som-without-mappings.xml").getPath());
406 } catch (FileNotFoundException e) {
407 Assert.fail("Can't find configuration file: ", e);
409 steps = _projectSettings.getStepsOf(Scenario.class);
410 Assert.assertTrue(steps.size() > 0, "No steps are created.");
411 for (Step step : steps) {
412 List<String> defTypes = _projectSettings.getDefaultFormats(step);
413 Assert.assertNotNull(defTypes,
414 "getDefaultFormats must always return not null list:");
415 Assert.assertEquals(defTypes.size(), 0);
416 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
418 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
420 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
422 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
424 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
426 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
428 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
430 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
432 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
436 // ////// Load workflow customization from not existing file
437 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
439 _projectSettings.configure(ClassLoader.getSystemResource("/")
443 .fail("Customization loading must fail for not existing configuration file.");
444 } catch (FileNotFoundException e) {
445 LOG.debug("Configuration file must not be found.", e);
448 rollbackNestedTransaction();
449 LOG.debug(">>>>> END testLoadDefaultDocTypes()");
453 * Test of repeated database configuration method (dynamic reconfiguration).<BR>
454 * The problem - duplication of the simer user admin role. <B>Description :</B> <BR>
455 * <i>Load customization twice and check the result.</i><BR>
456 * <B>Action : </B><BR>
457 * <i>1. call the method twice for som.xml</i><BR>
458 * <B>Test data : </B><BR>
459 * <i>test/som.xml</i><BR>
461 * <B>Outcome results:</B><BR>
464 * <li>step must be configured<BR>
469 * @throws IOException
470 * if configuration loading is failed
471 * @throws SQLException
472 * if configuration loading is failed
475 public void testConfigure() throws IOException, SQLException {
476 LOG.debug(">>>>> BEGIN testConfigure()");
477 startNestedTransaction();
478 // ///////////////////////////////////////////////////
479 // ////// Load good workflow customization
480 getHibernateTemplate().clear();
481 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
483 _projectSettings.configure(ClassLoader.getSystemResource(
484 "test/som.xml").getPath());
485 } catch (FileNotFoundException e) {
486 Assert.fail("Can't find configuration file: ", e);
489 List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
490 Assert.assertTrue(steps.size() > 0, "No steps are created.");
491 KnowledgeElementType ucase = _knowledgeElementTypeService.selectType("usecase");
492 Assert.assertNotNull(ucase, "Knowledge type 'usecase' must be created in the database.");
493 SimulationContextType prodtype = _simulationContextService.selectType("product");
494 Assert.assertNotNull(prodtype, "Simulation context type 'product' must be created in the database.");
496 // /////////////////////////////////////////////////////////
497 // ////// Test reconfiguration attempt
498 Database.getInstance().reset();
499 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
501 _projectSettings.configure(ClassLoader.getSystemResource(
502 "test/som.xml").getPath());
503 } catch (FileNotFoundException e) {
504 Assert.fail("Can't find configuration file: ", e);
506 steps = _projectSettings.getStepsOf(Scenario.class);
507 Assert.assertTrue(steps.size() > 0, "No steps are created.");
508 ucase = _knowledgeElementTypeService.selectType("usecase");
509 Assert.assertNotNull(ucase, "Knowledge type 'usecase' must be created in the database.");
510 prodtype = _simulationContextService.selectType("product");
511 Assert.assertNotNull(prodtype, "Simulation context type 'product' must be created in the database.");
515 * The next call to flush() must not throw the following exception: org.springframework.dao.DuplicateKeyException: a different
516 * object with the same identifier value was already associated with the session: [org.splat.dal.bo.kernel.Role#simer]; nested
517 * exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated
518 * with the session: [org.splat.dal.bo.kernel.Role#simer] at
519 * org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:662) at
520 * org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at
521 * org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) at
522 * org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at
523 * org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:881)
525 getHibernateTemplate().flush();
526 } catch (DuplicateKeyException dke) {
528 "User creation failed during the database reconfiguration: "
529 + dke.getMessage(), dke);
532 rollbackNestedTransaction();
533 LOG.debug(">>>>> END testConfigure()");
537 * Test of repeated database configuration method (dynamic reconfiguration).<BR>
538 * The problem - duplication of the simer user admin role. <B>Description :</B> <BR>
539 * <i>Load customization twice and check the result.</i><BR>
540 * <B>Action : </B><BR>
541 * <i>1. call the method twice for som.xml</i><BR>
542 * <B>Test data : </B><BR>
543 * <i>test/som.xml</i><BR>
545 * <B>Outcome results:</B><BR>
548 * <li>step must be configured<BR>
553 * @throws IOException
554 * if configuration loading is failed
555 * @throws SQLException
556 * if configuration loading is failed
559 public void testDatabaseInitialize() throws IOException, SQLException {
560 LOG.debug(">>>>> BEGIN testDatabaseInitialize()");
561 startNestedTransaction();
562 // ///////////////////////////////////////////////////
563 // ////// Load good workflow customization
564 getHibernateTemplate().bulkUpdate("delete from Role");
565 getHibernateTemplate().flush();
566 Database.getInstance().reset();
567 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
569 _projectSettings.configure(ClassLoader.getSystemResource(
570 "test/som.xml").getPath());
571 } catch (FileNotFoundException e) {
572 Assert.fail("Can't find configuration file: ", e);
576 * The next call to flush() must not throw the following exception: org.springframework.dao.DuplicateKeyException: a different
577 * object with the same identifier value was already associated with the session: [org.splat.dal.bo.kernel.Role#simer]; nested
578 * exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated
579 * with the session: [org.splat.dal.bo.kernel.Role#simer] at
580 * org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:662) at
581 * org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at
582 * org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) at
583 * org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at
584 * org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:881)
586 Database.getInstance().initialize();
587 getHibernateTemplate().flush();
588 } catch (DuplicateKeyException dke) {
590 "User creation failed during the database reconfiguration: "
591 + dke.getMessage(), dke);
594 KnowledgeElementType ucase = _knowledgeElementTypeService.selectType("usecase");
595 Assert.assertNotNull(ucase, "Knowledge type 'usecase' must be created in the database.");
596 SimulationContextType prodtype = _simulationContextService.selectType("product");
597 Assert.assertNotNull(prodtype, "Simulation context type 'product' must be created in the database.");
599 rollbackNestedTransaction();
600 LOG.debug(">>>>> END testDatabaseInitialize()");