Merge "inherit SOTN codes for CCVPN Services"
[so.git] / bpmn / MSOCoreBPMN / src / test / java / org / openecomp / mso / bpmn / core / TestBaseTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.BeforeClass;
36 import org.junit.Rule;
37 import org.junit.Test;
38
39 import org.openecomp.mso.bpmn.core.utils.CamundaDBSetup;
40 import org.openecomp.mso.logger.MsoLogger;
41
42 /**
43  * Unit test for BaseTask class.
44  */
45 public class TestBaseTask {
46
47         @Rule
48         public ProcessEngineRule processEngineRule = new ProcessEngineRule();
49
50         @BeforeClass
51         public static void setUpClass() {
52                 System.setProperty("mso.config.path", "src/test/resources");
53                 PropertyConfiguration.resetPropertyConfigurationSingletonInstance();
54         }
55
56         @Before
57         public void beforeTest() throws Exception {
58                 CamundaDBSetup.configure();
59                 PropertyConfigurationSetup.init();
60         }
61         
62         @Test
63         @Deployment(resources={"BaseTaskTest.bpmn"})
64         public void shouldInvokeService() {
65                 Map<String, Object> variables = new HashMap<>();
66                 variables.put("firstName", "Jane");
67                 variables.put("lastName", "Doe");
68                 variables.put("age", 25);
69                 variables.put("lastVisit", 1438270117000L);
70
71                 RuntimeService runtimeService = processEngineRule.getRuntimeService();
72                 assertNotNull(runtimeService);
73                 processEngineRule.getTaskService();
74                 runtimeService.startProcessInstanceByKey("BaseTaskTest", variables);
75         }
76         
77         /**
78          * Unit test code for BaseTask.
79          */
80         public static class TestTask extends BaseTask {
81                 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
82
83                 private Expression existingString;
84                 private Expression nonExistingString;
85                 private Expression existingStringFromVar;
86                 private Expression nonExistingStringFromVar;
87                 
88                 private Expression existingInteger;
89                 private Expression nonExistingInteger;
90                 private Expression existingIntegerFromVar;
91                 private Expression nonExistingIntegerFromVar;
92                 
93                 private Expression existingLong;
94                 private Expression nonExistingLong;
95                 private Expression existingLongFromVar;
96                 private Expression nonExistingLongFromVar;
97                 
98                 private Expression existingOutputVar;
99                 private Expression nonExistingOutputVar;
100                 private Expression existingBadOutputVar;
101                 
102                 public void execute(DelegateExecution execution) throws Exception {
103                         msoLogger.debug("Started executing " + getClass().getSimpleName());
104
105                         /*********************************************************************/
106                         msoLogger.debug("Running String Field Tests");
107                         /*********************************************************************/
108
109                         String s = getStringField(existingString, execution, "existingString");
110                         Assert.assertEquals("Hello World", s);
111
112                         try {
113                                 s = getStringField(nonExistingString, execution, "nonExistingString");
114                                 Assert.fail("Expected BadInjectedFieldException for nonExistingString");
115                         } catch (Exception e) {
116                                 if (!(e instanceof BadInjectedFieldException)) {
117                                         Assert.fail("Expected BadInjectedFieldException for nonExistingString");
118                                 }
119                         }
120
121                         s = getOptionalStringField(existingString, execution, "existingString");
122                         Assert.assertEquals("Hello World", s);
123
124                         s = getOptionalStringField(nonExistingString, execution, "nonExistingString");
125                         Assert.assertEquals(null, s);
126
127                         /*********************************************************************/
128                         msoLogger.debug("Running String Expression Tests");
129                         /*********************************************************************/
130
131                         s = getStringField(existingStringFromVar, execution, "existingStringFromVar");
132                         Assert.assertEquals("Jane", s);
133
134                         try {
135                                 s = getStringField(nonExistingStringFromVar, execution, "nonExistingStringFromVar");
136                                 Assert.fail("Expected BadInjectedFieldException for nonExistingString");
137                         } catch (Exception e) {
138                                 if (!(e instanceof BadInjectedFieldException)) {
139                                         Assert.fail("Expected BadInjectedFieldException for nonExistingStringFromVar");
140                                 }
141                         }
142
143                         s = getOptionalStringField(existingStringFromVar, execution, "existingStringFromVar");
144                         Assert.assertEquals("Jane", s);
145
146                         s = getOptionalStringField(nonExistingStringFromVar, execution, "nonExistingStringFromVar");
147                         Assert.assertEquals(null, s);
148
149                         /*********************************************************************/
150                         msoLogger.debug("Running Integer Field Tests");
151                         /*********************************************************************/
152
153                         Integer i = getIntegerField(existingInteger, execution, "existingInteger");
154                         Assert.assertEquals((Integer)42, i);
155
156                         try {
157                                 i = getIntegerField(nonExistingInteger, execution, "nonExistingInteger");
158                                 Assert.fail("Expected BadInjectedFieldException for nonExistingInteger");
159                         } catch (Exception e) {
160                                 if (!(e instanceof BadInjectedFieldException)) {
161                                         Assert.fail("Expected BadInjectedFieldException for nonExistingInteger");
162                                 }
163                         }
164
165                         i = getOptionalIntegerField(existingInteger, execution, "existingInteger");
166                         Assert.assertEquals((Integer)42, i);
167
168                         i = getOptionalIntegerField(nonExistingInteger, execution, "nonExistingInteger");
169                         Assert.assertEquals(null, i);
170
171                         /*********************************************************************/
172                         msoLogger.debug("Running Integer Expression Tests");
173                         /*********************************************************************/
174
175                         i = getIntegerField(existingIntegerFromVar, execution, "existingIntegerFromVar");
176                         Assert.assertEquals((Integer)25, i);
177
178                         try {
179                                 i = getIntegerField(nonExistingIntegerFromVar, execution, "nonExistingIntegerFromVar");
180                                 Assert.fail("Expected BadInjectedFieldException for nonExistingInteger");
181                         } catch (Exception e) {
182                                 if (!(e instanceof BadInjectedFieldException)) {
183                                         Assert.fail("Expected BadInjectedFieldException for nonExistingIntegerFromVar");
184                                 }
185                         }
186
187                         i = getOptionalIntegerField(existingIntegerFromVar, execution, "existingIntegerFromVar");
188                         Assert.assertEquals((Integer)25, i);
189
190                         i = getOptionalIntegerField(nonExistingIntegerFromVar, execution, "nonExistingIntegerFromVar");
191                         Assert.assertEquals(null, i);
192
193                         /*********************************************************************/
194                         msoLogger.debug("Running Long Field Tests");
195                         /*********************************************************************/
196
197                         Long l = getLongField(existingLong, execution, "existingLong");
198                         Assert.assertEquals((Long)123456789L, l);
199
200                         try {
201                                 l = getLongField(nonExistingLong, execution, "nonExistingLong");
202                                 Assert.fail("Expected BadInjectedFieldException for nonExistingLong");
203                         } catch (Exception e) {
204                                 if (!(e instanceof BadInjectedFieldException)) {
205                                         Assert.fail("Expected BadInjectedFieldException for nonExistingLong");
206                                 }
207                         }
208
209                         l = getOptionalLongField(existingLong, execution, "existingLong");
210                         Assert.assertEquals((Long)123456789L, l);
211
212                         l = getOptionalLongField(nonExistingLong, execution, "nonExistingLong");
213                         Assert.assertEquals(null, l);
214
215                         /*********************************************************************/
216                         msoLogger.debug("Running Long Expression Tests");
217                         /*********************************************************************/
218
219                         l = getLongField(existingLongFromVar, execution, "existingLongFromVar");
220                         Assert.assertEquals((Long)1438270117000L, l);
221
222                         try {
223                                 l = getLongField(nonExistingLongFromVar, execution, "nonExistingLongFromVar");
224                                 Assert.fail("Expected BadInjectedFieldException for nonExistingLong");
225                         } catch (Exception e) {
226                                 if (!(e instanceof BadInjectedFieldException)) {
227                                         Assert.fail("Expected BadInjectedFieldException for nonExistingLongFromVar");
228                                 }
229                         }
230
231                         l = getOptionalLongField(existingLongFromVar, execution, "existingLongFromVar");
232                         Assert.assertEquals((Long)1438270117000L, l);
233
234                         l = getOptionalLongField(nonExistingLongFromVar, execution, "nonExistingLongFromVar");
235                         Assert.assertEquals(null, i);
236
237                         /*********************************************************************/
238                         msoLogger.debug("Running Output Variable Field Tests");
239                         /*********************************************************************/
240
241                         String var = getOutputField(existingOutputVar, execution, "existingOutputVar");
242                         Assert.assertEquals("goodVariable", var);
243
244                         try {
245                                 var = getOutputField(nonExistingOutputVar, execution, "nonExistingOutputVar");
246                                 Assert.fail("Expected BadInjectedFieldException for nonExistingString");
247                         } catch (Exception e) {
248                                 if (!(e instanceof BadInjectedFieldException)) {
249                                         Assert.fail("Expected BadInjectedFieldException for nonExistingString");
250                                 }
251                         }
252
253                         var = getOptionalOutputField(existingOutputVar, execution, "existingOutputVar");
254                         Assert.assertEquals("goodVariable", var);
255
256                         var = getOptionalOutputField(nonExistingOutputVar, execution, "nonExistingOutputVar");
257                         Assert.assertEquals(null, var);
258
259                         try {
260                                 var = getOutputField(existingBadOutputVar, execution, "existingBadOutputVar");
261                                 Assert.fail("Expected BadInjectedFieldException for nonExistingString");
262                         } catch (Exception e) {
263                                 if (!(e instanceof BadInjectedFieldException)) {
264                                         Assert.fail("Expected BadInjectedFieldException for nonExistingString");
265                                 }
266                         }
267
268                         msoLogger.debug("Finished executing " + getClass().getSimpleName());
269                 }
270         }
271 }