3390b1a6f0c86a0544464db920fb0fe54d52dce8
[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.Rule
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.runners.MockitoJUnitRunner
38 import org.onap.so.bpmn.common.scripts.utils.XmlComparator
39 import org.onap.so.bpmn.core.WorkflowException
40 import org.onap.so.bpmn.mock.FileUtil
41
42 import static com.github.tomakehurst.wiremock.client.WireMock.*
43 import static org.mockito.Mockito.*
44
45 @RunWith(MockitoJUnitRunner.class)
46 class DoDeleteVfModuleTest {
47
48     @Rule
49     public WireMockRule wireMockRule = new WireMockRule(28090);
50
51     @Captor
52     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
53
54     @Before
55     public void init() throws IOException {
56         MockitoAnnotations.initMocks(this);
57     }
58
59     @Test
60     public void testPrepSDNCAdapterRequest() {
61         ExecutionEntity mockExecution = setupMock()
62         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
63         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
64         when(mockExecution.getVariable("requestId")).thenReturn("12345")
65         when(mockExecution.getVariable("source")).thenReturn("VID")
66         when(mockExecution.getVariable("serviceId")).thenReturn("12345")
67         when(mockExecution.getVariable("vnfId")).thenReturn("12345")
68         when(mockExecution.getVariable("tenantId")).thenReturn("19123c2924c648eb8e42a3c1f14b7682")
69         when(mockExecution.getVariable("vfModuleId")).thenReturn("12345")
70         when(mockExecution.getVariable("DoDVfMod_serviceInstanceIdToSdnc")).thenReturn("123456789")
71         when(mockExecution.getVariable("vfModuleName")).thenReturn("vfModuleName_test")
72         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:8090/SDNCAdapterCallback")
73
74         DoDeleteVfModule obj = new DoDeleteVfModule()
75         obj.prepSDNCAdapterRequest(mockExecution, 'release')
76
77         String expectedValue = FileUtil.readResourceFile("__files/DoDeleteVfModule/sdncAdapterWorkflowRequest.xml")
78         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
79         XmlComparator.assertXMLEquals(expectedValue, captor.getValue())
80     }
81    
82
83     @Test
84     void testDeleteNetworkPoliciesFromAAI() {
85         ExecutionEntity mockExecution = setupMock()
86         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
87         when(mockExecution.getVariable("mso.workflow.DoDeleteVfModule.aai.network-policy.uri")).thenReturn("/aai/v8/network/network-policies/network-policy")
88         when(mockExecution.getVariable("mso.workflow.custom.DoDeleteVfModule.aai.version")).thenReturn("8")
89         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
90         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
91         List fqdnList = new ArrayList()
92         fqdnList.add("test")
93         when(mockExecution.getVariable("DoDVfMod_contrailNetworkPolicyFqdnList")).thenReturn(fqdnList)
94         mockData()
95         DoDeleteVfModule obj = new DoDeleteVfModule()
96         obj.deleteNetworkPoliciesFromAAI(mockExecution)
97
98         Mockito.verify(mockExecution).setVariable("DCVFM_aaiQueryNetworkPolicyByFqdnReturnCode", 200)
99     }
100
101
102     @Test
103     void testQueryAAIVfModuleForStatus() {
104         ExecutionEntity mockExecution = setupMock()
105         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
106         when(mockExecution.getVariable("DCVFM_vnfId")).thenReturn("12345")
107         when(mockExecution.getVariable("DCVFM_vfModuleName")).thenReturn("module-0")
108         when(mockExecution.getVariable("mso.workflow.DoDeleteVfModule.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
109         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
110         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
111
112         mockData()
113         DoCreateVfModule obj = new DoCreateVfModule()
114         obj.queryAAIVfModuleForStatus(mockExecution)
115
116         Mockito.verify(mockExecution).setVariable("DCVFM_queryAAIVfModuleForStatusResponseCode", 200)
117     }
118
119   
120
121     private ExecutionEntity setupMock() {
122
123         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
124         when(mockProcessDefinition.getKey()).thenReturn("DoDeleteVfModule")
125         RepositoryService mockRepositoryService = mock(RepositoryService.class)
126         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
127         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoDeleteVfModule")
128         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
129         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
130         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
131
132         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
133         // Initialize prerequisite variables
134
135         when(mockExecution.getId()).thenReturn("100")
136         when(mockExecution.getProcessDefinitionId()).thenReturn("DoDeleteVfModule")
137         when(mockExecution.getProcessInstanceId()).thenReturn("DoDeleteVfModule")
138         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
139         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
140
141         return mockExecution
142
143     }
144
145     private static void mockData() {
146         stubFor(get(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy\\?network-policy-fqdn=.*"))
147                 .willReturn(aResponse()
148                 .withStatus(200)
149                 .withHeader("Content-Type", "text/xml")
150                 .withBodyFile("VfModularity/QueryNetworkPolicy_AAIResponse_Success.xml")))
151
152         stubFor(delete(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy/.*"))
153                 .willReturn(aResponse()
154                 .withStatus(200)))
155
156         stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345/vf-modules/vf-module[?]vf-module-name=module-0"))
157                 .willReturn(aResponse()
158                 .withStatus(200).withHeader("Content-Type", "text/xml")
159                 .withBodyFile("DoDeleteVfModule/getGenericVnfResponse.xml")))
160
161     }
162 }
163