d417abc6718425875c682ffc137aab3e3fa98d47
[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 static org.junit.Assert.assertEquals
24 import static org.junit.Assert.assertNotNull
25 import static org.mockito.ArgumentMatchers.anyObject
26 import static org.mockito.Mockito.spy
27 import static org.mockito.Mockito.times
28 import static org.mockito.Mockito.verify
29 import static org.mockito.Mockito.when
30 import org.camunda.bpm.engine.delegate.BpmnError
31 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
32 import org.junit.Before
33 import org.junit.Test
34 import org.mockito.ArgumentCaptor
35 import org.mockito.Captor
36 import org.mockito.Mockito
37 import org.mockito.MockitoAnnotations
38 import org.onap.aai.domain.yang.GenericVnf
39 import org.onap.aai.domain.yang.VolumeGroup
40 import org.onap.aai.domain.yang.VolumeGroups
41 import org.onap.aaiclient.client.aai.AAIObjectType
42 import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri
43 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
44 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
45 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
46 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
47 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
48 import org.onap.so.bpmn.core.RollbackData
49 import org.onap.so.constants.Defaults
50
51
52 class DoCreateVfModuleVolumeV2Test extends MsoGroovyTest {
53
54         private DoCreateVfModuleVolumeV2 doCreateVfModuleVolumeV2;
55
56         @Captor
57         static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
58
59         String volumeRollbackRequest = """
60 <rollbackVolumeGroupRequest>
61    <volumeGroupRollback>
62       <volumeGroupId>171907d6-cdf0-4e08-953d-81ee104005a7</volumeGroupId>
63       <volumeGroupStackId>{{VOLUMEGROUPSTACKID}}</volumeGroupStackId>
64       <tenantId>c2141e3fcae940fcb4797ec9115e5a7a</tenantId>
65       <cloudSiteId>mtwnj1a</cloudSiteId>
66       <volumeGroupCreated>true</volumeGroupCreated>
67       <msoRequest>
68          <requestId>230fd6ac-2a39-4be4-9b1e-7b7e1cc039b5</requestId>
69          <serviceInstanceId>88c871d6-be09-4982-8490-96b1d243fb34</serviceInstanceId>
70       </msoRequest>
71       <messageId>9a5a91e8-3b79-463c-81c3-874a78f5b567</messageId>
72    </volumeGroupRollback>
73    <skipAAI>true</skipAAI>
74    <notificationUrl>http://localhost:8080/mso/WorkflowMessage/VNFAResponse/9a5a91e8-3b79-463c-81c3-874a78f5b567</notificationUrl>
75 </rollbackVolumeGroupRequest>
76         """
77         
78         String volumeRollbackRequestWithStackId = """
79 <rollbackVolumeGroupRequest>
80    <volumeGroupRollback>
81       <volumeGroupId>171907d6-cdf0-4e08-953d-81ee104005a7</volumeGroupId>
82       <volumeGroupStackId>mdt22avrr_volume01/0f1aaae8-efe3-45ce-83e1-bfad01db58d8</volumeGroupStackId>
83       <tenantId>c2141e3fcae940fcb4797ec9115e5a7a</tenantId>
84       <cloudSiteId>mtwnj1a</cloudSiteId>
85       <volumeGroupCreated>true</volumeGroupCreated>
86       <msoRequest>
87          <requestId>230fd6ac-2a39-4be4-9b1e-7b7e1cc039b5</requestId>
88          <serviceInstanceId>88c871d6-be09-4982-8490-96b1d243fb34</serviceInstanceId>
89       </msoRequest>
90       <messageId>9a5a91e8-3b79-463c-81c3-874a78f5b567</messageId>
91    </volumeGroupRollback>
92    <skipAAI>true</skipAAI>
93    <notificationUrl>http://localhost:8080/mso/WorkflowMessage/VNFAResponse/9a5a91e8-3b79-463c-81c3-874a78f5b567</notificationUrl>
94 </rollbackVolumeGroupRequest>
95         """
96         
97         
98         
99         @Before
100         public void init()
101         {
102                 super.init("DoCreateVfModuleVolumeV2")
103                 doCreateVfModuleVolumeV2 = spy(DoCreateVfModuleVolumeV2.class)
104                 when(doCreateVfModuleVolumeV2.getAAIClient()).thenReturn(client)
105                 MockitoAnnotations.initMocks(this)
106         }
107         
108         @Test
109         public void testBuildRollbackVolumeGroupRequestXml() {
110                 DoCreateVfModuleVolumeV2 process = new DoCreateVfModuleVolumeV2()
111                 String xml = process.buildRollbackVolumeGroupRequestXml(
112                         "171907d6-cdf0-4e08-953d-81ee104005a7",         // volumeGroupId
113                         "mtwnj1a",                                                                      // cloudSiteId
114                         "c2141e3fcae940fcb4797ec9115e5a7a",             // tenantId
115                         "230fd6ac-2a39-4be4-9b1e-7b7e1cc039b5",         // requestId
116                         "88c871d6-be09-4982-8490-96b1d243fb34",         // serviceInstanceId
117                         "9a5a91e8-3b79-463c-81c3-874a78f5b567",         // messageId
118                         "http://localhost:8080/mso/WorkflowMessage/VNFAResponse/9a5a91e8-3b79-463c-81c3-874a78f5b567")  // notificationUrl
119
120                 assertEquals(volumeRollbackRequest.replaceAll("\\s", ""), xml.replaceAll("\\s", ""))
121         }
122
123
124         @Test
125         public void testUpdateRollbackVolumeGroupRequestXml() {
126                 DoCreateVfModuleVolumeV2 process = new DoCreateVfModuleVolumeV2()
127                 String updatedXml = process.updateRollbackVolumeGroupRequestXml(volumeRollbackRequest, "mdt22avrr_volume01/0f1aaae8-efe3-45ce-83e1-bfad01db58d8")
128                 assertEquals(volumeRollbackRequestWithStackId.replaceAll("\\s", ""), updatedXml.replaceAll("\\s", ""))
129         }
130
131         @Test
132         public void testPrepareVnfAdapterCreateRequest (){
133                 ExecutionEntity mockExecution = setupMock('DoCreateVfModuleVolumeV2')
134
135                 when(mockExecution.getVariable("prefix")).thenReturn('DCVFMODVOLV2_')
136                 when(mockExecution.getVariable("DCVFMODVOLV2_AAIQueryGenericVfnResponse")).thenReturn(new GenericVnf())
137                 when(mockExecution.getVariable("serviceInstanceId")).thenReturn('')
138                 when(mockExecution.getVariable("vnfId")).thenReturn('test-vnf-id')
139                 when(mockExecution.getVariable("mso-request-id")).thenReturn('1234')
140                 when(mockExecution.getVariable("volumeGroupId")).thenReturn('1234')
141                 when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
142                 when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn("http://localhost:28080/mso/WorkflowMesssage")
143                 Map vfModuleInputParams = new HashMap()
144                 vfModuleInputParams.put("param1","value1")
145                 when(mockExecution.getVariable("vfModuleInputParams")).thenReturn(vfModuleInputParams)
146                 DoCreateVfModuleVolumeV2 process = new DoCreateVfModuleVolumeV2()
147                 process.prepareVnfAdapterCreateRequest(mockExecution,"true");
148                 Mockito.verify(mockExecution,times(2)).setVariable(captor.capture(), captor.capture())
149                 String DCVFMODVOLV2_createVnfARequest = captor.getValue();
150                 assertNotNull(DCVFMODVOLV2_createVnfARequest)
151         }
152
153         @Test
154         void testcallRESTQueryAAIVolGrpName(){
155                 String volumeGroupName = "volumeGroupName"
156                 String lcpCloudRegionId = "lcpCloudRegionId"
157                 when(mockExecution.getVariable(volumeGroupName)).thenReturn(volumeGroupName)
158                 when(mockExecution.getVariable(lcpCloudRegionId)).thenReturn(lcpCloudRegionId)
159                 AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(Defaults.CLOUD_OWNER.toString(), lcpCloudRegionId).volumeGroups()).queryParam("volume-group-name", volumeGroupName)
160                 VolumeGroups volumeGroups = new VolumeGroups();
161                 VolumeGroup volumeGroup = new  VolumeGroup()
162                 volumeGroup.setVolumeGroupId("volumeGroupId")
163                 volumeGroups.getVolumeGroup().add(volumeGroup);
164                 when(client.get(VolumeGroups.class,uri)).thenReturn(Optional.of(volumeGroups))
165                 doCreateVfModuleVolumeV2.callRESTQueryAAIVolGrpName(mockExecution,null)
166                 verify(mockExecution).setVariable("DCVFMODVOLV2_AaiReturnCode",200)
167         }
168
169         @Test
170         void testcallRESTQueryAAIVolGrpName_NoData(){
171                 String volumeGroupName = "volumeGroupName"
172                 String lcpCloudRegionId = "lcpCloudRegionId"
173                 when(mockExecution.getVariable(volumeGroupName)).thenReturn(volumeGroupName)
174                 when(mockExecution.getVariable(lcpCloudRegionId)).thenReturn(lcpCloudRegionId)
175                 AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(Defaults.CLOUD_OWNER.toString(), lcpCloudRegionId).volumeGroups()).queryParam("volume-group-name", volumeGroupName)
176                 when(client.get(VolumeGroup.class,uri)).thenReturn(Optional.empty())
177                 thrown.expect(BpmnError.class)
178                 doCreateVfModuleVolumeV2.callRESTQueryAAIVolGrpName(mockExecution,null)
179         }
180
181         @Test
182         void testcallRESTUpdateCreatedVolGrpName(){
183                 String queriedVolumeGroupId = "queriedVolumeGroupId"
184                 String modelCustomizationId = "modelCustomizationId"
185                 String lcpCloudRegionId = "lcpCloudRegionId"
186                 when(mockExecution.getVariable(queriedVolumeGroupId)).thenReturn(queriedVolumeGroupId)
187                 when(mockExecution.getVariable(modelCustomizationId)).thenReturn(modelCustomizationId)
188                 when(mockExecution.getVariable(lcpCloudRegionId)).thenReturn(lcpCloudRegionId)
189                 when(mockExecution.getVariable("DCVFMODVOLV2_createVnfAResponse")).thenReturn("<createVnfAResponse><volumeGroupStackId>volumeGroupStackId</volumeGroupStackId></createVnfAResponse>")
190                 doCreateVfModuleVolumeV2.callRESTUpdateCreatedVolGrpName(mockExecution,null)
191                 verify(mockExecution).setVariable("DCVFMODVOLV2_heatStackId","volumeGroupStackId")
192         }
193
194         @Test
195         void testcallRESTUpdateCreatedVolGrpNameException(){
196                 String queriedVolumeGroupId = "queriedVolumeGroupId"
197                 String modelCustomizationId = "modelCustomizationId"
198                 String lcpCloudRegionId = "lcpCloudRegionId"
199                 when(mockExecution.getVariable(queriedVolumeGroupId)).thenReturn(queriedVolumeGroupId)
200                 when(mockExecution.getVariable(modelCustomizationId)).thenReturn(modelCustomizationId)
201                 when(mockExecution.getVariable(lcpCloudRegionId)).thenReturn(lcpCloudRegionId)
202                 when(mockExecution.getVariable("DCVFMODVOLV2_createVnfAResponse")).thenReturn("<createVnfAResponse><volumeGroupStackId>volumeGroupStackId</volumeGroupStackId></createVnfAResponse>")
203                 when(client.update(anyObject(),anyObject())).thenThrow(Exception.class)
204                 thrown.expect(BpmnError.class)
205                 doCreateVfModuleVolumeV2.callRESTUpdateCreatedVolGrpName(mockExecution,null)
206                 verify(mockExecution).setVariable("DCVFMODVOLV2_heatStackId","volumeGroupStackId")
207         }
208
209         @Test
210         void testcallRESTQueryAAIGenericVnf(){
211                 String vnfId = "vnfId"
212                 when(mockExecution.getVariable(vnfId)).thenReturn(vnfId)
213                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId))
214                 GenericVnf genericVnf = new GenericVnf()
215                 genericVnf.setVnfId(vnfId)
216                 when(client.get(GenericVnf.class,uri)).thenReturn(Optional.of(genericVnf))
217                 doCreateVfModuleVolumeV2.callRESTQueryAAIGenericVnf(mockExecution,null)
218                 verify(mockExecution).setVariable("DCVFMODVOLV2_AAIQueryGenericVfnResponse",genericVnf)
219         }
220
221         @Test
222         void testcallRESTQueryAAIGenericVnf_NotFound(){
223                 String vnfId = "vnfId"
224                 when(mockExecution.getVariable(vnfId)).thenReturn(vnfId)
225                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId))
226                 when(client.get(GenericVnf.class,uri)).thenReturn(Optional.empty())
227                 thrown.expect(BpmnError.class)
228                 doCreateVfModuleVolumeV2.callRESTQueryAAIGenericVnf(mockExecution,null)
229         }
230
231         @Test
232         void testcallRESTCreateAAIVolGrpName(){
233                 String vnfId = "vnfId"
234                 String volumeGroupId = "volumeGroupId"
235                 String volumeGroupName = "volumeGroupName"
236                 String modelCustomizationId = "modelCustomizationId"
237                 String vnfType= "vnfType"
238                 String tenantId = "tenantId"
239                 String lcpCloudRegionId= "lcpCloudRegionId"
240                 String cloudOwner = "cloudOwner"
241
242                 when(mockExecution.getVariable(vnfId)).thenReturn(vnfId)
243                 when(mockExecution.getVariable(volumeGroupId)).thenReturn(volumeGroupId)
244                 when(mockExecution.getVariable(volumeGroupName)).thenReturn(volumeGroupName)
245                 when(mockExecution.getVariable(modelCustomizationId)).thenReturn(modelCustomizationId)
246                 when(mockExecution.getVariable(vnfType)).thenReturn(vnfType)
247                 when(mockExecution.getVariable(tenantId)).thenReturn(tenantId)
248                 when(mockExecution.getVariable(lcpCloudRegionId)).thenReturn(lcpCloudRegionId)
249                 when(mockExecution.getVariable(cloudOwner)).thenReturn(cloudOwner)
250                 when(mockExecution.getVariable("rollbackData")).thenReturn(new RollbackData())
251                 doCreateVfModuleVolumeV2.callRESTCreateAAIVolGrpName(mockExecution,null)
252                 verify(mockExecution).setVariable("queriedVolumeGroupId", "volumeGroupId")
253         }
254
255         @Test
256         void testcallRESTCreateAAIVolGrpNameException(){
257                 String vnfId = "vnfId"
258                 String volumeGroupId = "volumeGroupId"
259                 String volumeGroupName = "volumeGroupName"
260                 String modelCustomizationId = "modelCustomizationId"
261                 String vnfType= "vnfType"
262                 String tenantId = "tenantId"
263                 String lcpCloudRegionId= "lcpCloudRegionId"
264                 String cloudOwner = "cloudOwner"
265
266                 when(mockExecution.getVariable(vnfId)).thenReturn(vnfId)
267                 when(mockExecution.getVariable(volumeGroupId)).thenReturn(volumeGroupId)
268                 when(mockExecution.getVariable(volumeGroupName)).thenReturn(volumeGroupName)
269                 when(mockExecution.getVariable(modelCustomizationId)).thenReturn(modelCustomizationId)
270                 when(mockExecution.getVariable(vnfType)).thenReturn(vnfType)
271                 when(mockExecution.getVariable(tenantId)).thenReturn(tenantId)
272                 when(mockExecution.getVariable(lcpCloudRegionId)).thenReturn(lcpCloudRegionId)
273                 when(mockExecution.getVariable(cloudOwner)).thenReturn(cloudOwner)
274                 thrown.expect(BpmnError.class)
275                 doCreateVfModuleVolumeV2.callRESTCreateAAIVolGrpName(mockExecution,null)
276         }
277 }