a45f1fdba2ce78d6ec2ae60f21e3b33f455079ec
[so.git] / bpmn / MSOCoreBPMN / src / test / java / org / openecomp / mso / bpmn / core / TestBaseTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.bpmn.core;
22
23 import static org.junit.Assert.assertNotNull;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.camunda.bpm.engine.RuntimeService;
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.camunda.bpm.engine.delegate.Expression;
31 import org.camunda.bpm.engine.test.Deployment;
32 import org.camunda.bpm.engine.test.ProcessEngineRule;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37
38 import org.openecomp.mso.logger.MsoLogger;
39
40 /**
41  * Unit test for BaseTask class.
42  */
43 public class TestBaseTask {
44
45         @Rule
46         public ProcessEngineRule processEngineRule = new ProcessEngineRule();
47         
48         @Before
49         public void beforeTest() throws Exception {
50                 CamundaDBSetup.configure();
51                 PropertyConfigurationSetup.init();
52         }
53         
54         @Test
55         @Deployment(resources={"BaseTaskTest.bpmn"})
56         public void shouldInvokeService() {
57                 Map<String, Object> variables = new HashMap<String, Object>();
58                 variables.put("firstName", "Jane");
59                 variables.put("lastName", "Doe");
60                 variables.put("age", (Integer)25);
61                 variables.put("lastVisit", (Long)1438270117000L);
62
63                 RuntimeService runtimeService = processEngineRule.getRuntimeService();
64                 assertNotNull(runtimeService);
65                 processEngineRule.getTaskService();
66                 runtimeService.startProcessInstanceByKey("BaseTaskTest", variables);
67         }
68         
69         /**
70          * Unit test code for BaseTask.
71          */
72         public static class TestTask extends BaseTask {
73                 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
74
75                 private Expression existingString;
76                 private Expression nonExistingString;
77                 private Expression existingStringFromVar;
78                 private Expression nonExistingStringFromVar;
79                 
80                 private Expression existingInteger;
81                 private Expression nonExistingInteger;
82                 private Expression existingIntegerFromVar;
83                 private Expression nonExistingIntegerFromVar;
84                 
85                 private Expression existingLong;
86                 private Expression nonExistingLong;
87                 private Expression existingLongFromVar;
88                 private Expression nonExistingLongFromVar;
89                 
90                 private Expression existingOutputVar;
91                 private Expression nonExistingOutputVar;
92                 private Expression existingBadOutputVar;
93                 
94                 public void execute(DelegateExecution execution) throws Exception {
95                         msoLogger.debug("Started executing " + getClass().getSimpleName());
96
97                         /*********************************************************************/
98                         msoLogger.debug("Running String Field Tests");
99                         /*********************************************************************/
100
101                         String s = getStringField(existingString, execution, "existingString");
102                         Assert.assertEquals("Hello World", s);
103
104                         try {
105                                 s = getStringField(nonExistingString, execution, "nonExistingString");
106                                 Assert.fail("Expected BadInjectedFieldException for nonExistingString");
107                         } catch (Exception e) {
108                                 if (!(e instanceof BadInjectedFieldException)) {
109                                         Assert.fail("Expected BadInjectedFieldException for nonExistingString");
110                                 }
111                         }
112
113                         s = getOptionalStringField(existingString, execution, "existingString");
114                         Assert.assertEquals("Hello World", s);
115
116                         s = getOptionalStringField(nonExistingString, execution, "nonExistingString");
117                         Assert.assertEquals(null, s);
118
119                         /*********************************************************************/
120                         msoLogger.debug("Running String Expression Tests");
121                         /*********************************************************************/
122
123                         s = getStringField(existingStringFromVar, execution, "existingStringFromVar");
124                         Assert.assertEquals("Jane", s);
125
126                         try {
127                                 s = getStringField(nonExistingStringFromVar, execution, "nonExistingStringFromVar");
128                                 Assert.fail("Expected BadInjectedFieldException for nonExistingString");
129                         } catch (Exception e) {
130                                 if (!(e instanceof BadInjectedFieldException)) {
131                                         Assert.fail("Expected BadInjectedFieldException for nonExistingStringFromVar");
132                                 }
133                         }
134
135                         s = getOptionalStringField(existingStringFromVar, execution, "existingStringFromVar");
136                         Assert.assertEquals("Jane", s);
137
138                         s = getOptionalStringField(nonExistingStringFromVar, execution, "nonExistingStringFromVar");
139                         Assert.assertEquals(null, s);
140
141                         /*********************************************************************/
142                         msoLogger.debug("Running Integer Field Tests");
143                         /*********************************************************************/
144
145                         Integer i = getIntegerField(existingInteger, execution, "existingInteger");
146                         Assert.assertEquals((Integer)42, i);
147
148                         try {
149                                 i = getIntegerField(nonExistingInteger, execution, "nonExistingInteger");
150                                 Assert.fail("Expected BadInjectedFieldException for nonExistingInteger");
151                         } catch (Exception e) {
152                                 if (!(e instanceof BadInjectedFieldException)) {
153                                         Assert.fail("Expected BadInjectedFieldException for nonExistingInteger");
154                                 }
155                         }
156
157                         i = getOptionalIntegerField(existingInteger, execution, "existingInteger");
158                         Assert.assertEquals((Integer)42, i);
159
160                         i = getOptionalIntegerField(nonExistingInteger, execution, "nonExistingInteger");
161                         Assert.assertEquals(null, i);
162
163                         /*********************************************************************/
164                         msoLogger.debug("Running Integer Expression Tests");
165                         /*********************************************************************/
166
167                         i = getIntegerField(existingIntegerFromVar, execution, "existingIntegerFromVar");
168                         Assert.assertEquals((Integer)25, i);
169
170                         try {
171                                 i = getIntegerField(nonExistingIntegerFromVar, execution, "nonExistingIntegerFromVar");
172                                 Assert.fail("Expected BadInjectedFieldException for nonExistingInteger");
173                         } catch (Exception e) {
174                                 if (!(e instanceof BadInjectedFieldException)) {
175                                         Assert.fail("Expected BadInjectedFieldException for nonExistingIntegerFromVar");
176                                 }
177                         }
178
179                         i = getOptionalIntegerField(existingIntegerFromVar, execution, "existingIntegerFromVar");
180                         Assert.assertEquals((Integer)25, i);
181
182                         i = getOptionalIntegerField(nonExistingIntegerFromVar, execution, "nonExistingIntegerFromVar");
183                         Assert.assertEquals(null, i);
184
185                         /*********************************************************************/
186                         msoLogger.debug("Running Long Field Tests");
187                         /*********************************************************************/
188
189                         Long l = getLongField(existingLong, execution, "existingLong");
190                         Assert.assertEquals((Long)123456789L, l);
191
192                         try {
193                                 l = getLongField(nonExistingLong, execution, "nonExistingLong");
194                                 Assert.fail("Expected BadInjectedFieldException for nonExistingLong");
195                         } catch (Exception e) {
196                                 if (!(e instanceof BadInjectedFieldException)) {
197                                         Assert.fail("Expected BadInjectedFieldException for nonExistingLong");
198                                 }
199                         }
200
201                         l = getOptionalLongField(existingLong, execution, "existingLong");
202                         Assert.assertEquals((Long)123456789L, l);
203
204                         l = getOptionalLongField(nonExistingLong, execution, "nonExistingLong");
205                         Assert.assertEquals(null, l);
206
207                         /*********************************************************************/
208                         msoLogger.debug("Running Long Expression Tests");
209                         /*********************************************************************/
210
211                         l = getLongField(existingLongFromVar, execution, "existingLongFromVar");
212                         Assert.assertEquals((Long)1438270117000L, l);
213
214                         try {
215                                 l = getLongField(nonExistingLongFromVar, execution, "nonExistingLongFromVar");
216                                 Assert.fail("Expected BadInjectedFieldException for nonExistingLong");
217                         } catch (Exception e) {
218                                 if (!(e instanceof BadInjectedFieldException)) {
219                                         Assert.fail("Expected BadInjectedFieldException for nonExistingLongFromVar");
220                                 }
221                         }
222
223                         l = getOptionalLongField(existingLongFromVar, execution, "existingLongFromVar");
224                         Assert.assertEquals((Long)1438270117000L, l);
225
226                         l = getOptionalLongField(nonExistingLongFromVar, execution, "nonExistingLongFromVar");
227                         Assert.assertEquals(null, i);
228
229                         /*********************************************************************/
230                         msoLogger.debug("Running Output Variable Field Tests");
231                         /*********************************************************************/
232
233                         String var = getOutputField(existingOutputVar, execution, "existingOutputVar");
234                         Assert.assertEquals("goodVariable", var);
235
236                         try {
237                                 var = getOutputField(nonExistingOutputVar, execution, "nonExistingOutputVar");
238                                 Assert.fail("Expected BadInjectedFieldException for nonExistingString");
239                         } catch (Exception e) {
240                                 if (!(e instanceof BadInjectedFieldException)) {
241                                         Assert.fail("Expected BadInjectedFieldException for nonExistingString");
242                                 }
243                         }
244
245                         var = getOptionalOutputField(existingOutputVar, execution, "existingOutputVar");
246                         Assert.assertEquals("goodVariable", var);
247
248                         var = getOptionalOutputField(nonExistingOutputVar, execution, "nonExistingOutputVar");
249                         Assert.assertEquals(null, var);
250
251                         try {
252                                 var = getOutputField(existingBadOutputVar, execution, "existingBadOutputVar");
253                                 Assert.fail("Expected BadInjectedFieldException for nonExistingString");
254                         } catch (Exception e) {
255                                 if (!(e instanceof BadInjectedFieldException)) {
256                                         Assert.fail("Expected BadInjectedFieldException for nonExistingString");
257                                 }
258                         }
259
260                         msoLogger.debug("Finished executing " + getClass().getSimpleName());
261                 }
262         }
263 }