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.List;
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;
44 import test.splat.common.BaseTest;
47 * Test class for KnowledgeElementDAO.
49 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
52 public class TestScenarioService extends BaseTest {
55 * Logger for the class.
57 private static final AppLogger LOG = AppLogger
58 .getLogger(TestScenarioService.class);
61 * The tested ScenarioService. Later injected by Spring.
64 @Qualifier("scenarioService")
65 private transient ScenarioService _scenarioService;
68 * The DocumentService. Later injected by Spring.
71 @Qualifier("documentService")
72 private transient DocumentService _documentService;
75 * The PublicationService. Later injected by Spring.
78 @Qualifier("publicationService")
79 private transient PublicationService _publicationService;
82 * The StepService. Later injected by Spring.
85 @Qualifier("stepService")
86 private transient StepService _stepService;
89 * The ProjectElementService. Later injected by Spring.
92 @Qualifier("projectElementService")
93 private transient ProjectElementService _projectElementService;
96 * The ProjectSettingsService. Later injected by Spring.
99 @Qualifier("projectSettings")
100 private transient ProjectSettingsService _projectSettings;
103 * The DocumentTypeService. Later injected by Spring.
106 @Qualifier("documentTypeService")
107 private transient DocumentTypeService _documentTypeService;
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>
120 * <B>Outcome results:</B><BR>
123 * <li>result DTO must contain list of all documents and files<BR>
125 * <li>Exception is thrown<BR>
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
142 public void testGetScenarioInfo() throws InvalidPropertyException,
143 MissedPropertyException, MultiplyDefinedException, IOException,
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.");
152 List<Step> projSteps = _projectSettings.getStepsOf(Scenario.class);
153 Assert.assertEquals(steps.size(), projSteps.size(),
154 "Not all steps are listed.");
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.");
167 Step aProjStep = null;
168 for (Step projStep : projSteps) {
169 if (projStep.getNumber() == step.getNumber()) {
170 aProjStep = projStep;
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 for (DocumentDTO doc : step.getDocs()) {
182 if (docName.equals(doc.getTitle())) {
183 Assert.assertTrue(doc.getId() > 0,
184 "Document id must be positive integer.");
185 Assert.assertEquals(doc.getTitle(), docName);
186 Assert.assertNotNull(doc.getFiles(),
187 "Document files list must not be null.");
188 Assert.assertTrue(doc.getFiles().size() > 1,
189 "Document must have more then 1 attached file.");
191 for (FileDTO file : doc.getFiles()) {
192 Assert.assertNotNull(file.getPath(),
193 "File path must not be null.");
194 Assert.assertFalse(file.getPath().isEmpty(),
195 "File path must not be empty.");
198 <document type="geometry">
199 <import format="brep"/> <!-- Result Shape -->
201 <document type="model">
202 <import format="med"/> <!-- Result mesh without input parameters -->
204 <document type="loads">
205 <import format="c3m"/> <!-- Input data created interactively -->
207 <document type="results">
208 <import format="med"/> <!-- Calculation results source file -->
212 // Check state and processing instruction
213 String fileFormat = file.getPath().substring(
214 file.getPath().lastIndexOf('.') + 1);
215 /* if (_projectSettings.doImport(dtype.getName(),
217 Assert.assertTrue(file.isResult(),
218 "The file must be a result file.");
220 Assert.assertFalse(file.isResult(),
221 "The file must be a source file.");
223 */ if ((docIndex % 2) == 0) { // New
224 Assert.assertEquals(file.getState(), 'Y',
225 "File state must be actual ('Y').");
226 if (_projectSettings.doImport(dtype.getName(),
228 Assert.assertEquals(file.getProcessing(),
230 "File must be imported.");
232 Assert.assertEquals(file.getProcessing(),
234 "File must be downloaded.");
237 Assert.assertEquals(file.getState(), 'O',
238 "File state must be actual ('O').");
240 .assertEquals(file.getProcessing(),
242 "Outdated document should not be imported but downloaded.");
251 // Call DAO's get method for a not existing id.
253 steps = _scenarioService.getScenarioInfo(-1L);
254 // getHibernateTemplate().flush();
256 .fail("Getting an object with not existing id must be failed.");
257 } catch (Exception e) {
258 LOG.debug("Expected exception is thrown: "
259 + e.getClass().getSimpleName() + ": " + e.getMessage());
261 LOG.debug(">>>>> END testGetScenarioInfo()");
265 * Create a transient KnowledgeElement for tests.
267 * @return a transient KnowledgeElement
268 * @throws InvalidPropertyException
269 * if an invalid property is used when creating objects
270 * @throws MultiplyDefinedException
271 * when trying to create an object with already existing id
272 * @throws MissedPropertyException
273 * if a mandatory property is not defined for an object to be created
274 * @throws IOException
275 * if document creation is failed
276 * @throws SQLException
277 * if project settings loading is failed
279 private long createScenario() throws InvalidPropertyException,
280 MissedPropertyException, MultiplyDefinedException, IOException,
282 // Create a scenario for tests
283 HibernateTemplate ht = getHibernateTemplate();
285 // Load workflow customization
287 _projectSettings.configure(ClassLoader.getSystemResource("test/som.xml").getPath());
288 } catch (FileNotFoundException e) {
289 Assert.fail("Can't find som.xml: ", e);
291 List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
292 Assert.assertTrue(steps.size() > 0, "No steps are created.");
294 // Create a test user
295 User.Properties uprop = new User.Properties();
296 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
297 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
298 .addRole("TST_user").setMailAddress(
299 "noreply@salome-platform.org");
300 uprop.disableCheck();
301 User anAuthor = new User(uprop);
304 // Create a test study
305 Study.Properties stprops = new Study.Properties().setReference(
306 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
307 Study aStudy = new Study(stprops);
308 ht.saveOrUpdate(aStudy);
310 // Create a test scenario
311 Scenario.Properties sprops = new Scenario.Properties().setTitle(
312 "TST_Study").setManager(anAuthor).setOwnerStudy(aStudy);
313 Scenario aScenario = new Scenario(sprops);
314 aStudy.getScenariiList().add(aScenario);
315 ht.saveOrUpdate(anAuthor);
316 ht.saveOrUpdate(aStudy);
317 ht.saveOrUpdate(aScenario);
319 Document.Properties dprop = new Document.Properties().setAuthor(
320 anAuthor).setDate(new Date());
323 for (Step step : steps) {
324 LOG.debug("Create scenario step: " + i);
326 org.splat.som.Step aScStep = new org.splat.som.Step(step, aScenario);
327 List<DocumentType> dtypes = _documentTypeService
328 .selectTypesOf(step);
329 for (DocumentType dtype : dtypes) {
330 dprop.setName("document" + i++).setType(dtype).setFormat(
332 // Create a document published in the scenario
333 // document<i>: document type[0] - first type used on the step
334 // <source-file>.brep
335 // <attached-file>.med
336 Publication pub = _stepService.createDocument(aScStep, dprop);
337 Assert.assertNotNull(pub.getOwner(),
338 "The publication must be attached to the scenario.");
339 Assert.assertEquals(pub.getOwner().getIndex(), aScenario
341 "The publication was not attached to the scenario.");
344 ht.saveOrUpdate(pub);
347 ht.saveOrUpdate(pub.value());
348 ht.saveOrUpdate(_publicationService.attach(pub, "med"));
350 // Create a document with outdated publication
351 dprop.setName("document" + i++).setType(dtype).setFormat(
353 // Create a document published in the scenario
354 // document<i>: document type[0] - first type used on the step
355 // <source-file>.brep
356 // <attached-file>.med
357 pub = _stepService.createDocument(aScStep, dprop);
358 Assert.assertNotNull(pub.getOwner(),
359 "The publication must be attached to the scenario.");
360 Assert.assertEquals(pub.getOwner().getIndex(), aScenario
362 "The publication was not attached to the scenario.");
366 ht.saveOrUpdate(pub);
369 ht.saveOrUpdate(pub.value());
370 ht.saveOrUpdate(_publicationService.attach(pub, "med"));
373 if (dtypes.size() <= 0) {
374 LOG.debug("No document types are found for scenario step " + i);
378 Assert.assertNotNull(ht.find("from Document"),
379 "No documents in the database.");
380 Assert.assertTrue(ht.find("from Document").size() > 0,
381 "No documents in the database.");
383 Assert.assertNotNull(ht.find("from Publication where owner="
384 + aScenario.getIndex()), "No publications in the database.");
386 ht.find("from Publication where owner=" + aScenario.getIndex())
387 .size() > 0, "No publications in the database.");
389 for (Publication p : (List<Publication>) ht
390 .find("from Publication where owner=" + aScenario.getIndex())) {
391 LOG.debug("Publication found: [id=" + p.getIndex() + ", owner="
392 + p.getOwner().getIndex() + ", doc=" + p.value().getIndex()
394 Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
395 "The publication was not attached to the scenario.");
399 Scenario aScen = ht.load(Scenario.class, aScenario.getIndex());
400 Assert.assertNotNull(aScen, "Scenario was not saved in the database.");
401 Assert.assertTrue(aScen.getDocums().size() > 0,
402 "No publications in the scenario.");
404 Assert.assertTrue(i > 0,
405 "More then one document must be in the database");
407 return aScenario.getIndex();