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