2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.core;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotEquals;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
28 import org.camunda.bpm.engine.ProcessEngineServices;
29 import org.camunda.bpm.engine.RepositoryService;
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.camunda.bpm.engine.delegate.Expression;
32 import org.camunda.bpm.engine.repository.ProcessDefinition;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
39 public class BaseTaskTest {
41 private String prefix = "PRE_";
42 private String processKey = "AnyProcessKey";
43 private String definitionId = "100";
44 private String anyVariable = "anyVariable";
45 private String anyValueString = "anyValue";
46 private String badValueString = "123abc";
47 private int anyValueInt = 123;
48 private Integer anyValueInteger = new Integer(anyValueInt);
49 private long anyValuelong = 123L;
50 private Long anyValueLong = new Long(anyValuelong);
52 private DelegateExecution mockExecution;
53 private Expression mockExpression;
54 private BaseTask baseTask;
57 private Object objectString;
58 private Object objectInteger;
59 private Object objectLong;
60 private Object objectBoolean;
63 public ExpectedException expectedException = ExpectedException.none();
66 public void before() throws Exception {
67 baseTask = new BaseTask();
68 ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class);
69 when(mockProcessDefinition.getKey()).thenReturn(processKey);
70 RepositoryService mockRepositoryService = mock(RepositoryService.class);
71 when(mockRepositoryService.getProcessDefinition(definitionId)).thenReturn(mockProcessDefinition);
72 ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class);
73 when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService);
74 mockExecution = mock(DelegateExecution.class);
75 when(mockExecution.getId()).thenReturn(definitionId);
76 when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices);
77 when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition);
78 when(mockExecution.getVariable("prefix")).thenReturn(prefix);
79 when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true");
80 mockExpression = mock(Expression.class);
84 public void testExecution() throws Exception{
85 baseTask.execute(mockExecution);
86 assertEquals("BaseTask", baseTask.getTaskName());
90 public void testGetFieldAndMissingInjectedException() throws Exception{
91 objectString = new String(anyValueString);
92 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
93 obj1 = baseTask.getField(mockExpression, mockExecution, anyVariable);
94 assertEquals(anyValueString, obj1.toString());
96 expectedException.expect(MissingInjectedFieldException.class);
97 obj2 = baseTask.getField(null, mockExecution, anyVariable);
101 public void testGetFieldAndBadInjectedFieldException() throws Exception{
102 expectedException.expect(BadInjectedFieldException.class);
103 obj1 = baseTask.getField(mockExpression, mockExecution, null);
107 public void testGetOptionalField() throws Exception{
108 objectString = new String(anyValueString);
109 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
110 obj1 = baseTask.getOptionalField(mockExpression, mockExecution, anyVariable);
111 assertEquals(anyValueString, obj1.toString());
115 public void testGetStringFieldAndMissingInjectedFieldException() throws Exception{
116 objectString = new String(anyValueString);
117 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
118 obj1 = baseTask.getStringField(mockExpression, mockExecution, anyVariable);
119 assertEquals(anyValueString, obj1.toString());
121 expectedException.expect(MissingInjectedFieldException.class);
122 Object objectBoolean = new Boolean(true); // bad data
123 when(mockExpression.getValue(mockExecution)).thenReturn(objectBoolean);
124 obj2 = baseTask.getStringField(null, mockExecution, anyVariable);
128 public void testGetStringFieldAndBadInjectedFieldException() throws Exception{
129 expectedException.expect(BadInjectedFieldException.class);
130 obj1 = baseTask.getStringField(mockExpression, mockExecution, null);
134 public void testGetOptionalStringField() throws Exception{
135 objectString = new String(anyValueString);
136 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
137 obj1 = baseTask.getOptionalStringField(mockExpression, mockExecution, anyVariable);
138 assertEquals(anyValueString, obj1.toString());
142 public void testGetIntegerFieldAndMissingInjectedFieldException() throws Exception{
143 objectInteger = new Integer(anyValueInt);
144 when(mockExpression.getValue(mockExecution)).thenReturn(objectInteger);
145 obj1 = baseTask.getIntegerField(mockExpression, mockExecution, anyVariable);
146 assertEquals(anyValueInteger, (Integer)obj1);
148 expectedException.expect(MissingInjectedFieldException.class);
149 objectString = new String(badValueString);
150 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
151 obj2 = baseTask.getIntegerField(null, mockExecution, anyVariable);
155 public void testGetIntegerFieldAndBadInjectedFieldException() throws Exception{
156 expectedException.expect(BadInjectedFieldException.class);
157 obj1 = baseTask.getIntegerField(mockExpression, mockExecution, null);
162 public void testGetOptionalIntegerField() throws Exception{
163 objectInteger = new Integer(anyValueInt);
164 when(mockExpression.getValue(mockExecution)).thenReturn(objectInteger);
165 obj1 = baseTask.getOptionalIntegerField(mockExpression, mockExecution, anyVariable);
166 assertEquals(anyValueInteger, (Integer)obj1);
170 public void testGetOptionalIntegerFieldAndBadInjectedFieldException() throws Exception{
171 expectedException.expect(BadInjectedFieldException.class);
172 objectBoolean = new Boolean(true);
173 when(mockExpression.getValue(mockExecution)).thenReturn(objectBoolean);
174 obj1 = baseTask.getOptionalIntegerField(mockExpression, mockExecution, anyVariable);
178 public void testGetLongFieldAndMissingInjectedFieldException() throws Exception{
179 objectLong = new Long(anyValuelong);
180 when(mockExpression.getValue(mockExecution)).thenReturn(objectLong);
181 obj1 = baseTask.getLongField(mockExpression, mockExecution, anyVariable);
182 assertEquals(anyValueLong, (Long)obj1);
184 expectedException.expect(MissingInjectedFieldException.class);
185 objectString = new String(badValueString);
186 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
187 obj2 = baseTask.getLongField(null, mockExecution, anyVariable);
191 public void testGetLongFieldAndBadInjectedFieldException() throws Exception{
192 expectedException.expect(BadInjectedFieldException.class);
193 obj2 = baseTask.getLongField(mockExpression, mockExecution, null);
197 public void testGetOptionalLongField() throws Exception{
198 objectLong = new Long(anyValuelong);
199 when(mockExpression.getValue(mockExecution)).thenReturn(objectLong);
200 obj1 = baseTask.getOptionalLongField(mockExpression, mockExecution, anyVariable);
201 assertEquals(anyValueLong, (Long)obj1);
205 public void testGetOptionalLongFieldAndBadInjectedFieldException() throws Exception{
206 expectedException.expect(BadInjectedFieldException.class);
207 objectBoolean = new Boolean(true);
208 when(mockExpression.getValue(mockExecution)).thenReturn(objectBoolean);
209 obj1 = baseTask.getOptionalLongField(mockExpression, mockExecution, anyVariable);
213 public void testGetOutputAndMissingInjectedFieldException() throws Exception{
214 objectString = new String(anyValueString);
215 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
216 obj1 = baseTask.getOutputField(mockExpression, mockExecution, anyVariable);
217 assertEquals(anyValueString, obj1.toString());
219 expectedException.expect(MissingInjectedFieldException.class);
220 objectString = new String(anyValueString);
221 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
222 obj2 = baseTask.getOutputField(null, mockExecution, anyVariable);
226 public void testGetOutputAndBadInjectedFieldException() throws Exception{
227 expectedException.expect(BadInjectedFieldException.class);
228 obj2 = baseTask.getOutputField(null, mockExecution, anyVariable);
232 public void testGetOptionalOutputField() throws Exception{
233 objectString = new String(anyValueString);
234 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);
235 obj1 = baseTask.getOptionalOutputField(mockExpression, mockExecution, anyVariable);
236 assertEquals(anyValueString, obj1.toString());
240 public void testGetOptionalOutputFieldAndBadInjectedFieldException() throws Exception{
241 expectedException.expect(BadInjectedFieldException.class);
242 objectBoolean = new Boolean(true);
243 when(mockExpression.getValue(mockExecution)).thenReturn(objectBoolean);
244 obj1 = baseTask.getOptionalOutputField(mockExpression, mockExecution, anyVariable);