Springboot 2.0 upgrade
[so.git] / bpmn / MSOCommonBPMN / src / test / groovy / org / onap / so / bpmn / common / scripts / DeleteAAIVfModuleTest.groovy
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.common.scripts
22
23 import static org.mockito.Mockito.*
24
25 import javax.ws.rs.NotFoundException
26
27 import org.camunda.bpm.engine.ProcessEngineServices
28 import org.camunda.bpm.engine.RepositoryService
29 import org.camunda.bpm.engine.delegate.DelegateExecution
30 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
31 import org.camunda.bpm.engine.repository.ProcessDefinition
32 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake
33 import org.junit.Assert
34 import org.junit.Before
35 import org.junit.Test
36 import org.mockito.ArgumentCaptor
37 import org.mockito.Captor
38 import org.mockito.Mockito
39 import org.mockito.Spy
40 import org.onap.aai.domain.yang.GenericVnf
41 import org.onap.so.bpmn.core.WorkflowException
42 import org.onap.so.client.aai.entities.uri.AAIResourceUri
43
44 class DeleteAAIVfModuleTest extends  MsoGroovyTest{
45
46     def prefix = "DAAIVfMod_"
47
48     @Spy
49     DeleteAAIVfModule deleteAAIVfModule ;
50
51     @Captor
52     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
53
54     @Before
55     void init() throws IOException {
56         super.init("DeleteAAIVfModule")
57         when(deleteAAIVfModule.getAAIClient()).thenReturn(client)
58     }
59
60     @Test
61     void testQueryAAIForGenericVnf() {
62         ExecutionEntity mockExecution = setupMock()
63         when(mockExecution.getVariable("DAAIVfMod_vnfId")).thenReturn("vnfId1")
64         mockAAIGenericVnf("vnfId1")
65         deleteAAIVfModule.queryAAIForGenericVnf(mockExecution)
66         Mockito.verify(mockExecution).setVariable(prefix + "queryGenericVnfResponseCode", 200)
67     }
68
69     @Test
70     void testQueryAAIForGenericVnfNotFound() {
71         ExecutionEntity mockExecution = setupMock()
72         when(mockExecution.getVariable("DAAIVfMod_vnfId")).thenReturn("vnfId1")
73         mockAAIGenericVnfNotFound("vnfId1")
74         deleteAAIVfModule.queryAAIForGenericVnf(mockExecution)
75         Mockito.verify(mockExecution).setVariable(prefix + "queryGenericVnfResponseCode", 404)
76     }
77     @Test
78     void testQueryAAIForGenericVnfEndpointNull() {
79         DelegateExecution execution = new DelegateExecutionFake();
80         execution.setVariable("DAAIVfMod_vnfId", "vnfId1")
81         try {
82             deleteAAIVfModule.queryAAIForGenericVnf(execution)
83         } catch (Exception ex) {
84             println " Test End - Handle catch-throw BpmnError()! "
85         }
86
87         Assert.assertEquals(404, execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode"))
88         Assert.assertEquals("Vnf Not Found!", execution.getVariable("DAAIVfMod_queryGenericVnfResponse"))
89     }
90
91     @Test
92     void testDeleteGenericVnf() {
93         ExecutionEntity mockExecution = setupMock()
94         when(mockExecution.getVariable("DAAIVfMod_vnfId")).thenReturn("vnfId1")
95         doNothing().when(client).delete(isA(AAIResourceUri.class))
96         deleteAAIVfModule.deleteGenericVnf(mockExecution)
97         Mockito.verify(mockExecution).setVariable(prefix + "deleteGenericVnfResponseCode", 200)
98     }
99
100     @Test
101     void testParseForVfModule() {
102         ExecutionEntity mockExecution = setupMock()
103         when(mockExecution.getVariable("DAAIVfMod_vfModuleId")).thenReturn("testVfModuleIdGWSec")
104         Optional<GenericVnf> genericVnf = getAAIObjectFromJson(GenericVnf.class,"__files/aai/GenericVnfVfModule.json");
105         when(mockExecution.getVariable("DAAIVfMod_queryGenericVnfResponse")).thenReturn(genericVnf.get())
106         deleteAAIVfModule.parseForVfModule(mockExecution)
107         Mockito.verify(mockExecution).setVariable("DAAIVfMod_moduleExists", true)
108         Mockito.verify(mockExecution).setVariable("DAAIVfMod_isBaseModule", false)
109         Mockito.verify(mockExecution).setVariable("DAAIVfMod_isLastModule", false)
110     }
111
112     @Test
113     void testParseForVfModuleNotFound() {
114         ExecutionEntity mockExecution = setupMock()
115         when(mockExecution.getVariable("DAAIVfMod_vfModuleId")).thenReturn("notFound")
116         when(mockExecution.getVariable("DAAIVfMod_moduleExists")).thenReturn(false)
117         Optional<GenericVnf> genericVnf = getAAIObjectFromJson(GenericVnf.class,"__files/aai/GenericVnfVfModule.json");
118         when(mockExecution.getVariable("DAAIVfMod_queryGenericVnfResponse")).thenReturn(genericVnf.get())
119         deleteAAIVfModule.parseForVfModule(mockExecution)
120         Mockito.verify(mockExecution).setVariable("DAAIVfMod_moduleExists", false)
121         Mockito.verify(mockExecution).setVariable("DAAIVfMod_isBaseModule", false)
122         Mockito.verify(mockExecution).setVariable("DAAIVfMod_isLastModule", false)
123     }
124
125     @Test
126     void testParseForVfModuleBase() {
127         ExecutionEntity mockExecution = setupMock()
128         when(mockExecution.getVariable("DAAIVfMod_vfModuleId")).thenReturn("lukewarm")
129         Optional<GenericVnf> genericVnf = getAAIObjectFromJson(GenericVnf.class,"__files/aai/GenericVnfVfModule.json");
130         when(mockExecution.getVariable("DAAIVfMod_queryGenericVnfResponse")).thenReturn(genericVnf.get())
131         deleteAAIVfModule.parseForVfModule(mockExecution)
132         Mockito.verify(mockExecution).setVariable("DAAIVfMod_moduleExists", true)
133         Mockito.verify(mockExecution).setVariable("DAAIVfMod_isBaseModule", true)
134         Mockito.verify(mockExecution).setVariable("DAAIVfMod_isLastModule", false)
135     }
136
137     @Test
138     void testParseForVfModuleLast() {
139         ExecutionEntity mockExecution = setupMock()
140         when(mockExecution.getVariable("DAAIVfMod_vfModuleId")).thenReturn("testVfModuleIdGWSec")
141         Optional<GenericVnf> genericVnfOps = getAAIObjectFromJson(GenericVnf.class,"__files/aai/GenericVnfVfModule.json");
142         GenericVnf genericVnf =  genericVnfOps.get();
143         genericVnf.getVfModules().getVfModule().remove(0)
144         when(mockExecution.getVariable("DAAIVfMod_queryGenericVnfResponse")).thenReturn(genericVnf)
145         deleteAAIVfModule.parseForVfModule(mockExecution)
146         Mockito.verify(mockExecution).setVariable("DAAIVfMod_moduleExists", true)
147         Mockito.verify(mockExecution).setVariable("DAAIVfMod_isBaseModule", false)
148         Mockito.verify(mockExecution).setVariable("DAAIVfMod_isLastModule", true)
149     }
150
151     @Test
152     void testParseForVfModuleBaseLast() {
153         ExecutionEntity mockExecution = setupMock()
154         when(mockExecution.getVariable("DAAIVfMod_vfModuleId")).thenReturn("lukewarm")
155         Optional<GenericVnf> genericVnfOps = getAAIObjectFromJson(GenericVnf.class,"__files/aai/GenericVnfVfModule.json");
156         GenericVnf genericVnf =  genericVnfOps.get();
157         genericVnf.getVfModules().getVfModule().remove(1)
158         when(mockExecution.getVariable("DAAIVfMod_queryGenericVnfResponse")).thenReturn(genericVnf)
159         deleteAAIVfModule.parseForVfModule(mockExecution)
160         Mockito.verify(mockExecution).setVariable("DAAIVfMod_moduleExists", true)
161         Mockito.verify(mockExecution).setVariable("DAAIVfMod_isBaseModule", true)
162         Mockito.verify(mockExecution).setVariable("DAAIVfMod_isLastModule", true)
163     }
164
165
166
167     @Test
168     void testDeleteGenericVnfEndpointNull() {
169         ExecutionEntity mockExecution = setupMock()
170         when(mockExecution.getVariable("DAAIVfMod_vnfId")).thenReturn("vnfId1")
171         try {
172             doThrow(new NotFoundException("Vnf Not Found")).when(client).delete(isA(AAIResourceUri.class))
173             deleteAAIVfModule.deleteGenericVnf(mockExecution)
174         } catch (Exception ex) {
175             println " Test End - Handle catch-throw BpmnError()! "
176         }
177
178         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
179         WorkflowException workflowException = captor.getValue()
180         Assert.assertEquals(5000, workflowException.getErrorCode())
181         Assert.assertEquals("Internal Error - Occured during deleteGenericVnf", workflowException.getErrorMessage())
182     }
183
184     @Test
185     void testDeleteVfModule() {
186         ExecutionEntity mockExecution = setupMock()
187         when(mockExecution.getVariable("DAAIVfMod_vnfId")).thenReturn("vnfId1")
188         when(mockExecution.getVariable("DAAIVfMod_vfModuleId")).thenReturn("vfModuleId1")
189         doNothing().when(client).delete(isA(AAIResourceUri.class))
190         deleteAAIVfModule.deleteVfModule(mockExecution)
191         Mockito.verify(mockExecution).setVariable(prefix + "deleteVfModuleResponseCode", 200)
192     }
193
194     @Test
195     void testDeleteVfModuleEndpointNull() {
196         ExecutionEntity mockExecution = setupMock()
197         when(mockExecution.getVariable("DAAIVfMod_vnfId")).thenReturn("vnfId1")
198         when(mockExecution.getVariable("DAAIVfMod_vfModuleId")).thenReturn("vfModuleId1")
199         try {
200             doThrow(new NotFoundException("Vnf Not Found")).when(client).delete(isA(AAIResourceUri.class))
201             deleteAAIVfModule.deleteVfModule(mockExecution)
202         } catch (Exception ex) {
203             println " Test End - Handle catch-throw BpmnError()! "
204         }
205
206         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
207         WorkflowException workflowException = captor.getValue()
208         Assert.assertEquals(5000, workflowException.getErrorCode())
209         Assert.assertEquals("Internal Error - Occured during deleteVfModule", workflowException.getErrorMessage())
210     }
211
212     private static ExecutionEntity setupMock() {
213         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
214         when(mockProcessDefinition.getKey()).thenReturn("DeleteAAIVfModule")
215         RepositoryService mockRepositoryService = mock(RepositoryService.class)
216         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
217         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DeleteAAIVfModule")
218         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
219         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
220         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
221
222         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
223         // Initialize prerequisite variables
224         when(mockExecution.getId()).thenReturn("100")
225         when(mockExecution.getProcessDefinitionId()).thenReturn("DeleteAAIVfModule")
226         when(mockExecution.getProcessInstanceId()).thenReturn("DeleteAAIVfModule")
227         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
228         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
229
230         return mockExecution
231     }
232 }