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