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.common.scripts
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
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
43 import static com.github.tomakehurst.wiremock.client.WireMock.*
44 import static org.mockito.Mockito.*
46 @RunWith(MockitoJUnitRunner.class)
48 class DeleteAAIVfModuleTest {
50 def prefix = "DAAIVfMod_"
53 public WireMockRule wireMockRule = new WireMockRule(28090)
56 static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
59 void init() throws IOException {
60 MockitoAnnotations.initMocks(this);
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")
74 StubResponseAAI.MockAAIVfModule()
75 DeleteAAIVfModule obj = new DeleteAAIVfModule()
76 obj.queryAAIForGenericVnf(mockExecution)
78 Mockito.verify(mockExecution).setVariable(prefix + "queryGenericVnfResponseCode", 200)
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")
91 StubResponseAAI.MockAAIVfModule()
93 DeleteAAIVfModule obj = new DeleteAAIVfModule()
94 obj.queryAAIForGenericVnf(mockExecution)
95 } catch (Exception ex) {
96 println " Test End - Handle catch-throw BpmnError()! "
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())
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")
115 stubFor(delete(urlMatching("/aai/v[0-9]+/cloud-infrastructure/volume-groups/volume-group/78987/[?]resource-version=0000020"))
116 .willReturn(aResponse()
118 .withHeader("Content-Type", "text/xml")
121 StubResponseAAI.MockAAIVfModule()
122 DeleteAAIVfModule obj = new DeleteAAIVfModule()
123 obj.deleteGenericVnf(mockExecution)
125 Mockito.verify(mockExecution).setVariable(prefix + "deleteGenericVnfResponseCode", 200)
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")
138 StubResponseAAI.MockAAIVfModule()
140 DeleteAAIVfModule obj = new DeleteAAIVfModule()
141 obj.deleteGenericVnf(mockExecution)
142 } catch (Exception ex) {
143 println " Test End - Handle catch-throw BpmnError()! "
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())
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")
162 stubFor(delete(urlMatching("/aai/v[0-9]+/cloud-infrastructure/volume-groups/volume-group/78987/[?]resource-version=0000020"))
163 .willReturn(aResponse()
165 .withHeader("Content-Type", "text/xml")
168 DeleteAAIVfModule obj = new DeleteAAIVfModule()
169 obj.deleteVfModule(mockExecution)
171 Mockito.verify(mockExecution).setVariable(prefix + "deleteVfModuleResponseCode", 200)
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")
184 StubResponseAAI.MockAAIVfModule()
186 DeleteAAIVfModule obj = new DeleteAAIVfModule()
187 obj.deleteVfModule(mockExecution)
188 } catch (Exception ex) {
189 println " Test End - Handle catch-throw BpmnError()! "
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())
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)
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)