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 @Test(sequential = true, groups = { "service", "functional", "business" })
53 public class TestScenarioService extends BaseTest {
56 * Logger for the class.
58 private static final AppLogger LOG = AppLogger
59 .getLogger(TestScenarioService.class);
62 * The tested ScenarioService. Later injected by Spring.
65 @Qualifier("scenarioService")
66 private transient ScenarioService _scenarioService;
69 * The DocumentService. Later injected by Spring.
72 @Qualifier("documentService")
73 private transient DocumentService _documentService;
76 * The PublicationService. Later injected by Spring.
79 @Qualifier("publicationService")
80 private transient PublicationService _publicationService;
83 * The StepService. Later injected by Spring.
86 @Qualifier("stepService")
87 private transient StepService _stepService;
90 * The ProjectElementService. Later injected by Spring.
93 @Qualifier("projectElementService")
94 private transient ProjectElementService _projectElementService;
97 * The ProjectSettingsService. Later injected by Spring.
100 @Qualifier("projectSettings")
101 private transient ProjectSettingsService _projectSettings;
104 * The DocumentTypeService. Later injected by Spring.
107 @Qualifier("documentTypeService")
108 private transient DocumentTypeService _documentTypeService;
111 * Test of getting a scenario content for building siman-salome.conf.<BR>
112 * <B>Description :</B> <BR>
113 * <i>Create a scenario try to get an info for it.</i><BR>
114 * <B>Action : </B><BR>
115 * <i>1. call the method for an existing scenario id.</i><BR>
116 * <i>2. call the method for a not existing scenario id.</i><BR>
117 * <B>Test data : </B><BR>
118 * <i>no input parameters</i><BR>
119 * <i>no input parameters</i><BR>
121 * <B>Outcome results:</B><BR>
124 * <li>result DTO must contain list of all documents and files<BR>
126 * <li>Exception is thrown<BR>
131 * @throws InvalidPropertyException
132 * if an invalid property is used when creating objects
133 * @throws MultiplyDefinedException
134 * when trying to create an object with already existing id
135 * @throws MissedPropertyException
136 * if a mandatory property is not defined for an object to be created
137 * @throws IOException
138 * if scenario creation is failed
139 * @throws SQLException
140 * if scenario creation is failed
143 public void testGetScenarioInfo() throws InvalidPropertyException,
144 MissedPropertyException, MultiplyDefinedException, IOException,
146 LOG.debug(">>>>> BEGIN testGetScenarioInfo()");
147 long scenarioId = createScenario();
148 // Call DAO's create method for a good transient knowledge element.
149 List<StepDTO> steps = _scenarioService.getScenarioInfo(scenarioId);
150 Assert.assertNotNull(steps, "List of steps must not be null.");
151 Assert.assertTrue(steps.size() > 0, "No steps are read.");
153 List<Step> projSteps = _projectSettings.getStepsOf(Scenario.class);
154 Assert.assertEquals(steps.size(), projSteps.size(),
155 "Not all steps are listed.");
157 for (StepDTO step : steps) {
158 LOG.debug("check the step " + step.getNumber() + ":\n" + step);
159 Assert.assertNotNull(step, "Step DTO must not be null.");
160 Assert.assertNotNull(step.getKey(), "Step name must not be null.");
161 Assert.assertFalse(step.getKey().isEmpty(),
162 "Step name must not empty.");
163 Assert.assertTrue(step.getNumber() > 0,
164 "Step number must be positive integer.");
165 Assert.assertNotNull(step.getDocs(),
166 "Step documents list must not be null.");
168 Step aProjStep = null;
169 for (Step projStep : projSteps) {
170 if (projStep.getNumber() == step.getNumber()) {
171 aProjStep = projStep;
176 List<DocumentType> dtypes = _documentTypeService
177 .selectTypesOf(aProjStep);
178 for (DocumentType dtype : dtypes) {
179 Assert.assertTrue(step.getDocs().size() > 0,
180 "Step documents list must not be empty.");
181 String docName = "document" + docIndex;
183 for (DocumentDTO doc : step.getDocs()) {
184 if (docName.equals(doc.getTitle())) {
185 Assert.assertTrue(doc.getId() > 0,
186 "Document id must be positive integer.");
187 Assert.assertEquals(doc.getTitle(), docName);
188 Assert.assertNotNull(doc.getFiles(),
189 "Document files list must not be null.");
190 Assert.assertTrue(doc.getFiles().size() > 1,
191 "Document must have more then 1 attached file.");
193 for (FileDTO file : doc.getFiles()) {
194 Assert.assertNotNull(file.getPath(),
195 "File path must not be null.");
196 Assert.assertFalse(file.getPath().isEmpty(),
197 "File path must not be empty.");
198 Assert.assertEquals(file.getState(), 'Y',
199 "File state must be actual ('Y').");
206 // Call DAO's get method for a not existing id.
208 steps = _scenarioService.getScenarioInfo(-1L);
209 // getHibernateTemplate().flush();
211 .fail("Getting an object with not existing id must be failed.");
212 } catch (Exception e) {
213 LOG.debug("Expected exception is thrown: "
214 + e.getClass().getSimpleName() + ": " + e.getMessage());
216 LOG.debug(">>>>> END testGetScenarioInfo()");
220 * Create a transient KnowledgeElement for tests.
222 * @return a transient KnowledgeElement
223 * @throws InvalidPropertyException
224 * if an invalid property is used when creating objects
225 * @throws MultiplyDefinedException
226 * when trying to create an object with already existing id
227 * @throws MissedPropertyException
228 * if a mandatory property is not defined for an object to be created
229 * @throws IOException
230 * if document creation is failed
231 * @throws SQLException
232 * if project settings loading is failed
234 private long createScenario() throws InvalidPropertyException,
235 MissedPropertyException, MultiplyDefinedException, IOException,
237 // Create a scenario for tests
238 HibernateTemplate ht = getHibernateTemplate();
240 // Load workflow customization
242 _projectSettings.configure("src/test/som.xml");
243 } catch (FileNotFoundException e) {
244 Assert.fail("Can't find som.xml: ", e);
246 List<Step> steps = _projectSettings.getStepsOf(Scenario.class);
247 Assert.assertTrue(steps.size() > 0, "No steps are created.");
249 // Create a test user
250 User.Properties uprop = new User.Properties();
251 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
252 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
253 .addRole("TST_user").setMailAddress(
254 "noreply@salome-platform.org");
255 uprop.disableCheck();
256 User anAuthor = new User(uprop);
259 // Create a test study
260 Study.Properties stprops = new Study.Properties().setReference(
261 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
262 Study aStudy = new Study(stprops);
263 ht.saveOrUpdate(aStudy);
265 // Create a test scenario
266 Scenario.Properties sprops = new Scenario.Properties().setTitle(
267 "TST_Study").setManager(anAuthor).setOwnerStudy(aStudy);
268 Scenario aScenario = new Scenario(sprops);
269 aStudy.getScenariiList().add(aScenario);
270 ht.saveOrUpdate(anAuthor);
271 ht.saveOrUpdate(aStudy);
272 ht.saveOrUpdate(aScenario);
274 Document.Properties dprop = new Document.Properties().setAuthor(
275 anAuthor).setDate(new Date());
278 for (Step step : steps) {
279 LOG.debug("Create scenario step: " + i);
281 org.splat.som.Step aScStep = new org.splat.som.Step(step, aScenario);
282 List<DocumentType> dtypes = _documentTypeService
283 .selectTypesOf(step);
284 for (DocumentType dtype : dtypes) {
285 dprop.setName("document" + i++).setType(dtype).setFormat(
287 // Create a document published in the scenario
288 Publication pub = _stepService.createDocument(aScStep, dprop);
289 Assert.assertNotNull(pub.getOwner(),
290 "The publication must be attached to the scenario.");
291 Assert.assertEquals(pub.getOwner().getIndex(), aScenario
293 "The publication was not attached to the scenario.");
296 ht.saveOrUpdate(pub);
299 ht.saveOrUpdate(pub.value());
300 ht.saveOrUpdate(_publicationService.attach(pub, "med"));
302 if (dtypes.size() <= 0) {
303 LOG.debug("No document types are found for scenario step " + i);
307 Assert.assertNotNull(ht.find("from Document"),
308 "No documents in the database.");
309 Assert.assertTrue(ht.find("from Document").size() > 0,
310 "No documents in the database.");
312 Assert.assertNotNull(ht.find("from Publication where owner="
313 + aScenario.getIndex()), "No publications in the database.");
315 ht.find("from Publication where owner=" + aScenario.getIndex())
316 .size() > 0, "No publications in the database.");
318 for (Publication p : (List<Publication>) ht
319 .find("from Publication where owner=" + aScenario.getIndex())) {
320 LOG.debug("Publication found: [id=" + p.getIndex() + ", owner="
321 + p.getOwner().getIndex() + ", doc=" + p.value().getIndex()
323 Assert.assertEquals(p.getOwner().getIndex(), aScenario.getIndex(),
324 "The publication was not attached to the scenario.");
328 Scenario aScen = ht.load(Scenario.class, aScenario.getIndex());
329 Assert.assertNotNull(aScen, "Scenario was not saved in the database.");
330 Assert.assertTrue(aScen.getDocums().size() > 0,
331 "No publications in the scenario.");
333 Assert.assertTrue(i > 0,
334 "More then one document must be in the database");
336 return aScenario.getIndex();