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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vnfmadapter.rest;
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.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
29 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
30 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
31 import com.google.gson.Gson;
33 import java.net.URISyntaxException;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
38 import java.util.Optional;
39 import org.hamcrest.BaseMatcher;
40 import org.hamcrest.Description;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.ArgumentCaptor;
45 import org.mockito.hamcrest.MockitoHamcrest;
46 import org.onap.aai.domain.yang.GenericVnf;
47 import org.onap.aai.domain.yang.Relationship;
48 import org.onap.aai.domain.yang.Vserver;
49 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
50 import org.onap.so.adapters.vnfmadapter.extclients.vim.model.AccessInfo;
51 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
52 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum;
53 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationComputeResource;
54 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinks;
55 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance;
56 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfIdentifierCreationNotification;
57 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification;
58 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationEnum;
59 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
60 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
61 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201Links;
62 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201LinksSelf;
63 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
64 import org.onap.so.client.aai.AAIResourcesClient;
65 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
66 import org.springframework.beans.factory.annotation.Autowired;
67 import org.springframework.beans.factory.annotation.Qualifier;
68 import org.springframework.boot.test.context.SpringBootTest;
69 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
70 import org.springframework.boot.test.mock.mockito.MockBean;
71 import org.springframework.boot.web.server.LocalServerPort;
72 import org.springframework.http.HttpStatus;
73 import org.springframework.http.MediaType;
74 import org.springframework.http.ResponseEntity;
75 import org.springframework.test.context.ActiveProfiles;
76 import org.springframework.test.context.junit4.SpringRunner;
77 import org.springframework.test.web.client.MockRestServiceServer;
78 import org.springframework.web.client.RestTemplate;
81 @RunWith(SpringRunner.class)
82 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
83 @ActiveProfiles("test")
84 public class Sol003LcnControllerTest {
86 private static final String CLOUD_OWNER = "myTestCloudOwner";
87 private static final String REGION = "myTestRegion";
88 private static final String TENANT_ID = "myTestTenantId";
93 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
94 private RestTemplate testRestTemplate;
95 private MockRestServiceServer mockRestServer;
98 private AAIResourcesClient aaiResourcesClient;
101 private Sol003LcnContoller controller;
102 private final Gson gson = new Gson();
105 public void setUp() throws Exception {
106 mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
110 public void lcnNotification_IdentifierCreated_Returns204() throws URISyntaxException, InterruptedException {
111 final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
112 new VnfIdentifierCreationNotification();
113 final ResponseEntity<Void> response =
114 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
115 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
119 public void lcnNotification_IdentifierDeleted_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());
128 public void lcnNotification_InstantiateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
129 final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification = createNotification();
130 final InlineResponse201 vnfInstance = createVnfInstance();
132 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
133 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
135 final GenericVnf genericVnf = createGenericVnf("vnfmType1");
136 final List<GenericVnf> genericVnfs = new ArrayList<>();
137 genericVnfs.add(genericVnf);
138 doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(List.class),
139 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
140 "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
142 final ResponseEntity<Void> response =
143 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
144 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
146 final ArgumentCaptor<Object> bodyArgument = ArgumentCaptor.forClass(Object.class);
147 final ArgumentCaptor<AAIResourceUri> uriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
149 verify(aaiResourcesClient, timeout(1000).times(2)).update(uriArgument.capture(), bodyArgument.capture());
151 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId",
152 uriArgument.getAllValues().get(0).build().toString());
153 final GenericVnf updatedGenericVnf = (GenericVnf) bodyArgument.getAllValues().get(0);
154 assertEquals("10.10.10.10", updatedGenericVnf.getIpv4OamAddress());
155 assertEquals("Created", updatedGenericVnf.getOrchestrationStatus());
158 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
159 + TENANT_ID + "/vservers/vserver/myVnfc1",
160 uriArgument.getAllValues().get(1).build().toString());
162 final Vserver vserver = (Vserver) bodyArgument.getAllValues().get(1);
163 assertEquals("myVnfc1", vserver.getVserverId());
164 final Relationship relationship = vserver.getRelationshipList().getRelationship().get(0);
165 assertEquals("generic-vnf", relationship.getRelatedTo());
166 assertEquals("tosca.relationships.HostedOn", relationship.getRelationshipLabel());
167 assertEquals("/aai/v15/network/generic-vnfs/generic-vnf/myTestVnfId", relationship.getRelatedLink());
168 assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
169 assertEquals("myTestVnfId", relationship.getRelationshipData().get(0).getRelationshipValue());
172 private VnfLcmOperationOccurrenceNotification createNotification() {
173 final VnfLcmOperationOccurrenceNotification notification = new VnfLcmOperationOccurrenceNotification();
174 notification.setOperation(OperationEnum.INSTANTIATE);
175 notification.setOperationState(OperationStateEnum.COMPLETED);
177 final LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance linkToVnfInstance =
178 new LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance()
179 .href("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
180 final LcnVnfLcmOperationOccurrenceNotificationLinks operationLinks =
181 new LcnVnfLcmOperationOccurrenceNotificationLinks().vnfInstance(linkToVnfInstance);
182 notification.setLinks(operationLinks);
184 final List<LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs> affectedVnfcs = new ArrayList<>();;
185 final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc =
186 new LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs();
187 vnfc.changeType(ChangeTypeEnum.ADDED);
188 final LcnVnfLcmOperationOccurrenceNotificationComputeResource computeResource =
189 new LcnVnfLcmOperationOccurrenceNotificationComputeResource();
190 computeResource.setResourceId("myVnfc1");
191 computeResource.setVimConnectionId(CLOUD_OWNER + "_" + REGION);
192 vnfc.setComputeResource(computeResource);
193 affectedVnfcs.add(vnfc);
194 notification.setAffectedVnfcs(affectedVnfcs);
198 private InlineResponse201 createVnfInstance() {
199 final InlineResponse201 vnfInstance = new InlineResponse201();
200 final InlineResponse201LinksSelf selfLink = new InlineResponse201LinksSelf();
201 selfLink.setHref("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
202 final InlineResponse201Links VnfInstancelinks = new InlineResponse201Links();
203 VnfInstancelinks.setSelf(selfLink);
204 vnfInstance.setLinks(VnfInstancelinks);
206 final Map<String, String> vnfConfigurableProperties = new HashMap<>();
207 vnfConfigurableProperties.put("vnfIpAddress", "10.10.10.10");
208 vnfInstance.setVnfConfigurableProperties(vnfConfigurableProperties);
210 final List<InlineResponse201VimConnectionInfo> vimConnectionInfo = new ArrayList<>();;
211 final InlineResponse201VimConnectionInfo vimConnection = new InlineResponse201VimConnectionInfo();
212 vimConnection.setVimId(CLOUD_OWNER + "_" + REGION);
213 vimConnection.setId(CLOUD_OWNER + "_" + REGION);
214 final AccessInfo accessInfo = new AccessInfo();
215 accessInfo.setProjectId(TENANT_ID);
216 vimConnection.setAccessInfo(accessInfo);
217 vimConnectionInfo.add(vimConnection);
218 vnfInstance.setVimConnectionInfo(vimConnectionInfo);
222 private GenericVnf createGenericVnf(final String type) {
223 final GenericVnf genericVnf = new GenericVnf();
224 genericVnf.setVnfId("myTestVnfId");
225 genericVnf.setNfType(type);
229 private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
231 final String uriAsString;
233 public AaiResourceUriMatcher(final String uriAsString) {
234 this.uriAsString = uriAsString;
238 public boolean matches(final Object item) {
239 if (item instanceof AAIResourceUri) {
240 if (uriAsString.endsWith("...")) {
241 return ((AAIResourceUri) item).build().toString()
242 .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
244 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
250 public void describeTo(final Description description) {}