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