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