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