2 * ============LICENSE_START======================================================= ONAP : SO
3 * ================================================================================ Copyright (C) 2018 AT&T Intellectual
4 * Property. All rights reserved. ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12 * specific language governing permissions and limitations under the License.
13 * ============LICENSE_END=========================================================
16 package org.onap.so.bpmn.core;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotEquals;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.when;
22 import org.camunda.bpm.engine.ProcessEngineServices;
23 import org.camunda.bpm.engine.RepositoryService;
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.camunda.bpm.engine.delegate.Expression;
26 import org.camunda.bpm.engine.repository.ProcessDefinition;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
32 public class BaseTaskTest {
34 private String prefix = "PRE_";
35 private String processKey = "AnyProcessKey";
36 private String definitionId = "100";
37 private String anyVariable = "anyVariable";
38 private String anyValueString = "anyValue";
39 private String badValueString = "123abc";
40 private int anyValueInt = 123;
41 private Integer anyValueInteger = new Integer(anyValueInt);
42 private long anyValuelong = 123L;
43 private Long anyValueLong = new Long(anyValuelong);
45 private DelegateExecution mockExecution;
46 private Expression mockExpression;
47 private BaseTask baseTask;
50 private Object objectString;
51 private Object objectInteger;
52 private Object objectLong;
53 private Object objectBoolean;
56 public ExpectedException expectedException = ExpectedException.none();
59 public void before() throws Exception {
60 baseTask = new BaseTask();
61 ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class);
62 when(mockProcessDefinition.getKey()).thenReturn(processKey);
63 RepositoryService mockRepositoryService = mock(RepositoryService.class);
64 when(mockRepositoryService.getProcessDefinition(definitionId)).thenReturn(mockProcessDefinition);
65 ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class);
66 when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService);
67 mockExecution = mock(DelegateExecution.class);
68 when(mockExecution.getId()).thenReturn(definitionId);
69 when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices);
70 when(mockExecution.getProcessEngineServices().getRepositoryService()
71 .getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition);
72 when(mockExecution.getVariable("prefix")).thenReturn(prefix);
73 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true");
74 mockExpression = mock(Expression.class);
78 public void testExecution() throws Exception {
79 baseTask.execute(mockExecution);
80 assertEquals("BaseTask", baseTask.getTaskName());
84 public void testGetFieldAndMissingInjectedException() throws Exception {
85 objectString = new String(anyValueString);
86 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
87 obj1 = baseTask.getField(mockExpression, mockExecution, anyVariable);
88 assertEquals(anyValueString, obj1.toString());
90 expectedException.expect(MissingInjectedFieldException.class);
91 obj2 = baseTask.getField(null, mockExecution, anyVariable);
95 public void testGetFieldAndBadInjectedFieldException() throws Exception {
96 expectedException.expect(BadInjectedFieldException.class);
97 obj1 = baseTask.getField(mockExpression, mockExecution, null);
101 public void testGetOptionalField() throws Exception {
102 objectString = new String(anyValueString);
103 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
104 obj1 = baseTask.getOptionalField(mockExpression, mockExecution, anyVariable);
105 assertEquals(anyValueString, obj1.toString());
109 public void testGetStringFieldAndMissingInjectedFieldException() throws Exception {
110 objectString = new String(anyValueString);
111 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
112 obj1 = baseTask.getStringField(mockExpression, mockExecution, anyVariable);
113 assertEquals(anyValueString, obj1.toString());
115 expectedException.expect(MissingInjectedFieldException.class);
116 Object objectBoolean = new Boolean(true); // bad data
117 when(mockExpression.getValue(mockExecution)).thenReturn(objectBoolean);
118 obj2 = baseTask.getStringField(null, mockExecution, anyVariable);
122 public void testGetStringFieldAndBadInjectedFieldException() throws Exception {
123 expectedException.expect(BadInjectedFieldException.class);
124 obj1 = baseTask.getStringField(mockExpression, mockExecution, null);
128 public void testGetOptionalStringField() throws Exception {
129 objectString = new String(anyValueString);
130 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
131 obj1 = baseTask.getOptionalStringField(mockExpression, mockExecution, anyVariable);
132 assertEquals(anyValueString, obj1.toString());
136 public void testGetIntegerFieldAndMissingInjectedFieldException() throws Exception {
137 objectInteger = new Integer(anyValueInt);
138 when(mockExpression.getValue(mockExecution)).thenReturn(objectInteger);
139 obj1 = baseTask.getIntegerField(mockExpression, mockExecution, anyVariable);
140 assertEquals(anyValueInteger, (Integer) obj1);
142 expectedException.expect(MissingInjectedFieldException.class);
143 objectString = new String(badValueString);
144 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
145 obj2 = baseTask.getIntegerField(null, mockExecution, anyVariable);
149 public void testGetIntegerFieldAndBadInjectedFieldException() throws Exception {
150 expectedException.expect(BadInjectedFieldException.class);
151 obj1 = baseTask.getIntegerField(mockExpression, mockExecution, null);
156 public void testGetOptionalIntegerField() throws Exception {
157 objectInteger = new Integer(anyValueInt);
158 when(mockExpression.getValue(mockExecution)).thenReturn(objectInteger);
159 obj1 = baseTask.getOptionalIntegerField(mockExpression, mockExecution, anyVariable);
160 assertEquals(anyValueInteger, (Integer) obj1);
164 public void testGetOptionalIntegerFieldAndBadInjectedFieldException() throws Exception {
165 expectedException.expect(BadInjectedFieldException.class);
166 objectBoolean = new Boolean(true);
167 when(mockExpression.getValue(mockExecution)).thenReturn(objectBoolean);
168 obj1 = baseTask.getOptionalIntegerField(mockExpression, mockExecution, anyVariable);
172 public void testGetLongFieldAndMissingInjectedFieldException() throws Exception {
173 objectLong = new Long(anyValuelong);
174 when(mockExpression.getValue(mockExecution)).thenReturn(objectLong);
175 obj1 = baseTask.getLongField(mockExpression, mockExecution, anyVariable);
176 assertEquals(anyValueLong, (Long) obj1);
178 expectedException.expect(MissingInjectedFieldException.class);
179 objectString = new String(badValueString);
180 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
181 obj2 = baseTask.getLongField(null, mockExecution, anyVariable);
185 public void testGetLongFieldAndBadInjectedFieldException() throws Exception {
186 expectedException.expect(BadInjectedFieldException.class);
187 obj2 = baseTask.getLongField(mockExpression, mockExecution, null);
191 public void testGetOptionalLongField() throws Exception {
192 objectLong = new Long(anyValuelong);
193 when(mockExpression.getValue(mockExecution)).thenReturn(objectLong);
194 obj1 = baseTask.getOptionalLongField(mockExpression, mockExecution, anyVariable);
195 assertEquals(anyValueLong, (Long) obj1);
199 public void testGetOptionalLongFieldAndBadInjectedFieldException() throws Exception {
200 expectedException.expect(BadInjectedFieldException.class);
201 objectBoolean = new Boolean(true);
202 when(mockExpression.getValue(mockExecution)).thenReturn(objectBoolean);
203 obj1 = baseTask.getOptionalLongField(mockExpression, mockExecution, anyVariable);
207 public void testGetOutputAndMissingInjectedFieldException() throws Exception {
208 objectString = new String(anyValueString);
209 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
210 obj1 = baseTask.getOutputField(mockExpression, mockExecution, anyVariable);
211 assertEquals(anyValueString, obj1.toString());
213 expectedException.expect(MissingInjectedFieldException.class);
214 objectString = new String(anyValueString);
215 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
216 obj2 = baseTask.getOutputField(null, mockExecution, anyVariable);
220 public void testGetOutputAndBadInjectedFieldException() throws Exception {
221 expectedException.expect(BadInjectedFieldException.class);
222 obj2 = baseTask.getOutputField(null, mockExecution, anyVariable);
226 public void testGetOptionalOutputField() throws Exception {
227 objectString = new String(anyValueString);
228 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
229 obj1 = baseTask.getOptionalOutputField(mockExpression, mockExecution, anyVariable);
230 assertEquals(anyValueString, obj1.toString());
234 public void testGetOptionalOutputFieldAndBadInjectedFieldException() throws Exception {
235 expectedException.expect(BadInjectedFieldException.class);
236 objectBoolean = new Boolean(true);
237 when(mockExpression.getValue(mockExecution)).thenReturn(objectBoolean);
238 obj1 = baseTask.getOptionalOutputField(mockExpression, mockExecution, anyVariable);