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