bf877bd725d1e69c4e3bd8acf51727f100752705
[so.git] /
1 /*- 
2  * ============LICENSE_START======================================================= 
3  * ONAP - SO 
4  * ================================================================================ 
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. 
6  * ================================================================================ 
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License. 
11  * You may obtain a copy of the License at 
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0 
14  * 
15  * Unless required by applicable law or agreed to in writing, software 
16  * distributed under the License is distributed on an "AS IS" BASIS, 
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
18  * See the License for the specific language governing permissions and 
19  * limitations under the License. 
20  * ============LICENSE_END========================================================= 
21  */ 
22
23 package org.onap.so.bpmn.infrastructure.scripts
24
25
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity
28 import org.junit.Assert
29 import org.junit.Before
30 import org.junit.Rule
31 import org.junit.Test
32 import org.junit.rules.ExpectedException
33 import org.junit.runner.RunWith
34 import org.mockito.ArgumentCaptor
35 import org.mockito.Captor
36 import org.mockito.MockitoAnnotations
37 import org.mockito.junit.MockitoJUnitRunner
38 import org.onap.aai.domain.yang.GenericVnf
39 import org.onap.aai.domain.yang.VfModule
40 import org.onap.aai.domain.yang.VolumeGroup
41 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
42 import org.onap.so.bpmn.mock.FileUtil
43 import org.onap.aaiclient.client.aai.AAIObjectType
44 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
45 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
46 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
47 import org.onap.so.constants.Defaults
48
49 import javax.ws.rs.core.UriBuilder
50
51 import static org.mockito.Mockito.*
52
53 @RunWith(MockitoJUnitRunner.Silent.class)
54 class UpdateVfModuleVolumeInfraV1Test extends MsoGroovyTest{
55
56     def prefix = "UPDVfModVol_"
57     @Captor
58     static ArgumentCaptor<ExecutionEntity> captor = ArgumentCaptor.forClass(ExecutionEntity.class)
59
60     @Rule
61     public ExpectedException thrown = ExpectedException.none()
62
63
64     @Before
65     public void init() {
66         super.init("UpdateVfModuleVolumeInfraV1")
67         MockitoAnnotations.initMocks(this)
68     }
69
70     @Test
71     void testQueryAAIForVfModule() {
72         ExecutionEntity mockExecution = setupMock()
73         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
74         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
75         when(mockExecution.getVariable("UPDVfModVol_relatedVfModuleLink")).thenReturn("/aai/v8/network/generic-vnfs/generic-vnf/12345/vf-modules/vf-module/12345")
76         when(mockExecution.getVariable("aai.endpoint")).thenReturn("http://localhost:28090")
77         when(mockExecution.getVariable("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7")
78         when(mockExecution.getVariable("aai.auth")).thenReturn("757A94191D685FD2092AC1490730A4FC")
79
80         UpdateVfModuleVolumeInfraV1 obj = spy(UpdateVfModuleVolumeInfraV1.class)
81         when(obj.getAAIClient()).thenReturn(client)
82         AAIResourceUri uri = AAIUriFactory.createResourceFromExistingURI(AAIObjectType.VF_MODULE, UriBuilder.fromPath("/aai/v8/network/generic-vnfs/generic-vnf/12345/vf-modules/vf-module/12345").build())
83         VfModule vfModule = new VfModule();
84         vfModule.setVfModuleId("12345")
85         vfModule.setModelInvariantId("ff5256d2-5a33-55df-13ab-12abad84e7ff")
86         when(client.get(VfModule.class,uri)).thenReturn(Optional.of(vfModule))
87         obj.queryAAIForVfModule(mockExecution, "true")
88
89         verify(mockExecution, atLeastOnce()).setVariable("UPDVfModVol_personaModelId", "ff5256d2-5a33-55df-13ab-12abad84e7ff")
90     }
91
92     @Test
93     void testQueryAAIForVolumeGroup() {
94         String aicCloudRegion = "aicCloudRegionId"
95         String volumeGroupId = "volumeGroupId"
96         when(mockExecution.getVariable("UPDVfModVol_volumeGroupId")).thenReturn(volumeGroupId)
97         when(mockExecution.getVariable("UPDVfModVol_aicCloudRegion")).thenReturn(aicCloudRegion)
98
99         UpdateVfModuleVolumeInfraV1 obj = spy(UpdateVfModuleVolumeInfraV1.class)
100         when(obj.getAAIClient()).thenReturn(client)
101         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, Defaults.CLOUD_OWNER.toString(), aicCloudRegion, volumeGroupId)
102         VolumeGroup volumeGroup = new VolumeGroup();
103         volumeGroup.setVolumeGroupId(volumeGroupId)
104
105         AAIResultWrapper wrapper = new AAIResultWrapper(FileUtil.readResourceFile("__files/aai/VolumeGroupWithTenant.json"))
106         when(client.get(uri)).thenReturn(wrapper)
107         thrown.expect(BpmnError.class)
108         obj.queryAAIForVolumeGroup(mockExecution, "true")
109     }
110
111     @Test
112     void testQueryAAIForGenericVnf() {
113         String vnfId = "vnfId"
114         when(mockExecution.getVariable("vnfId")).thenReturn(vnfId)
115
116         UpdateVfModuleVolumeInfraV1 obj = spy(UpdateVfModuleVolumeInfraV1.class)
117         when(obj.getAAIClient()).thenReturn(client)
118
119         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
120         GenericVnf genericVnf = new GenericVnf()
121         genericVnf.setVnfId(vnfId)
122         genericVnf.setVnfName("testvnfName")
123         when(client.get(GenericVnf.class,uri)).thenReturn(Optional.of(genericVnf))
124         obj.queryAAIForGenericVnf(mockExecution, "true")
125         verify(mockExecution).setVariable("UPDVfModVol_AAIQueryGenericVfnResponse", genericVnf)
126     }
127
128     @Test
129     void testQueryAAIForGenericVnfNodata() {
130         String vnfId = "vnfId"
131         when(mockExecution.getVariable("vnfId")).thenReturn(vnfId)
132
133         UpdateVfModuleVolumeInfraV1 obj = spy(UpdateVfModuleVolumeInfraV1.class)
134         when(obj.getAAIClient()).thenReturn(client)
135
136         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
137         when(client.get(GenericVnf.class,uri)).thenReturn(Optional.empty())
138         thrown.expect(BpmnError.class)
139         obj.queryAAIForGenericVnf(mockExecution, "true")
140         verify(mockExecution).setVariable("UPDVfModVol_AAIQueryGenericVfnResponse", genericVnf)
141     }
142
143     @Test
144     void testPrepVnfAdapterRest() {
145         when(mockExecution.getVariable("prefix")).thenReturn(prefix)
146         when(mockExecution.getVariable("isDebugLogEnabled")).thenReturn("true")
147         when(mockExecution.getVariable(prefix + "aicCloudRegion")).thenReturn("RDM2WAGPLCP")
148         when(mockExecution.getVariable(prefix + "cloudOwner")).thenReturn("CloudOwner")
149         when(mockExecution.getVariable(prefix + "tenantId")).thenReturn("")
150         VolumeGroup volumeGroup = new VolumeGroup();
151         volumeGroup.setHeatStackId("heatStackId")
152         when(mockExecution.getVariable(prefix + "aaiVolumeGroupResponse")).thenReturn(volumeGroup)
153         when(mockExecution.getVariable(prefix + "vnfType")).thenReturn("vnf1")
154         when(mockExecution.getVariable(prefix + "vnfVersion")).thenReturn("1")
155         GenericVnf genericVnf = new GenericVnf()
156         genericVnf.setVnfId("vnfId")
157         genericVnf.setVnfName("testvnfName")
158         when(mockExecution.getVariable(prefix + "AAIQueryGenericVfnResponse")).thenReturn(genericVnf)
159         when(mockExecution.getVariable(prefix + "requestId")).thenReturn("12345")
160         when(mockExecution.getVariable(prefix + "serviceId")).thenReturn("12345")
161         when(mockExecution.getVariable("mso-request-id")).thenReturn("12345")
162         when(mockExecution.getVariable("mso.workflow.message.endpoint")).thenReturn('http://localhost:28080/mso/WorkflowMessage')
163         when(mockExecution.getVariable("mso.use.qualified.host")).thenReturn("true")
164
165         UpdateVfModuleVolumeInfraV1 obj = new UpdateVfModuleVolumeInfraV1()
166         obj.prepVnfAdapterRest(mockExecution, "true")
167
168         verify(mockExecution, times(1)).setVariable(captor.capture(), captor.capture())
169         String updateVolumeGroupRequest = captor.getValue()
170         Assert.assertTrue(updateVolumeGroupRequest.contains("testvnfName"))
171     }
172 }