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