]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/test/splat/service/TestScenarioService.java
Salome HOME
ScenarioService.getScenarioInfo method is added.
[tools/siman.git] / Workspace / Siman-Common / src / test / splat / service / TestScenarioService.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   12 Oct 2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9 package test.splat.service;
10
11 import java.io.IOException;
12 import java.util.Date;
13 import java.util.List;
14
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;
39
40 import test.splat.common.BaseTest;
41
42 /**
43  * Test class for KnowledgeElementDAO.
44  * 
45  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
46  * 
47  */
48 public class TestScenarioService extends BaseTest {
49
50         /**
51          * Logger for the class.
52          */
53         private static final AppLogger LOG = AppLogger
54                         .getLogger(TestScenarioService.class);
55
56         /**
57          * The tested ScenarioService. Later injected by Spring.
58          */
59         @Autowired
60         @Qualifier("scenarioService")
61         private transient ScenarioService _scenarioService;
62
63         /**
64          * The DocumentService. Later injected by Spring.
65          */
66         @Autowired
67         @Qualifier("documentService")
68         private transient DocumentService _documentService;
69
70         /**
71          * The PublicationService. Later injected by Spring.
72          */
73         @Autowired
74         @Qualifier("publicationService")
75         private transient PublicationService _publicationService;
76
77         /**
78          * The StepService. Later injected by Spring.
79          */
80         @Autowired
81         @Qualifier("stepService")
82         private transient StepService _stepService;
83
84         /**
85          * The ProjectElementService. Later injected by Spring.
86          */
87         @Autowired
88         @Qualifier("projectElementService")
89         private transient ProjectElementService _projectElementService;
90
91         /**
92          * The DocumentTypeService. Later injected by Spring.
93          */
94         @Autowired
95         @Qualifier("documentTypeService")
96         private transient DocumentTypeService _documentTypeService;
97
98         /**
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>
108          * 
109          * <B>Outcome results:</B><BR>
110          * <i>
111          * <ul>
112          * <li>result DTO must contain list of all documents and files<BR>
113          * </li>
114          * <li>Exception is thrown<BR>
115          * </li>
116          * </ul>
117          * </i>
118          * 
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
127          * 
128          */
129         @Test
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.");
138
139                 // Call DAO's get method for a not existing id.
140                 try {
141                         steps = _scenarioService.getScenarioInfo(-1L);
142                         // getHibernateTemplate().flush();
143                         Assert
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());
148                 }
149                 LOG.debug(">>>>> END testGetScenarioInfo()");
150         }
151
152         /**
153          * Create a transient KnowledgeElement for tests.
154          * 
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
164          */
165         private long createScenario() throws InvalidPropertyException,
166                         MissedPropertyException, MultiplyDefinedException, IOException {
167                 // Create a scenario for tests
168                 HibernateTemplate ht = getHibernateTemplate();
169
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);
178                 ht.update(anAuthor);
179
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);
185
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);
192                 ht.update(aStudy);
193                 ht.saveOrUpdate(aScenario);
194
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.");
199                 int i = 0;
200                 for (Step step : steps) {
201                         List<DocumentType> dtypes = _documentTypeService.selectTypesOf(step
202                                         .getStep());
203                         dprop.setName("document" + i++).setType(dtypes.get(0));
204                         // Create a document published in the scenario
205                         Publication pub = _stepService.createDocument(step, dprop);
206                         // Attach a file
207                         _publicationService.attach(pub, "brep"); // TODO: test for different formats
208                 }
209
210                 return aScenario.getIndex();
211         }
212
213         /**
214          * Check that given objects are equal.
215          * 
216          * @param anActual
217          *            the object to check
218          * @param anExpected
219          *            the expected object
220          */
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.");
229                 Assert
230                                 .assertEquals(anActual.getOwnerScenario(), anExpected
231                                                 .getOwnerScenario(),
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.");
243         }
244 }