faa6a0e3958a32cbfd60bc5a0f63c044e45f0751
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateVfModuleTest.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 DoCreateVfModuleTest {
48     def prefix = "DCVFM_"
49
50     @Captor
51     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
52
53     @Before
54     void init() throws IOException {
55         MockitoAnnotations.initMocks(this);
56     }
57   
58     @Test
59     void testQueryAAIVfModule() {
60         ExecutionEntity mockExecution = setupMock()
61         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
62         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
63         when(mockExecution.getVariable("DCVFM_vnfId")).thenReturn("12345")
64         when(mockExecution.getVariable("mso.workflow.DoCreateVfModule.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
65         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
66         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
67
68         mockData()
69         DoCreateVfModule obj = new DoCreateVfModule()
70         obj.queryAAIVfModule(mockExecution)
71         Mockito.verify(mockExecution).setVariable("DCVFM_queryAAIVfModuleResponseCode", 200)
72     }
73
74
75     @Test
76     void testQueryAAIVfModuleForStatus() {
77         ExecutionEntity mockExecution = setupMock()
78         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
79         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
80         when(mockExecution.getVariable("DCVFM_vnfId")).thenReturn("12345")
81         when(mockExecution.getVariable("DCVFM_vfModuleName")).thenReturn("module-0")
82         when(mockExecution.getVariable("mso.workflow.DoCreateVfModule.aai.generic-vnf.uri")).thenReturn("/aai/v9/network/generic-vnfs/generic-vnf")
83         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
84         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
85
86         mockData()
87         DoCreateVfModule obj = new DoCreateVfModule()
88         obj.queryAAIVfModuleForStatus(mockExecution)
89         Mockito.verify(mockExecution).setVariable("DCVFM_orchestrationStatus", '')
90         Mockito.verify(mockExecution).setVariable("DCVFM_queryAAIVfModuleForStatusResponseCode", 200)
91     }
92
93   
94
95     @Test
96     void testPreProcessVNFAdapterRequest() {
97         ExecutionEntity mockExecution = setupMock()
98         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
99         when(mockExecution.getVariable(prefix + "cloudSiteId")).thenReturn("RDM2WAGPLCP")
100         when(mockExecution.getVariable(prefix + "vfModuleId")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ce32")
101         when(mockExecution.getVariable("volumeGroupStackId")).thenReturn("12345")
102         when(mockExecution.getVariable(prefix + "vfModuleName")).thenReturn("PCRF::module-0-2")
103         when(mockExecution.getVariable("lcpCloudRegionId")).thenReturn("MDTWNJ21")
104         when(mockExecution.getVariable(prefix + "tenantId")).thenReturn("fba1bd1e195a404cacb9ce17a9b2b421")
105         when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vRRaas")
106         when(mockExecution.getVariable(prefix + "vnfName")).thenReturn("skask-test")
107         when(mockExecution.getVariable(prefix + "vnfId")).thenReturn("skask")
108         when(mockExecution.getVariable(prefix + "vfModuleModelName")).thenReturn("PCRF::module-0-2")
109         when(mockExecution.getVariable(prefix + "vfModuleIndex")).thenReturn("index")
110         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("testRequestId")
111         when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
112         when(mockExecution.getVariable(prefix + "serviceInstanceId")).thenReturn("MIS/1604/0026/SW_INTERNET")
113         when(mockExecution.getVariable(prefix + "backoutOnFailure")).thenReturn("12345")
114         when(mockExecution.getVariable(prefix + "volumeGroupId")).thenReturn("12345")
115         when(mockExecution.getVariable(prefix + "asdcServiceModelVersion")).thenReturn("1.0")
116         when(mockExecution.getVariable(prefix + "modelCustomizationUuid")).thenReturn("cb510af0-5b21-4bc7-86d9-323cb396ced3")
117         when(mockExecution.getVariable("baseVfModuleId")).thenReturn("12345")
118         when(mockExecution.getVariable(prefix + "baseVfModuleHeatStackId")).thenReturn("12345")
119         String sdncGetResponse = FileUtil.readResourceFile("__files/DoCreateVfModule/sdncGetResponse.xml");
120         when(mockExecution.getVariable(prefix + "getSDNCAdapterResponse")).thenReturn(sdncGetResponse)
121         Map<String, String> map = new HashMap<String, String>();
122         map.put("vrr_image_name", "MDT17");
123         map.put("availability_zone_0", "nova");
124         map.put("vrr_flavor_name", "ns.c16r32d128.v1");
125         when(mockExecution.getVariable("vnfParamsMap")).thenReturn(map)
126         when(mockExecution.getVariable("mso-request-id")).thenReturn("testRequestId-1503410089303")
127         when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
128         when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn("http://localhost:28080/mso/WorkflowMesssage")
129
130         mockData()
131         DoCreateVfModule obj = new DoCreateVfModule()
132         obj.preProcessVNFAdapterRequest(mockExecution)
133
134         String createVnfARequest = FileUtil.readResourceFile("__files/DoCreateVfModule/createVnfARequest.xml")
135         Mockito.verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
136         XmlComparator.assertXMLEquals(createVnfARequest, captor.getValue(), "messageId", "notificationUrl")
137     }
138
139     @Test
140     void testQueryCloudRegion() {
141         ExecutionEntity mockExecution = setupMock()
142         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
143         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
144         when(mockExecution.getVariable("DCVFM_cloudSiteId")).thenReturn("12345")
145         when(mockExecution.getVariable("mso.workflow.DoCreateVfModule.aai.cloud-region.uri")).thenReturn("/aai/v8/cloud-infrastructure/cloud-regions/cloud-region")
146         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
147         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
148
149         mockData()
150         DoCreateVfModule obj = new DoCreateVfModule()
151         obj.queryCloudRegion(mockExecution)
152
153         Mockito.verify(mockExecution).setVariable("prefix", prefix)
154         Mockito.verify(mockExecution).setVariable(prefix + "queryCloudRegionRequest", "http://localhost:28090/aai/v8/cloud-infrastructure/cloud-regions/cloud-region/12345")
155         Mockito.verify(mockExecution).setVariable(prefix + "queryCloudRegionReturnCode", "200")
156     }
157
158   
159
160     @Test
161     void testCreateNetworkPoliciesInAAI() {
162         ExecutionEntity mockExecution = setupMock()
163         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
164         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
165         when(mockExecution.getVariable("DCVFM_cloudSiteId")).thenReturn("12345")
166         when(mockExecution.getVariable("mso.workflow.DoCreateVfModule.aai.network-policy.uri")).thenReturn("/aai/v8/network/network-policies/network-policy")
167         when(mockExecution.getVariable("mso.workflow.custom.DoCreateVfModule.aai.version")).thenReturn("8")
168         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
169         when(mockExecution.getVariable("mso.workflow.global.default.aai.namespace")).thenReturn("http://org.openecomp.aai.inventory/")
170         when(mockExecution.getVariable("rollbackData")).thenReturn(new RollbackData())
171         List fqdnList = new ArrayList()
172         fqdnList.add("test")
173         when(mockExecution.getVariable("DCVFM_contrailNetworkPolicyFqdnList")).thenReturn(fqdnList)
174
175         mockData()
176         DoCreateVfModule obj = new DoCreateVfModule()
177         obj.createNetworkPoliciesInAAI(mockExecution)
178
179         Mockito.verify(mockExecution).setVariable("prefix", prefix)
180         Mockito.verify(mockExecution).setVariable(prefix + "networkPolicyFqdnCount", 1)
181         Mockito.verify(mockExecution).setVariable(prefix + "aaiQqueryNetworkPolicyByFqdnReturnCode", 200)
182     }
183
184    
185
186     private static ExecutionEntity setupMock() {
187         ProcessDefinition mockProcessDefinition = mock(ProcessDefinition.class)
188         when(mockProcessDefinition.getKey()).thenReturn("DoCreateVfModule")
189         RepositoryService mockRepositoryService = mock(RepositoryService.class)
190         when(mockRepositoryService.getProcessDefinition()).thenReturn(mockProcessDefinition)
191         when(mockRepositoryService.getProcessDefinition().getKey()).thenReturn("DoCreateVfModule")
192         when(mockRepositoryService.getProcessDefinition().getId()).thenReturn("100")
193         ProcessEngineServices mockProcessEngineServices = mock(ProcessEngineServices.class)
194         when(mockProcessEngineServices.getRepositoryService()).thenReturn(mockRepositoryService)
195
196         ExecutionEntity mockExecution = mock(ExecutionEntity.class)
197         // Initialize prerequisite variables
198         when(mockExecution.getId()).thenReturn("100")
199         when(mockExecution.getProcessDefinitionId()).thenReturn("DoCreateVfModule")
200         when(mockExecution.getProcessInstanceId()).thenReturn("DoCreateVfModule")
201         when(mockExecution.getProcessEngineServices()).thenReturn(mockProcessEngineServices)
202         when(mockExecution.getProcessEngineServices().getRepositoryService().getProcessDefinition(mockExecution.getProcessDefinitionId())).thenReturn(mockProcessDefinition)
203
204         return mockExecution
205     }
206
207     private static void mockData() {
208         stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345[?]depth=1"))
209                 .willReturn(aResponse()
210                 .withStatus(200).withHeader("Content-Type", "text/xml")
211                 .withBodyFile("DoCreateVfModule/getGenericVnfResponse.xml")))
212         stubFor(get(urlMatching(".*/aai/v[0-9]+/network/generic-vnfs/generic-vnf/12345/vf-modules/vf-module[?]vf-module-name=module-0"))
213                 .willReturn(aResponse()
214                 .withStatus(200).withHeader("Content-Type", "text/xml")
215                 .withBodyFile("DoCreateVfModule/getGenericVnfResponse.xml")))
216         stubFor(get(urlMatching(".*/aai/v[0-9]+/cloud-infrastructure/cloud-regions/cloud-region/12345"))
217                 .willReturn(aResponse()
218                 .withStatus(200).withHeader("Content-Type", "text/xml")
219                 .withBodyFile("DoCreateVfModule/cloudRegion_AAIResponse_Success.xml")))
220         stubFor(get(urlMatching("/aai/v[0-9]+/network/network-policies/network-policy\\?network-policy-fqdn=.*"))
221                 .willReturn(aResponse()
222                 .withStatus(200)
223                 .withHeader("Content-Type", "text/xml")
224                 .withBodyFile("VfModularity/QueryNetworkPolicy_AAIResponse_Success.xml")))
225     }
226 }