Get operation state from VNFM failing
[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.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\":[\"BASIC\"],\"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
218         final InlineResponse201 reponse = new InlineResponse201();
219         mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
220                 .andRespond(withSuccess(gson.toJson(reponse), MediaType.APPLICATION_JSON));
221
222         controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
223     }
224
225     @Test(expected = VnfmNotFoundException.class)
226     public void createVnf_NoMatchingVnfmFound_ThrowsException() throws Exception {
227         final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
228         final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
229
230         setUpGenericVnfInMockAai("anotherType");
231         setUpVnfmsInMockAai();
232
233         controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
234     }
235
236     @Test
237     public void createVnf_VnfmAlreadyAssociatedWithVnf_Returns202AndJobId() throws Exception {
238         final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
239         final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
240
241         final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType2");
242         addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm1");
243         setUpVnfmsInMockAai();
244         setUpVimInMockAai();
245
246         final InlineResponse201 createResponse = createCreateResponse();
247         mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_instances"))
248                 .andRespond(withSuccess(gson.toJson(createResponse), MediaType.APPLICATION_JSON));
249
250         mockRestServer.expect(requestTo("http://vnfm2:8080/subscriptions")).andRespond(withBadRequest());
251
252         mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_instances/vnfId/instantiate"))
253                 .andRespond(withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
254                         .location(new URI("http://vnfm2:8080/vnf_lcm_op_occs/123456")));
255
256         final ResponseEntity<CreateVnfResponse> response =
257                 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
258         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
259         assertNotNull(response.getBody().getJobId());
260     }
261
262     @Test
263     public void createVnf_UnauthorizedUser_Returns401() throws Exception {
264         final TestRestTemplate restTemplateWrongPassword = new TestRestTemplate("test", "wrongPassword");
265         final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
266         final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
267
268         final RequestEntity<CreateVnfRequest> request =
269                 RequestEntity.post(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myVnfId"))
270                         .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
271                         .header("X-ONAP-RequestId", "myRequestId").header("X-ONAP-InvocationID", "myInvocationId")
272                         .body(createVnfRequest);
273         final ResponseEntity<CreateVnfResponse> response =
274                 restTemplateWrongPassword.exchange(request, CreateVnfResponse.class);
275         assertEquals(401, response.getStatusCode().value());
276     }
277
278     @Test
279     public void deleteVnf_ValidRequest_Returns202AndJobId() throws Exception {
280         final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
281
282         final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType");
283         addSelfLinkToGenericVnf(genericVnf);
284         addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm");
285
286         mockRestServer.expect(requestTo("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm/terminate"))
287                 .andRespond(withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
288                         .location(new URI("http://vnfm2:8080/vnf_lcm_op_occs/1234567")));
289
290         final InlineResponse200 firstOperationQueryResponse = createOperationQueryResponse(
291                 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.TERMINATE,
292                 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.PROCESSING);
293         mockRestServer.expect(requestTo("http://vnfm:8080/vnf_lcm_op_occs/1234567"))
294                 .andRespond(withSuccess(gson.toJson(firstOperationQueryResponse), MediaType.APPLICATION_JSON));
295
296         final InlineResponse200 secondOperationQueryReponse = createOperationQueryResponse(
297                 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.TERMINATE,
298                 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.COMPLETED);
299         mockRestServer.expect(requestTo("http://vnfm:8080/vnf_lcm_op_occs/1234567"))
300                 .andRespond(withSuccess(gson.toJson(secondOperationQueryReponse), MediaType.APPLICATION_JSON));
301
302         final RequestEntity<Void> request = RequestEntity
303                 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myTestVnfId"))
304                 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
305                 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
306         final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
307                 restTemplate.exchange(request, DeleteVnfResponse.class);
308         assertEquals(202, deleteVnfResponse.getStatusCode().value());
309         assertNotNull(deleteVnfResponse.getBody().getJobId());
310
311         final EsrSystemInfo esrSystemInfo = new EsrSystemInfo();
312         esrSystemInfo.setServiceUrl("http://vnfm:8080");
313         esrSystemInfo.setType("vnfmType");
314         esrSystemInfo.setSystemType("VNFM");
315         final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
316         esrSystemInfoList.getEsrSystemInfo().add(esrSystemInfo);
317
318         doReturn(Optional.of(esrSystemInfoList)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
319                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/...")));
320
321         final ResponseEntity<QueryJobResponse> firstJobQueryResponse =
322                 controller.jobQuery(deleteVnfResponse.getBody().getJobId(), "", "so", "1213");
323         assertEquals(OperationEnum.TERMINATE, firstJobQueryResponse.getBody().getOperation());
324         assertEquals(OperationStateEnum.PROCESSING, firstJobQueryResponse.getBody().getOperationState());
325         assertEquals(JAN_1_2019_12_00, firstJobQueryResponse.getBody().getStartTime());
326         assertEquals(JAN_1_2019_1_00, firstJobQueryResponse.getBody().getStateEnteredTime());
327
328         final ResponseEntity<QueryJobResponse> secondJobQueryResponse =
329                 controller.jobQuery(deleteVnfResponse.getBody().getJobId(), "", "so", "1213");
330         assertEquals(OperationEnum.TERMINATE, secondJobQueryResponse.getBody().getOperation());
331         assertEquals(OperationStateEnum.PROCESSING, secondJobQueryResponse.getBody().getOperationState());
332         assertEquals(JAN_1_2019_12_00, secondJobQueryResponse.getBody().getStartTime());
333         assertEquals(JAN_1_2019_1_00, secondJobQueryResponse.getBody().getStateEnteredTime());
334     }
335
336     @Test
337     public void deleteVnf_GenericVnfNotFound_Returns404() throws Exception {
338         final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
339
340         final RequestEntity<Void> request = RequestEntity
341                 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myNonExistingVnfId"))
342                 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
343                 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
344         final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
345                 restTemplate.exchange(request, DeleteVnfResponse.class);
346         assertEquals(404, deleteVnfResponse.getStatusCode().value());
347         assertNull(deleteVnfResponse.getBody().getJobId());
348     }
349
350     @Test
351     public void deleteVnf_NoAssignedVnfm_Returns400() throws Exception {
352         final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
353
354         setUpGenericVnfInMockAai("vnfmType");
355
356         final RequestEntity<Void> request = RequestEntity
357                 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myTestVnfId"))
358                 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
359                 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
360         final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
361                 restTemplate.exchange(request, DeleteVnfResponse.class);
362         assertEquals(400, deleteVnfResponse.getStatusCode().value());
363         assertNull(deleteVnfResponse.getBody().getJobId());
364     }
365
366     @Test
367     public void deleteVnf_ErrorStatusCodeFromVnfm_Returns500() throws Exception {
368         final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
369
370         final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType");
371         addSelfLinkToGenericVnf(genericVnf);
372         addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm");
373
374         mockRestServer.expect(requestTo("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm/terminate"))
375                 .andRespond(withStatus(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON));
376
377         final RequestEntity<Void> request = RequestEntity
378                 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myTestVnfId"))
379                 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
380                 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
381         final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
382                 restTemplate.exchange(request, DeleteVnfResponse.class);
383         assertEquals(500, deleteVnfResponse.getStatusCode().value());
384         assertNull(deleteVnfResponse.getBody().getJobId());
385
386     }
387
388     private InlineResponse200 createOperationQueryResponse(
389             final org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum operation,
390             final org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum operationState) {
391         final InlineResponse200 response = new InlineResponse200();
392         response.setId("9876");
393         response.setOperation(operation);
394         response.setOperationState(operationState);
395         response.setStartTime(JAN_1_2019_12_00);
396         response.setStateEnteredTime(JAN_1_2019_1_00);
397         response.setVnfInstanceId("myVnfInstanceId");
398         return response;
399     }
400
401     private GenericVnf createGenericVnf(final String type) {
402         final GenericVnf genericVnf = new GenericVnf();
403         genericVnf.setVnfId("myTestVnfId");
404         genericVnf.setNfType(type);
405         return genericVnf;
406     }
407
408     private GenericVnf setUpGenericVnfInMockAai(final String type) {
409         final GenericVnf genericVnf = createGenericVnf(type);
410
411         doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class),
412                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId")));
413         return genericVnf;
414     }
415
416     private void addSelfLinkToGenericVnf(final GenericVnf vnf) {
417         vnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
418     }
419
420     private void addRelationshipFromGenericVnfToVnfm(final GenericVnf genericVnf, final String vnfmId) {
421         final Relationship relationshipToVnfm = new Relationship();
422         relationshipToVnfm.setRelatedLink(
423                 "/aai/v15/external-system/esr-vnfm-li//        final InlineResponse201 vnfInstance = new InlineResponse201();\n"
424                         + "//        vnfInstance.setInstantiationState(InstantiationStateEnum.NOT_INSTANTIATED);\n"
425                         + "//        mockRestServer.expect(requestTo(\"http://dummy.value/until/create/implememted/vnfId\"))\n"
426                         + "//                .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));st/esr-vnfm/"
427                         + vnfmId);
428         relationshipToVnfm.setRelatedTo("esr-vnfm");
429         final RelationshipData relationshipData = new RelationshipData();
430         relationshipData.setRelationshipKey("esr-vnfm.vnfm-id");
431         relationshipData.setRelationshipValue(vnfmId);
432         relationshipToVnfm.getRelationshipData().add(relationshipData);
433
434         final RelationshipList relationshipList = new RelationshipList();
435         relationshipList.getRelationship().add(relationshipToVnfm);
436         genericVnf.setRelationshipList(relationshipList);
437     }
438
439     private void setUpVnfmsInMockAai() {
440         final EsrSystemInfo esrSystemInfo1 = new EsrSystemInfo();
441         esrSystemInfo1.setServiceUrl("http://vnfm1:8080");
442         esrSystemInfo1.setType("vnfmType1");
443         esrSystemInfo1.setSystemType("VNFM");
444         final EsrSystemInfoList esrSystemInfoList1 = new EsrSystemInfoList();
445         esrSystemInfoList1.getEsrSystemInfo().add(esrSystemInfo1);
446
447         final EsrVnfm esrVnfm1 = new EsrVnfm();
448         esrVnfm1.setVnfmId("vnfm1");
449         esrVnfm1.setEsrSystemInfoList(esrSystemInfoList1);
450         esrVnfm1.setResourceVersion("1234");
451
452         final EsrSystemInfo esrSystemInfo2 = new EsrSystemInfo();
453         esrSystemInfo2.setServiceUrl("http://vnfm2:8080");
454         esrSystemInfo2.setType("vnfmType2");
455         esrSystemInfo2.setSystemType("VNFM");
456         final EsrSystemInfoList esrSystemInfoList2 = new EsrSystemInfoList();
457         esrSystemInfoList2.getEsrSystemInfo().add(esrSystemInfo2);
458
459         final EsrVnfm esrVnfm2 = new EsrVnfm();
460         esrVnfm2.setVnfmId("vnfm2");
461         esrVnfm2.setEsrSystemInfoList(esrSystemInfoList2);
462         esrVnfm2.setResourceVersion("1234");
463
464         final EsrVnfmList esrVnfmList = new EsrVnfmList();
465         esrVnfmList.getEsrVnfm().add(esrVnfm1);
466         esrVnfmList.getEsrVnfm().add(esrVnfm2);
467
468         doReturn(Optional.of(esrVnfmList)).when(aaiResourcesClient).get(eq(EsrVnfmList.class),
469                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list")));
470
471         doReturn(Optional.of(esrSystemInfoList1)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
472                 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
473                         "/external-system/esr-vnfm-list/esr-vnfm/vnfm1/esr-system-info-list")));
474         doReturn(Optional.of(esrSystemInfoList2)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
475                 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
476                         "/external-system/esr-vnfm-list/esr-vnfm/vnfm2/esr-system-info-list")));
477     }
478
479     private void setUpVimInMockAai() {
480         final EsrSystemInfo esrSystemInfo = new EsrSystemInfo();
481         esrSystemInfo.setServiceUrl("http://myVim:8080");
482         esrSystemInfo.setType("openstack");
483         esrSystemInfo.setSystemType("VIM");
484         esrSystemInfo.setCloudDomain("myDomain");
485         esrSystemInfo.setUserName("myUser");
486         esrSystemInfo.setPassword("myPassword");
487
488         final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
489         esrSystemInfoList.getEsrSystemInfo().add(esrSystemInfo);
490
491         doReturn(Optional.of(esrSystemInfoList)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
492                 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/cloud-infrastructure/cloud-regions/cloud-region/"
493                         + CLOUD_OWNER + "/" + REGION + "/esr-system-info-list")));
494     }
495
496     private InlineResponse201 createCreateResponse() {
497         final InlineResponse201 createResponse = new InlineResponse201();
498         createResponse.setVnfdId("myTestVnfd");
499         final InlineResponse201Links links = new InlineResponse201Links();
500         final InlineResponse201LinksSelf self = new InlineResponse201LinksSelf();
501         self.setHref("http://vnfm2:8080/vnf_instances/vnfId");
502         links.setSelf(self);
503         createResponse.setLinks(links);
504         createResponse.setId("vnfId");
505         return createResponse;
506     }
507
508
509     private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
510
511         final String uriAsString;
512
513         public AaiResourceUriMatcher(final String uriAsString) {
514             this.uriAsString = uriAsString;
515         }
516
517         @Override
518         public boolean matches(final Object item) {
519             if (item instanceof AAIResourceUri) {
520                 if (uriAsString.endsWith("...")) {
521                     return ((AAIResourceUri) item).build().toString()
522                             .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
523                 }
524                 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
525             }
526             return false;
527         }
528
529         @Override
530         public void describeTo(final Description description) {}
531
532     }
533
534 }