7776ab3fe6173f492886bcb262f2db8ce20c09ba
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoDeleteVfModuleTest.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.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.NetworkPolicies
41 import org.onap.aai.domain.yang.NetworkPolicy
42 import org.onap.aai.domain.yang.VfModule
43 import org.onap.aai.domain.yang.VfModules
44 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
45 import org.onap.so.bpmn.common.scripts.utils.XmlComparator
46 import org.onap.so.bpmn.core.WorkflowException
47 import org.onap.so.bpmn.mock.FileUtil
48 import org.onap.so.client.aai.AAIObjectPlurals
49 import org.onap.so.client.aai.AAIObjectType
50 import org.onap.so.client.aai.entities.uri.AAIResourceUri
51 import org.onap.so.client.aai.entities.uri.AAIUriFactory
52
53 import javax.ws.rs.NotFoundException
54
55 import static com.github.tomakehurst.wiremock.client.WireMock.*
56 import static org.mockito.Mockito.*
57
58 @RunWith(MockitoJUnitRunner.class)
59 class DoDeleteVfModuleTest extends MsoGroovyTest{
60
61     @Spy
62     DoDeleteVfModule doDeleteVfModule
63
64     @Rule
65     public WireMockRule wireMockRule = new WireMockRule(28090);
66
67     @Captor
68     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
69
70     @Before
71     public void init() throws IOException {
72         super.init("DoDeleteVfModule")
73         MockitoAnnotations.initMocks(this);
74         when(doDeleteVfModule.getAAIClient()).thenReturn(client)
75     }
76
77     @Test
78     public void testPrepSDNCAdapterRequest() {
79         ExecutionEntity mockExecution = setupMock()
80         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
81         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
82         when(mockExecution.getVariable("requestId")).thenReturn("12345")
83         when(mockExecution.getVariable("source")).thenReturn("VID")
84         when(mockExecution.getVariable("serviceId")).thenReturn("12345")
85         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
86         when(mockExecution.getVariable("tenantId")).thenReturn("19123c2924c648eb8e42a3c1f14b7682")
87         when(mockExecution.getVariable("vfModuleId")).thenReturn("12345")
88         when(mockExecution.getVariable("DoDVfMod_serviceInstanceIdToSdnc")).thenReturn("123456789")
89         when(mockExecution.getVariable("vfModuleName")).thenReturn("vfModuleName_test")
90         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapterCallback")
91
92         DoDeleteVfModule obj = new DoDeleteVfModule()
93         obj.prepSDNCAdapterRequest(mockExecution, 'release')
94
95         String expectedValue = FileUtil.readResourceFile("__files/DoDeleteVfModule/sdncAdapterWorkflowRequest.xml")
96         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
97         XmlComparator.assertXMLEquals(expectedValue, captor.getValue())
98     }
99    
100
101     @Test
102     void testDeleteNetworkPoliciesFromAAI() {
103         List fqdnList = new ArrayList()
104         fqdnList.add("test")
105         when(mockExecution.getVariable("DoDVfMod_contrailNetworkPolicyFqdnList")).thenReturn(fqdnList)
106         NetworkPolicies networkPolicies = new NetworkPolicies()
107         NetworkPolicy networkPolicy = new NetworkPolicy()
108         networkPolicy.setNetworkPolicyId("NP1")
109         networkPolicies.getNetworkPolicy().add(networkPolicy)
110         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
111         uri.queryParam("network-policy-fqdn", "test")
112         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
113         doDeleteVfModule.deleteNetworkPoliciesFromAAI(mockExecution)
114         Mockito.verify(mockExecution).setVariable("DCVFM_aaiQueryNetworkPolicyByFqdnReturnCode", 200)
115     }
116
117     @Test
118     void testDeleteNetworkPoliciesFromAAIError() {
119         List fqdnList = new ArrayList()
120         fqdnList.add("test")
121         when(mockExecution.getVariable("DoDVfMod_contrailNetworkPolicyFqdnList")).thenReturn(fqdnList)
122         NetworkPolicies networkPolicies = new NetworkPolicies()
123         NetworkPolicy networkPolicy = new NetworkPolicy()
124         networkPolicy.setNetworkPolicyId("NP1")
125         networkPolicies.getNetworkPolicy().add(networkPolicy)
126         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
127         uri.queryParam("network-policy-fqdn", "test")
128         when(client.get(NetworkPolicies.class, uri)).thenReturn(Optional.of(networkPolicies))
129         AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIObjectType.NETWORK_POLICY, "NP1")
130         doThrow(new NotFoundException(("Not Found !"))).when(client).delete(delUri)
131         doDeleteVfModule.deleteNetworkPoliciesFromAAI(mockExecution)
132         Mockito.verify(client).delete(delUri)
133     }
134
135     @Test
136     void testQueryAAIVfModuleForStatus() {
137         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
138         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
139         when(mockExecution.getVariable("vfModuleId")).thenReturn("module-0")
140         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE,"12345","module-0")
141         VfModule vfModule = new VfModule()
142         vfModule.setOrchestrationStatus("Created")
143         when(client.get(VfModule.class, uri)).thenReturn(Optional.of(vfModule))
144         doDeleteVfModule.queryAAIVfModuleForStatus(mockExecution)
145         Mockito.verify(mockExecution).setVariable("DoDVfMod_queryAAIVfModuleForStatusResponseCode", 200)
146     }
147
148   
149
150     private ExecutionEntity setupMock() {
151
152         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
153         when(mockProcessDefinition.getKey()).thenReturn("DoDeleteVfModule")
154         RepositoryService mockRepositoryService = mock(RepositoryService.class)
155         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
156         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoDeleteVfModule")
157         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
158         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
159         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
160
161         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
162         // Initialize prerequisite variables
163
164         when(mockExecution.getId()).thenReturn("100")
165         when(mockExecution.getProcessDefinitionId()).thenReturn("DoDeleteVfModule")
166         when(mockExecution.getProcessInstanceId()).thenReturn("DoDeleteVfModule")
167         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
168         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
169
170         return mockExecution
171
172     }
173
174     private static void mockData() {
175         stubFor(get(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy\\?network-policy-fqdn=.*"))
176                 .willReturn(aResponse()
177                 .withStatus(200)
178                 .withHeader("Content-Type", "text/xml")
179                 .withBodyFile("VfModularity/QueryNetworkPolicy_AAIResponse_Success.xml")))
180
181         stubFor(delete(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy/.*"))
182                 .willReturn(aResponse()
183                 .withStatus(200)))
184
185         stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345/vf-modules/vf-module[?]vf-module-name=module-0"))
186                 .willReturn(aResponse()
187                 .withStatus(200).withHeader("Content-Type", "text/xml")
188                 .withBodyFile("DoDeleteVfModule/getGenericVnfResponse.xml")))
189
190     }
191 }
192