28551baa7a5cc95de29b8f42b30d98f76ba47c9c
[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
33 import org.camunda.bpm.engine.delegate.BpmnError;
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.mockito.ArgumentMatchers;
39 import org.mockito.InjectMocks;
40 import org.onap.so.bpmn.BaseTaskTest;
41 import org.onap.so.bpmn.common.BuildingBlockExecution;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
45 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
46 import org.onap.so.client.exception.BBObjectNotFoundException;
47
48 public class SDNCQueryTasksTest extends BaseTaskTest{
49         @InjectMocks
50         private SDNCQueryTasks sdncQueryTasks = new SDNCQueryTasks();
51         
52         @Rule
53         public ExpectedException expectedException = ExpectedException.none();
54                 
55         private ServiceInstance serviceInstance;
56         private GenericVnf genericVnf;
57         private VfModule vfModule;
58         
59         @Before
60         public void before() throws BBObjectNotFoundException {
61                 serviceInstance = setServiceInstance();
62                 genericVnf = setGenericVnf();
63                 vfModule = setVfModule();
64                 
65                 doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
66                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID))).thenReturn(serviceInstance);
67                 
68                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(genericVnf);
69
70                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
71
72         }
73         
74         @Test
75         public void queryVfModuleTest() throws Exception {
76                 String sdncQueryResponse = "response";
77                 vfModule.setSelflink("vfModuleSelfLink");
78                 
79                 doReturn(sdncQueryResponse).when(sdncVfModuleResources).queryVfModule(vfModule);
80                 
81                 assertNotEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId()));
82                 sdncQueryTasks.queryVfModule(execution);
83                 assertEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId()));
84
85                 verify(sdncVfModuleResources, times(1)).queryVfModule(vfModule);
86         }
87
88         @Test
89         public void queryVnfTest() throws Exception {
90                 String sdncQueryResponse = "response";
91                 
92                 doReturn(sdncQueryResponse).when(sdncVnfResources).queryVnf(genericVnf);
93
94                 assertNotEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + genericVnf.getVnfId()));
95                 sdncQueryTasks.queryVnf(execution);
96                 assertEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + genericVnf.getVnfId()));
97
98                 verify(sdncVnfResources, times(1)).queryVnf(genericVnf);
99         }       
100         
101         @Test
102         public void queryVfModuleForVolumeGroupTest() throws Exception {
103                 String sdncQueryResponse = "response";
104                 vfModule.setSelflink("vfModuleSelfLink");
105                 
106                 doReturn(sdncQueryResponse).when(sdncVfModuleResources).queryVfModule(vfModule);
107                 
108                 assertNotEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId()));
109                 sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
110                 assertEquals(sdncQueryResponse, execution.getVariable("SDNCQueryResponse_" + vfModule.getVfModuleId()));
111
112                 verify(sdncVfModuleResources, times(1)).queryVfModule(vfModule);
113         }
114         
115         @Test
116         public void queryVfModuleForVolumeGroupNoSelfLinkExceptionTest() throws Exception {
117                 expectedException.expect(BpmnError.class);
118                 
119                 vfModule.setSelflink("");
120                 
121                 sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
122         }
123         
124         @Test
125         public void queryVfModuleForVolumeGroupVfObjectExceptionTest() throws Exception {
126                 expectedException.expect(BpmnError.class);
127                 doThrow(RuntimeException.class).when(extractPojosForBB).extractByKey(any(),ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID));      
128                 sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
129                 
130                 verify(sdncVfModuleResources, times(0)).queryVfModule(any(VfModule.class));
131         }
132         
133         @Test
134         public void queryVfModuleForVolumeGroupNonVfObjectExceptionTest() throws Exception {
135                 expectedException.expect(BpmnError.class);
136                 
137                 sdncQueryTasks.queryVfModuleForVolumeGroup(execution);
138         }
139 }