Check for existing VNF in VNFM
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / test / java / org / onap / so / adapters / vnfmadapter / rest / VnfmAdapterControllerTest.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.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;
32 import java.net.URI;
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;
73
74
75 @RunWith(SpringRunner.class)
76 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
77 @ActiveProfiles("test")
78
79 public class VnfmAdapterControllerTest {
80
81     @LocalServerPort
82     private int port;
83     @Autowired
84     @Qualifier(CONFIGURABLE_REST_TEMPLATE)
85     private RestTemplate testRestTemplate;
86     private MockRestServiceServer mockRestServer;
87
88     @MockBean
89     AAIResourcesClient aaiResourcesClient;
90
91     @Autowired
92     VnfmAdapterController controller;
93     Gson gson = new Gson();
94
95     @Before
96     public void setUp() throws Exception {
97         mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
98     }
99
100     @Test
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);
105
106         final GenericVnf genericVnf = new GenericVnf();
107         genericVnf.setVnfId("myTestVnfId");
108         genericVnf.setNfType("vnfmType2");
109
110         doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class), MockitoHamcrest
111                 .argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId?depth=1")));
112
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);
119
120         final EsrVnfm esrVnfm1 = new EsrVnfm();
121         esrVnfm1.setVnfmId("vnfm1");
122         esrVnfm1.setEsrSystemInfoList(esrSystemInfoList1);
123         esrVnfm1.setResourceVersion("1234");
124
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);
131
132         final EsrVnfm esrVnfm2 = new EsrVnfm();
133         esrVnfm2.setVnfmId("vnfm2");
134         esrVnfm2.setEsrSystemInfoList(esrSystemInfoList2);
135         esrVnfm2.setResourceVersion("1234");
136
137         final EsrVnfmList esrVnfmList = new EsrVnfmList();
138         esrVnfmList.getEsrVnfm().add(esrVnfm1);
139         esrVnfmList.getEsrVnfm().add(esrVnfm2);
140
141         doReturn(Optional.of(esrVnfmList)).when(aaiResourcesClient).get(eq(EsrVnfmList.class),
142                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list")));
143
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")));
147
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")));
151
152
153         final ResponseEntity<CreateVnfResponse> response =
154                 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
155         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
156         assertNotNull(response.getBody().getJobId());
157
158         final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
159         final ArgumentCaptor<AAIResourceUri> uriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
160
161         verify(aaiResourcesClient).update(uriArgument.capture(), genericVnfArgument.capture());
162
163         assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", uriArgument.getValue().build().toString());
164
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());
172     }
173
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);
179
180         final GenericVnf genericVnf = new GenericVnf();
181         genericVnf.setVnfId("myTestVnfId");
182         genericVnf.setNfType("vnfmType");
183         genericVnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
184
185         doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class), MockitoHamcrest
186                 .argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId?depth=1")));
187
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);
194
195         final EsrVnfm esrVnfm = new EsrVnfm();
196         esrVnfm.setVnfmId("vnfm");
197         esrVnfm.setEsrSystemInfoList(esrSystemInfoList);
198         esrVnfm.setResourceVersion("1234");
199
200         final EsrVnfmList esrVnfmList = new EsrVnfmList();
201         esrVnfmList.getEsrVnfm().add(esrVnfm);
202
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));
206
207         doReturn(Optional.of(esrVnfmList)).when(aaiResourcesClient).get(eq(EsrVnfmList.class),
208                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list")));
209
210         controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
211     }
212
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);
218
219         final GenericVnf genericVnf = new GenericVnf();
220         genericVnf.setVnfId("myTestVnfId");
221         genericVnf.setNfType("anotherType");
222
223         doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class), MockitoHamcrest
224                 .argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId?depth=1")));
225
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);
232
233         final EsrVnfm esrVnfm1 = new EsrVnfm();
234         esrVnfm1.setVnfmId("vnfm1");
235         esrVnfm1.setEsrSystemInfoList(esrSystemInfoList1);
236         esrVnfm1.setResourceVersion("1234");
237
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);
244
245         final EsrVnfm esrVnfm2 = new EsrVnfm();
246         esrVnfm2.setVnfmId("vnfm2");
247         esrVnfm2.setEsrSystemInfoList(esrSystemInfoList2);
248         esrVnfm2.setResourceVersion("1234");
249
250         final EsrVnfmList esrVnfmList = new EsrVnfmList();
251         esrVnfmList.getEsrVnfm().add(esrVnfm1);
252         esrVnfmList.getEsrVnfm().add(esrVnfm2);
253
254         doReturn(Optional.of(esrVnfmList)).when(aaiResourcesClient).get(eq(EsrVnfmList.class),
255                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list")));
256
257
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")));
261
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")));
265
266         controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
267     }
268
269     @Test
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);
274
275         final GenericVnf genericVnf = new GenericVnf();
276         genericVnf.setVnfId("myTestVnfId");
277         genericVnf.setNfType("vnfmType2");
278
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);
286
287         final RelationshipList relationshipList = new RelationshipList();
288         relationshipList.getRelationship().add(relationshipToVnfm);
289         genericVnf.setRelationshipList(relationshipList);
290
291         doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class), MockitoHamcrest
292                 .argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId?depth=1")));
293
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);
300
301         final EsrVnfm esrVnfm1 = new EsrVnfm();
302         esrVnfm1.setVnfmId("vnfm1");
303         esrVnfm1.setEsrSystemInfoList(esrSystemInfoList1);
304         esrVnfm1.setResourceVersion("1234");
305
306         doReturn(Optional.of(esrVnfm1)).when(aaiResourcesClient).get(eq(EsrVnfm.class),
307                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/vnfm1")));
308
309         final ResponseEntity<CreateVnfResponse> response =
310                 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
311         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
312         assertNotNull(response.getBody().getJobId());
313     }
314
315     @Test
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);
321
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());
330     }
331
332     @Test
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());
342     }
343
344     private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
345
346         final String uriAsString;
347
348         public AaiResourceUriMatcher(final String uriAsString) {
349             this.uriAsString = uriAsString;
350         }
351
352         @Override
353         public boolean matches(final Object item) {
354             if (item instanceof AAIResourceUri) {
355                 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
356             }
357             return false;
358         }
359
360         @Override
361         public void describeTo(final Description description) {}
362
363     }
364
365
366 }