40fcb982c1e5d0f449b7f4fd07af6d1145a429b0
[so.git] / bpmn / MSOCoreBPMN / src / test / java / org / onap / so / bpmn / core / BaseTaskTest.java
1 /*
2 * ============LICENSE_START=======================================================
3  * ONAP : SO
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
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.onap.so.bpmn.core;
22
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;
27
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;
36
37 import org.junit.rules.ExpectedException;
38
39 public class BaseTaskTest {
40
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);
51         
52         private DelegateExecution mockExecution;
53         private Expression mockExpression;      
54         private BaseTask baseTask;
55         private Object obj1;
56         private Object obj2;
57         private Object objectString;
58         private Object objectInteger;
59         private Object objectLong;
60         private Object objectBoolean;
61
62         @Rule
63         public ExpectedException expectedException = ExpectedException.none();  
64         
65         @Before
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);
81         }               
82         
83         @Test
84         public void testExecution() throws Exception{
85                 baseTask.execute(mockExecution);
86                 assertEquals("BaseTask", baseTask.getTaskName());
87         }
88         
89         @Test
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());
95                 
96                 expectedException.expect(MissingInjectedFieldException.class);
97                 obj2 = baseTask.getField(null, mockExecution, anyVariable);
98         }       
99         
100         @Test
101         public void testGetFieldAndBadInjectedFieldException() throws Exception{
102                 expectedException.expect(BadInjectedFieldException.class);
103                 obj1 = baseTask.getField(mockExpression, mockExecution, null);
104         }               
105         
106         @Test
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());          
112         }       
113
114         @Test
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());          
120         
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);
125         }               
126         
127         @Test
128         public void testGetStringFieldAndBadInjectedFieldException() throws Exception{
129                 expectedException.expect(BadInjectedFieldException.class);              
130                 obj1 = baseTask.getStringField(mockExpression, mockExecution, null);
131         }       
132         
133         @Test
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());          
139         }       
140         
141         @Test
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);
147                 
148                 expectedException.expect(MissingInjectedFieldException.class);
149                 objectString = new String(badValueString); 
150                 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);          
151                 obj2 = baseTask.getIntegerField(null, mockExecution, anyVariable);
152         }       
153
154         @Test
155         public void testGetIntegerFieldAndBadInjectedFieldException() throws Exception{
156                 expectedException.expect(BadInjectedFieldException.class);
157                 obj1 = baseTask.getIntegerField(mockExpression, mockExecution, null);
158         }       
159         
160         
161         @Test
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);           
167         }       
168         
169         @Test
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);
175         }               
176         
177         @Test
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);
183                 
184                 expectedException.expect(MissingInjectedFieldException.class);
185                 objectString = new String(badValueString);
186                 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);          
187                 obj2 = baseTask.getLongField(null, mockExecution, anyVariable);         
188         }       
189         
190         @Test
191         public void testGetLongFieldAndBadInjectedFieldException() throws Exception{
192                 expectedException.expect(BadInjectedFieldException.class);
193                 obj2 = baseTask.getLongField(mockExpression, mockExecution, null);              
194         }               
195         
196         @Test
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);
202         }       
203
204         @Test
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);
210         }       
211         
212         @Test
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());
218                 
219                 expectedException.expect(MissingInjectedFieldException.class);
220                 objectString = new String(anyValueString);
221                 when(mockExpression.getValue(mockExecution)).thenReturn(objectString);          
222                 obj2 = baseTask.getOutputField(null, mockExecution, anyVariable);               
223         }       
224         
225         @Test
226         public void testGetOutputAndBadInjectedFieldException() throws Exception{
227                 expectedException.expect(BadInjectedFieldException.class);
228                 obj2 = baseTask.getOutputField(null, mockExecution, anyVariable);               
229         }               
230         
231         @Test
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());
237         }       
238         
239         @Test
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);
245         }       
246         
247 }