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.junit.Assert.assertNotNull;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.doReturn;
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.util.Optional;
34 import org.hamcrest.BaseMatcher;
35 import org.hamcrest.Description;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.ArgumentCaptor;
40 import org.mockito.hamcrest.MockitoHamcrest;
41 import org.onap.aai.domain.yang.EsrSystemInfo;
42 import org.onap.aai.domain.yang.EsrSystemInfoList;
43 import org.onap.aai.domain.yang.EsrVnfm;
44 import org.onap.aai.domain.yang.EsrVnfmList;
45 import org.onap.aai.domain.yang.GenericVnf;
46 import org.onap.aai.domain.yang.Relationship;
47 import org.onap.aai.domain.yang.RelationshipData;
48 import org.onap.aai.domain.yang.RelationshipList;
49 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
50 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
51 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
52 import org.onap.so.client.aai.AAIResourcesClient;
53 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
54 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
55 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
56 import org.onap.vnfmadapter.v1.model.DeleteVnfResponse;
57 import org.onap.vnfmadapter.v1.model.Tenant;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.beans.factory.annotation.Qualifier;
60 import org.springframework.boot.test.context.SpringBootTest;
61 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
62 import org.springframework.boot.test.mock.mockito.MockBean;
63 import org.springframework.boot.test.web.client.TestRestTemplate;
64 import org.springframework.boot.web.server.LocalServerPort;
65 import org.springframework.http.HttpStatus;
66 import org.springframework.http.MediaType;
67 import org.springframework.http.RequestEntity;
68 import org.springframework.http.ResponseEntity;
69 import org.springframework.test.context.ActiveProfiles;
70 import org.springframework.test.context.junit4.SpringRunner;
71 import org.springframework.test.web.client.MockRestServiceServer;
72 import org.springframework.web.client.RestTemplate;
75 @RunWith(SpringRunner.class)
76 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
77 @ActiveProfiles("test")
79 public class VnfmAdapterControllerTest {
84 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
85 private RestTemplate testRestTemplate;
86 private MockRestServiceServer mockRestServer;
89 AAIResourcesClient aaiResourcesClient;
92 VnfmAdapterController controller;
93 Gson gson = new Gson();
96 public void setUp() throws Exception {
97 mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
101 public void createVnf_ValidRequest_Returns202AndJobId() throws Exception {
102 final Tenant tenant =
103 new Tenant().cloudOwner("myTestCloudOwner").regionName("myTestRegion").tenantId("myTestTenantId");
104 final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
106 final GenericVnf genericVnf = new GenericVnf();
107 genericVnf.setVnfId("myTestVnfId");
108 genericVnf.setNfType("vnfmType2");
110 doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class), MockitoHamcrest
111 .argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId?depth=1")));
113 final EsrSystemInfo esrSystemInfo1 = new EsrSystemInfo();
114 esrSystemInfo1.setServiceUrl("http://vnfm1:8080");
115 esrSystemInfo1.setType("vnfmType1");
116 esrSystemInfo1.setSystemType("VNFM");
117 final EsrSystemInfoList esrSystemInfoList1 = new EsrSystemInfoList();
118 esrSystemInfoList1.getEsrSystemInfo().add(esrSystemInfo1);
120 final EsrVnfm esrVnfm1 = new EsrVnfm();
121 esrVnfm1.setVnfmId("vnfm1");
122 esrVnfm1.setEsrSystemInfoList(esrSystemInfoList1);
123 esrVnfm1.setResourceVersion("1234");
125 final EsrSystemInfo esrSystemInfo2 = new EsrSystemInfo();
126 esrSystemInfo2.setServiceUrl("http://vnfm2:8080");
127 esrSystemInfo2.setType("vnfmType2");
128 esrSystemInfo2.setSystemType("VNFM");
129 final EsrSystemInfoList esrSystemInfoList2 = new EsrSystemInfoList();
130 esrSystemInfoList2.getEsrSystemInfo().add(esrSystemInfo2);
132 final EsrVnfm esrVnfm2 = new EsrVnfm();
133 esrVnfm2.setVnfmId("vnfm2");
134 esrVnfm2.setEsrSystemInfoList(esrSystemInfoList2);
135 esrVnfm2.setResourceVersion("1234");
137 final EsrVnfmList esrVnfmList = new EsrVnfmList();
138 esrVnfmList.getEsrVnfm().add(esrVnfm1);
139 esrVnfmList.getEsrVnfm().add(esrVnfm2);
141 doReturn(Optional.of(esrVnfmList)).when(aaiResourcesClient).get(eq(EsrVnfmList.class),
142 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list")));
144 doReturn(Optional.of(esrSystemInfoList1)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
145 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
146 "/external-system/esr-vnfm-list/esr-vnfm/vnfm1/esr-system-info-list")));
148 doReturn(Optional.of(esrSystemInfoList2)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
149 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
150 "/external-system/esr-vnfm-list/esr-vnfm/vnfm2/esr-system-info-list")));
153 final ResponseEntity<CreateVnfResponse> response =
154 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
155 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
156 assertNotNull(response.getBody().getJobId());
158 final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
159 final ArgumentCaptor<AAIResourceUri> uriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
161 verify(aaiResourcesClient).update(uriArgument.capture(), genericVnfArgument.capture());
163 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", uriArgument.getValue().build().toString());
165 assertEquals("myTestVnfId", genericVnfArgument.getValue().getVnfId());
166 assertEquals(1, genericVnfArgument.getValue().getRelationshipList().getRelationship().size());
167 final Relationship createdRelationship =
168 genericVnfArgument.getValue().getRelationshipList().getRelationship().get(0);
169 assertEquals("esr-vnfm", createdRelationship.getRelatedTo());
170 assertEquals("tosca.relationships.DependsOn", createdRelationship.getRelationshipLabel());
171 assertEquals("/aai/v15/external-system/esr-vnfm-list/esr-vnfm/vnfm2", createdRelationship.getRelatedLink());
174 @Test(expected = IllegalArgumentException.class)
175 public void createVnf_VnfAlreadyExistsOnVnfm_ThrowsIllegalArgumentException() throws Exception {
176 final Tenant tenant =
177 new Tenant().cloudOwner("myTestCloudOwner").regionName("myTestRegion").tenantId("myTestTenantId");
178 final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
180 final GenericVnf genericVnf = new GenericVnf();
181 genericVnf.setVnfId("myTestVnfId");
182 genericVnf.setNfType("vnfmType");
183 genericVnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
185 doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class), MockitoHamcrest
186 .argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId?depth=1")));
188 final EsrSystemInfo esrSystemInfo = new EsrSystemInfo();
189 esrSystemInfo.setServiceUrl("http://vnfm:8080");
190 esrSystemInfo.setType("vnfmType");
191 esrSystemInfo.setSystemType("VNFM");
192 final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
193 esrSystemInfoList.getEsrSystemInfo().add(esrSystemInfo);
195 final EsrVnfm esrVnfm = new EsrVnfm();
196 esrVnfm.setVnfmId("vnfm");
197 esrVnfm.setEsrSystemInfoList(esrSystemInfoList);
198 esrVnfm.setResourceVersion("1234");
200 final EsrVnfmList esrVnfmList = new EsrVnfmList();
201 esrVnfmList.getEsrVnfm().add(esrVnfm);
203 final InlineResponse201 reponse = new InlineResponse201();
204 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
205 .andRespond(withSuccess(gson.toJson(reponse), MediaType.APPLICATION_JSON));
207 doReturn(Optional.of(esrVnfmList)).when(aaiResourcesClient).get(eq(EsrVnfmList.class),
208 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list")));
210 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
213 @Test(expected = VnfmNotFoundException.class)
214 public void createVnf_NoMatchingVnfmFound_ThrowsException() throws Exception {
215 final Tenant tenant =
216 new Tenant().cloudOwner("myTestCloudOwner").regionName("myTestRegion").tenantId("myTestTenantId");
217 final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
219 final GenericVnf genericVnf = new GenericVnf();
220 genericVnf.setVnfId("myTestVnfId");
221 genericVnf.setNfType("anotherType");
223 doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class), MockitoHamcrest
224 .argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId?depth=1")));
226 final EsrSystemInfo esrSystemInfo1 = new EsrSystemInfo();
227 esrSystemInfo1.setServiceUrl("http://vnfm1:8080");
228 esrSystemInfo1.setType("vnfmType1");
229 esrSystemInfo1.setSystemType("VNFM");
230 final EsrSystemInfoList esrSystemInfoList1 = new EsrSystemInfoList();
231 esrSystemInfoList1.getEsrSystemInfo().add(esrSystemInfo1);
233 final EsrVnfm esrVnfm1 = new EsrVnfm();
234 esrVnfm1.setVnfmId("vnfm1");
235 esrVnfm1.setEsrSystemInfoList(esrSystemInfoList1);
236 esrVnfm1.setResourceVersion("1234");
238 final EsrSystemInfo esrSystemInfo2 = new EsrSystemInfo();
239 esrSystemInfo2.setServiceUrl("http://vnfm2:8080");
240 esrSystemInfo2.setType("vnfmType2");
241 esrSystemInfo2.setSystemType("VNFM");
242 final EsrSystemInfoList esrSystemInfoList2 = new EsrSystemInfoList();
243 esrSystemInfoList2.getEsrSystemInfo().add(esrSystemInfo2);
245 final EsrVnfm esrVnfm2 = new EsrVnfm();
246 esrVnfm2.setVnfmId("vnfm2");
247 esrVnfm2.setEsrSystemInfoList(esrSystemInfoList2);
248 esrVnfm2.setResourceVersion("1234");
250 final EsrVnfmList esrVnfmList = new EsrVnfmList();
251 esrVnfmList.getEsrVnfm().add(esrVnfm1);
252 esrVnfmList.getEsrVnfm().add(esrVnfm2);
254 doReturn(Optional.of(esrVnfmList)).when(aaiResourcesClient).get(eq(EsrVnfmList.class),
255 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list")));
258 doReturn(Optional.of(esrSystemInfoList1)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
259 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
260 "/external-system/esr-vnfm-list/esr-vnfm/vnfm1/esr-system-info-list")));
262 doReturn(Optional.of(esrSystemInfoList2)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
263 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
264 "/external-system/esr-vnfm-list/esr-vnfm/vnfm2/esr-system-info-list")));
266 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
270 public void createVnf_VnfmAlreadyAssociatedWithVnf_Returns202AndJobId() throws Exception {
271 final Tenant tenant =
272 new Tenant().cloudOwner("myTestCloudOwner").regionName("myTestRegion").tenantId("myTestTenantId");
273 final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
275 final GenericVnf genericVnf = new GenericVnf();
276 genericVnf.setVnfId("myTestVnfId");
277 genericVnf.setNfType("vnfmType2");
279 final Relationship relationshipToVnfm = new Relationship();
280 relationshipToVnfm.setRelatedLink("/aai/v15/external-system/esr-vnfm-list/esr-vnfm/vnfm1");
281 relationshipToVnfm.setRelatedTo("esr-vnfm");
282 final RelationshipData relationshipData = new RelationshipData();
283 relationshipData.setRelationshipKey("esr-vnfm.vnfm-id");
284 relationshipData.setRelationshipValue("vnfm1");
285 relationshipToVnfm.getRelationshipData().add(relationshipData);
287 final RelationshipList relationshipList = new RelationshipList();
288 relationshipList.getRelationship().add(relationshipToVnfm);
289 genericVnf.setRelationshipList(relationshipList);
291 doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class), MockitoHamcrest
292 .argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId?depth=1")));
294 final EsrSystemInfo esrSystemInfo1 = new EsrSystemInfo();
295 esrSystemInfo1.setServiceUrl("http://vnfm1:8080");
296 esrSystemInfo1.setType("vnfmType1");
297 esrSystemInfo1.setSystemType("VNFM");
298 final EsrSystemInfoList esrSystemInfoList1 = new EsrSystemInfoList();
299 esrSystemInfoList1.getEsrSystemInfo().add(esrSystemInfo1);
301 final EsrVnfm esrVnfm1 = new EsrVnfm();
302 esrVnfm1.setVnfmId("vnfm1");
303 esrVnfm1.setEsrSystemInfoList(esrSystemInfoList1);
304 esrVnfm1.setResourceVersion("1234");
306 doReturn(Optional.of(esrVnfm1)).when(aaiResourcesClient).get(eq(EsrVnfm.class),
307 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/vnfm1")));
309 final ResponseEntity<CreateVnfResponse> response =
310 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
311 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
312 assertNotNull(response.getBody().getJobId());
316 public void createVnf_UnauthorizedUser_Returns401() throws Exception {
317 final TestRestTemplate restTemplateWrongPassword = new TestRestTemplate("test", "wrongPassword");
318 final Tenant tenant =
319 new Tenant().cloudOwner("myTestCloudOwner").regionName("myTestRegion").tenantId("myTestTenantId");
320 final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
322 final RequestEntity<CreateVnfRequest> request =
323 RequestEntity.post(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myVnfId"))
324 .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
325 .header("X-ONAP-RequestId", "myRequestId").header("X-ONAP-InvocationID", "myInvocationId")
326 .body(createVnfRequest);
327 final ResponseEntity<CreateVnfResponse> response =
328 restTemplateWrongPassword.exchange(request, CreateVnfResponse.class);
329 assertEquals(401, response.getStatusCode().value());
333 public void deleteVnf_ValidRequest_Returns202AndJobId() throws Exception {
334 final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
335 final RequestEntity<Void> request = RequestEntity
336 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myVnfId"))
337 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
338 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
339 final ResponseEntity<DeleteVnfResponse> response = restTemplate.exchange(request, DeleteVnfResponse.class);
340 assertEquals(202, response.getStatusCode().value());
341 assertNotNull(response.getBody().getJobId());
344 private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
346 final String uriAsString;
348 public AaiResourceUriMatcher(final String uriAsString) {
349 this.uriAsString = uriAsString;
353 public boolean matches(final Object item) {
354 if (item instanceof AAIResourceUri) {
355 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
361 public void describeTo(final Description description) {}