Merge "Enhanced List Level flow with backward support"
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / sdnc / tasks / SDNCQueryTasksTest.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.onap.so.bpmn.infrastructure.sdnc.tasks;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotEquals;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32 import org.camunda.bpm.engine.delegate.BpmnError;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.mockito.ArgumentMatchers;
38 import org.mockito.InjectMocks;
39 import org.onap.so.bpmn.BaseTaskTest;
40 import org.onap.so.bpmn.common.BuildingBlockExecution;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
44 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
45 import org.onap.so.client.exception.BBObjectNotFoundException;
46 import org.onap.so.client.exception.BadResponseException;
47 import org.onap.so.utils.TargetEntities;
48 import org.onap.so.utils.TargetEntity;
49
50 public class SDNCQueryTasksTest extends BaseTaskTest {
51     @InjectMocks
52     private SDNCQueryTasks sdncQueryTasks = new SDNCQueryTasks();
53
54     @Rule
55     public ExpectedException expectedException = ExpectedException.none();
56
57     private ServiceInstance serviceInstance;
58     private GenericVnf genericVnf;
59     private VfModule vfModule;
60
61     @Before
62     public void before() throws BBObjectNotFoundException {
63         serviceInstance = setServiceInstance();
64         genericVnf = setGenericVnf();
65         vfModule = setVfModule();
66
67         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(
68                 any(BuildingBlockExecution.class), eq(7000), any(Exception.class), any(TargetEntities.class));
69         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID)))
70                 .thenReturn(serviceInstance);
71
72         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID)))
73                 .thenReturn(genericVnf);
74
75         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
76
77     }
78
79     @Test
80     public void queryVfModuleTest() throws Exception {
81         String sdncQueryResponse = "response";
82         vfModule.setSelflink("vfModuleSelfLink");
83
84         doReturn(sdncQueryResponse).when(sdncVfModuleResources).queryVfModule(vfModule);
85
86         assertNotEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId()));
87         sdncQueryTasks.queryVfModule(execution);
88         assertEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId()));
89
90         verify(sdncVfModuleResources, times(1)).queryVfModule(vfModule);
91     }
92
93     @Test
94     public void queryVfModuleBadResponseExceptionTest() throws Exception {
95         BadResponseException exception = new BadResponseException("Error received from SDNC");
96         doThrow(exception).when(sdncVfModuleResources).queryVfModule(vfModule);
97
98         expectedException.expect(BpmnError.class);
99         sdncQueryTasks.queryVfModule(execution);
100
101         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(execution, 700, exception, TargetEntity.SDNC);
102     }
103
104     @Test
105     public void queryVfModuleResponseExceptionNoResponseTest() throws Exception {
106         BadResponseException exception = new BadResponseException("Error did not receive a response from SDNC.");
107         doThrow(exception).when(sdncVfModuleResources).queryVfModule(vfModule);
108
109         expectedException.expect(BpmnError.class);
110         sdncQueryTasks.queryVfModule(execution);
111
112         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(execution, 700, exception, TargetEntity.SO);
113     }
114
115     @Test
116     public void queryVnfTest() throws Exception {
117         String sdncQueryResponse = "response";
118
119         doReturn(sdncQueryResponse).when(sdncVnfResources).queryVnf(genericVnf);
120
121         assertNotEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + genericVnf.getVnfId()));
122         sdncQueryTasks.queryVnf(execution);
123         assertEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + genericVnf.getVnfId()));
124
125         verify(sdncVnfResources, times(1)).queryVnf(genericVnf);
126     }
127
128     @Test
129     public void queryVnfBadResponseExceptionTest() throws Exception {
130         BadResponseException exception = new BadResponseException("Error received from SDNC");
131         doThrow(exception).when(sdncVnfResources).queryVnf(genericVnf);
132
133         expectedException.expect(BpmnError.class);
134         sdncQueryTasks.queryVnf(execution);
135
136         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(execution, 700, exception, TargetEntity.SDNC);
137     }
138
139     @Test
140     public void queryVnfBadResponseExceptionNoResponseTest() throws Exception {
141         BadResponseException exception = new BadResponseException("Error did not receive a response from SDNC.");
142         doThrow(exception).when(sdncVnfResources).queryVnf(genericVnf);
143
144         expectedException.expect(BpmnError.class);
145         sdncQueryTasks.queryVnf(execution);
146
147         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(execution, 700, exception, TargetEntity.SO);
148     }
149
150     @Test
151     public void queryVfModuleForVolumeGroupTest() throws Exception {
152         String sdncQueryResponse = "response";
153         vfModule.setSelflink("vfModuleSelfLink");
154
155         doReturn(sdncQueryResponse).when(sdncVfModuleResources).queryVfModule(vfModule);
156
157         assertNotEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId()));
158         sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
159         assertEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId()));
160
161         verify(sdncVfModuleResources, times(1)).queryVfModule(vfModule);
162     }
163
164     @Test
165     public void queryVfModuleForVolumeGroupBadResponseExceptionTest() throws Exception {
166         BadResponseException exception = new BadResponseException("Error received from SDNC");
167         doThrow(exception).when(sdncVfModuleResources).queryVfModule(vfModule);
168
169         expectedException.expect(BpmnError.class);
170         sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
171
172         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(execution, 700, exception, TargetEntity.SDNC);
173     }
174
175     @Test
176     public void queryVfModuleForVolumeGroupBadResponseExceptionNoResponseTest() throws Exception {
177         BadResponseException exception = new BadResponseException("Error did not receive a response from SDNC.");
178         doThrow(exception).when(sdncVfModuleResources).queryVfModule(vfModule);
179
180         expectedException.expect(BpmnError.class);
181         sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
182
183         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(execution, 700, exception, TargetEntity.SO);
184     }
185
186     @Test
187     public void queryVfModuleForVolumeGroupNoSelfLinkExceptionTest() throws Exception {
188         expectedException.expect(BpmnError.class);
189
190         vfModule.setSelflink("");
191
192         sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
193     }
194
195     @Test
196     public void queryVfModuleForVolumeGroupVfObjectExceptionTest() throws Exception {
197         expectedException.expect(BpmnError.class);
198         doThrow(RuntimeException.class).when(extractPojosForBB).extractByKey(any(),
199                 ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID));
200         sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
201
202         verify(sdncVfModuleResources, times(0)).queryVfModule(any(VfModule.class));
203     }
204
205     @Test
206     public void queryVfModuleForVolumeGroupNonVfObjectExceptionTest() throws Exception {
207         expectedException.expect(BpmnError.class);
208
209         sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
210     }
211 }