1 /*****************************************************************************
5 * Creation date 12 Oct 2012
8 *****************************************************************************/
9 package test.splat.service;
11 import java.io.IOException;
12 import java.util.Date;
13 import java.util.List;
15 import org.splat.dal.bo.kernel.User;
16 import org.splat.dal.bo.som.Document;
17 import org.splat.dal.bo.som.DocumentType;
18 import org.splat.dal.bo.som.KnowledgeElement;
19 import org.splat.dal.bo.som.Publication;
20 import org.splat.dal.bo.som.Scenario;
21 import org.splat.dal.bo.som.Study;
22 import org.splat.kernel.InvalidPropertyException;
23 import org.splat.kernel.MissedPropertyException;
24 import org.splat.kernel.MultiplyDefinedException;
25 import org.splat.log.AppLogger;
26 import org.splat.service.DocumentService;
27 import org.splat.service.DocumentTypeService;
28 import org.splat.service.ProjectElementService;
29 import org.splat.service.PublicationService;
30 import org.splat.service.ScenarioService;
31 import org.splat.service.StepService;
32 import org.splat.service.dto.StepDTO;
33 import org.splat.som.Step;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Qualifier;
36 import org.springframework.orm.hibernate3.HibernateTemplate;
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
40 import test.splat.common.BaseTest;
43 * Test class for KnowledgeElementDAO.
45 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
48 public class TestScenarioService extends BaseTest {
51 * Logger for the class.
53 private static final AppLogger LOG = AppLogger
54 .getLogger(TestScenarioService.class);
57 * The tested ScenarioService. Later injected by Spring.
60 @Qualifier("scenarioService")
61 private transient ScenarioService _scenarioService;
64 * The DocumentService. Later injected by Spring.
67 @Qualifier("documentService")
68 private transient DocumentService _documentService;
71 * The PublicationService. Later injected by Spring.
74 @Qualifier("publicationService")
75 private transient PublicationService _publicationService;
78 * The StepService. Later injected by Spring.
81 @Qualifier("stepService")
82 private transient StepService _stepService;
85 * The ProjectElementService. Later injected by Spring.
88 @Qualifier("projectElementService")
89 private transient ProjectElementService _projectElementService;
92 * The DocumentTypeService. Later injected by Spring.
95 @Qualifier("documentTypeService")
96 private transient DocumentTypeService _documentTypeService;
99 * Test of getting a scenario content for building siman-salome.conf.<BR>
100 * <B>Description :</B> <BR>
101 * <i>Create a scenario try to get an info for it.</i><BR>
102 * <B>Action : </B><BR>
103 * <i>1. call the method for an existing scenario id.</i><BR>
104 * <i>2. call the method for a not existing scenario id.</i><BR>
105 * <B>Test data : </B><BR>
106 * <i>no input parameters</i><BR>
107 * <i>no input parameters</i><BR>
109 * <B>Outcome results:</B><BR>
112 * <li>result DTO must contain list of all documents and files<BR>
114 * <li>Exception is thrown<BR>
119 * @throws InvalidPropertyException
120 * if an invalid property is used when creating objects
121 * @throws MultiplyDefinedException
122 * when trying to create an object with already existing id
123 * @throws MissedPropertyException
124 * if a mandatory property is not defined for an object to be created
125 * @throws IOException
126 * if scenario creation is failed
130 public void testGetScenarioInfo() throws InvalidPropertyException,
131 MissedPropertyException, MultiplyDefinedException, IOException {
132 LOG.debug(">>>>> BEGIN testGetScenarioInfo()");
133 long scenarioId = createScenario();
134 // Call DAO's create method for a good transient knowledge element.
135 List<StepDTO> steps = _scenarioService.getScenarioInfo(scenarioId);
136 Assert.assertNotNull(steps, "List of steps must not be null.");
137 Assert.assertTrue(steps.size() > 0, "No steps are read.");
139 // Call DAO's get method for a not existing id.
141 steps = _scenarioService.getScenarioInfo(-1L);
142 // getHibernateTemplate().flush();
144 .fail("Getting an object with not existing id must be failed.");
145 } catch (Exception e) {
146 LOG.debug("Expected exception is thrown: "
147 + e.getClass().getSimpleName() + ": " + e.getMessage());
149 LOG.debug(">>>>> END testGetScenarioInfo()");
153 * Create a transient KnowledgeElement for tests.
155 * @return a transient KnowledgeElement
156 * @throws InvalidPropertyException
157 * if an invalid property is used when creating objects
158 * @throws MultiplyDefinedException
159 * when trying to create an object with already existing id
160 * @throws MissedPropertyException
161 * if a mandatory property is not defined for an object to be created
162 * @throws IOException
163 * if document creation is failed
165 private long createScenario() throws InvalidPropertyException,
166 MissedPropertyException, MultiplyDefinedException, IOException {
167 // Create a scenario for tests
168 HibernateTemplate ht = getHibernateTemplate();
170 // Create a test user
171 User.Properties uprop = new User.Properties();
172 uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser")
173 .setFirstName("TST_FirstName").setDisplayName("TST_test.user")
174 .addRole("TST_user").setMailAddress(
175 "noreply@salome-platform.org");
176 uprop.disableCheck();
177 User anAuthor = new User(uprop);
180 // Create a test study
181 Study.Properties stprops = new Study.Properties().setReference(
182 "TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
183 Study aStudy = new Study(stprops);
184 ht.saveOrUpdate(aStudy);
186 // Create a test scenario
187 Scenario.Properties sprops = new Scenario.Properties().setTitle(
188 "TST_Study").setManager(anAuthor).setOwnerStudy(aStudy);
189 Scenario aScenario = new Scenario(sprops);
190 aStudy.getScenariiList().add(aScenario);
191 ht.saveOrUpdate(anAuthor);
193 ht.saveOrUpdate(aScenario);
195 Document.Properties dprop = new Document.Properties().setAuthor(
196 anAuthor).setDate(new Date());
197 Step[] steps = _projectElementService.getSteps(aScenario);
198 Assert.assertTrue(steps.length > 0, "No steps are created.");
200 for (Step step : steps) {
201 List<DocumentType> dtypes = _documentTypeService.selectTypesOf(step
203 dprop.setName("document" + i++).setType(dtypes.get(0));
204 // Create a document published in the scenario
205 Publication pub = _stepService.createDocument(step, dprop);
207 _publicationService.attach(pub, "brep"); // TODO: test for different formats
210 return aScenario.getIndex();
214 * Check that given objects are equal.
217 * the object to check
219 * the expected object
221 private void compareObjects(final KnowledgeElement anActual,
222 final KnowledgeElement anExpected) {
223 Assert.assertNotNull(anActual,
224 "Created object is not found in the database.");
225 Assert.assertEquals(anActual.getAuthor(), anExpected.getAuthor(),
226 "Knowledge Element author is not saved.");
227 Assert.assertEquals(anActual.getDate(), anExpected.getDate(),
228 "Knowledge Element date is not saved.");
230 .assertEquals(anActual.getOwnerScenario(), anExpected
232 "Knowledge Element scenario is not saved.");
233 Assert.assertEquals(anActual.getProgressState(), anExpected
234 .getProgressState(), "Knowledge Element state is not saved.");
235 Assert.assertEquals(anActual.getTitle(), anExpected.getTitle(),
236 "Knowledge Element title is not saved.");
237 Assert.assertEquals(anActual.getType(), anExpected.getType(),
238 "Knowledge Element type is not saved.");
239 Assert.assertEquals(anActual.getValue(), anExpected.getValue(),
240 "Knowledge Element value is not saved.");
241 Assert.assertEquals(anActual.getIndex(), anExpected.getIndex(),
242 "Knowledge Element index is not saved.");