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.splat.service.technical.StepsConfigService;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Qualifier;
29 import org.springframework.dao.DuplicateKeyException;
30 import org.testng.Assert;
31 import org.testng.annotations.Test;
33 import test.splat.common.BaseTest;
36 * Test class for ProjectService.
38 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
41 public class TestProjectSettingsService extends BaseTest {
44 * Logger for the class.
46 private static final AppLogger LOG = AppLogger
47 .getLogger(TestProjectSettingsService.class);
50 * The ProjectSettingsService. Later injected by Spring.
53 @Qualifier("projectSettings")
54 private transient ProjectSettingsService _projectSettings;
57 * The StepsConfigService. Later injected by Spring.
60 @Qualifier("stepsConfigService")
61 private transient StepsConfigService _stepsConfigService;
64 * The DocumentTypeService. Later injected by Spring.
67 @Qualifier("documentTypeService")
68 private transient DocumentTypeService _documentTypeService;
71 * The KnowledgeElementTypeService. Later injected by Spring.
74 @Qualifier("knowledgeElementTypeService")
75 private transient KnowledgeElementTypeService _knowledgeElementTypeService;
78 * The DocumentTypeService. Later injected by Spring.
81 @Qualifier("simulationContextService")
82 private transient SimulationContextService _simulationContextService;
85 * Test of loading document mappings to file formats from customization XML file.<BR>
86 * <B>Description :</B> <BR>
87 * <i>Load customization and check the result.</i><BR>
88 * <B>Action : </B><BR>
89 * <i>1. call the method for som.xml</i><BR>
90 * <i>2. call the method for a xml without mappings.</i><BR>
91 * <i>3. call the method for a not existing file.</i><BR>
92 * <B>Test data : </B><BR>
93 * <i>test/som.xml</i><BR>
94 * <i>test/som-without-mappings.xml</i><BR>
95 * <i>not existing xxx.xml</i><BR>
97 * <B>Outcome results:</B><BR>
100 * <li>doImport() must return true for mapped formats<BR>
102 * <li>doImport() must always return false<BR>
104 * <li>Exception is thrown<BR>
109 * @throws IOException
110 * if configuration loading is failed
111 * @throws SQLException
112 * if configuration loading is failed
115 public void testLoadMappings() throws IOException, SQLException {
116 LOG.debug(">>>>> BEGIN testLoadMappings()()");
117 startNestedTransaction();
118 // ////// Load good workflow customization
120 * geometry: brep model: med loads: c3m results: med
122 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
124 _projectSettings.configure("classpath:test/som.xml");
125 } catch (FileNotFoundException e) {
126 Assert.fail("Can't find configuration file: ", e);
128 List<Step> steps = _stepsConfigService.getStepsOf(Scenario.class);
129 Assert.assertTrue(steps.size() > 0, "No steps are created.");
130 Assert.assertTrue(_projectSettings.doImport("geometry", "brep"));
131 Assert.assertTrue(_projectSettings.doImport("model", "med"));
132 Assert.assertTrue(_projectSettings.doImport("loads", "c3m"));
133 Assert.assertTrue(_projectSettings.doImport("results", "med"));
135 // ////// Load workflow customization with empty mappings
136 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
138 _projectSettings.configure("classpath:test/som-without-mappings.xml");
139 } catch (FileNotFoundException e) {
140 Assert.fail("Can't find configuration file: ", e);
142 steps = _stepsConfigService.getStepsOf(Scenario.class);
143 Assert.assertTrue(steps.size() > 0, "No steps are created.");
144 Assert.assertFalse(_projectSettings.doImport("geometry", "brep"));
145 Assert.assertFalse(_projectSettings.doImport("model", "med"));
146 Assert.assertFalse(_projectSettings.doImport("loads", "c3m"));
147 Assert.assertFalse(_projectSettings.doImport("results", "med"));
149 // ////// Load workflow customization from not existing file
150 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
152 _projectSettings.configure("classpath:test/xxx.xml");
154 .fail("Customization loading must fail for not existing configuration file.");
155 } catch (FileNotFoundException e) {
156 LOG.debug("Configuration file must not be found.", e);
159 rollbackNestedTransaction();
160 LOG.debug(">>>>> END testLoadMappings()()");
164 * Test of loading file formats mappings to document types from customization XML file.<BR>
165 * <B>Description :</B> <BR>
166 * <i>Load customization and check the result.</i><BR>
167 * <B>Action : </B><BR>
168 * <i>1. call the method for som.xml</i><BR>
169 * <i>2. call the method for a xml without mappings.</i><BR>
170 * <i>3. call the method for a not existing file.</i><BR>
171 * <B>Test data : </B><BR>
172 * <i>test/som.xml</i><BR>
173 * <i>test/som-without-mappings.xml</i><BR>
174 * <i>not existing xxx.xml</i><BR>
176 * <B>Outcome results:</B><BR>
179 * <li>Following mappings must be loaded:<BR/>
183 * <li>pdf: requirements</li>
184 * <li>doc: specification</li>
185 * <!-- Microsoft Word 2003 and earlier -->
186 * <li>docx: specification</li>
187 * <!-- Microsoft Word 2007 and later -->
188 * <li>xml: specification</li>
189 * <!-- Microsoft Word 2007 Open XML -->
194 * <li>doc: design</li>
195 * <li>docx: design</li>
196 * <li>xml: design</li>
201 * <li>doc: memorandum</li>
202 * <li>docx: memorandum</li>
203 * <li>xml: memorandum</li>
204 * <li>sldprt: geometry</li>
205 * <!-- SolidWorks Part -->
206 * <li>sldasm: geometry</li>
207 * <!-- SolidWorks Assembly -->
208 * <li>part: geometry</li>
210 * <li>py: geometry</li>
211 * <!-- GEOM Python script -->
216 * <li>doc: memorandum</li>
217 * <li>docx: memorandum</li>
218 * <li>xml: memorandum</li>
219 * <li>med: model</li>
221 * <!-- SMESH Python script -->
226 * <li>getDefaultDocumentType must always return null<BR>
228 * <li>Exception is thrown<BR>
233 * @throws IOException
234 * if configuration loading is failed
235 * @throws SQLException
236 * if configuration loading is failed
239 public void testLoadDefaultDocTypes() throws IOException, SQLException {
240 LOG.debug(">>>>> BEGIN testLoadDefaultDocTypes()");
241 startNestedTransaction();
242 // ////// Load good workflow customization
244 * geometry: brep model: med loads: c3m results: med
246 Database.getInstance().reset();
247 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
249 _projectSettings.configure("classpath:test/som.xml");
250 } catch (FileNotFoundException e) {
251 Assert.fail("Can't find configuration file: ", e);
253 Assert.assertTrue(_documentTypeService.selectAllTypes().size() > 0,
254 "No document types are created.");
255 List<Step> steps = _stepsConfigService.getStepsOf(Scenario.class);
256 Assert.assertTrue(steps.size() > 0, "No steps are created.");
258 for (Step step : steps) {
259 List<String> defTypes = _projectSettings.getDefaultFormats(step);
260 Assert.assertNotNull(defTypes,
261 "getDefaultFormats must always return not null list:");
262 switch (step.getNumber()) {
264 Assert.assertEquals(defTypes.size(), 4);
265 Assert.assertNotNull(_projectSettings
266 .getDefaultDocumentType(step, "pdf"));
267 Assert.assertEquals(_projectSettings
268 .getDefaultDocumentType(step, "pdf").getName(),
270 Assert.assertNotNull(_projectSettings
271 .getDefaultDocumentType(step, "doc"));
272 Assert.assertEquals(_projectSettings
273 .getDefaultDocumentType(step, "doc").getName(),
275 Assert.assertNotNull(_projectSettings
276 .getDefaultDocumentType(step, "docx"));
277 Assert.assertEquals(_projectSettings
278 .getDefaultDocumentType(step, "docx").getName(),
280 Assert.assertNotNull(_projectSettings
281 .getDefaultDocumentType(step, "xml"));
282 Assert.assertEquals(_projectSettings
283 .getDefaultDocumentType(step, "xml").getName(),
287 Assert.assertEquals(defTypes.size(), 3);
288 Assert.assertNull(_projectSettings.getDefaultDocumentType(
290 Assert.assertNotNull(_projectSettings
291 .getDefaultDocumentType(step, "doc"));
292 Assert.assertEquals(_projectSettings
293 .getDefaultDocumentType(step, "doc").getName(),
295 Assert.assertNotNull(_projectSettings
296 .getDefaultDocumentType(step, "docx"));
297 Assert.assertEquals(_projectSettings
298 .getDefaultDocumentType(step, "docx").getName(),
300 Assert.assertNotNull(_projectSettings
301 .getDefaultDocumentType(step, "xml"));
302 Assert.assertEquals(_projectSettings
303 .getDefaultDocumentType(step, "xml").getName(),
307 Assert.assertEquals(defTypes.size(), 8);
308 Assert.assertNull(_projectSettings.getDefaultDocumentType(
310 Assert.assertNotNull(_projectSettings
311 .getDefaultDocumentType(step, "sldprt"));
312 Assert.assertEquals(_projectSettings
313 .getDefaultDocumentType(step, "sldprt").getName(),
315 Assert.assertNotNull(_projectSettings
316 .getDefaultDocumentType(step, "sldasm"));
317 Assert.assertEquals(_projectSettings
318 .getDefaultDocumentType(step, "sldasm").getName(),
320 Assert.assertNotNull(_projectSettings
321 .getDefaultDocumentType(step, "part"));
322 Assert.assertEquals(_projectSettings
323 .getDefaultDocumentType(step, "part").getName(),
325 Assert.assertNotNull(_projectSettings
326 .getDefaultDocumentType(step, "py"));
327 Assert.assertEquals(_projectSettings
328 .getDefaultDocumentType(step, "py").getName(),
330 Assert.assertNotNull(_projectSettings
331 .getDefaultDocumentType(step, "brep"));
332 Assert.assertEquals(_projectSettings
333 .getDefaultDocumentType(step, "brep").getName(),
335 Assert.assertNotNull(_projectSettings
336 .getDefaultDocumentType(step, "doc"));
337 Assert.assertEquals(_projectSettings
338 .getDefaultDocumentType(step, "doc").getName(),
340 Assert.assertNotNull(_projectSettings
341 .getDefaultDocumentType(step, "docx"));
342 Assert.assertEquals(_projectSettings
343 .getDefaultDocumentType(step, "docx").getName(),
345 Assert.assertNotNull(_projectSettings
346 .getDefaultDocumentType(step, "xml"));
347 Assert.assertEquals(_projectSettings
348 .getDefaultDocumentType(step, "xml").getName(),
352 Assert.assertEquals(defTypes.size(), 5);
353 Assert.assertNull(_projectSettings.getDefaultDocumentType(
355 Assert.assertNotNull(_projectSettings
356 .getDefaultDocumentType(step, "med"));
357 Assert.assertEquals(_projectSettings
358 .getDefaultDocumentType(step, "med").getName(),
360 Assert.assertNotNull(_projectSettings
361 .getDefaultDocumentType(step, "py"));
362 Assert.assertEquals(_projectSettings
363 .getDefaultDocumentType(step, "py").getName(),
365 Assert.assertNotNull(_projectSettings
366 .getDefaultDocumentType(step, "doc"));
367 Assert.assertEquals(_projectSettings
368 .getDefaultDocumentType(step, "doc").getName(),
370 Assert.assertNotNull(_projectSettings
371 .getDefaultDocumentType(step, "docx"));
372 Assert.assertEquals(_projectSettings
373 .getDefaultDocumentType(step, "docx").getName(),
375 Assert.assertNotNull(_projectSettings
376 .getDefaultDocumentType(step, "xml"));
377 Assert.assertEquals(_projectSettings
378 .getDefaultDocumentType(step, "xml").getName(),
382 Assert.assertEquals(defTypes.size(), 2);
383 Assert.assertNull(_projectSettings.getDefaultDocumentType(
385 Assert.assertNull(_projectSettings
386 .getDefaultDocumentType(step, "med"));
387 Assert.assertNotNull(_projectSettings
388 .getDefaultDocumentType(step, "py"));
389 Assert.assertEquals(_projectSettings
390 .getDefaultDocumentType(step, "py").getName(),
392 Assert.assertNotNull(_projectSettings
393 .getDefaultDocumentType(step, "srd"));
394 Assert.assertEquals(_projectSettings
395 .getDefaultDocumentType(step, "srd").getName(),
399 Assert.assertEquals(defTypes.size(), 1);
400 Assert.assertNull(_projectSettings.getDefaultDocumentType(
402 Assert.assertNull(_projectSettings
403 .getDefaultDocumentType(step, "med"));
404 Assert.assertNull(_projectSettings
405 .getDefaultDocumentType(step, "py"));
406 Assert.assertNotNull(_projectSettings
407 .getDefaultDocumentType(step, "xml"));
408 Assert.assertEquals(_projectSettings
409 .getDefaultDocumentType(step, "xml").getName(),
413 Assert.assertEquals(defTypes.size(), 0);
414 Assert.assertNull(_projectSettings.getDefaultDocumentType(
416 Assert.assertNull(_projectSettings.getDefaultDocumentType(
418 Assert.assertNull(_projectSettings.getDefaultDocumentType(
420 Assert.assertNull(_projectSettings.getDefaultDocumentType(
422 Assert.assertNull(_projectSettings.getDefaultDocumentType(
424 Assert.assertNull(_projectSettings.getDefaultDocumentType(
426 Assert.assertNull(_projectSettings.getDefaultDocumentType(
428 Assert.assertNull(_projectSettings.getDefaultDocumentType(
430 Assert.assertNull(_projectSettings.getDefaultDocumentType(
435 // ////// Load workflow customization with empty mappings
436 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
438 _projectSettings.configure("classpath:test/som-without-mappings.xml");
439 } catch (FileNotFoundException e) {
440 Assert.fail("Can't find configuration file: ", e);
442 steps = _stepsConfigService.getStepsOf(Scenario.class);
443 Assert.assertTrue(steps.size() > 0, "No steps are created.");
444 for (Step step : steps) {
445 List<String> defTypes = _projectSettings.getDefaultFormats(step);
446 Assert.assertNotNull(defTypes,
447 "getDefaultFormats must always return not null list:");
448 Assert.assertEquals(defTypes.size(), 0);
449 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
451 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
453 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
455 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
457 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
459 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
461 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
463 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
465 Assert.assertNull(_projectSettings.getDefaultDocumentType(step,
469 // ////// Load workflow customization from not existing file
470 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
472 _projectSettings.configure("classpath:test/xxx.xml");
474 .fail("Customization loading must fail for not existing configuration file.");
475 } catch (FileNotFoundException e) {
476 LOG.debug("Configuration file must not be found.", e);
479 rollbackNestedTransaction();
480 LOG.debug(">>>>> END testLoadDefaultDocTypes()");
484 * Test of repeated database configuration method (dynamic reconfiguration).<BR>
485 * The problem - duplication of the simer user admin role. <B>Description :</B> <BR>
486 * <i>Load customization twice and check the result.</i><BR>
487 * <B>Action : </B><BR>
488 * <i>1. call the method twice for som.xml</i><BR>
489 * <B>Test data : </B><BR>
490 * <i>test/som.xml</i><BR>
492 * <B>Outcome results:</B><BR>
495 * <li>step must be configured<BR>
500 * @throws IOException
501 * if configuration loading is failed
502 * @throws SQLException
503 * if configuration loading is failed
506 public void testConfigure() throws IOException, SQLException {
507 LOG.debug(">>>>> BEGIN testConfigure()");
508 startNestedTransaction();
509 // ///////////////////////////////////////////////////
510 // ////// Load good workflow customization
511 getHibernateTemplate().clear();
512 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
513 Database.getInstance().reset();
515 _projectSettings.configure("classpath:test/som.xml");
516 } catch (FileNotFoundException e) {
517 Assert.fail("Can't find configuration file: ", e);
520 getHibernateTemplate().flush();
521 List<Step> steps = _stepsConfigService.getStepsOf(Scenario.class);
522 Assert.assertTrue(steps.size() > 0, "No steps are created.");
523 KnowledgeElementType ucase = _knowledgeElementTypeService.selectType("usecase");
524 Assert.assertNotNull(ucase, "Knowledge type 'usecase' must be created in the database.");
525 SimulationContextType prodtype = _simulationContextService.selectType("product");
526 Assert.assertNotNull(prodtype, "Simulation context type 'product' must be created in the database.");
528 // /////////////////////////////////////////////////////////
529 // ////// Test reconfiguration attempt
530 Database.getInstance().reset();
531 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
533 _projectSettings.configure("classpath:test/som.xml");
534 } catch (FileNotFoundException e) {
535 Assert.fail("Can't find configuration file: ", e);
537 steps = _stepsConfigService.getStepsOf(Scenario.class);
538 Assert.assertTrue(steps.size() > 0, "No steps are created.");
539 ucase = _knowledgeElementTypeService.selectType("usecase");
540 Assert.assertNotNull(ucase, "Knowledge type 'usecase' must be created in the database.");
541 prodtype = _simulationContextService.selectType("product");
542 Assert.assertNotNull(prodtype, "Simulation context type 'product' must be created in the database.");
546 * The next call to flush() must not throw the following exception: org.springframework.dao.DuplicateKeyException: a different
547 * object with the same identifier value was already associated with the session: [org.splat.dal.bo.kernel.Role#simer]; nested
548 * exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated
549 * with the session: [org.splat.dal.bo.kernel.Role#simer] at
550 * org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:662) at
551 * org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at
552 * org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) at
553 * org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at
554 * org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:881)
556 getHibernateTemplate().flush();
557 } catch (DuplicateKeyException dke) {
559 "User creation failed during the database reconfiguration: "
560 + dke.getMessage(), dke);
563 rollbackNestedTransaction();
564 LOG.debug(">>>>> END testConfigure()");
568 * Test of repeated database configuration method (dynamic reconfiguration).<BR>
569 * The problem - duplication of the simer user admin role. <B>Description :</B> <BR>
570 * <i>Load customization twice and check the result.</i><BR>
571 * <B>Action : </B><BR>
572 * <i>1. call the method twice for som.xml</i><BR>
573 * <B>Test data : </B><BR>
574 * <i>test/som.xml</i><BR>
576 * <B>Outcome results:</B><BR>
579 * <li>step must be configured<BR>
584 * @throws IOException
585 * if configuration loading is failed
586 * @throws SQLException
587 * if configuration loading is failed
590 public void testDatabaseInitialize() throws IOException, SQLException {
591 LOG.debug(">>>>> BEGIN testDatabaseInitialize()");
592 startNestedTransaction();
593 // ///////////////////////////////////////////////////
594 // ////// Load good workflow customization
595 getHibernateTemplate().bulkUpdate("delete from User");
596 getHibernateTemplate().bulkUpdate("delete from Role");
597 getHibernateTemplate().flush();
598 Database.getInstance().reset();
599 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
601 _projectSettings.configure("classpath:test/som.xml");
602 } catch (FileNotFoundException e) {
603 Assert.fail("Can't find configuration file: ", e);
607 * The next call to flush() must not throw the following exception: org.springframework.dao.DuplicateKeyException: a different
608 * object with the same identifier value was already associated with the session: [org.splat.dal.bo.kernel.Role#simer]; nested
609 * exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated
610 * with the session: [org.splat.dal.bo.kernel.Role#simer] at
611 * org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:662) at
612 * org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at
613 * org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411) at
614 * org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at
615 * org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:881)
617 Database.getInstance().initialize();
618 getHibernateTemplate().flush();
619 } catch (DuplicateKeyException dke) {
621 "User creation failed during the database reconfiguration: "
622 + dke.getMessage(), dke);
625 KnowledgeElementType ucase = _knowledgeElementTypeService.selectType("usecase");
626 Assert.assertNotNull(ucase, "Knowledge type 'usecase' must be created in the database.");
627 SimulationContextType prodtype = _simulationContextService.selectType("product");
628 Assert.assertNotNull(prodtype, "Simulation context type 'product' must be created in the database.");
630 rollbackNestedTransaction();
631 LOG.debug(">>>>> END testDatabaseInitialize()");