CLEANED UP IMPORTS IN MSO-VFNM-ETSI Adapter
[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 com.google.gson.Gson;
24 import org.hamcrest.BaseMatcher;
25 import org.hamcrest.Description;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.hamcrest.MockitoHamcrest;
31 import org.onap.aai.domain.yang.EsrSystemInfo;
32 import org.onap.aai.domain.yang.EsrSystemInfoList;
33 import org.onap.aai.domain.yang.EsrVnfm;
34 import org.onap.aai.domain.yang.EsrVnfmList;
35 import org.onap.aai.domain.yang.GenericVnf;
36 import org.onap.aai.domain.yang.Relationship;
37 import org.onap.aai.domain.yang.RelationshipData;
38 import org.onap.aai.domain.yang.RelationshipList;
39 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201Links;
40 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201LinksSelf;
41 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
42 import org.onap.vnfmadapter.v1.model.DeleteVnfResponse;
43 import org.onap.vnfmadapter.v1.model.OperationEnum;
44 import org.onap.vnfmadapter.v1.model.OperationStateEnum;
45 import org.onap.vnfmadapter.v1.model.QueryJobResponse;
46 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
47 import org.onap.so.adapters.vnfmadapter.extclients.SdcPackageProvider;
48 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.*;
49 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
50 import org.onap.so.client.aai.AAIResourcesClient;
51 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
52 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
53 import org.onap.vnfmadapter.v1.model.Tenant;
54 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
55 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse2001;
56 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.beans.factory.annotation.Qualifier;
59 import org.springframework.boot.test.context.SpringBootTest;
60 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
61 import org.springframework.boot.test.mock.mockito.MockBean;
62 import org.springframework.boot.test.web.client.TestRestTemplate;
63 import org.springframework.boot.web.server.LocalServerPort;
64 import org.springframework.http.HttpStatus;
65 import org.springframework.http.MediaType;
66 import org.springframework.http.RequestEntity;
67 import org.springframework.http.ResponseEntity;
68 import org.springframework.test.context.ActiveProfiles;
69 import org.springframework.test.context.junit4.SpringRunner;
70 import org.springframework.test.web.client.MockRestServiceServer;
71 import org.springframework.web.client.RestTemplate;
72 import org.threeten.bp.LocalDateTime;
73 import org.threeten.bp.OffsetDateTime;
74 import org.threeten.bp.ZoneOffset;
75 import java.net.URI;
76 import java.util.Optional;
77 import static org.junit.Assert.assertEquals;
78 import static org.junit.Assert.assertNotNull;
79 import static org.junit.Assert.assertNull;
80 import static org.mockito.ArgumentMatchers.eq;
81 import static org.mockito.Mockito.doReturn;
82 import static org.mockito.Mockito.verify;
83 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
84 import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
85 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
86 import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
87
88 @RunWith(SpringRunner.class)
89 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
90 @ActiveProfiles("test")
91
92 public class VnfmAdapterControllerTest {
93
94     private static final OffsetDateTime JAN_1_2019_12_00 =
95             OffsetDateTime.of(LocalDateTime.of(2019, 1, 1, 12, 0), ZoneOffset.UTC);
96     private static final OffsetDateTime JAN_1_2019_1_00 =
97             OffsetDateTime.of(LocalDateTime.of(2019, 1, 1, 1, 0), ZoneOffset.UTC);
98     private static final String CLOUD_OWNER = "myTestCloudOwner";
99     private static final String REGION = "myTestRegion";
100     private static final String TENANT_ID = "myTestTenantId";
101
102     @LocalServerPort
103     private int port;
104     @Autowired
105     @Qualifier(CONFIGURABLE_REST_TEMPLATE)
106     private RestTemplate testRestTemplate;
107     private MockRestServiceServer mockRestServer;
108
109     @MockBean
110     AAIResourcesClient aaiResourcesClient;
111
112     @MockBean
113     SdcPackageProvider sdcPackageProvider;
114
115     @Autowired
116     VnfmAdapterController controller;
117     Gson gson = new Gson();
118
119     @Before
120     public void setUp() throws Exception {
121         mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
122     }
123
124     @Test
125     public void createVnf_ValidRequest_Returns202AndJobId() throws Exception {
126         final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
127         final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
128
129         setUpGenericVnfInMockAai("vnfmType2");
130         setUpVnfmsInMockAai();
131         setUpVimInMockAai();
132
133         final String expectedsubscriptionRequest =
134                 "{\"filter\":{\"vnfInstanceSubscriptionFilter\":{\"vnfInstanceIds\":[\"vnfId\"]},\"notificationTypes\":[\"VnfLcmOperationOccurrenceNotification\"]},\"callbackUri\":\"https://so-vnfm-adapter.onap:30406/so/vnfm-adapter/v1/lcn/VnfLcmOperationOccurrenceNotification\",\"authentication\":{\"authType\":[\"BASIC\"],\"paramsBasic\":{\"userName\":\"vnfm\",\"password\":\"$2a$10$Fh9ffgPw2vnmsghsRD3ZauBL1aKXebigbq3BB1RPWtE62UDILsjke\"}}}";
135         final InlineResponse2001 subscriptionResponse = new InlineResponse2001();
136
137         final InlineResponse201 createResponse = createCreateResponse();
138         mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_instances"))
139                 .andRespond(withSuccess(gson.toJson(createResponse), MediaType.APPLICATION_JSON));
140
141         mockRestServer.expect(requestTo("http://vnfm2:8080/subscriptions"))
142                 .andExpect(content().json(expectedsubscriptionRequest))
143                 .andRespond(withSuccess(gson.toJson(subscriptionResponse), MediaType.APPLICATION_JSON));
144
145         mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_instances/vnfId/instantiate"))
146                 .andRespond(withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
147                         .location(new URI("http://vnfm2:8080/vnf_lcm_op_occs/123456")));
148
149         final InlineResponse200 firstOperationQueryResponse = createOperationQueryResponse(
150                 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.INSTANTIATE,
151                 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.PROCESSING);
152         mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_lcm_op_occs/123456"))
153                 .andRespond(withSuccess(gson.toJson(firstOperationQueryResponse), MediaType.APPLICATION_JSON));
154
155         final InlineResponse200 secondOperationQueryReponse = createOperationQueryResponse(
156                 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.INSTANTIATE,
157                 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.COMPLETED);
158         mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_lcm_op_occs/123456"))
159                 .andRespond(withSuccess(gson.toJson(secondOperationQueryReponse), MediaType.APPLICATION_JSON));
160
161         // Invoke the create request
162
163         final ResponseEntity<CreateVnfResponse> createVnfResponse =
164                 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
165         assertEquals(HttpStatus.ACCEPTED, createVnfResponse.getStatusCode());
166         assertNotNull(createVnfResponse.getBody().getJobId());
167
168         final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
169         final ArgumentCaptor<AAIResourceUri> uriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
170
171         verify(aaiResourcesClient).update(uriArgument.capture(), genericVnfArgument.capture());
172
173         assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", uriArgument.getValue().build().toString());
174
175         assertEquals("myTestVnfId", genericVnfArgument.getValue().getVnfId());
176         assertEquals(2, genericVnfArgument.getValue().getRelationshipList().getRelationship().size());
177         final Relationship vnfmRelationship =
178                 genericVnfArgument.getValue().getRelationshipList().getRelationship().get(0);
179         assertEquals("esr-vnfm", vnfmRelationship.getRelatedTo());
180         assertEquals("tosca.relationships.DependsOn", vnfmRelationship.getRelationshipLabel());
181         assertEquals("/aai/v15/external-system/esr-vnfm-list/esr-vnfm/vnfm2", vnfmRelationship.getRelatedLink());
182
183         final Relationship tenantRelationship =
184                 genericVnfArgument.getValue().getRelationshipList().getRelationship().get(1);
185         assertEquals("tenant", tenantRelationship.getRelatedTo());
186         assertEquals(
187                 "/aai/v15/cloud-infrastructure/cloud-regions/cloud-region/myTestCloudOwner/myTestRegion/tenants/tenant/myTestTenantId",
188                 tenantRelationship.getRelatedLink());
189
190
191         // check the job status
192
193         final ResponseEntity<QueryJobResponse> firstJobQueryResponse =
194                 controller.jobQuery(createVnfResponse.getBody().getJobId(), "", "so", "1213");
195         assertEquals(OperationEnum.INSTANTIATE, firstJobQueryResponse.getBody().getOperation());
196         assertEquals(OperationStateEnum.PROCESSING, firstJobQueryResponse.getBody().getOperationState());
197         assertEquals(JAN_1_2019_12_00, firstJobQueryResponse.getBody().getStartTime());
198         assertEquals(JAN_1_2019_1_00, firstJobQueryResponse.getBody().getStateEnteredTime());
199
200         final ResponseEntity<QueryJobResponse> secondJobQueryResponse =
201                 controller.jobQuery(createVnfResponse.getBody().getJobId(), "", "so", "1213");
202         assertEquals(OperationEnum.INSTANTIATE, secondJobQueryResponse.getBody().getOperation());
203         assertEquals(OperationStateEnum.COMPLETED, secondJobQueryResponse.getBody().getOperationState());
204         assertEquals(JAN_1_2019_12_00, secondJobQueryResponse.getBody().getStartTime());
205         assertEquals(JAN_1_2019_1_00, secondJobQueryResponse.getBody().getStateEnteredTime());
206     }
207
208     @Test(expected = IllegalArgumentException.class)
209     public void createVnf_VnfAlreadyExistsOnVnfm_ThrowsIllegalArgumentException() throws Exception {
210         final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
211         final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
212
213         final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType1");
214         addSelfLinkToGenericVnf(genericVnf);
215
216         final InlineResponse201 reponse = new InlineResponse201();
217         mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
218                 .andRespond(withSuccess(gson.toJson(reponse), MediaType.APPLICATION_JSON));
219
220         controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
221     }
222
223     @Test(expected = VnfmNotFoundException.class)
224     public void createVnf_NoMatchingVnfmFound_ThrowsException() throws Exception {
225         final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
226         final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
227
228         setUpGenericVnfInMockAai("anotherType");
229         setUpVnfmsInMockAai();
230
231         controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
232     }
233
234     @Test
235     public void createVnf_VnfmAlreadyAssociatedWithVnf_Returns202AndJobId() throws Exception {
236         final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
237         final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
238
239         final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType2");
240         addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm1");
241         setUpVnfmsInMockAai();
242         setUpVimInMockAai();
243
244         final InlineResponse201 createResponse = createCreateResponse();
245         mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_instances"))
246                 .andRespond(withSuccess(gson.toJson(createResponse), MediaType.APPLICATION_JSON));
247
248         mockRestServer.expect(requestTo("http://vnfm2:8080/subscriptions")).andRespond(withBadRequest());
249
250         mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_instances/vnfId/instantiate"))
251                 .andRespond(withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
252                         .location(new URI("http://vnfm2:8080/vnf_lcm_op_occs/123456")));
253
254         final ResponseEntity<CreateVnfResponse> response =
255                 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
256         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
257         assertNotNull(response.getBody().getJobId());
258     }
259
260     @Test
261     public void createVnf_UnauthorizedUser_Returns401() throws Exception {
262         final TestRestTemplate restTemplateWrongPassword = new TestRestTemplate("test", "wrongPassword");
263         final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
264         final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
265
266         final RequestEntity<CreateVnfRequest> request =
267                 RequestEntity.post(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myVnfId"))
268                         .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
269                         .header("X-ONAP-RequestId", "myRequestId").header("X-ONAP-InvocationID", "myInvocationId")
270                         .body(createVnfRequest);
271         final ResponseEntity<CreateVnfResponse> response =
272                 restTemplateWrongPassword.exchange(request, CreateVnfResponse.class);
273         assertEquals(401, response.getStatusCode().value());
274     }
275
276     @Test
277     public void deleteVnf_ValidRequest_Returns202AndJobId() throws Exception {
278         final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
279
280         final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType");
281         addSelfLinkToGenericVnf(genericVnf);
282         addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm");
283
284         mockRestServer.expect(requestTo("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm/terminate"))
285                 .andRespond(withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
286                         .location(new URI("http://vnfm2:8080/vnf_lcm_op_occs/1234567")));
287
288         final InlineResponse200 firstOperationQueryResponse = createOperationQueryResponse(
289                 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.TERMINATE,
290                 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.PROCESSING);
291         mockRestServer.expect(requestTo("http://vnfm:8080/vnf_lcm_op_occs/1234567"))
292                 .andRespond(withSuccess(gson.toJson(firstOperationQueryResponse), MediaType.APPLICATION_JSON));
293
294         final InlineResponse200 secondOperationQueryReponse = createOperationQueryResponse(
295                 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.TERMINATE,
296                 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.COMPLETED);
297         mockRestServer.expect(requestTo("http://vnfm:8080/vnf_lcm_op_occs/1234567"))
298                 .andRespond(withSuccess(gson.toJson(secondOperationQueryReponse), MediaType.APPLICATION_JSON));
299
300         final RequestEntity<Void> request = RequestEntity
301                 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myTestVnfId"))
302                 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
303                 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
304         final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
305                 restTemplate.exchange(request, DeleteVnfResponse.class);
306         assertEquals(202, deleteVnfResponse.getStatusCode().value());
307         assertNotNull(deleteVnfResponse.getBody().getJobId());
308
309         final EsrSystemInfo esrSystemInfo = new EsrSystemInfo();
310         esrSystemInfo.setServiceUrl("http://vnfm:8080");
311         esrSystemInfo.setType("vnfmType");
312         esrSystemInfo.setSystemType("VNFM");
313         final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
314         esrSystemInfoList.getEsrSystemInfo().add(esrSystemInfo);
315
316         doReturn(Optional.of(esrSystemInfoList)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
317                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/...")));
318
319         final ResponseEntity<QueryJobResponse> firstJobQueryResponse =
320                 controller.jobQuery(deleteVnfResponse.getBody().getJobId(), "", "so", "1213");
321         assertEquals(OperationEnum.TERMINATE, firstJobQueryResponse.getBody().getOperation());
322         assertEquals(OperationStateEnum.PROCESSING, firstJobQueryResponse.getBody().getOperationState());
323         assertEquals(JAN_1_2019_12_00, firstJobQueryResponse.getBody().getStartTime());
324         assertEquals(JAN_1_2019_1_00, firstJobQueryResponse.getBody().getStateEnteredTime());
325
326         final ResponseEntity<QueryJobResponse> secondJobQueryResponse =
327                 controller.jobQuery(deleteVnfResponse.getBody().getJobId(), "", "so", "1213");
328         assertEquals(OperationEnum.TERMINATE, secondJobQueryResponse.getBody().getOperation());
329         assertEquals(OperationStateEnum.PROCESSING, secondJobQueryResponse.getBody().getOperationState());
330         assertEquals(JAN_1_2019_12_00, secondJobQueryResponse.getBody().getStartTime());
331         assertEquals(JAN_1_2019_1_00, secondJobQueryResponse.getBody().getStateEnteredTime());
332     }
333
334     @Test
335     public void deleteVnf_GenericVnfNotFound_Returns404() throws Exception {
336         final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
337
338         final RequestEntity<Void> request = RequestEntity
339                 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myNonExistingVnfId"))
340                 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
341                 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
342         final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
343                 restTemplate.exchange(request, DeleteVnfResponse.class);
344         assertEquals(404, deleteVnfResponse.getStatusCode().value());
345         assertNull(deleteVnfResponse.getBody().getJobId());
346     }
347
348     @Test
349     public void deleteVnf_NoAssignedVnfm_Returns400() throws Exception {
350         final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
351
352         setUpGenericVnfInMockAai("vnfmType");
353
354         final RequestEntity<Void> request = RequestEntity
355                 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myTestVnfId"))
356                 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
357                 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
358         final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
359                 restTemplate.exchange(request, DeleteVnfResponse.class);
360         assertEquals(400, deleteVnfResponse.getStatusCode().value());
361         assertNull(deleteVnfResponse.getBody().getJobId());
362     }
363
364     @Test
365     public void deleteVnf_ErrorStatusCodeFromVnfm_Returns500() throws Exception {
366         final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
367
368         final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType");
369         addSelfLinkToGenericVnf(genericVnf);
370         addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm");
371
372         mockRestServer.expect(requestTo("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm/terminate"))
373                 .andRespond(withStatus(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON));
374
375         final RequestEntity<Void> request = RequestEntity
376                 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myTestVnfId"))
377                 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
378                 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
379         final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
380                 restTemplate.exchange(request, DeleteVnfResponse.class);
381         assertEquals(500, deleteVnfResponse.getStatusCode().value());
382         assertNull(deleteVnfResponse.getBody().getJobId());
383
384     }
385
386     private InlineResponse200 createOperationQueryResponse(
387             final org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum operation,
388             final org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum operationState) {
389         final InlineResponse200 response = new InlineResponse200();
390         response.setId("9876");
391         response.setOperation(operation);
392         response.setOperationState(operationState);
393         response.setStartTime(JAN_1_2019_12_00);
394         response.setStateEnteredTime(JAN_1_2019_1_00);
395         response.setVnfInstanceId("myVnfInstanceId");
396         return response;
397     }
398
399     private GenericVnf createGenericVnf(final String type) {
400         final GenericVnf genericVnf = new GenericVnf();
401         genericVnf.setVnfId("myTestVnfId");
402         genericVnf.setNfType(type);
403         return genericVnf;
404     }
405
406     private GenericVnf setUpGenericVnfInMockAai(final String type) {
407         final GenericVnf genericVnf = createGenericVnf(type);
408
409         doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class),
410                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId")));
411         return genericVnf;
412     }
413
414     private void addSelfLinkToGenericVnf(final GenericVnf vnf) {
415         vnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
416     }
417
418     private void addRelationshipFromGenericVnfToVnfm(final GenericVnf genericVnf, final String vnfmId) {
419         final Relationship relationshipToVnfm = new Relationship();
420         relationshipToVnfm.setRelatedLink(
421                 "/aai/v15/external-system/esr-vnfm-li//        final InlineResponse201 vnfInstance = new InlineResponse201();\n"
422                         + "//        vnfInstance.setInstantiationState(InstantiationStateEnum.NOT_INSTANTIATED);\n"
423                         + "//        mockRestServer.expect(requestTo(\"http://dummy.value/until/create/implememted/vnfId\"))\n"
424                         + "//                .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));st/esr-vnfm/"
425                         + vnfmId);
426         relationshipToVnfm.setRelatedTo("esr-vnfm");
427         final RelationshipData relationshipData = new RelationshipData();
428         relationshipData.setRelationshipKey("esr-vnfm.vnfm-id");
429         relationshipData.setRelationshipValue(vnfmId);
430         relationshipToVnfm.getRelationshipData().add(relationshipData);
431
432         final RelationshipList relationshipList = new RelationshipList();
433         relationshipList.getRelationship().add(relationshipToVnfm);
434         genericVnf.setRelationshipList(relationshipList);
435     }
436
437     private void setUpVnfmsInMockAai() {
438         final EsrSystemInfo esrSystemInfo1 = new EsrSystemInfo();
439         esrSystemInfo1.setServiceUrl("http://vnfm1:8080");
440         esrSystemInfo1.setType("vnfmType1");
441         esrSystemInfo1.setSystemType("VNFM");
442         final EsrSystemInfoList esrSystemInfoList1 = new EsrSystemInfoList();
443         esrSystemInfoList1.getEsrSystemInfo().add(esrSystemInfo1);
444
445         final EsrVnfm esrVnfm1 = new EsrVnfm();
446         esrVnfm1.setVnfmId("vnfm1");
447         esrVnfm1.setEsrSystemInfoList(esrSystemInfoList1);
448         esrVnfm1.setResourceVersion("1234");
449
450         final EsrSystemInfo esrSystemInfo2 = new EsrSystemInfo();
451         esrSystemInfo2.setServiceUrl("http://vnfm2:8080");
452         esrSystemInfo2.setType("vnfmType2");
453         esrSystemInfo2.setSystemType("VNFM");
454         final EsrSystemInfoList esrSystemInfoList2 = new EsrSystemInfoList();
455         esrSystemInfoList2.getEsrSystemInfo().add(esrSystemInfo2);
456
457         final EsrVnfm esrVnfm2 = new EsrVnfm();
458         esrVnfm2.setVnfmId("vnfm2");
459         esrVnfm2.setEsrSystemInfoList(esrSystemInfoList2);
460         esrVnfm2.setResourceVersion("1234");
461
462         final EsrVnfmList esrVnfmList = new EsrVnfmList();
463         esrVnfmList.getEsrVnfm().add(esrVnfm1);
464         esrVnfmList.getEsrVnfm().add(esrVnfm2);
465
466         doReturn(Optional.of(esrVnfmList)).when(aaiResourcesClient).get(eq(EsrVnfmList.class),
467                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list")));
468
469         doReturn(Optional.of(esrSystemInfoList1)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
470                 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
471                         "/external-system/esr-vnfm-list/esr-vnfm/vnfm1/esr-system-info-list")));
472         doReturn(Optional.of(esrSystemInfoList2)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
473                 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
474                         "/external-system/esr-vnfm-list/esr-vnfm/vnfm2/esr-system-info-list")));
475     }
476
477     private void setUpVimInMockAai() {
478         final EsrSystemInfo esrSystemInfo = new EsrSystemInfo();
479         esrSystemInfo.setServiceUrl("http://myVim:8080");
480         esrSystemInfo.setType("openstack");
481         esrSystemInfo.setSystemType("VIM");
482         esrSystemInfo.setCloudDomain("myDomain");
483         esrSystemInfo.setUserName("myUser");
484         esrSystemInfo.setPassword("myPassword");
485
486         final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
487         esrSystemInfoList.getEsrSystemInfo().add(esrSystemInfo);
488
489         doReturn(Optional.of(esrSystemInfoList)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
490                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/cloud-infrastructure/cloud-regions/cloud-region/"
491                         + CLOUD_OWNER + "/" + REGION + "/esr-system-info-list")));
492     }
493
494     private InlineResponse201 createCreateResponse() {
495         final InlineResponse201 createResponse = new InlineResponse201();
496         createResponse.setVnfdId("myTestVnfd");
497         final InlineResponse201Links links = new InlineResponse201Links();
498         final InlineResponse201LinksSelf self = new InlineResponse201LinksSelf();
499         self.setHref("http://vnfm2:8080/vnf_instances/vnfId");
500         links.setSelf(self);
501         createResponse.setLinks(links);
502         createResponse.setId("vnfId");
503         return createResponse;
504     }
505
506
507     private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
508
509         final String uriAsString;
510
511         public AaiResourceUriMatcher(final String uriAsString) {
512             this.uriAsString = uriAsString;
513         }
514
515         @Override
516         public boolean matches(final Object item) {
517             if (item instanceof AAIResourceUri) {
518                 if (uriAsString.endsWith("...")) {
519                     return ((AAIResourceUri) item).build().toString()
520                             .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
521                 }
522                 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
523             }
524             return false;
525         }
526
527         @Override
528         public void describeTo(final Description description) {}
529
530     }
531
532 }