2 * ============LICENSE_START=======================================================
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
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.infrastructure.sdnc.tasks;
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;
50 public class SDNCQueryTasksTest extends BaseTaskTest {
52 private SDNCQueryTasks sdncQueryTasks = new SDNCQueryTasks();
55 public ExpectedException expectedException = ExpectedException.none();
57 private ServiceInstance serviceInstance;
58 private GenericVnf genericVnf;
59 private VfModule vfModule;
62 public void before() throws BBObjectNotFoundException {
63 serviceInstance = setServiceInstance();
64 genericVnf = setGenericVnf();
65 vfModule = setVfModule();
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);
72 when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID)))
73 .thenReturn(genericVnf);
75 when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
80 public void queryVfModuleTest() throws Exception {
81 String sdncQueryResponse = "response";
82 vfModule.setSelflink("vfModuleSelfLink");
84 doReturn(sdncQueryResponse).when(sdncVfModuleResources).queryVfModule(vfModule);
86 assertNotEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId()));
87 sdncQueryTasks.queryVfModule(execution);
88 assertEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId()));
90 verify(sdncVfModuleResources, times(1)).queryVfModule(vfModule);
94 public void queryVfModuleBadResponseExceptionTest() throws Exception {
95 BadResponseException exception = new BadResponseException("Error received from SDNC");
96 doThrow(exception).when(sdncVfModuleResources).queryVfModule(vfModule);
98 expectedException.expect(BpmnError.class);
99 sdncQueryTasks.queryVfModule(execution);
101 verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(execution, 700, exception, TargetEntity.SDNC);
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);
109 expectedException.expect(BpmnError.class);
110 sdncQueryTasks.queryVfModule(execution);
112 verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(execution, 700, exception, TargetEntity.SO);
116 public void queryVnfTest() throws Exception {
117 String sdncQueryResponse = "response";
119 doReturn(sdncQueryResponse).when(sdncVnfResources).queryVnf(genericVnf);
121 assertNotEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + genericVnf.getVnfId()));
122 sdncQueryTasks.queryVnf(execution);
123 assertEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + genericVnf.getVnfId()));
125 verify(sdncVnfResources, times(1)).queryVnf(genericVnf);
129 public void queryVnfBadResponseExceptionTest() throws Exception {
130 BadResponseException exception = new BadResponseException("Error received from SDNC");
131 doThrow(exception).when(sdncVnfResources).queryVnf(genericVnf);
133 expectedException.expect(BpmnError.class);
134 sdncQueryTasks.queryVnf(execution);
136 verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(execution, 700, exception, TargetEntity.SDNC);
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);
144 expectedException.expect(BpmnError.class);
145 sdncQueryTasks.queryVnf(execution);
147 verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(execution, 700, exception, TargetEntity.SO);
151 public void queryVfModuleForVolumeGroupTest() throws Exception {
152 String sdncQueryResponse = "response";
153 vfModule.setSelflink("vfModuleSelfLink");
155 doReturn(sdncQueryResponse).when(sdncVfModuleResources).queryVfModule(vfModule);
157 assertNotEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId()));
158 sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
159 assertEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId()));
161 verify(sdncVfModuleResources, times(1)).queryVfModule(vfModule);
165 public void queryVfModuleForVolumeGroupBadResponseExceptionTest() throws Exception {
166 BadResponseException exception = new BadResponseException("Error received from SDNC");
167 doThrow(exception).when(sdncVfModuleResources).queryVfModule(vfModule);
169 expectedException.expect(BpmnError.class);
170 sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
172 verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(execution, 700, exception, TargetEntity.SDNC);
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);
180 expectedException.expect(BpmnError.class);
181 sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
183 verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(execution, 700, exception, TargetEntity.SO);
187 public void queryVfModuleForVolumeGroupNoSelfLinkExceptionTest() throws Exception {
188 expectedException.expect(BpmnError.class);
190 vfModule.setSelflink("");
192 sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
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);
202 verify(sdncVfModuleResources, times(0)).queryVfModule(any(VfModule.class));
206 public void queryVfModuleForVolumeGroupNonVfObjectExceptionTest() throws Exception {
207 expectedException.expect(BpmnError.class);
209 sdncQueryTasks.queryVfModuleForVolumeGroup(execution);