025bea5325c9aeeaf1580cf09063bcb0cf4a336d
[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 com.github.tomakehurst.wiremock.junit.WireMockRule
24 import org.camunda.bpm.engine.ProcessEngineServices
25 import org.camunda.bpm.engine.RepositoryService
26 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
27 import org.camunda.bpm.engine.repository.ProcessDefinition
28 import org.junit.Assert
29 import org.junit.Before
30 import org.junit.Ignore
31 import org.junit.Rule
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 import org.mockito.ArgumentCaptor
35 import org.mockito.Captor
36 import org.mockito.Mock
37 import org.mockito.Mockito
38 import org.mockito.MockitoAnnotations
39 import org.mockito.runners.MockitoJUnitRunner
40 import org.onap.so.bpmn.core.WorkflowException
41 import org.onap.so.bpmn.mock.StubResponseAAI
42
43 import static com.github.tomakehurst.wiremock.client.WireMock.*
44 import static org.mockito.Mockito.*
45
46 @RunWith(MockitoJUnitRunner.class)
47 @Ignore
48 class DeleteAAIVfModuleTest {
49
50     def prefix = "DAAIVfMod_"
51
52     @Rule
53     public WireMockRule wireMockRule = new WireMockRule(28090)
54
55     @Captor
56     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
57
58     @Before
59     void init() throws IOException {
60         MockitoAnnotations.initMocks(this);
61     }
62
63     @Test
64     void testQueryAAIForGenericVnf() {
65         ExecutionEntity mockExecution = setupMock()
66         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
67         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
68         when(mockExecution.getVariable(prefix + "genericVnfEndpoint")).thenReturn("/aai/v8/network/generic-vnfs/generic-vnf/skask")
69         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
70         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
71         when(mockExecution.getVariable("aai.auth")).thenReturn("9B2278E8B8E95F256A560719055F4DF3")
72         when(mockExecution.getVariable("mso.msoKey")).thenReturn("aa3871669d893c7fb8abbcda31b88b4f")
73
74         StubResponseAAI.MockAAIVfModule()
75         DeleteAAIVfModule obj = new DeleteAAIVfModule()
76         obj.queryAAIForGenericVnf(mockExecution)
77
78         Mockito.verify(mockExecution).setVariable(prefix + "queryGenericVnfResponseCode", 200)
79     }
80
81     @Test
82     void testQueryAAIForGenericVnfEndpointNull() {
83         ExecutionEntity mockExecution = setupMock()
84         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
85         when(mockExecution.getVariable(prefix + "genericVnfEndpoint")).thenReturn("/aai/v8/network/generic-vnfs/generic-vnf/skask")
86         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("supercool")
87         when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
88         when(mockExecution.getVariable("aai.auth")).thenReturn("9B2278E8B8E95F256A560719055F4DF3")
89         when(mockExecution.getVariable("mso.msoKey")).thenReturn("aa3871669d893c7fb8abbcda31b88b4f")
90
91         StubResponseAAI.MockAAIVfModule()
92         try {
93             DeleteAAIVfModule obj = new DeleteAAIVfModule()
94             obj.queryAAIForGenericVnf(mockExecution)
95         } catch (Exception ex) {
96             println " Test End - Handle catch-throw BpmnError()! "
97         }
98
99         Mockito.verify(mockExecution, times(2)).setVariable(captor.capture(), captor.capture())
100         WorkflowException workflowException = captor.getValue()
101         Assert.assertEquals(5000, workflowException.getErrorCode())
102         Assert.assertEquals("Internal Error - Occured during queryAAIForGenericVnf", workflowException.getErrorMessage())
103     }
104
105     @Test
106     void testDeleteGenericVnf() {
107         ExecutionEntity mockExecution = setupMock()
108         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
109         when(mockExecution.getVariable(prefix + "genericVnfEndpoint")).thenReturn("/aai/v9/cloud-infrastructure/volume-groups/volume-group/78987")
110         when(mockExecution.getVariable(prefix + "genVnfRsrcVer")).thenReturn("0000020")
111         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
112         when(mockExecution.getVariable("aai.auth")).thenReturn("9B2278E8B8E95F256A560719055F4DF3")
113         when(mockExecution.getVariable("mso.msoKey")).thenReturn("aa3871669d893c7fb8abbcda31b88b4f")
114
115         stubFor(delete(urlMatching("/aai/v[0-9]+/cloud-infrastructure/volume-groups/volume-group/78987/[?]resource-version=0000020"))
116                 .willReturn(aResponse()
117                 .withStatus(200)
118                 .withHeader("Content-Type", "text/xml")
119                 .withBodyFile("")))
120
121         StubResponseAAI.MockAAIVfModule()
122         DeleteAAIVfModule obj = new DeleteAAIVfModule()
123         obj.deleteGenericVnf(mockExecution)
124
125         Mockito.verify(mockExecution).setVariable(prefix + "deleteGenericVnfResponseCode", 200)
126     }
127
128     @Test
129     void testDeleteGenericVnfEndpointNull() {
130         ExecutionEntity mockExecution = setupMock()
131         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
132         when(mockExecution.getVariable(prefix + "genericVnfEndpoint")).thenReturn("/aai/v9/cloud-infrastructure/volume-groups/volume-group/78987")
133         when(mockExecution.getVariable(prefix + "genVnfRsrcVer")).thenReturn("0000020")
134         when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
135         when(mockExecution.getVariable("aai.auth")).thenReturn("9B2278E8B8E95F256A560719055F4DF3")
136         when(mockExecution.getVariable("mso.msoKey")).thenReturn("aa3871669d893c7fb8abbcda31b88b4f")
137
138         StubResponseAAI.MockAAIVfModule()
139         try {
140             DeleteAAIVfModule obj = new DeleteAAIVfModule()
141             obj.deleteGenericVnf(mockExecution)
142         } catch (Exception ex) {
143             println " Test End - Handle catch-throw BpmnError()! "
144         }
145
146         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
147         WorkflowException workflowException = captor.getValue()
148         Assert.assertEquals(5000, workflowException.getErrorCode())
149         Assert.assertEquals("Internal Error - Occured during deleteGenericVnf", workflowException.getErrorMessage())
150     }
151
152     @Test
153     void testDeleteVfModule() {
154         ExecutionEntity mockExecution = setupMock()
155         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
156         when(mockExecution.getVariable(prefix + "vfModuleEndpoint")).thenReturn("/aai/v9/cloud-infrastructure/volume-groups/volume-group/78987")
157         when(mockExecution.getVariable(prefix + "vfModRsrcVer")).thenReturn("0000020")
158         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
159         when(mockExecution.getVariable("aai.auth")).thenReturn("9B2278E8B8E95F256A560719055F4DF3")
160         when(mockExecution.getVariable("mso.msoKey")).thenReturn("aa3871669d893c7fb8abbcda31b88b4f")
161
162         stubFor(delete(urlMatching("/aai/v[0-9]+/cloud-infrastructure/volume-groups/volume-group/78987/[?]resource-version=0000020"))
163                 .willReturn(aResponse()
164                 .withStatus(200)
165                 .withHeader("Content-Type", "text/xml")
166                 .withBodyFile("")))
167
168         DeleteAAIVfModule obj = new DeleteAAIVfModule()
169         obj.deleteVfModule(mockExecution)
170
171         Mockito.verify(mockExecution).setVariable(prefix + "deleteVfModuleResponseCode", 200)
172     }
173
174     @Test
175     void testDeleteVfModuleEndpointNull() {
176         ExecutionEntity mockExecution = setupMock()
177         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
178         when(mockExecution.getVariable(prefix + "vfModuleEndpoint")).thenReturn("/aai/v9/cloud-infrastructure/volume-groups/volume-group/78987")
179         when(mockExecution.getVariable(prefix + "vfModRsrcVer")).thenReturn("0000020")
180         when(mockExecution.getVariable("aai.endpoint")).thenReturn(null)
181         when(mockExecution.getVariable("aai.auth")).thenReturn("9B2278E8B8E95F256A560719055F4DF3")
182         when(mockExecution.getVariable("mso.msoKey")).thenReturn("aa3871669d893c7fb8abbcda31b88b4f")
183
184         StubResponseAAI.MockAAIVfModule()
185         try {
186             DeleteAAIVfModule obj = new DeleteAAIVfModule()
187             obj.deleteVfModule(mockExecution)
188         } catch (Exception ex) {
189             println " Test End - Handle catch-throw BpmnError()! "
190         }
191
192         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
193         WorkflowException workflowException = captor.getValue()
194         Assert.assertEquals(5000, workflowException.getErrorCode())
195         Assert.assertEquals("Internal Error - Occured during deleteVfModule", workflowException.getErrorMessage())
196     }
197
198     private static ExecutionEntity setupMock() {
199         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
200         when(mockProcessDefinition.getKey()).thenReturn("DeleteAAIVfModule")
201         RepositoryService mockRepositoryService = mock(RepositoryService.class)
202         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
203         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DeleteAAIVfModule")
204         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
205         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
206         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
207
208         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
209         // Initialize prerequisite variables
210         when(mockExecution.getId()).thenReturn("100")
211         when(mockExecution.getProcessDefinitionId()).thenReturn("DeleteAAIVfModule")
212         when(mockExecution.getProcessInstanceId()).thenReturn("DeleteAAIVfModule")
213         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
214         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
215
216         return mockExecution
217     }
218 }