Initial OpenECOMP MSO commit
[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 java.util.HashMap;
24 import java.util.Map;
25
26 import org.camunda.bpm.engine.RuntimeService;
27 import org.camunda.bpm.engine.delegate.DelegateExecution;
28 import org.camunda.bpm.engine.delegate.Expression;
29 import org.camunda.bpm.engine.test.Deployment;
30 import org.camunda.bpm.engine.test.ProcessEngineRule;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35
36 import org.openecomp.mso.bpmn.test.CamundaDBSetup;
37 import org.openecomp.mso.bpmn.test.PropertyConfigurationSetup;
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                 processEngineRule.getTaskService();
65                 runtimeService.startProcessInstanceByKey("BaseTaskTest", variables);
66         }
67         
68         /**
69          * Unit test code for BaseTask.
70          */
71         public static class TestTask extends BaseTask {
72                 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
73
74                 private Expression existingString;
75                 private Expression nonExistingString;
76                 private Expression existingStringFromVar;
77                 private Expression nonExistingStringFromVar;
78                 
79                 private Expression existingInteger;
80                 private Expression nonExistingInteger;
81                 private Expression existingIntegerFromVar;
82                 private Expression nonExistingIntegerFromVar;
83                 
84                 private Expression existingLong;
85                 private Expression nonExistingLong;
86                 private Expression existingLongFromVar;
87                 private Expression nonExistingLongFromVar;
88                 
89                 private Expression existingOutputVar;
90                 private Expression nonExistingOutputVar;
91                 private Expression existingBadOutputVar;
92                 
93                 public void execute(DelegateExecution execution) throws Exception {
94                         msoLogger.debug("Started executing " + getClass().getSimpleName());
95
96                         /*********************************************************************/
97                         msoLogger.debug("Running String Field Tests");
98                         /*********************************************************************/
99
100                         String s = getStringField(existingString, execution, "existingString");
101                         Assert.assertEquals("Hello World", s);
102
103                         try {
104                                 s = getStringField(nonExistingString, execution, "nonExistingString");
105                                 Assert.fail("Expected BadInjectedFieldException for nonExistingString");
106                         } catch (Exception e) {
107                                 if (!(e instanceof BadInjectedFieldException)) {
108                                         Assert.fail("Expected BadInjectedFieldException for nonExistingString");
109                                 }
110                         }
111
112                         s = getOptionalStringField(existingString, execution, "existingString");
113                         Assert.assertEquals("Hello World", s);
114
115                         s = getOptionalStringField(nonExistingString, execution, "nonExistingString");
116                         Assert.assertEquals(null, s);
117
118                         /*********************************************************************/
119                         msoLogger.debug("Running String Expression Tests");
120                         /*********************************************************************/
121
122                         s = getStringField(existingStringFromVar, execution, "existingStringFromVar");
123                         Assert.assertEquals("Jane", s);
124
125                         try {
126                                 s = getStringField(nonExistingStringFromVar, execution, "nonExistingStringFromVar");
127                                 Assert.fail("Expected BadInjectedFieldException for nonExistingString");
128                         } catch (Exception e) {
129                                 if (!(e instanceof BadInjectedFieldException)) {
130                                         Assert.fail("Expected BadInjectedFieldException for nonExistingStringFromVar");
131                                 }
132                         }
133
134                         s = getOptionalStringField(existingStringFromVar, execution, "existingStringFromVar");
135                         Assert.assertEquals("Jane", s);
136
137                         s = getOptionalStringField(nonExistingStringFromVar, execution, "nonExistingStringFromVar");
138                         Assert.assertEquals(null, s);
139
140                         /*********************************************************************/
141                         msoLogger.debug("Running Integer Field Tests");
142                         /*********************************************************************/
143
144                         Integer i = getIntegerField(existingInteger, execution, "existingInteger");
145                         Assert.assertEquals((Integer)42, i);
146
147                         try {
148                                 i = getIntegerField(nonExistingInteger, execution, "nonExistingInteger");
149                                 Assert.fail("Expected BadInjectedFieldException for nonExistingInteger");
150                         } catch (Exception e) {
151                                 if (!(e instanceof BadInjectedFieldException)) {
152                                         Assert.fail("Expected BadInjectedFieldException for nonExistingInteger");
153                                 }
154                         }
155
156                         i = getOptionalIntegerField(existingInteger, execution, "existingInteger");
157                         Assert.assertEquals((Integer)42, i);
158
159                         i = getOptionalIntegerField(nonExistingInteger, execution, "nonExistingInteger");
160                         Assert.assertEquals(null, i);
161
162                         /*********************************************************************/
163                         msoLogger.debug("Running Integer Expression Tests");
164                         /*********************************************************************/
165
166                         i = getIntegerField(existingIntegerFromVar, execution, "existingIntegerFromVar");
167                         Assert.assertEquals((Integer)25, i);
168
169                         try {
170                                 i = getIntegerField(nonExistingIntegerFromVar, execution, "nonExistingIntegerFromVar");
171                                 Assert.fail("Expected BadInjectedFieldException for nonExistingInteger");
172                         } catch (Exception e) {
173                                 if (!(e instanceof BadInjectedFieldException)) {
174                                         Assert.fail("Expected BadInjectedFieldException for nonExistingIntegerFromVar");
175                                 }
176                         }
177
178                         i = getOptionalIntegerField(existingIntegerFromVar, execution, "existingIntegerFromVar");
179                         Assert.assertEquals((Integer)25, i);
180
181                         i = getOptionalIntegerField(nonExistingIntegerFromVar, execution, "nonExistingIntegerFromVar");
182                         Assert.assertEquals(null, i);
183
184                         /*********************************************************************/
185                         msoLogger.debug("Running Long Field Tests");
186                         /*********************************************************************/
187
188                         Long l = getLongField(existingLong, execution, "existingLong");
189                         Assert.assertEquals((Long)123456789L, l);
190
191                         try {
192                                 l = getLongField(nonExistingLong, execution, "nonExistingLong");
193                                 Assert.fail("Expected BadInjectedFieldException for nonExistingLong");
194                         } catch (Exception e) {
195                                 if (!(e instanceof BadInjectedFieldException)) {
196                                         Assert.fail("Expected BadInjectedFieldException for nonExistingLong");
197                                 }
198                         }
199
200                         l = getOptionalLongField(existingLong, execution, "existingLong");
201                         Assert.assertEquals((Long)123456789L, l);
202
203                         l = getOptionalLongField(nonExistingLong, execution, "nonExistingLong");
204                         Assert.assertEquals(null, l);
205
206                         /*********************************************************************/
207                         msoLogger.debug("Running Long Expression Tests");
208                         /*********************************************************************/
209
210                         l = getLongField(existingLongFromVar, execution, "existingLongFromVar");
211                         Assert.assertEquals((Long)1438270117000L, l);
212
213                         try {
214                                 l = getLongField(nonExistingLongFromVar, execution, "nonExistingLongFromVar");
215                                 Assert.fail("Expected BadInjectedFieldException for nonExistingLong");
216                         } catch (Exception e) {
217                                 if (!(e instanceof BadInjectedFieldException)) {
218                                         Assert.fail("Expected BadInjectedFieldException for nonExistingLongFromVar");
219                                 }
220                         }
221
222                         l = getOptionalLongField(existingLongFromVar, execution, "existingLongFromVar");
223                         Assert.assertEquals((Long)1438270117000L, l);
224
225                         l = getOptionalLongField(nonExistingLongFromVar, execution, "nonExistingLongFromVar");
226                         Assert.assertEquals(null, i);
227
228                         /*********************************************************************/
229                         msoLogger.debug("Running Output Variable Field Tests");
230                         /*********************************************************************/
231
232                         String var = getOutputField(existingOutputVar, execution, "existingOutputVar");
233                         Assert.assertEquals("goodVariable", var);
234
235                         try {
236                                 var = getOutputField(nonExistingOutputVar, execution, "nonExistingOutputVar");
237                                 Assert.fail("Expected BadInjectedFieldException for nonExistingString");
238                         } catch (Exception e) {
239                                 if (!(e instanceof BadInjectedFieldException)) {
240                                         Assert.fail("Expected BadInjectedFieldException for nonExistingString");
241                                 }
242                         }
243
244                         var = getOptionalOutputField(existingOutputVar, execution, "existingOutputVar");
245                         Assert.assertEquals("goodVariable", var);
246
247                         var = getOptionalOutputField(nonExistingOutputVar, execution, "nonExistingOutputVar");
248                         Assert.assertEquals(null, var);
249
250                         try {
251                                 var = getOutputField(existingBadOutputVar, execution, "existingBadOutputVar");
252                                 Assert.fail("Expected BadInjectedFieldException for nonExistingString");
253                         } catch (Exception e) {
254                                 if (!(e instanceof BadInjectedFieldException)) {
255                                         Assert.fail("Expected BadInjectedFieldException for nonExistingString");
256                                 }
257                         }
258
259                         msoLogger.debug("Finished executing " + getClass().getSimpleName());
260                 }
261         }
262 }