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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vnfmadapter.rest;
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;
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.model.InlineResponse200;
56 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse2001;
57 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
58 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201Links;
59 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201LinksSelf;
60 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
61 import org.onap.so.client.aai.AAIResourcesClient;
62 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
63 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
64 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
65 import org.onap.vnfmadapter.v1.model.DeleteVnfResponse;
66 import org.onap.vnfmadapter.v1.model.OperationEnum;
67 import org.onap.vnfmadapter.v1.model.OperationStateEnum;
68 import org.onap.vnfmadapter.v1.model.QueryJobResponse;
69 import org.onap.vnfmadapter.v1.model.Tenant;
70 import org.springframework.beans.factory.annotation.Autowired;
71 import org.springframework.beans.factory.annotation.Qualifier;
72 import org.springframework.boot.test.context.SpringBootTest;
73 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
74 import org.springframework.boot.test.mock.mockito.MockBean;
75 import org.springframework.boot.test.web.client.TestRestTemplate;
76 import org.springframework.boot.web.server.LocalServerPort;
77 import org.springframework.http.HttpStatus;
78 import org.springframework.http.MediaType;
79 import org.springframework.http.RequestEntity;
80 import org.springframework.http.ResponseEntity;
81 import org.springframework.test.context.ActiveProfiles;
82 import org.springframework.test.context.junit4.SpringRunner;
83 import org.springframework.test.web.client.MockRestServiceServer;
84 import org.springframework.web.client.RestTemplate;
85 import org.threeten.bp.LocalDateTime;
86 import org.threeten.bp.OffsetDateTime;
87 import org.threeten.bp.ZoneOffset;
89 @RunWith(SpringRunner.class)
90 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
91 @ActiveProfiles("test")
93 public class VnfmAdapterControllerTest {
95 private static final OffsetDateTime JAN_1_2019_12_00 =
96 OffsetDateTime.of(LocalDateTime.of(2019, 1, 1, 12, 0), ZoneOffset.UTC);
97 private static final OffsetDateTime JAN_1_2019_1_00 =
98 OffsetDateTime.of(LocalDateTime.of(2019, 1, 1, 1, 0), ZoneOffset.UTC);
99 private static final String CLOUD_OWNER = "myTestCloudOwner";
100 private static final String REGION = "myTestRegion";
101 private static final String TENANT_ID = "myTestTenantId";
106 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
107 private RestTemplate testRestTemplate;
108 private MockRestServiceServer mockRestServer;
111 AAIResourcesClient aaiResourcesClient;
114 SdcPackageProvider sdcPackageProvider;
117 VnfmAdapterController controller;
118 Gson gson = new Gson();
121 public void setUp() throws Exception {
122 mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
126 public void createVnf_ValidRequest_Returns202AndJobId() throws Exception {
127 final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
128 final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
130 setUpGenericVnfInMockAai("vnfmType2");
131 setUpVnfmsInMockAai();
134 final String expectedsubscriptionRequest =
135 "{\"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$\"}}}";
136 final InlineResponse2001 subscriptionResponse = new InlineResponse2001();
138 final InlineResponse201 createResponse = createCreateResponse();
139 mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_instances"))
140 .andRespond(withSuccess(gson.toJson(createResponse), MediaType.APPLICATION_JSON));
142 mockRestServer.expect(requestTo("http://vnfm2:8080/subscriptions"))
143 .andExpect(content().json(expectedsubscriptionRequest))
144 .andRespond(withSuccess(gson.toJson(subscriptionResponse), MediaType.APPLICATION_JSON));
146 mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_instances/vnfId/instantiate"))
147 .andRespond(withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
148 .location(new URI("http://vnfm2:8080/vnf_lcm_op_occs/123456")));
150 final InlineResponse200 firstOperationQueryResponse = createOperationQueryResponse(
151 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.INSTANTIATE,
152 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.PROCESSING);
153 mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_lcm_op_occs/123456"))
154 .andRespond(withSuccess(gson.toJson(firstOperationQueryResponse), MediaType.APPLICATION_JSON));
156 final InlineResponse200 secondOperationQueryReponse = createOperationQueryResponse(
157 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.INSTANTIATE,
158 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.COMPLETED);
159 mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_lcm_op_occs/123456"))
160 .andRespond(withSuccess(gson.toJson(secondOperationQueryReponse), MediaType.APPLICATION_JSON));
162 // Invoke the create request
164 final ResponseEntity<CreateVnfResponse> createVnfResponse =
165 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
166 assertEquals(HttpStatus.ACCEPTED, createVnfResponse.getStatusCode());
167 assertNotNull(createVnfResponse.getBody().getJobId());
169 final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
170 final ArgumentCaptor<AAIResourceUri> uriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
172 verify(aaiResourcesClient).update(uriArgument.capture(), genericVnfArgument.capture());
174 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", uriArgument.getValue().build().toString());
176 assertEquals("myTestVnfId", genericVnfArgument.getValue().getVnfId());
177 assertEquals(2, genericVnfArgument.getValue().getRelationshipList().getRelationship().size());
178 final Relationship vnfmRelationship =
179 genericVnfArgument.getValue().getRelationshipList().getRelationship().get(0);
180 assertEquals("esr-vnfm", vnfmRelationship.getRelatedTo());
181 assertEquals("tosca.relationships.DependsOn", vnfmRelationship.getRelationshipLabel());
182 assertEquals("/aai/v15/external-system/esr-vnfm-list/esr-vnfm/vnfm2", vnfmRelationship.getRelatedLink());
184 final Relationship tenantRelationship =
185 genericVnfArgument.getValue().getRelationshipList().getRelationship().get(1);
186 assertEquals("tenant", tenantRelationship.getRelatedTo());
188 "/aai/v15/cloud-infrastructure/cloud-regions/cloud-region/myTestCloudOwner/myTestRegion/tenants/tenant/myTestTenantId",
189 tenantRelationship.getRelatedLink());
192 // check the job status
194 final ResponseEntity<QueryJobResponse> firstJobQueryResponse =
195 controller.jobQuery(createVnfResponse.getBody().getJobId(), "", "so", "1213");
196 assertEquals(OperationEnum.INSTANTIATE, firstJobQueryResponse.getBody().getOperation());
197 assertEquals(OperationStateEnum.PROCESSING, firstJobQueryResponse.getBody().getOperationState());
198 assertEquals(JAN_1_2019_12_00, firstJobQueryResponse.getBody().getStartTime());
199 assertEquals(JAN_1_2019_1_00, firstJobQueryResponse.getBody().getStateEnteredTime());
201 final ResponseEntity<QueryJobResponse> secondJobQueryResponse =
202 controller.jobQuery(createVnfResponse.getBody().getJobId(), "", "so", "1213");
203 assertEquals(OperationEnum.INSTANTIATE, secondJobQueryResponse.getBody().getOperation());
204 assertEquals(OperationStateEnum.COMPLETED, secondJobQueryResponse.getBody().getOperationState());
205 assertEquals(JAN_1_2019_12_00, secondJobQueryResponse.getBody().getStartTime());
206 assertEquals(JAN_1_2019_1_00, secondJobQueryResponse.getBody().getStateEnteredTime());
209 @Test(expected = IllegalArgumentException.class)
210 public void createVnf_VnfAlreadyExistsOnVnfm_ThrowsIllegalArgumentException() throws Exception {
211 final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
212 final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
214 final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType1");
215 addSelfLinkToGenericVnf(genericVnf);
217 final InlineResponse201 reponse = new InlineResponse201();
218 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
219 .andRespond(withSuccess(gson.toJson(reponse), MediaType.APPLICATION_JSON));
221 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
224 @Test(expected = VnfmNotFoundException.class)
225 public void createVnf_NoMatchingVnfmFound_ThrowsException() throws Exception {
226 final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
227 final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
229 setUpGenericVnfInMockAai("anotherType");
230 setUpVnfmsInMockAai();
232 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
236 public void createVnf_VnfmAlreadyAssociatedWithVnf_Returns202AndJobId() throws Exception {
237 final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
238 final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
240 final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType2");
241 addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm1");
242 setUpVnfmsInMockAai();
245 final InlineResponse201 createResponse = createCreateResponse();
246 mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_instances"))
247 .andRespond(withSuccess(gson.toJson(createResponse), MediaType.APPLICATION_JSON));
249 mockRestServer.expect(requestTo("http://vnfm2:8080/subscriptions")).andRespond(withBadRequest());
251 mockRestServer.expect(requestTo("http://vnfm2:8080/vnf_instances/vnfId/instantiate"))
252 .andRespond(withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
253 .location(new URI("http://vnfm2:8080/vnf_lcm_op_occs/123456")));
255 final ResponseEntity<CreateVnfResponse> response =
256 controller.vnfCreate("myTestVnfId", createVnfRequest, "asadas", "so", "1213");
257 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
258 assertNotNull(response.getBody().getJobId());
262 public void createVnf_UnauthorizedUser_Returns401() throws Exception {
263 final TestRestTemplate restTemplateWrongPassword = new TestRestTemplate("test", "wrongPassword");
264 final Tenant tenant = new Tenant().cloudOwner(CLOUD_OWNER).regionName(REGION).tenantId(TENANT_ID);
265 final CreateVnfRequest createVnfRequest = new CreateVnfRequest().name("myTestName").tenant(tenant);
267 final RequestEntity<CreateVnfRequest> request =
268 RequestEntity.post(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myVnfId"))
269 .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
270 .header("X-ONAP-RequestId", "myRequestId").header("X-ONAP-InvocationID", "myInvocationId")
271 .body(createVnfRequest);
272 final ResponseEntity<CreateVnfResponse> response =
273 restTemplateWrongPassword.exchange(request, CreateVnfResponse.class);
274 assertEquals(401, response.getStatusCode().value());
278 public void deleteVnf_ValidRequest_Returns202AndJobId() throws Exception {
279 final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
281 final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType");
282 addSelfLinkToGenericVnf(genericVnf);
283 addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm");
285 mockRestServer.expect(requestTo("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm/terminate"))
286 .andRespond(withStatus(HttpStatus.ACCEPTED).contentType(MediaType.APPLICATION_JSON)
287 .location(new URI("http://vnfm2:8080/vnf_lcm_op_occs/1234567")));
289 final InlineResponse200 firstOperationQueryResponse = createOperationQueryResponse(
290 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.TERMINATE,
291 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.PROCESSING);
292 mockRestServer.expect(requestTo("http://vnfm:8080/vnf_lcm_op_occs/1234567"))
293 .andRespond(withSuccess(gson.toJson(firstOperationQueryResponse), MediaType.APPLICATION_JSON));
295 final InlineResponse200 secondOperationQueryReponse = createOperationQueryResponse(
296 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum.TERMINATE,
297 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum.COMPLETED);
298 mockRestServer.expect(requestTo("http://vnfm:8080/vnf_lcm_op_occs/1234567"))
299 .andRespond(withSuccess(gson.toJson(secondOperationQueryReponse), MediaType.APPLICATION_JSON));
301 final RequestEntity<Void> request = RequestEntity
302 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myTestVnfId"))
303 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
304 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
305 final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
306 restTemplate.exchange(request, DeleteVnfResponse.class);
307 assertEquals(202, deleteVnfResponse.getStatusCode().value());
308 assertNotNull(deleteVnfResponse.getBody().getJobId());
310 final EsrSystemInfo esrSystemInfo = new EsrSystemInfo();
311 esrSystemInfo.setServiceUrl("http://vnfm:8080");
312 esrSystemInfo.setType("vnfmType");
313 esrSystemInfo.setSystemType("VNFM");
314 final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
315 esrSystemInfoList.getEsrSystemInfo().add(esrSystemInfo);
317 doReturn(Optional.of(esrSystemInfoList)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
318 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/...")));
320 final ResponseEntity<QueryJobResponse> firstJobQueryResponse =
321 controller.jobQuery(deleteVnfResponse.getBody().getJobId(), "", "so", "1213");
322 assertEquals(OperationEnum.TERMINATE, firstJobQueryResponse.getBody().getOperation());
323 assertEquals(OperationStateEnum.PROCESSING, firstJobQueryResponse.getBody().getOperationState());
324 assertEquals(JAN_1_2019_12_00, firstJobQueryResponse.getBody().getStartTime());
325 assertEquals(JAN_1_2019_1_00, firstJobQueryResponse.getBody().getStateEnteredTime());
327 final ResponseEntity<QueryJobResponse> secondJobQueryResponse =
328 controller.jobQuery(deleteVnfResponse.getBody().getJobId(), "", "so", "1213");
329 assertEquals(OperationEnum.TERMINATE, secondJobQueryResponse.getBody().getOperation());
330 assertEquals(OperationStateEnum.PROCESSING, secondJobQueryResponse.getBody().getOperationState());
331 assertEquals(JAN_1_2019_12_00, secondJobQueryResponse.getBody().getStartTime());
332 assertEquals(JAN_1_2019_1_00, secondJobQueryResponse.getBody().getStateEnteredTime());
336 public void deleteVnf_GenericVnfNotFound_Returns404() throws Exception {
337 final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
339 final RequestEntity<Void> request = RequestEntity
340 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myNonExistingVnfId"))
341 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
342 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
343 final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
344 restTemplate.exchange(request, DeleteVnfResponse.class);
345 assertEquals(404, deleteVnfResponse.getStatusCode().value());
346 assertNull(deleteVnfResponse.getBody().getJobId());
350 public void deleteVnf_NoAssignedVnfm_Returns400() throws Exception {
351 final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
353 setUpGenericVnfInMockAai("vnfmType");
355 final RequestEntity<Void> request = RequestEntity
356 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myTestVnfId"))
357 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
358 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
359 final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
360 restTemplate.exchange(request, DeleteVnfResponse.class);
361 assertEquals(400, deleteVnfResponse.getStatusCode().value());
362 assertNull(deleteVnfResponse.getBody().getJobId());
366 public void deleteVnf_ErrorStatusCodeFromVnfm_Returns500() throws Exception {
367 final TestRestTemplate restTemplate = new TestRestTemplate("test", "test");
369 final GenericVnf genericVnf = setUpGenericVnfInMockAai("vnfmType");
370 addSelfLinkToGenericVnf(genericVnf);
371 addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm");
373 mockRestServer.expect(requestTo("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm/terminate"))
374 .andRespond(withStatus(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON));
376 final RequestEntity<Void> request = RequestEntity
377 .delete(new URI("http://localhost:" + port + "/so/vnfm-adapter/v1/vnfs/myTestVnfId"))
378 .accept(MediaType.APPLICATION_JSON).header("X-ONAP-RequestId", "myRequestId")
379 .header("X-ONAP-InvocationID", "myInvocationId").header("Content-Type", "application/json").build();
380 final ResponseEntity<DeleteVnfResponse> deleteVnfResponse =
381 restTemplate.exchange(request, DeleteVnfResponse.class);
382 assertEquals(500, deleteVnfResponse.getStatusCode().value());
383 assertNull(deleteVnfResponse.getBody().getJobId());
387 private InlineResponse200 createOperationQueryResponse(
388 final org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum operation,
389 final org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationStateEnum operationState) {
390 final InlineResponse200 response = new InlineResponse200();
391 response.setId("9876");
392 response.setOperation(operation);
393 response.setOperationState(operationState);
394 response.setStartTime(JAN_1_2019_12_00);
395 response.setStateEnteredTime(JAN_1_2019_1_00);
396 response.setVnfInstanceId("myVnfInstanceId");
400 private GenericVnf createGenericVnf(final String type) {
401 final GenericVnf genericVnf = new GenericVnf();
402 genericVnf.setVnfId("myTestVnfId");
403 genericVnf.setNfType(type);
407 private GenericVnf setUpGenericVnfInMockAai(final String type) {
408 final GenericVnf genericVnf = createGenericVnf(type);
410 doReturn(Optional.of(genericVnf)).when(aaiResourcesClient).get(eq(GenericVnf.class),
411 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/network/generic-vnfs/generic-vnf/myTestVnfId")));
415 private void addSelfLinkToGenericVnf(final GenericVnf vnf) {
416 vnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
419 private void addRelationshipFromGenericVnfToVnfm(final GenericVnf genericVnf, final String vnfmId) {
420 final Relationship relationshipToVnfm = new Relationship();
421 relationshipToVnfm.setRelatedLink(
422 "/aai/v15/external-system/esr-vnfm-li// final InlineResponse201 vnfInstance = new InlineResponse201();\n"
423 + "// vnfInstance.setInstantiationState(InstantiationStateEnum.NOT_INSTANTIATED);\n"
424 + "// mockRestServer.expect(requestTo(\"http://dummy.value/until/create/implememted/vnfId\"))\n"
425 + "// .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));st/esr-vnfm/"
427 relationshipToVnfm.setRelatedTo("esr-vnfm");
428 final RelationshipData relationshipData = new RelationshipData();
429 relationshipData.setRelationshipKey("esr-vnfm.vnfm-id");
430 relationshipData.setRelationshipValue(vnfmId);
431 relationshipToVnfm.getRelationshipData().add(relationshipData);
433 final RelationshipList relationshipList = new RelationshipList();
434 relationshipList.getRelationship().add(relationshipToVnfm);
435 genericVnf.setRelationshipList(relationshipList);
438 private void setUpVnfmsInMockAai() {
439 final EsrSystemInfo esrSystemInfo1 = new EsrSystemInfo();
440 esrSystemInfo1.setServiceUrl("http://vnfm1:8080");
441 esrSystemInfo1.setType("vnfmType1");
442 esrSystemInfo1.setSystemType("VNFM");
443 final EsrSystemInfoList esrSystemInfoList1 = new EsrSystemInfoList();
444 esrSystemInfoList1.getEsrSystemInfo().add(esrSystemInfo1);
446 final EsrVnfm esrVnfm1 = new EsrVnfm();
447 esrVnfm1.setVnfmId("vnfm1");
448 esrVnfm1.setEsrSystemInfoList(esrSystemInfoList1);
449 esrVnfm1.setResourceVersion("1234");
451 final EsrSystemInfo esrSystemInfo2 = new EsrSystemInfo();
452 esrSystemInfo2.setServiceUrl("http://vnfm2:8080");
453 esrSystemInfo2.setType("vnfmType2");
454 esrSystemInfo2.setSystemType("VNFM");
455 final EsrSystemInfoList esrSystemInfoList2 = new EsrSystemInfoList();
456 esrSystemInfoList2.getEsrSystemInfo().add(esrSystemInfo2);
458 final EsrVnfm esrVnfm2 = new EsrVnfm();
459 esrVnfm2.setVnfmId("vnfm2");
460 esrVnfm2.setEsrSystemInfoList(esrSystemInfoList2);
461 esrVnfm2.setResourceVersion("1234");
463 final EsrVnfmList esrVnfmList = new EsrVnfmList();
464 esrVnfmList.getEsrVnfm().add(esrVnfm1);
465 esrVnfmList.getEsrVnfm().add(esrVnfm2);
467 doReturn(Optional.of(esrVnfmList)).when(aaiResourcesClient).get(eq(EsrVnfmList.class),
468 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list")));
470 doReturn(Optional.of(esrSystemInfoList1)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
471 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
472 "/external-system/esr-vnfm-list/esr-vnfm/vnfm1/esr-system-info-list")));
473 doReturn(Optional.of(esrSystemInfoList2)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
474 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
475 "/external-system/esr-vnfm-list/esr-vnfm/vnfm2/esr-system-info-list")));
478 private void setUpVimInMockAai() {
479 final EsrSystemInfo esrSystemInfo = new EsrSystemInfo();
480 esrSystemInfo.setServiceUrl("http://myVim:8080");
481 esrSystemInfo.setType("openstack");
482 esrSystemInfo.setSystemType("VIM");
483 esrSystemInfo.setCloudDomain("myDomain");
484 esrSystemInfo.setUserName("myUser");
485 esrSystemInfo.setPassword("myPassword");
487 final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
488 esrSystemInfoList.getEsrSystemInfo().add(esrSystemInfo);
490 doReturn(Optional.of(esrSystemInfoList)).when(aaiResourcesClient).get(eq(EsrSystemInfoList.class),
491 MockitoHamcrest.argThat(new AaiResourceUriMatcher("/cloud-infrastructure/cloud-regions/cloud-region/"
492 + CLOUD_OWNER + "/" + REGION + "/esr-system-info-list")));
495 private InlineResponse201 createCreateResponse() {
496 final InlineResponse201 createResponse = new InlineResponse201();
497 createResponse.setVnfdId("myTestVnfd");
498 final InlineResponse201Links links = new InlineResponse201Links();
499 final InlineResponse201LinksSelf self = new InlineResponse201LinksSelf();
500 self.setHref("http://vnfm2:8080/vnf_instances/vnfId");
502 createResponse.setLinks(links);
503 createResponse.setId("vnfId");
504 return createResponse;
508 private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
510 final String uriAsString;
512 public AaiResourceUriMatcher(final String uriAsString) {
513 this.uriAsString = uriAsString;
517 public boolean matches(final Object item) {
518 if (item instanceof AAIResourceUri) {
519 if (uriAsString.endsWith("...")) {
520 return ((AAIResourceUri) item).build().toString()
521 .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
523 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
529 public void describeTo(final Description description) {}