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