Fixed issues found in delete integration testing
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / test / java / org / onap / so / adapters / vnfmadapter / rest / Sol003LcnControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.rest;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.timeout;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.verifyZeroInteractions;
29 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
30 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
31 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
32 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
33 import com.google.gson.Gson;
34 import java.net.URI;
35 import java.net.URISyntaxException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Optional;
41 import javax.inject.Inject;
42 import org.hamcrest.BaseMatcher;
43 import org.hamcrest.Description;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.ArgumentCaptor;
48 import org.mockito.hamcrest.MockitoHamcrest;
49 import org.onap.aai.domain.yang.GenericVnf;
50 import org.onap.aai.domain.yang.GenericVnfs;
51 import org.onap.aai.domain.yang.Relationship;
52 import org.onap.aai.domain.yang.RelationshipData;
53 import org.onap.aai.domain.yang.RelationshipList;
54 import org.onap.aai.domain.yang.Vserver;
55 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
56 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
57 import org.onap.so.adapters.vnfmadapter.extclients.aai.OamIpAddressSource;
58 import org.onap.so.adapters.vnfmadapter.extclients.aai.OamIpAddressSource.OamIpAddressType;
59 import org.onap.so.adapters.vnfmadapter.extclients.vim.model.AccessInfo;
60 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
61 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum;
62 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationComputeResource;
63 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinks;
64 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance;
65 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfIdentifierCreationNotification;
66 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification;
67 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationEnum;
68 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
69 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
70 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201Links;
71 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201LinksSelf;
72 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
73 import org.onap.so.client.aai.AAIResourcesClient;
74 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
75 import org.springframework.beans.factory.annotation.Autowired;
76 import org.springframework.beans.factory.annotation.Qualifier;
77 import org.springframework.boot.test.context.SpringBootTest;
78 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
79 import org.springframework.boot.test.mock.mockito.MockBean;
80 import org.springframework.boot.web.server.LocalServerPort;
81 import org.springframework.http.HttpStatus;
82 import org.springframework.http.MediaType;
83 import org.springframework.http.ResponseEntity;
84 import org.springframework.test.context.ActiveProfiles;
85 import org.springframework.test.context.junit4.SpringRunner;
86 import org.springframework.test.web.client.MockRestServiceServer;
87 import org.springframework.web.client.RestTemplate;
88
89 @RunWith(SpringRunner.class)
90 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
91 @ActiveProfiles("test")
92 public class Sol003LcnControllerTest {
93
94     private static final String CLOUD_OWNER = "myTestCloudOwner";
95     private static final String REGION = "myTestRegion";
96     private static final String TENANT_ID = "myTestTenantId";
97
98     @LocalServerPort
99     private int port;
100     @Autowired
101     @Qualifier(CONFIGURABLE_REST_TEMPLATE)
102     private RestTemplate testRestTemplate;
103     private MockRestServiceServer mockRestServer;
104
105     @MockBean
106     private AAIResourcesClient aaiResourcesClient;
107
108     @Autowired
109     private Sol003LcnContoller controller;
110     private final Gson gson = new Gson();
111
112     @Inject
113     private AaiHelper aaiHelper;
114
115     @Before
116     public void setUp() throws Exception {
117         mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
118     }
119
120     @Test
121     public void lcnNotification_IdentifierCreated_Returns204() throws URISyntaxException, InterruptedException {
122         final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
123                 new VnfIdentifierCreationNotification();
124         final ResponseEntity<Void> response =
125                 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
126         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
127     }
128
129     @Test
130     public void lcnNotification_IdentifierDeleted_Returns204() throws URISyntaxException, InterruptedException {
131         final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
132                 new VnfIdentifierCreationNotification();
133         final ResponseEntity<Void> response =
134                 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
135         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
136     }
137
138     @Test
139     public void lcnNotification_InstantiateStartingOrProcessing_NoAction()
140             throws URISyntaxException, InterruptedException {
141         final VnfLcmOperationOccurrenceNotification startingNotification = new VnfLcmOperationOccurrenceNotification();
142         startingNotification.setOperation(OperationEnum.INSTANTIATE);
143         startingNotification.setOperationState(OperationStateEnum.STARTING);
144
145         ResponseEntity<Void> response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(startingNotification);
146         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
147
148         verifyZeroInteractions(aaiResourcesClient);
149
150         final VnfLcmOperationOccurrenceNotification processingNotification =
151                 new VnfLcmOperationOccurrenceNotification();
152         processingNotification.setOperation(OperationEnum.INSTANTIATE);
153         processingNotification.setOperationState(OperationStateEnum.STARTING);
154
155         response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(processingNotification);
156         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
157
158         verifyZeroInteractions(aaiResourcesClient);
159     }
160
161     @Test
162     public void lcnNotification_InstantiateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
163         final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
164                 createNotification(OperationEnum.INSTANTIATE);
165         addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.ADDED);
166         final InlineResponse201 vnfInstance = createVnfInstance();
167
168         mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
169                 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
170
171         final GenericVnf genericVnf = createGenericVnf("vnfmType1");
172         final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
173         listOfGenericVnfs.add(genericVnf);
174         final GenericVnfs genericVnfs = new GenericVnfs();
175         genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
176         doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
177                 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
178                         "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
179
180         final ResponseEntity<Void> response =
181                 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
182         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
183
184         final ArgumentCaptor<Object> bodyArgument1 = ArgumentCaptor.forClass(Object.class);
185         final ArgumentCaptor<AAIResourceUri> uriArgument1 = ArgumentCaptor.forClass(AAIResourceUri.class);
186
187         verify(aaiResourcesClient, timeout(1000)).update(uriArgument1.capture(), bodyArgument1.capture());
188
189         assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId",
190                 uriArgument1.getAllValues().get(0).build().toString());
191         final GenericVnf updatedGenericVnf = (GenericVnf) bodyArgument1.getAllValues().get(0);
192         assertEquals("10.10.10.10", updatedGenericVnf.getIpv4OamAddress());
193         assertEquals("Created", updatedGenericVnf.getOrchestrationStatus());
194
195         final ArgumentCaptor<Object> bodyArgument2 = ArgumentCaptor.forClass(Object.class);
196         final ArgumentCaptor<AAIResourceUri> uriArgument2 = ArgumentCaptor.forClass(AAIResourceUri.class);
197         verify(aaiResourcesClient, timeout(1000)).create(uriArgument2.capture(), bodyArgument2.capture());
198
199         assertEquals(
200                 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
201                         + TENANT_ID + "/vservers/vserver/myVnfc1",
202                 uriArgument2.getAllValues().get(0).build().toString());
203
204         final Vserver vserver = (Vserver) bodyArgument2.getAllValues().get(0);
205         assertEquals("myVnfc1", vserver.getVserverId());
206         final Relationship relationship = vserver.getRelationshipList().getRelationship().get(0);
207         assertEquals("generic-vnf", relationship.getRelatedTo());
208         assertEquals("tosca.relationships.HostedOn", relationship.getRelationshipLabel());
209         assertEquals("/aai/v15/network/generic-vnfs/generic-vnf/myTestVnfId", relationship.getRelatedLink());
210         assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
211         assertEquals("myTestVnfId", relationship.getRelationshipData().get(0).getRelationshipValue());
212     }
213
214     @Test
215     public void lcnNotification_TerminateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
216         final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
217                 createNotification(OperationEnum.TERMINATE);
218         addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.REMOVED);
219
220         final InlineResponse201 vnfInstance = createVnfInstance();
221
222         mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
223                 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
224
225         mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
226                 .andRespond(withStatus(HttpStatus.NO_CONTENT).contentType(MediaType.APPLICATION_JSON));
227
228         final GenericVnf genericVnf = createGenericVnf("vnfmType1");
229         genericVnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
230         final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
231         listOfGenericVnfs.add(genericVnf);
232         final GenericVnfs genericVnfs = new GenericVnfs();
233         genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
234         addRelationshipFromGenericVnfToVserver(genericVnf, "myVnfc1");
235
236         doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
237                 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
238                         "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
239
240         final ResponseEntity<Void> response =
241                 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
242         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
243
244         final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
245         final ArgumentCaptor<AAIResourceUri> updateUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
246         verify(aaiResourcesClient, timeout(10000000)).update(updateUriArgument.capture(), genericVnfArgument.capture());
247         assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", updateUriArgument.getValue().build().toString());
248         assertEquals("Assigned", genericVnfArgument.getValue().getOrchestrationStatus());
249
250         final ArgumentCaptor<AAIResourceUri> deleteUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
251
252         verify(aaiResourcesClient, timeout(10000000)).delete(deleteUriArgument.capture());
253
254         assertEquals(
255                 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
256                         + TENANT_ID + "/vservers/vserver/myVnfc1",
257                 deleteUriArgument.getAllValues().get(0).build().toString());
258     }
259
260     private VnfLcmOperationOccurrenceNotification createNotification(final OperationEnum operation) {
261         final VnfLcmOperationOccurrenceNotification notification = new VnfLcmOperationOccurrenceNotification();
262         notification.setOperation(operation);
263         notification.setOperationState(OperationStateEnum.COMPLETED);
264
265         final LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance linkToVnfInstance =
266                 new LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance()
267                         .href("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
268         final LcnVnfLcmOperationOccurrenceNotificationLinks operationLinks =
269                 new LcnVnfLcmOperationOccurrenceNotificationLinks().vnfInstance(linkToVnfInstance);
270         notification.setLinks(operationLinks);
271
272         return notification;
273     }
274
275     private void addVnfcsToNotification(final VnfLcmOperationOccurrenceNotification notification,
276             final ChangeTypeEnum changeType) {
277         final List<LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs> affectedVnfcs = new ArrayList<>();;
278         final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc =
279                 new LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs();
280         vnfc.changeType(changeType);
281         final LcnVnfLcmOperationOccurrenceNotificationComputeResource computeResource =
282                 new LcnVnfLcmOperationOccurrenceNotificationComputeResource();
283         computeResource.setResourceId("myVnfc1");
284         computeResource.setVimConnectionId(CLOUD_OWNER + "_" + REGION);
285         vnfc.setComputeResource(computeResource);
286         affectedVnfcs.add(vnfc);
287         notification.setAffectedVnfcs(affectedVnfcs);
288     }
289
290     private InlineResponse201 createVnfInstance() {
291         final InlineResponse201 vnfInstance = new InlineResponse201();
292         vnfInstance.setId("myTestVnfIdOnVnfm");
293         final InlineResponse201LinksSelf selfLink = new InlineResponse201LinksSelf();
294         selfLink.setHref("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
295         final InlineResponse201Links VnfInstancelinks = new InlineResponse201Links();
296         VnfInstancelinks.setSelf(selfLink);
297         vnfInstance.setLinks(VnfInstancelinks);
298
299         final Map<String, String> vnfConfigurableProperties = new HashMap<>();
300         vnfConfigurableProperties.put("vnfIpAddress", "10.10.10.10");
301         vnfInstance.setVnfConfigurableProperties(vnfConfigurableProperties);
302
303         final List<InlineResponse201VimConnectionInfo> vimConnectionInfo = new ArrayList<>();;
304         final InlineResponse201VimConnectionInfo vimConnection = new InlineResponse201VimConnectionInfo();
305         vimConnection.setVimId(CLOUD_OWNER + "_" + REGION);
306         vimConnection.setId(CLOUD_OWNER + "_" + REGION);
307         final AccessInfo accessInfo = new AccessInfo();
308         accessInfo.setProjectId(TENANT_ID);
309         vimConnection.setAccessInfo(accessInfo);
310         vimConnectionInfo.add(vimConnection);
311         vnfInstance.setVimConnectionInfo(vimConnectionInfo);
312
313         final OamIpAddressSource oamIpAddressSource =
314                 new OamIpAddressSource(OamIpAddressType.CONFIGURABLE_PROPERTY, "vnfIpAddress");
315         aaiHelper.setOamIpAddressSource("myTestVnfIdOnVnfm", oamIpAddressSource);
316         return vnfInstance;
317     }
318
319     private GenericVnf createGenericVnf(final String type) {
320         final GenericVnf genericVnf = new GenericVnf();
321         genericVnf.setVnfId("myTestVnfId");
322         genericVnf.setNfType(type);
323         return genericVnf;
324     }
325
326     private void addRelationshipFromGenericVnfToVserver(final GenericVnf genericVnf, final String vserverId) {
327         final Relationship relationshipToVserver = new Relationship();
328         relationshipToVserver.setRelatedTo("vserver");
329         final RelationshipData relationshipData1 = new RelationshipData();
330         relationshipData1.setRelationshipKey("vserver.vserver-id");
331         relationshipData1.setRelationshipValue(vserverId);
332         relationshipToVserver.getRelationshipData().add(relationshipData1);
333         final RelationshipData relationshipData2 = new RelationshipData();
334         relationshipData2.setRelationshipKey("cloud-region.cloud-owner");
335         relationshipData2.setRelationshipValue(CLOUD_OWNER);
336         relationshipToVserver.getRelationshipData().add(relationshipData2);
337         final RelationshipData relationshipData3 = new RelationshipData();
338         relationshipData3.setRelationshipKey("cloud-region.cloud-region-id");
339         relationshipData3.setRelationshipValue(REGION);
340         relationshipToVserver.getRelationshipData().add(relationshipData3);
341         final RelationshipData relationshipData4 = new RelationshipData();
342         relationshipData4.setRelationshipKey("tenant.tenant-id");
343         relationshipData4.setRelationshipValue(TENANT_ID);
344         relationshipToVserver.getRelationshipData().add(relationshipData4);
345
346         final RelationshipList relationshipList = new RelationshipList();
347         relationshipList.getRelationship().add(relationshipToVserver);
348         genericVnf.setRelationshipList(relationshipList);
349     }
350
351     private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
352
353         final String uriAsString;
354
355         public AaiResourceUriMatcher(final String uriAsString) {
356             this.uriAsString = uriAsString;
357         }
358
359         @Override
360         public boolean matches(final Object item) {
361             if (item instanceof AAIResourceUri) {
362                 if (uriAsString.endsWith("...")) {
363                     return ((AAIResourceUri) item).build().toString()
364                             .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
365                 }
366                 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
367             }
368             return false;
369         }
370
371         @Override
372         public void describeTo(final Description description) {}
373
374     }
375
376 }