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.Date;
15 import java.util.HashMap;
16 import java.util.List;
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;
54 import test.splat.common.BaseTest;
57 * Test class for StepService.
59 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
62 public class TestStepService extends BaseTest {
65 * Logger for the class.
67 private static final AppLogger LOG = AppLogger
68 .getLogger(TestStepService.class);
71 * The StudyService. Later injected by Spring.
74 @Qualifier("studyService")
75 private transient StudyService _studyService;
78 * The ScenarioService. Later injected by Spring.
81 @Qualifier("scenarioService")
82 private transient ScenarioService _scenarioService;
85 * The ProjectElementService. Later injected by Spring.
88 @Qualifier("projectElementService")
89 private transient ProjectElementService _projectElementService;
92 * The RepositoryService. Later injected by Spring.
95 @Qualifier("repositoryService")
96 private transient RepositoryService _repositoryService;
99 * The Scenario DAO. Later injected by Spring.
102 @Qualifier("scenarioDAO")
103 private transient ScenarioDAO _scenarioDAO;
106 * The PublicationService. Later injected by Spring.
109 @Qualifier("publicationService")
110 private transient PublicationService _publicationService;
113 * The StepService. Later injected by Spring.
116 @Qualifier("stepService")
117 private transient StepService _stepService;
120 * The SimulationContextService. Later injected by Spring.
123 @Qualifier("simulationContextService")
124 private transient SimulationContextService _simulationContextService;
127 * The ProjectSettingsService. Later injected by Spring.
130 @Qualifier("projectSettings")
131 private transient ProjectSettingsService _projectSettings;
134 * The DocumentTypeService. Later injected by Spring.
137 @Qualifier("documentTypeService")
138 private transient DocumentTypeService _documentTypeService;
141 * The KnowledgeElementTypeService. Later injected by Spring.
144 @Qualifier("knowledgeElementTypeService")
145 private transient KnowledgeElementTypeService _knowledgeElementTypeService;
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:
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>
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>
172 * <B>Outcome results:</B><BR>
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>
178 * <li>Exception is thrown<BR>
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
195 * @throws MismatchException
199 public void testRemoveDocument() throws InvalidPropertyException,
200 MissedPropertyException, MultiplyDefinedException, IOException,
201 SQLException, MismatchException, NotApplicableException {
202 LOG.debug(">>>>> BEGIN testRemoveDocument()");
203 startNestedTransaction();
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();
210 long scenarioId = createScenario();
212 _scenarioDAO.flush();
213 Scenario aScen = _scenarioDAO.get(scenarioId);
215 Assert.assertTrue(ht.find("from UsesRelation").size() > 0,
216 "Uses relations were not created in the database.");
218 Assert.assertTrue(ht.find("from UsedByRelation").size() > 0,
219 "UsedBy relations were not created in the database.");
221 // ////////////////////////////////////////////////////////
222 // Call removeDocument method for each document of the
223 // study/scenario starting from the last document.
225 ProjectElement projElem;
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()
239 org.splat.som.Step aScStep = new org.splat.som.Step(step, projElem);
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());
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()
252 for (UsesRelation rel : uses) {
253 LOG.debug(rel.getTo().getTitle());
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());
262 LOG.debug("From db: It is used by following " + usedBy.size()
264 for (UsedByRelation rel : usedBy) {
265 LOG.debug(rel.getTo().getTitle());
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());
274 ok = _stepService.removeDocument(aScStep, docId);
276 Assert.assertTrue(ok, "Removing was failed.");
277 _scenarioDAO.flush();
279 Assert.assertEquals(ht.find(
280 "from UsesRelation where owner=" + docId).size(), 0,
281 "UsesRelation(s) were not removed from the database.");
283 .assertEquals(ht.find(
284 "from UsedByRelation where owner=" + docId)
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.");
291 .assertEquals(ht.find(
292 "from UsedByRelation where refer=" + docId)
294 "Referencing UsedByRelation(s) were not removed from the database.");
296 .assertEquals(ht.find(
297 "from ConvertsRelation where owner=" + docId)
299 "ConvertsRelation(s) were not removed from the database.");
303 Assert.assertEquals(ht.find("from Document").size(), 0,
304 "Documents were not removed from the database.");
306 Assert.assertEquals(ht.find(
307 "from Publication where owner=" + aScen.getIndex()).size(), 0,
308 "Publications were not removed from the database.");
310 Assert.assertEquals(ht.find("from UsesRelation").size(), 0,
311 "Uses relations were not removed from the database.");
313 Assert.assertEquals(ht.find("from UsedByRelation").size(), 0,
314 "UsedBy relations were not removed from the database.");
316 rollbackNestedTransaction();
317 LOG.debug(">>>>> END testRemoveDocument()");
321 * Create a persistent scenario for tests.
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
335 private long createScenario() throws InvalidPropertyException,
336 MissedPropertyException, MultiplyDefinedException, IOException,
338 // Create a scenario for tests
339 HibernateTemplate ht = getHibernateTemplate();
341 Database.getInstance().reset();
342 _projectSettings.getAllSteps().clear(); // Clear config to be able to load it again
343 // Load workflow customization
345 _projectSettings.configure(ClassLoader.getSystemResource(
346 "test/som.xml").getPath());
347 } catch (FileNotFoundException e) {
348 Assert.fail("Can't find som.xml: ", e);
350 List<Step> steps = _projectSettings.getAllSteps();
351 Assert.assertTrue(steps.size() > 0, "No steps are created.");
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);
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);
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);
378 // Create documents for each scenario step
379 Document.Properties dprop = new Document.Properties().setAuthor(
380 anAuthor).setDate(new Date());
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;
389 if (step.appliesTo(Study.class)) {
392 projElem = aScenario;
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
404 dprop.setName("document" + stepNum).setType(dtype);
405 if (step.getNumber() > 3) {
406 dprop.setFormat("med");
408 dprop.setFormat("py");
410 Publication pub = createDoc(projElem, aScStep, dprop, "med",
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());
419 usedMap.put(pub.getIndex(), usedPub.getIndex());
423 if (dtypes.size() <= 0) {
424 LOG.debug("No document types are found for scenario step " + i);
428 // Check that the scenario and its documents have been created correctly.
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.");
435 Assert.assertNotNull(ht.find("from Publication where owner="
436 + aScenario.getIndex()), "No publications in the database.");
438 ht.find("from Publication where owner=" + aScenario.getIndex())
439 .size() > 0, "No publications in the database.");
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()
446 Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
447 "The publication was not attached to the scenario.");
450 // Remove the scenario from the current hibernate session.
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.");
458 Assert.assertTrue(i > 0,
459 "More then one document must be in the database");
461 // Check created uses relations
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));
477 for (Publication used : aStudy.getDocums()) {
478 found = (used.getIndex() == usedMap.get(usingId));
484 Assert.assertTrue(found,
485 "Uses relation was not created in the database.");
486 foundAny = foundAny || found;
490 Assert.assertTrue(foundAny,
491 "No Uses relation was created in the database.");
493 return aScenario.getIndex();
497 * Create a document published in the scenario. <BR>
499 * document type - type used on the step <BR>
500 * <source-file>.brep <BR>
501 * <attached-file>.med
504 * the scenario to add the document to
506 * scenario step where the document to be published
508 * document properties
509 * @param attachedFileExt
510 * extension of the secon attached (exported) file
512 * outdated document flag
513 * @return the publication of the created document
514 * @throws IOException
515 * @throws MultiplyDefinedException
516 * @throws InvalidPropertyException
517 * @throws MissedPropertyException
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.");
538 HibernateTemplate ht = getHibernateTemplate();
539 ht.saveOrUpdate(pub);
542 ht.save(pub.value());
544 ht.saveOrUpdate(_publicationService.attach(pub, attachedFileExt));