2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nokia.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.vid.controller;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.core.Is.is;
26 import static org.mockito.ArgumentMatchers.anyString;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.ArgumentMatchers.isA;
29 import static org.mockito.BDDMockito.given;
30 import static org.mockito.Mockito.mock;
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
32 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
33 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
34 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.google.common.collect.ImmutableList;
38 import com.google.common.collect.ImmutableMap;
39 import com.google.common.collect.ImmutableMultimap;
40 import com.google.common.collect.Lists;
41 import com.google.common.collect.Multimap;
43 import java.util.UUID;
44 import javax.ws.rs.core.Response;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.Answers;
49 import org.mockito.Mock;
50 import org.mockito.Mockito;
51 import org.mockito.junit.MockitoJUnitRunner;
52 import org.onap.vid.aai.AaiResponse;
53 import org.onap.vid.aai.AaiResponseTranslator;
54 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
55 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
56 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
57 import org.onap.vid.aai.model.AaiGetAicZone.AicZones;
58 import org.onap.vid.aai.model.AaiGetAicZone.Zone;
59 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
60 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetails;
61 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsError;
62 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsOk;
63 import org.onap.vid.aai.util.AAIRestInterface;
64 import org.onap.vid.model.VersionByInvariantIdsRequest;
65 import org.onap.vid.properties.Features;
66 import org.onap.vid.roles.RoleProvider;
67 import org.onap.vid.roles.RoleValidatorByRoles;
68 import org.onap.vid.services.AaiService;
69 import org.onap.vid.utils.SystemPropertiesWrapper;
70 import org.onap.vid.utils.Unchecked;
71 import org.springframework.http.HttpStatus;
72 import org.springframework.http.MediaType;
73 import org.springframework.test.web.servlet.MockMvc;
74 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
75 import org.togglz.core.manager.FeatureManager;
77 @RunWith(MockitoJUnitRunner.class)
78 public class AaiControllerTest {
80 private static final String ID_1 = "id1";
81 private static final String ID_2 = "id2";
82 private final ObjectMapper objectMapper = new ObjectMapper();
84 private AaiService aaiService;
85 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
86 private AAIRestInterface aaiRestInterface;
88 private RoleProvider roleProvider;
90 private SystemPropertiesWrapper systemPropertiesWrapper;
93 private FeatureManager featureManager;
95 private MockMvc mockMvc;
96 private AaiController aaiController;
100 aaiController = new AaiController(aaiService, aaiRestInterface, roleProvider, systemPropertiesWrapper,
102 mockMvc = MockMvcBuilders.standaloneSetup(aaiController).build();
106 public void getAicZoneForPnf_shouldReturnOKResponse() throws Exception {
107 String globalCustomerId = "testCustomerId";
108 String serviceType = "testServiceType";
109 String serviceId = "testServiceId";
110 String expectedResponseBody = "OK_RESPONSE";
111 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, null, HttpStatus.OK.value());
112 given(aaiService.getAicZoneForPnf(globalCustomerId, serviceType, serviceId)).willReturn(aaiResponse);
115 get("/aai_get_aic_zone_for_pnf/{globalCustomerId}/{serviceType}/{serviceId}", globalCustomerId, serviceType,
117 .contentType(MediaType.APPLICATION_JSON)
118 .accept(MediaType.APPLICATION_JSON))
119 .andExpect(status().isOk())
120 .andExpect(content().string(objectMapper.writeValueAsString(expectedResponseBody)));
124 public void getInstanceGroupsByVnfInstanceId_shouldReturnOKResponse() throws Exception {
125 String vnfInstanceId = "testVndInstanceId";
126 String expectedResponseBody = "OK_RESPONSE";
127 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, null, HttpStatus.OK.value());
128 given(aaiService.getInstanceGroupsByVnfInstanceId(vnfInstanceId)).willReturn(aaiResponse);
130 mockMvc.perform(get("/aai_get_instance_groups_by_vnf_instance_id/{vnfInstanceId}", vnfInstanceId)
131 .contentType(MediaType.APPLICATION_JSON)
132 .accept(MediaType.APPLICATION_JSON))
133 .andExpect(status().isOk())
134 .andExpect(content().string(objectMapper.writeValueAsString(expectedResponseBody)));
138 public void doGetServiceInstance_shouldFetchServiceInstance_byServiceInstanceId() throws Exception {
139 String serviceInstanceId = "testServiceInstanceId";
140 String serviceInstanceType = "Service Instance Id";
141 String expectedResponseBody = "OK_RESPONSE";
142 Response response = mock(Response.class);
143 given(response.readEntity(String.class)).willReturn(expectedResponseBody);
144 given(response.getStatus()).willReturn(HttpStatus.OK.value());
146 given(aaiRestInterface.RestGet(eq("VidAaiController"), anyString(), eq(Unchecked.toURI(
147 "search/nodes-query?search-node-type=service-instance&filter=service-instance-id:EQUALS:"
148 + serviceInstanceId)),
149 eq(false)).getResponse()).willReturn(response);
152 .perform(get("/aai_get_service_instance/{service-instance-id}/{service-instance-type}", serviceInstanceId,
154 .contentType(MediaType.APPLICATION_JSON)
155 .accept(MediaType.APPLICATION_JSON))
156 .andExpect(status().isOk())
157 .andExpect(content().string(expectedResponseBody));
161 public void doGetServiceInstance_shouldFetchServiceInstance_byServiceInstanceName() throws Exception {
162 String serviceInstanceId = "testServiceInstanceId";
163 String serviceInstanceType = "testServiceInstanceType";
164 String expectedResponseBody = "OK_RESPONSE";
165 Response response = mock(Response.class);
166 given(response.readEntity(String.class)).willReturn(expectedResponseBody);
167 given(response.getStatus()).willReturn(HttpStatus.OK.value());
169 given(aaiRestInterface.RestGet(eq("VidAaiController"), anyString(), eq(Unchecked.toURI(
170 "search/nodes-query?search-node-type=service-instance&filter=service-instance-name:EQUALS:"
171 + serviceInstanceId)),
172 eq(false)).getResponse()).willReturn(response);
175 .perform(get("/aai_get_service_instance/{service-instance-id}/{service-instance-type}", serviceInstanceId,
177 .contentType(MediaType.APPLICATION_JSON)
178 .accept(MediaType.APPLICATION_JSON))
179 .andExpect(status().isOk())
180 .andExpect(content().string(expectedResponseBody));
184 public void doGetServices_shouldReturnOkResponse() throws Exception {
185 String globalCustomerId = "testGlobalCustomerId";
186 String serviceSubscriptionId = "testServiceSubscriptionId";
187 String expectedResponseBody = "OK_RESPONSE";
188 Response response = mock(Response.class);
189 given(response.readEntity(String.class)).willReturn(expectedResponseBody);
190 given(response.getStatus()).willReturn(HttpStatus.OK.value());
192 given(aaiRestInterface.RestGet(
193 eq("VidAaiController"),
196 "business/customers/customer/" + globalCustomerId + "/service-subscriptions/service-subscription/"
197 + serviceSubscriptionId + "?depth=0")),
198 eq(false)).getResponse()).willReturn(response);
202 get("/aai_get_service_subscription/{global-customer-id}/{service-subscription-id}", globalCustomerId,
203 serviceSubscriptionId)
204 .contentType(MediaType.APPLICATION_JSON)
205 .accept(MediaType.APPLICATION_JSON))
206 .andExpect(status().isOk())
207 .andExpect(content().string(expectedResponseBody));
211 public void doGetServices_shouldReturnInternalServerError_forEmptyResponse() throws Exception {
212 String globalCustomerId = "testGlobalCustomerId";
213 String serviceSubscriptionId = "testServiceSubscriptionId";
214 String expectedResponseBody = "Failed to fetch data from A&AI, check server logs for details.";
215 given(aaiRestInterface.RestGet(
216 eq("VidAaiController"),
219 "business/customers/customer/" + globalCustomerId + "/service-subscriptions/service-subscription/"
220 + serviceSubscriptionId + "?depth=0")),
221 eq(false)).getResponse()).willReturn(null);
225 get("/aai_get_service_subscription/{global-customer-id}/{service-subscription-id}", globalCustomerId,
226 serviceSubscriptionId)
227 .contentType(MediaType.APPLICATION_JSON)
228 .accept(MediaType.APPLICATION_JSON))
229 .andExpect(status().isInternalServerError())
230 .andExpect(content().string(expectedResponseBody));
234 public void getPortMirroringConfigData_givenIds_shouldReturnConfigDataMappedById() throws Exception {
235 PortMirroringConfigDataOk okConfigData = new PortMirroringConfigDataOk("foo");
236 PortMirroringConfigDataError errorConfigData = new PortMirroringConfigDataError("bar", "{ baz: qux }");
237 Map<String, PortMirroringConfigData> expectedJson = ImmutableMap.of(
239 ID_2, errorConfigData);
240 given(aaiService.getPortMirroringConfigData(ID_1)).willReturn(okConfigData);
241 given(aaiService.getPortMirroringConfigData(ID_2)).willReturn(errorConfigData);
244 .perform(get("/aai_getPortMirroringConfigsData")
245 .param("configurationIds", ID_1, ID_2)
246 .contentType(MediaType.APPLICATION_JSON)
247 .accept(MediaType.APPLICATION_JSON))
248 .andExpect(status().isOk())
249 .andExpect(content().json(objectMapper.writeValueAsString(expectedJson)));
253 public void getPortMirroringSourcePorts_givenIds_shouldReturnPortDetailsMappedById() throws Exception {
254 PortDetailsOk portDetailsOk = new PortDetailsOk("foo", "testInterface", true);
255 PortDetailsError portDetailsError = new PortDetailsError("bar", "{ baz: qux }");
256 Multimap<String, PortDetails> expectedJson = ImmutableMultimap.of(
258 ID_2, portDetailsError);
259 given(aaiService.getPortMirroringSourcePorts(ID_1)).willReturn(Lists.newArrayList(portDetailsOk));
260 given(aaiService.getPortMirroringSourcePorts(ID_2)).willReturn(Lists.newArrayList(portDetailsError));
263 .perform(get("/aai_getPortMirroringSourcePorts")
264 .param("configurationIds", ID_1, ID_2)
265 .contentType(MediaType.APPLICATION_JSON)
266 .accept(MediaType.APPLICATION_JSON))
267 .andExpect(status().isOk())
268 .andExpect(content().json(objectMapper.writeValueAsString(expectedJson.asMap())));
272 public void getNodeTemplateInstances_givenParams_shouldReturnCorrectResponse() throws Exception {
273 String globalCustomerId = "testCustomerId";
274 String serviceType = "testServiceType";
275 String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
276 String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
277 String cloudRegion = "testRegion";
278 String urlTemplate = "/aai_get_vnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}";
279 String expectedResponseBody = "myResponse";
280 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());
282 .getNodeTemplateInstances(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion))
283 .willReturn(aaiResponse);
286 .perform(get(urlTemplate, globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion)
287 .contentType(MediaType.APPLICATION_JSON)
288 .accept(MediaType.APPLICATION_JSON))
289 .andExpect(status().isOk())
290 .andExpect(content().string(expectedResponseBody));
294 public void getAicZones_shouldReturnAaiZones_whenAaiHttpStatusIsOK() throws Exception {
295 AicZones aicZones = new AicZones(ImmutableList.of(new Zone("TEST_ZONE_ID", "TEST_ZONE_NAME")));
296 given(aaiService.getAaiZones()).willReturn(new AaiResponse(aicZones, "", HttpStatus.OK.value()));
298 mockMvc.perform(get("/aai_get_aic_zones")
299 .contentType(MediaType.APPLICATION_JSON)
300 .accept(MediaType.APPLICATION_JSON))
301 .andExpect(status().isOk())
302 .andExpect(content().json(objectMapper.writeValueAsString(aicZones)));
306 public void getAicZones_shouldReturnErrorResponse_whenAaiHttpStatusOtherThanOK() throws Exception {
307 String expectedErrorMessage = "Calling AAI Failed";
308 given(aaiService.getAaiZones())
309 .willReturn(new AaiResponse(null, expectedErrorMessage, HttpStatus.INTERNAL_SERVER_ERROR.value()));
311 mockMvc.perform(get("/aai_get_aic_zones")
312 .contentType(MediaType.APPLICATION_JSON)
313 .accept(MediaType.APPLICATION_JSON))
314 .andExpect(status().isInternalServerError())
315 .andExpect(content().string(expectedErrorMessage));
319 public void getSpecificPnf_shouldReturnPnfObjectForPnfId() throws Exception {
320 String pnfId = "MyPnfId";
321 Pnf pnf = Pnf.builder()
323 .withPnfName("TestPnf")
324 .withPnfName2("pnfName2")
325 .withPnfName2Source("pnfNameSource")
326 .withEquipModel("model")
327 .withEquipType("type")
328 .withEquipVendor("vendor")
330 AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "", HttpStatus.OK.value());
331 given(aaiService.getSpecificPnf(pnfId)).willReturn(aaiResponse);
333 mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
334 .contentType(MediaType.APPLICATION_JSON)
335 .accept(MediaType.APPLICATION_JSON))
336 .andExpect(status().isOk())
337 .andExpect(content().json(objectMapper.writeValueAsString(pnf)));
341 public void getSpecificPnf_shouldHandleAAIServiceException() throws Exception {
342 String pnfId = "MyPnfId";
343 String expectedErrorMessage = "AAI Service Error";
344 given(aaiService.getSpecificPnf(pnfId)).willThrow(new RuntimeException(expectedErrorMessage));
346 mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
347 .contentType(MediaType.APPLICATION_JSON)
348 .accept(MediaType.APPLICATION_JSON))
349 .andExpect(status().isInternalServerError())
350 .andExpect(content().string(expectedErrorMessage));
353 public void getPNFInstances_shouldReturnOKResponseFromAAIService() throws Exception {
354 String globalCustomerId = "testCustomerId";
355 String serviceType = "testServiceType";
356 String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
357 String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
358 String cloudRegion = "testRegion";
359 String equipVendor = "testVendor";
360 String equipModel = "model123";
361 String urlTemplate = "/aai_get_pnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}/{equipVendor}/{equipModel}";
362 String expectedResponseBody = "myResponse";
363 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());
366 .getPNFData(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion, equipVendor,
367 equipModel)).willReturn(aaiResponse);
370 get(urlTemplate, globalCustomerId, serviceType, modelVersionId,
371 modelInvariantId, cloudRegion, equipVendor, equipModel)
372 .contentType(MediaType.APPLICATION_JSON)
373 .accept(MediaType.APPLICATION_JSON))
374 .andExpect(status().isOk())
375 .andExpect(content().string(expectedResponseBody));
379 public void getVersionByInvariantId_shouldReturnOKResponse() throws Exception {
380 String expectedResponse = "OKResponse";
381 VersionByInvariantIdsRequest request = new VersionByInvariantIdsRequest();
382 request.versions = ImmutableList.of("ver1", "ver2");
383 Response response = mock(Response.class);
384 given(response.readEntity(String.class)).willReturn(expectedResponse);
386 .getVersionByInvariantId(request.versions)).willReturn(response);
389 post("/aai_get_version_by_invariant_id")
390 .content(objectMapper.writeValueAsString(request))
391 .contentType(MediaType.APPLICATION_JSON)
392 .accept(MediaType.APPLICATION_JSON))
393 .andExpect(status().isOk())
394 .andExpect(content().string(expectedResponse));
398 public void getSubscriberDetails_shouldOmitServiceInstancesFromSubscriberData_whenFeatureEnabled_andOmitFlagIsTrue()
400 boolean isFeatureActive = true;
401 boolean omitServiceInstances = true;
403 String subscriberId = "subscriberId";
404 String okResponseBody = "OK_RESPONSE";
405 AaiResponse<String> aaiResponse = new AaiResponse<>(okResponseBody, "", HttpStatus.OK.value());
406 given(featureManager.isActive(Features.FLAG_1906_AAI_SUB_DETAILS_REDUCE_DEPTH)).willReturn(isFeatureActive);
407 given(aaiService.getSubscriberData(eq(subscriberId), isA(RoleValidatorByRoles.class),
408 eq(isFeatureActive && omitServiceInstances)))
409 .willReturn(aaiResponse);
412 get("/aai_sub_details/{subscriberId}", subscriberId)
413 .param("omitServiceInstances", Boolean.toString(omitServiceInstances))
414 .contentType(MediaType.APPLICATION_JSON)
415 .accept(MediaType.APPLICATION_JSON))
416 .andExpect(status().isOk())
417 .andExpect(content().string(objectMapper.writeValueAsString(okResponseBody)));
421 public void getSubscriberDetails_shouldIncludeServiceInstancesFromSubscriberData_whenFeatureEnabled_andOmitFlagIsFalse()
423 boolean isFeatureActive = true;
424 boolean omitServiceInstances = false;
426 getSubscriberDetails_assertServiceInstancesInclusion(isFeatureActive, omitServiceInstances);
430 public void getSubscriberDetails_shouldIncludeServiceInstancesFromSubscriberData_whenFeatureDisabled_andOmitFlagIsTrue()
432 boolean isFeatureActive = false;
433 boolean omitServiceInstances = true;
435 getSubscriberDetails_assertServiceInstancesInclusion(isFeatureActive, omitServiceInstances);
439 public void getPortMirroringConfigData_givenThreeIds_ReturnsThreeResults() {
441 final AaiResponseTranslator.PortMirroringConfigDataOk toBeReturnedForA = new AaiResponseTranslator.PortMirroringConfigDataOk("foobar");
442 final AaiResponseTranslator.PortMirroringConfigDataError toBeReturnedForB = new AaiResponseTranslator.PortMirroringConfigDataError("foo", "{ baz: qux }");
443 final AaiResponseTranslator.PortMirroringConfigDataOk toBeReturnedForC = new AaiResponseTranslator.PortMirroringConfigDataOk("corge");
446 .doReturn(toBeReturnedForA)
447 .doReturn(toBeReturnedForB)
448 .doReturn(toBeReturnedForC)
449 .when(aaiService).getPortMirroringConfigData(Mockito.anyString());
451 final Map<String, AaiResponseTranslator.PortMirroringConfigData> result = aaiController.getPortMirroringConfigsData(ImmutableList.of("a", "b", "c"));
453 assertThat(result, is(ImmutableMap.of(
454 "a", toBeReturnedForA,
455 "b", toBeReturnedForB,
456 "c", toBeReturnedForC
461 public void getSubscriberDetails_shouldIncludeServiceInstancesFromSubscriberData_whenFeatureDisabled_andOmitFlagIsFalse()
463 boolean isFeatureActive = false;
464 boolean omitServiceInstances = false;
465 getSubscriberDetails_assertServiceInstancesInclusion(isFeatureActive, omitServiceInstances);
468 private void getSubscriberDetails_assertServiceInstancesInclusion(boolean isFeatureActive,
469 boolean omitServiceInstances) throws Exception {
470 String subscriberId = "subscriberId";
471 String okResponseBody = "OK_RESPONSE";
472 AaiResponse<String> aaiResponse = new AaiResponse<>(okResponseBody, "", HttpStatus.OK.value());
473 given(featureManager.isActive(Features.FLAG_1906_AAI_SUB_DETAILS_REDUCE_DEPTH)).willReturn(isFeatureActive);
474 given(aaiService.getSubscriberData(eq(subscriberId), isA(RoleValidatorByRoles.class),
475 eq(isFeatureActive && omitServiceInstances)))
476 .willReturn(aaiResponse);
479 get("/aai_sub_details/{subscriberId}", subscriberId)
480 .param("omitServiceInstances", Boolean.toString(omitServiceInstances))
481 .contentType(MediaType.APPLICATION_JSON)
482 .accept(MediaType.APPLICATION_JSON))
483 .andExpect(status().isOk())
484 .andExpect(content().string(objectMapper.writeValueAsString(okResponseBody)));