a8d07b696c74523c26de2116e064e32756695202
[so.git] /
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.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.Mockito
37 import org.mockito.MockitoAnnotations
38 import org.mockito.Spy
39 import org.mockito.runners.MockitoJUnitRunner
40 import org.onap.aai.domain.yang.GenericVnf
41 import org.onap.aai.domain.yang.NetworkPolicies
42 import org.onap.aai.domain.yang.NetworkPolicy
43 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
44 import org.onap.so.bpmn.core.WorkflowException
45 import org.onap.so.client.aai.AAIObjectPlurals
46 import org.onap.so.client.aai.AAIObjectType
47 import org.onap.so.client.aai.entities.uri.AAIPluralResourceUri
48 import org.onap.so.client.aai.entities.uri.AAIResourceUri
49 import org.onap.so.client.aai.entities.uri.AAIUriFactory
50 import org.onap.so.client.graphinventory.entities.uri.Depth
51
52 import static com.github.tomakehurst.wiremock.client.WireMock.*
53 import static org.mockito.Mockito.*
54
55 @RunWith(MockitoJUnitRunner.class)
56 class DoDeleteVfModuleFromVnfTest extends MsoGroovyTest {
57
58     @Captor
59     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
60
61     @Spy
62     DoDeleteVfModuleFromVnf deleteVfModuleFromVnf
63
64     @Before
65     public void init() throws IOException {
66         super.init("DoDeleteVfModuleFromVnf")
67         MockitoAnnotations.initMocks(this)
68         when(deleteVfModuleFromVnf.getAAIClient()).thenReturn(client)
69     }
70
71     @Test
72     public void testPreProcessRequest() {
73         ExecutionEntity mockExecution = setupMock()
74         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
75         when(mockExecution.getVariable("msoRequestId")).thenReturn("12345")
76         when(mockExecution.getVariable("source")).thenReturn("VID")
77         when(mockExecution.getVariable("serviceInstanceId")).thenReturn("12345")
78         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
79         when(mockExecution.getVariable("tenantId")).thenReturn("19123c2924c648eb8e42a3c1f14b7682")
80         when(mockExecution.getVariable("vfModuleId")).thenReturn("12345")
81         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("12345")
82         when(mockExecution.getVariable("sdncVersion")).thenReturn("8")
83         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapterCallback")
84
85         DoDeleteVfModuleFromVnf obj = new DoDeleteVfModuleFromVnf()
86         obj.preProcessRequest(mockExecution)
87
88         Mockito.verify(mockExecution).setVariable("prefix", "DDVFMV_")
89         Mockito.verify(mockExecution).setVariable("DDVFMV_contrailNetworkPolicyFqdnList", null)
90         Mockito.verify(mockExecution).setVariable("mso-request-id", "12345")
91         Mockito.verify(mockExecution).setVariable("requestId", "12345")
92         Mockito.verify(mockExecution).setVariable("cloudSiteId", "12345")
93         Mockito.verify(mockExecution).setVariable("source", "VID")
94         Mockito.verify(mockExecution).setVariable("isVidRequest", "true")
95         Mockito.verify(mockExecution).setVariable("srvInstId", "")
96         Mockito.verify(mockExecution).setVariable("DDVFMV_serviceInstanceIdToSdnc", "12345")
97         Mockito.verify(mockExecution).setVariable("DDVFMV_sdncVersion", "8")
98         Mockito.verify(mockExecution).setVariable("sdncCallbackUrl", "http://localhost:8090/SDNCAdapterCallback")
99     }
100
101
102
103     @Test
104     void testDeleteNetworkPoliciesFromAAI() {
105
106         List fqdnList = new ArrayList()
107         fqdnList.add("test")
108         when(mockExecution.getVariable("DDVFMV_contrailNetworkPolicyFqdnList")).thenReturn(fqdnList)
109         AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
110         uri.queryParam("network-policy-fqdn", "test")
111         NetworkPolicies networkPolicies = new NetworkPolicies();
112         NetworkPolicy networkPolicy = new NetworkPolicy();
113         networkPolicy.setNetworkPolicyId("NP1")
114         networkPolicies.getNetworkPolicy().add(networkPolicy)
115         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
116
117         AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIObjectType.NETWORK_POLICY, networkPolicy.getNetworkPolicyId())
118         doNothing().when(client).delete(delUri)
119         deleteVfModuleFromVnf.deleteNetworkPoliciesFromAAI(mockExecution)
120
121         Mockito.verify(mockExecution).setVariable("prefix", 'DDVFMV_')
122         Mockito.verify(mockExecution).setVariable("DCVFM_aaiQueryNetworkPolicyByFqdnReturnCode", 200)
123     }
124
125     @Test
126     void testDeleteNetworkPoliciesFromAAINotFound() {
127
128         List fqdnList = new ArrayList()
129         fqdnList.add("test")
130         when(mockExecution.getVariable("DDVFMV_contrailNetworkPolicyFqdnList")).thenReturn(fqdnList)
131         AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
132         uri.queryParam("network-policy-fqdn", "test")
133         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.empty())
134         deleteVfModuleFromVnf.deleteNetworkPoliciesFromAAI(mockExecution)
135
136         Mockito.verify(mockExecution).setVariable("prefix", 'DDVFMV_')
137         Mockito.verify(mockExecution).setVariable("DCVFM_aaiQueryNetworkPolicyByFqdnReturnCode", 404)
138     }
139
140
141     @Test
142     void testQueryAAIForVfModule() {
143         ExecutionEntity mockExecution = setupMock()
144         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
145         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "12345").depth(Depth.ONE)
146         GenericVnf genericVnf = new GenericVnf()
147         genericVnf.setVnfId("test1")
148         when(client.get(GenericVnf.class, uri)).thenReturn(Optional.of(genericVnf))
149         deleteVfModuleFromVnf.queryAAIForVfModule(mockExecution)
150
151         Mockito.verify(mockExecution, atLeastOnce()).setVariable("DDVMFV_getVnfResponseCode", 200)
152         Mockito.verify(mockExecution, atLeastOnce()).setVariable("DDVMFV_getVnfResponse", genericVnf)
153     }
154
155     @Test
156     void testQueryAAIForVfModuleNotFound() {
157         ExecutionEntity mockExecution = setupMock()
158         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
159         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "12345").depth(Depth.ONE)
160         when(client.get(GenericVnf.class, uri)).thenReturn(Optional.empty())
161         deleteVfModuleFromVnf.queryAAIForVfModule(mockExecution)
162         Mockito.verify(mockExecution, atLeastOnce()).setVariable("DDVMFV_getVnfResponseCode", 404)
163     }
164
165
166
167     private ExecutionEntity setupMock() {
168
169         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
170         when(mockProcessDefinition.getKey()).thenReturn("DoDeleteVfModuleFromVnf")
171         RepositoryService mockRepositoryService = mock(RepositoryService.class)
172         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
173         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoDeleteVfModuleFromVnf")
174         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
175         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
176         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
177
178         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
179         // Initialize prerequisite variables
180
181         when(mockExecution.getId()).thenReturn("100")
182         when(mockExecution.getProcessDefinitionId()).thenReturn("DoDeleteVfModuleFromVnf")
183         when(mockExecution.getProcessInstanceId()).thenReturn("DoDeleteVfModuleFromVnf")
184         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
185         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
186
187         return mockExecution
188
189     }
190
191     private static void mockData() {
192         stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345[?]depth=1"))
193                 .willReturn(aResponse()
194                 .withStatus(200).withHeader("Content-Type", "text/xml")
195                 .withBodyFile("VfModularity/GenerateVfModuleName_AAIResponse_Success.xml")))
196
197         stubFor(get(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy\\?network-policy-fqdn=.*"))
198                 .willReturn(aResponse()
199                 .withStatus(200)
200                 .withHeader("Content-Type", "text/xml")
201                 .withBodyFile("VfModularity/QueryNetworkPolicy_AAIResponse_Success.xml")))
202
203         stubFor(delete(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy/.*"))
204                 .willReturn(aResponse()
205                 .withStatus(200)));
206
207     }
208 }