c5c6187648c697ce608010b0aa030a4684d5c54f
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateVfModuleRollbackTest.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.RollbackData
40 import org.onap.so.bpmn.core.WorkflowException
41 import org.onap.so.bpmn.mock.FileUtil
42
43 import static com.github.tomakehurst.wiremock.client.WireMock.*
44 import static org.mockito.Mockito.*
45
46 @RunWith(MockitoJUnitRunner.class)
47 class DoCreateVfModuleRollbackTest {
48
49     def prefix = "DCVFMR_"
50
51     @Rule
52     public WireMockRule wireMockRule = new WireMockRule(28090)
53
54     @Captor
55     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
56
57     @Before
58     void init() throws IOException {
59         MockitoAnnotations.initMocks(this);
60     }
61
62     @Test
63     void testPrepSDNCAdapterRequest() {
64         ExecutionEntity mockExecution = setupMock()
65         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
66         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
67         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("12345")
68         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
69         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:28080/mso/SDNCAdapterCallbackService")
70         when(mockExecution.getVariable(prefix + "source")).thenReturn("VID")
71         when(mockExecution.getVariable(prefix + "tenantId")).thenReturn("fba1bd1e195a404cacb9ce17a9b2b421")
72         when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
73         when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
74         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
75         when(mockExecution.getVariable(prefix + "vfModuleModelName")).thenReturn("PCRF::module-0-2")
76         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
77         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
78         when(mockExecution.getVariable(prefix + "vfModuleName")).thenReturn("PCRF::module-0-2")
79         when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
80         when(mockExecution.getVariable(prefix + "cloudSiteId")).thenReturn("RDM2WAGPLCP")
81
82         when(mockExecution.getVariable(prefix + "rollbackSDNCRequestActivate")).thenReturn("true")
83
84
85         DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
86         obj.prepSDNCAdapterRequest(mockExecution)
87
88         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
89         String expectedValue = FileUtil.readResourceFile("__files/DoCreateVfModuleRollback/sdncAdapterWorkflowRequest.xml")
90         XmlComparator.assertXMLEquals(expectedValue, captor.getValue())
91     }
92
93     
94
95     @Test
96     void testBuildSDNCRequest() {
97         ExecutionEntity mockExecution = setupMock()
98         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
99         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
100         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("12345")
101         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
102         when(mockExecution.getVariable("mso.workflow.sdncadapter.callback")).thenReturn("http://localhost:28080/mso/SDNCAdapterCallbackService")
103         when(mockExecution.getVariable(prefix + "source")).thenReturn("VID")
104         when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
105         when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
106         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
107         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
108         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
109         when(mockExecution.getVariable(prefix + "vfModuleName")).thenReturn("PCRF::module-0-2")
110         when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
111
112         when(mockExecution.getVariable(prefix + "rollbackSDNCRequestActivate")).thenReturn("true")
113
114
115         DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
116         String sdncRequest = obj.buildSDNCRequest(mockExecution, "svcInstId_test", "deactivate")
117         String expectedValue = FileUtil.readResourceFile("__files/DoCreateVfModuleRollback/deactivateSDNCRequest.xml")
118         XmlComparator.assertXMLEquals(expectedValue, sdncRequest)
119     }
120
121    
122
123     @Test
124     void testPrepVNFAdapterRequest() {
125         ExecutionEntity mockExecution = setupMock()
126         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
127         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
128         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("12345")
129         when(mockExecution.getVariable("testReqId")).thenReturn("testReqId")
130         when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
131         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
132         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
133         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
134         when(mockExecution.getVariable(prefix + "mso-request-id")).thenReturn("12345")
135         when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn('http://localhost:18080/mso/WorkflowMessage/')
136
137
138         DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
139         String sdncRequest = obj.prepVNFAdapterRequest(mockExecution)
140
141         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
142         String expectedValue = FileUtil.readResourceFile("__files/DoCreateVfModuleRollback/vnfAdapterRestV1Request.xml")
143         XmlComparator.assertXMLEquals(expectedValue, captor.getValue(), "messageId", "notificationUrl")
144     }
145
146     @Test
147     void testDeleteNetworkPoliciesFromAAI() {
148         ExecutionEntity mockExecution = setupMock()
149         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
150         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
151         when(mockExecution.getVariable("DCVFM_cloudSiteId")).thenReturn("12345")
152         when(mockExecution.getVariable("mso.workflow.DoCreateVfModuleRollback.aai.network-policy.uri")).thenReturn("/aai/v8/network/network-policies/network-policy")
153         when(mockExecution.getVariable("mso.workflow.custom.DoCreateVfModuleRollback.aai.version")).thenReturn("8")
154         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
155         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
156         when(mockExecution.getVariable("rollbackData")).thenReturn(new RollbackData())
157         List fqdnList = new ArrayList()
158         fqdnList.add("test")
159         when(mockExecution.getVariable(prefix + "createdNetworkPolicyFqdnList")).thenReturn(fqdnList)
160         mockData()
161         DoCreateVfModuleRollback obj = new DoCreateVfModuleRollback()
162         obj.deleteNetworkPoliciesFromAAI(mockExecution)
163
164         Mockito.verify(mockExecution).setVariable("prefix", prefix)
165         Mockito.verify(mockExecution).setVariable(prefix + "networkPolicyFqdnCount", 1)
166         Mockito.verify(mockExecution).setVariable(prefix + "aaiQueryNetworkPolicyByFqdnReturnCode", 200)
167     }
168
169
170     private static ExecutionEntity setupMock() {
171         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
172         when(mockProcessDefinition.getKey()).thenReturn("DoCreateVfModuleRollback")
173         RepositoryService mockRepositoryService = mock(RepositoryService.class)
174         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
175         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoCreateVfModuleRollback")
176         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
177         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
178         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
179
180         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
181         // Initialize prerequisite variables
182         when(mockExecution.getId()).thenReturn("100")
183         when(mockExecution.getProcessDefinitionId()).thenReturn("DoCreateVfModuleRollback")
184         when(mockExecution.getProcessInstanceId()).thenReturn("DoCreateVfModuleRollback")
185         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
186         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
187
188         return mockExecution
189     }
190
191     private static void mockData() {
192         stubFor(get(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy\\?network-policy-fqdn=.*"))
193                 .willReturn(aResponse()
194                 .withStatus(200)
195                 .withHeader("Content-Type", "text/xml")
196                 .withBodyFile("VfModularity/QueryNetworkPolicy_AAIResponse_Success.xml")))
197         stubFor(delete(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy/.*"))
198                 .willReturn(aResponse()
199                 .withStatus(200)));
200
201     }
202 }