Fixed issues found in 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.Vserver;
53 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
54 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
55 import org.onap.so.adapters.vnfmadapter.extclients.aai.OamIpAddressSource;
56 import org.onap.so.adapters.vnfmadapter.extclients.aai.OamIpAddressSource.OamIpAddressType;
57 import org.onap.so.adapters.vnfmadapter.extclients.vim.model.AccessInfo;
58 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
59 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum;
60 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationComputeResource;
61 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinks;
62 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance;
63 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfIdentifierCreationNotification;
64 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification;
65 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationEnum;
66 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
67 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
68 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201Links;
69 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201LinksSelf;
70 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
71 import org.onap.so.client.aai.AAIResourcesClient;
72 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
73 import org.springframework.beans.factory.annotation.Autowired;
74 import org.springframework.beans.factory.annotation.Qualifier;
75 import org.springframework.boot.test.context.SpringBootTest;
76 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
77 import org.springframework.boot.test.mock.mockito.MockBean;
78 import org.springframework.boot.web.server.LocalServerPort;
79 import org.springframework.http.HttpStatus;
80 import org.springframework.http.MediaType;
81 import org.springframework.http.ResponseEntity;
82 import org.springframework.test.context.ActiveProfiles;
83 import org.springframework.test.context.junit4.SpringRunner;
84 import org.springframework.test.web.client.MockRestServiceServer;
85 import org.springframework.web.client.RestTemplate;
86
87 @RunWith(SpringRunner.class)
88 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
89 @ActiveProfiles("test")
90 public class Sol003LcnControllerTest {
91
92     private static final String CLOUD_OWNER = "myTestCloudOwner";
93     private static final String REGION = "myTestRegion";
94     private static final String TENANT_ID = "myTestTenantId";
95
96     @LocalServerPort
97     private int port;
98     @Autowired
99     @Qualifier(CONFIGURABLE_REST_TEMPLATE)
100     private RestTemplate testRestTemplate;
101     private MockRestServiceServer mockRestServer;
102
103     @MockBean
104     private AAIResourcesClient aaiResourcesClient;
105
106     @Autowired
107     private Sol003LcnContoller controller;
108     private final Gson gson = new Gson();
109
110     @Inject
111     private AaiHelper aaiHelper;
112
113     @Before
114     public void setUp() throws Exception {
115         mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
116     }
117
118     @Test
119     public void lcnNotification_IdentifierCreated_Returns204() throws URISyntaxException, InterruptedException {
120         final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
121                 new VnfIdentifierCreationNotification();
122         final ResponseEntity<Void> response =
123                 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
124         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
125     }
126
127     @Test
128     public void lcnNotification_IdentifierDeleted_Returns204() throws URISyntaxException, InterruptedException {
129         final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
130                 new VnfIdentifierCreationNotification();
131         final ResponseEntity<Void> response =
132                 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
133         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
134     }
135
136     @Test
137     public void lcnNotification_InstantiateStartingOrProcessing_NoAction()
138             throws URISyntaxException, InterruptedException {
139         final VnfLcmOperationOccurrenceNotification startingNotification = new VnfLcmOperationOccurrenceNotification();
140         startingNotification.setOperation(OperationEnum.INSTANTIATE);
141         startingNotification.setOperationState(OperationStateEnum.STARTING);
142
143         ResponseEntity<Void> response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(startingNotification);
144         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
145
146         verifyZeroInteractions(aaiResourcesClient);
147
148         final VnfLcmOperationOccurrenceNotification processingNotification =
149                 new VnfLcmOperationOccurrenceNotification();
150         processingNotification.setOperation(OperationEnum.INSTANTIATE);
151         processingNotification.setOperationState(OperationStateEnum.STARTING);
152
153         response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(processingNotification);
154         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
155
156         verifyZeroInteractions(aaiResourcesClient);
157     }
158
159     @Test
160     public void lcnNotification_InstantiateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
161         final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
162                 createNotification(OperationEnum.INSTANTIATE);
163         addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.ADDED);
164         final InlineResponse201 vnfInstance = createVnfInstance();
165
166         mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
167                 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
168
169         final GenericVnf genericVnf = createGenericVnf("vnfmType1");
170         final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
171         listOfGenericVnfs.add(genericVnf);
172         final GenericVnfs genericVnfs = new GenericVnfs();
173         genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
174         doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
175                 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
176                         "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
177
178         final ResponseEntity<Void> response =
179                 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
180         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
181
182         final ArgumentCaptor<Object> bodyArgument1 = ArgumentCaptor.forClass(Object.class);
183         final ArgumentCaptor<AAIResourceUri> uriArgument1 = ArgumentCaptor.forClass(AAIResourceUri.class);
184
185         verify(aaiResourcesClient, timeout(1000)).update(uriArgument1.capture(), bodyArgument1.capture());
186
187         assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId",
188                 uriArgument1.getAllValues().get(0).build().toString());
189         final GenericVnf updatedGenericVnf = (GenericVnf) bodyArgument1.getAllValues().get(0);
190         assertEquals("10.10.10.10", updatedGenericVnf.getIpv4OamAddress());
191         assertEquals("Created", updatedGenericVnf.getOrchestrationStatus());
192
193         final ArgumentCaptor<Object> bodyArgument2 = ArgumentCaptor.forClass(Object.class);
194         final ArgumentCaptor<AAIResourceUri> uriArgument2 = ArgumentCaptor.forClass(AAIResourceUri.class);
195         verify(aaiResourcesClient, timeout(1000)).create(uriArgument2.capture(), bodyArgument2.capture());
196
197         assertEquals(
198                 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
199                         + TENANT_ID + "/vservers/vserver/myVnfc1",
200                 uriArgument2.getAllValues().get(0).build().toString());
201
202         final Vserver vserver = (Vserver) bodyArgument2.getAllValues().get(0);
203         assertEquals("myVnfc1", vserver.getVserverId());
204         final Relationship relationship = vserver.getRelationshipList().getRelationship().get(0);
205         assertEquals("generic-vnf", relationship.getRelatedTo());
206         assertEquals("tosca.relationships.HostedOn", relationship.getRelationshipLabel());
207         assertEquals("/aai/v15/network/generic-vnfs/generic-vnf/myTestVnfId", relationship.getRelatedLink());
208         assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
209         assertEquals("myTestVnfId", relationship.getRelationshipData().get(0).getRelationshipValue());
210     }
211
212     @Test
213     public void lcnNotification_TerminateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
214         final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
215                 createNotification(OperationEnum.TERMINATE);
216         addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.REMOVED);
217
218         final InlineResponse201 vnfInstance = createVnfInstance();
219
220         mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
221                 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
222
223         mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
224                 .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON));
225
226         final GenericVnf genericVnf = createGenericVnf("vnfmType1");
227         genericVnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
228         final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
229         listOfGenericVnfs.add(genericVnf);
230         final GenericVnfs genericVnfs = new GenericVnfs();
231         genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
232
233         doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
234                 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
235                         "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
236
237         final ResponseEntity<Void> response =
238                 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
239         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
240
241         final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
242         final ArgumentCaptor<AAIResourceUri> updateUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
243         verify(aaiResourcesClient, timeout(10000000)).update(updateUriArgument.capture(), genericVnfArgument.capture());
244         assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", updateUriArgument.getValue().build().toString());
245         assertEquals("Assigned", genericVnfArgument.getValue().getOrchestrationStatus());
246
247         final ArgumentCaptor<AAIResourceUri> deleteUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
248
249         verify(aaiResourcesClient, timeout(10000000)).delete(deleteUriArgument.capture());
250
251         assertEquals(
252                 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
253                         + TENANT_ID + "/vservers/vserver/myVnfc1",
254                 deleteUriArgument.getAllValues().get(0).build().toString());
255     }
256
257     private VnfLcmOperationOccurrenceNotification createNotification(final OperationEnum operation) {
258         final VnfLcmOperationOccurrenceNotification notification = new VnfLcmOperationOccurrenceNotification();
259         notification.setOperation(operation);
260         notification.setOperationState(OperationStateEnum.COMPLETED);
261
262         final LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance linkToVnfInstance =
263                 new LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance()
264                         .href("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
265         final LcnVnfLcmOperationOccurrenceNotificationLinks operationLinks =
266                 new LcnVnfLcmOperationOccurrenceNotificationLinks().vnfInstance(linkToVnfInstance);
267         notification.setLinks(operationLinks);
268
269         return notification;
270     }
271
272     private void addVnfcsToNotification(final VnfLcmOperationOccurrenceNotification notification,
273             final ChangeTypeEnum changeType) {
274         final List<LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs> affectedVnfcs = new ArrayList<>();;
275         final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc =
276                 new LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs();
277         vnfc.changeType(changeType);
278         final LcnVnfLcmOperationOccurrenceNotificationComputeResource computeResource =
279                 new LcnVnfLcmOperationOccurrenceNotificationComputeResource();
280         computeResource.setResourceId("myVnfc1");
281         computeResource.setVimConnectionId(CLOUD_OWNER + "_" + REGION);
282         vnfc.setComputeResource(computeResource);
283         affectedVnfcs.add(vnfc);
284         notification.setAffectedVnfcs(affectedVnfcs);
285     }
286
287     private InlineResponse201 createVnfInstance() {
288         final InlineResponse201 vnfInstance = new InlineResponse201();
289         vnfInstance.setId("myTestVnfIdOnVnfm");
290         final InlineResponse201LinksSelf selfLink = new InlineResponse201LinksSelf();
291         selfLink.setHref("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
292         final InlineResponse201Links VnfInstancelinks = new InlineResponse201Links();
293         VnfInstancelinks.setSelf(selfLink);
294         vnfInstance.setLinks(VnfInstancelinks);
295
296         final Map<String, String> vnfConfigurableProperties = new HashMap<>();
297         vnfConfigurableProperties.put("vnfIpAddress", "10.10.10.10");
298         vnfInstance.setVnfConfigurableProperties(vnfConfigurableProperties);
299
300         final List<InlineResponse201VimConnectionInfo> vimConnectionInfo = new ArrayList<>();;
301         final InlineResponse201VimConnectionInfo vimConnection = new InlineResponse201VimConnectionInfo();
302         vimConnection.setVimId(CLOUD_OWNER + "_" + REGION);
303         vimConnection.setId(CLOUD_OWNER + "_" + REGION);
304         final AccessInfo accessInfo = new AccessInfo();
305         accessInfo.setProjectId(TENANT_ID);
306         vimConnection.setAccessInfo(accessInfo);
307         vimConnectionInfo.add(vimConnection);
308         vnfInstance.setVimConnectionInfo(vimConnectionInfo);
309
310         final OamIpAddressSource oamIpAddressSource =
311                 new OamIpAddressSource(OamIpAddressType.CONFIGURABLE_PROPERTY, "vnfIpAddress");
312         aaiHelper.setOamIpAddressSource("myTestVnfIdOnVnfm", oamIpAddressSource);
313         return vnfInstance;
314     }
315
316     private GenericVnf createGenericVnf(final String type) {
317         final GenericVnf genericVnf = new GenericVnf();
318         genericVnf.setVnfId("myTestVnfId");
319         genericVnf.setNfType(type);
320         return genericVnf;
321     }
322
323     private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
324
325         final String uriAsString;
326
327         public AaiResourceUriMatcher(final String uriAsString) {
328             this.uriAsString = uriAsString;
329         }
330
331         @Override
332         public boolean matches(final Object item) {
333             if (item instanceof AAIResourceUri) {
334                 if (uriAsString.endsWith("...")) {
335                     return ((AAIResourceUri) item).build().toString()
336                             .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
337                 }
338                 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
339             }
340             return false;
341         }
342
343         @Override
344         public void describeTo(final Description description) {}
345
346     }
347
348 }