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