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