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.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.ArgumentMatchers.isA;
27 import static org.mockito.BDDMockito.given;
28 import static org.mockito.Mockito.mock;
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
31 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
32 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35 import com.google.common.collect.ImmutableList;
36 import com.google.common.collect.ImmutableMap;
37 import com.google.common.collect.ImmutableMultimap;
38 import com.google.common.collect.Lists;
39 import com.google.common.collect.Multimap;
41 import java.util.UUID;
42 import javax.ws.rs.core.Response;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Answers;
47 import org.mockito.Mock;
48 import org.mockito.junit.MockitoJUnitRunner;
49 import org.onap.vid.aai.AaiResponse;
50 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
51 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
52 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
53 import org.onap.vid.aai.model.AaiGetAicZone.AicZones;
54 import org.onap.vid.aai.model.AaiGetAicZone.Zone;
55 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
56 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetails;
57 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsError;
58 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsOk;
59 import org.onap.vid.aai.util.AAIRestInterface;
60 import org.onap.vid.model.VersionByInvariantIdsRequest;
61 import org.onap.vid.properties.Features;
62 import org.onap.vid.roles.RoleProvider;
63 import org.onap.vid.roles.RoleValidatorByRoles;
64 import org.onap.vid.services.AaiService;
65 import org.onap.vid.utils.SystemPropertiesWrapper;
66 import org.onap.vid.utils.Unchecked;
67 import org.springframework.http.HttpStatus;
68 import org.springframework.http.MediaType;
69 import org.springframework.test.web.servlet.MockMvc;
70 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
71 import org.togglz.core.manager.FeatureManager;
73 @RunWith(MockitoJUnitRunner.class)
74 public class AaiControllerTest {
76 private static final String ID_1 = "id1";
77 private static final String ID_2 = "id2";
78 private final ObjectMapper objectMapper = new ObjectMapper();
80 private AaiService aaiService;
81 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
82 private AAIRestInterface aaiRestInterface;
84 private RoleProvider roleProvider;
86 private SystemPropertiesWrapper systemPropertiesWrapper;
89 private FeatureManager featureManager;
91 private MockMvc mockMvc;
92 private AaiController aaiController;
96 aaiController = new AaiController(aaiService, aaiRestInterface, roleProvider, systemPropertiesWrapper,
98 mockMvc = MockMvcBuilders.standaloneSetup(aaiController).build();
102 public void getAicZoneForPnf_shouldReturnOKResponse() throws Exception {
103 String globalCustomerId = "testCustomerId";
104 String serviceType = "testServiceType";
105 String serviceId = "testServiceId";
106 String expectedResponseBody = "OK_RESPONSE";
107 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, null, HttpStatus.OK.value());
108 given(aaiService.getAicZoneForPnf(globalCustomerId, serviceType, serviceId)).willReturn(aaiResponse);
111 get("/aai_get_aic_zone_for_pnf/{globalCustomerId}/{serviceType}/{serviceId}", globalCustomerId, serviceType,
113 .contentType(MediaType.APPLICATION_JSON)
114 .accept(MediaType.APPLICATION_JSON))
115 .andExpect(status().isOk())
116 .andExpect(content().string(objectMapper.writeValueAsString(expectedResponseBody)));
120 public void getInstanceGroupsByVnfInstanceId_shouldReturnOKResponse() throws Exception {
121 String vnfInstanceId = "testVndInstanceId";
122 String expectedResponseBody = "OK_RESPONSE";
123 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, null, HttpStatus.OK.value());
124 given(aaiService.getInstanceGroupsByVnfInstanceId(vnfInstanceId)).willReturn(aaiResponse);
126 mockMvc.perform(get("/aai_get_instance_groups_by_vnf_instance_id/{vnfInstanceId}", vnfInstanceId)
127 .contentType(MediaType.APPLICATION_JSON)
128 .accept(MediaType.APPLICATION_JSON))
129 .andExpect(status().isOk())
130 .andExpect(content().string(objectMapper.writeValueAsString(expectedResponseBody)));
134 public void doGetServiceInstance_shouldFetchServiceInstance_byServiceInstanceId() throws Exception {
135 String serviceInstanceId = "testServiceInstanceId";
136 String serviceInstanceType = "Service Instance Id";
137 String expectedResponseBody = "OK_RESPONSE";
138 Response response = mock(Response.class);
139 given(response.readEntity(String.class)).willReturn(expectedResponseBody);
140 given(response.getStatus()).willReturn(HttpStatus.OK.value());
142 given(aaiRestInterface.RestGet(eq("VidAaiController"), anyString(), eq(Unchecked.toURI(
143 "search/nodes-query?search-node-type=service-instance&filter=service-instance-id:EQUALS:"
144 + serviceInstanceId)),
145 eq(false)).getResponse()).willReturn(response);
148 .perform(get("/aai_get_service_instance/{service-instance-id}/{service-instance-type}", serviceInstanceId,
150 .contentType(MediaType.APPLICATION_JSON)
151 .accept(MediaType.APPLICATION_JSON))
152 .andExpect(status().isOk())
153 .andExpect(content().string(expectedResponseBody));
157 public void doGetServiceInstance_shouldFetchServiceInstance_byServiceInstanceName() throws Exception {
158 String serviceInstanceId = "testServiceInstanceId";
159 String serviceInstanceType = "testServiceInstanceType";
160 String expectedResponseBody = "OK_RESPONSE";
161 Response response = mock(Response.class);
162 given(response.readEntity(String.class)).willReturn(expectedResponseBody);
163 given(response.getStatus()).willReturn(HttpStatus.OK.value());
165 given(aaiRestInterface.RestGet(eq("VidAaiController"), anyString(), eq(Unchecked.toURI(
166 "search/nodes-query?search-node-type=service-instance&filter=service-instance-name:EQUALS:"
167 + serviceInstanceId)),
168 eq(false)).getResponse()).willReturn(response);
171 .perform(get("/aai_get_service_instance/{service-instance-id}/{service-instance-type}", serviceInstanceId,
173 .contentType(MediaType.APPLICATION_JSON)
174 .accept(MediaType.APPLICATION_JSON))
175 .andExpect(status().isOk())
176 .andExpect(content().string(expectedResponseBody));
180 public void doGetServices_shouldReturnOkResponse() throws Exception {
181 String globalCustomerId = "testGlobalCustomerId";
182 String serviceSubscriptionId = "testServiceSubscriptionId";
183 String expectedResponseBody = "OK_RESPONSE";
184 Response response = mock(Response.class);
185 given(response.readEntity(String.class)).willReturn(expectedResponseBody);
186 given(response.getStatus()).willReturn(HttpStatus.OK.value());
188 given(aaiRestInterface.RestGet(
189 eq("VidAaiController"),
192 "business/customers/customer/" + globalCustomerId + "/service-subscriptions/service-subscription/"
193 + serviceSubscriptionId + "?depth=0")),
194 eq(false)).getResponse()).willReturn(response);
198 get("/aai_get_service_subscription/{global-customer-id}/{service-subscription-id}", globalCustomerId,
199 serviceSubscriptionId)
200 .contentType(MediaType.APPLICATION_JSON)
201 .accept(MediaType.APPLICATION_JSON))
202 .andExpect(status().isOk())
203 .andExpect(content().string(expectedResponseBody));
207 public void doGetServices_shouldReturnInternalServerError_forEmptyResponse() throws Exception {
208 String globalCustomerId = "testGlobalCustomerId";
209 String serviceSubscriptionId = "testServiceSubscriptionId";
210 String expectedResponseBody = "Failed to fetch data from A&AI, check server logs for details.";
211 given(aaiRestInterface.RestGet(
212 eq("VidAaiController"),
215 "business/customers/customer/" + globalCustomerId + "/service-subscriptions/service-subscription/"
216 + serviceSubscriptionId + "?depth=0")),
217 eq(false)).getResponse()).willReturn(null);
221 get("/aai_get_service_subscription/{global-customer-id}/{service-subscription-id}", globalCustomerId,
222 serviceSubscriptionId)
223 .contentType(MediaType.APPLICATION_JSON)
224 .accept(MediaType.APPLICATION_JSON))
225 .andExpect(status().isInternalServerError())
226 .andExpect(content().string(expectedResponseBody));
230 public void getPortMirroringConfigData_givenIds_shouldReturnConfigDataMappedById() throws Exception {
231 PortMirroringConfigDataOk okConfigData = new PortMirroringConfigDataOk("foo");
232 PortMirroringConfigDataError errorConfigData = new PortMirroringConfigDataError("bar", "{ baz: qux }");
233 Map<String, PortMirroringConfigData> expectedJson = ImmutableMap.of(
235 ID_2, errorConfigData);
236 given(aaiService.getPortMirroringConfigData(ID_1)).willReturn(okConfigData);
237 given(aaiService.getPortMirroringConfigData(ID_2)).willReturn(errorConfigData);
240 .perform(get("/aai_getPortMirroringConfigsData")
241 .param("configurationIds", ID_1, ID_2)
242 .contentType(MediaType.APPLICATION_JSON)
243 .accept(MediaType.APPLICATION_JSON))
244 .andExpect(status().isOk())
245 .andExpect(content().json(objectMapper.writeValueAsString(expectedJson)));
249 public void getPortMirroringSourcePorts_givenIds_shouldReturnPortDetailsMappedById() throws Exception {
250 PortDetailsOk portDetailsOk = new PortDetailsOk("foo", "testInterface", true);
251 PortDetailsError portDetailsError = new PortDetailsError("bar", "{ baz: qux }");
252 Multimap<String, PortDetails> expectedJson = ImmutableMultimap.of(
254 ID_2, portDetailsError);
255 given(aaiService.getPortMirroringSourcePorts(ID_1)).willReturn(Lists.newArrayList(portDetailsOk));
256 given(aaiService.getPortMirroringSourcePorts(ID_2)).willReturn(Lists.newArrayList(portDetailsError));
259 .perform(get("/aai_getPortMirroringSourcePorts")
260 .param("configurationIds", ID_1, ID_2)
261 .contentType(MediaType.APPLICATION_JSON)
262 .accept(MediaType.APPLICATION_JSON))
263 .andExpect(status().isOk())
264 .andExpect(content().json(objectMapper.writeValueAsString(expectedJson.asMap())));
268 public void getNodeTemplateInstances_givenParams_shouldReturnCorrectResponse() throws Exception {
269 String globalCustomerId = "testCustomerId";
270 String serviceType = "testServiceType";
271 String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
272 String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
273 String cloudRegion = "testRegion";
274 String urlTemplate = "/aai_get_vnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}";
275 String expectedResponseBody = "myResponse";
276 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());
278 .getNodeTemplateInstances(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion))
279 .willReturn(aaiResponse);
282 .perform(get(urlTemplate, globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion)
283 .contentType(MediaType.APPLICATION_JSON)
284 .accept(MediaType.APPLICATION_JSON))
285 .andExpect(status().isOk())
286 .andExpect(content().string(expectedResponseBody));
290 public void getAicZones_shouldReturnAaiZones_whenAaiHttpStatusIsOK() throws Exception {
291 AicZones aicZones = new AicZones(ImmutableList.of(new Zone("TEST_ZONE_ID", "TEST_ZONE_NAME")));
292 given(aaiService.getAaiZones()).willReturn(new AaiResponse(aicZones, "", HttpStatus.OK.value()));
294 mockMvc.perform(get("/aai_get_aic_zones")
295 .contentType(MediaType.APPLICATION_JSON)
296 .accept(MediaType.APPLICATION_JSON))
297 .andExpect(status().isOk())
298 .andExpect(content().json(objectMapper.writeValueAsString(aicZones)));
302 public void getAicZones_shouldReturnErrorResponse_whenAaiHttpStatusOtherThanOK() throws Exception {
303 String expectedErrorMessage = "Calling AAI Failed";
304 given(aaiService.getAaiZones())
305 .willReturn(new AaiResponse(null, expectedErrorMessage, HttpStatus.INTERNAL_SERVER_ERROR.value()));
307 mockMvc.perform(get("/aai_get_aic_zones")
308 .contentType(MediaType.APPLICATION_JSON)
309 .accept(MediaType.APPLICATION_JSON))
310 .andExpect(status().isInternalServerError())
311 .andExpect(content().string(expectedErrorMessage));
315 public void getSpecificPnf_shouldReturnPnfObjectForPnfId() throws Exception {
316 String pnfId = "MyPnfId";
317 Pnf pnf = Pnf.builder()
319 .withPnfName("TestPnf")
320 .withPnfName2("pnfName2")
321 .withPnfName2Source("pnfNameSource")
322 .withEquipModel("model")
323 .withEquipType("type")
324 .withEquipVendor("vendor")
326 AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "", HttpStatus.OK.value());
327 given(aaiService.getSpecificPnf(pnfId)).willReturn(aaiResponse);
329 mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
330 .contentType(MediaType.APPLICATION_JSON)
331 .accept(MediaType.APPLICATION_JSON))
332 .andExpect(status().isOk())
333 .andExpect(content().json(objectMapper.writeValueAsString(pnf)));
337 public void getSpecificPnf_shouldHandleAAIServiceException() throws Exception {
338 String pnfId = "MyPnfId";
339 String expectedErrorMessage = "AAI Service Error";
340 given(aaiService.getSpecificPnf(pnfId)).willThrow(new RuntimeException(expectedErrorMessage));
342 mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
343 .contentType(MediaType.APPLICATION_JSON)
344 .accept(MediaType.APPLICATION_JSON))
345 .andExpect(status().isInternalServerError())
346 .andExpect(content().string(expectedErrorMessage));
349 public void getPNFInstances_shouldReturnOKResponseFromAAIService() throws Exception {
350 String globalCustomerId = "testCustomerId";
351 String serviceType = "testServiceType";
352 String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
353 String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
354 String cloudRegion = "testRegion";
355 String equipVendor = "testVendor";
356 String equipModel = "model123";
357 String urlTemplate = "/aai_get_pnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}/{equipVendor}/{equipModel}";
358 String expectedResponseBody = "myResponse";
359 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());
362 .getPNFData(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion, equipVendor,
363 equipModel)).willReturn(aaiResponse);
366 get(urlTemplate, globalCustomerId, serviceType, modelVersionId,
367 modelInvariantId, cloudRegion, equipVendor, equipModel)
368 .contentType(MediaType.APPLICATION_JSON)
369 .accept(MediaType.APPLICATION_JSON))
370 .andExpect(status().isOk())
371 .andExpect(content().string(expectedResponseBody));
375 public void getVersionByInvariantId_shouldReturnOKResponse() throws Exception {
376 String expectedResponse = "OKResponse";
377 VersionByInvariantIdsRequest request = new VersionByInvariantIdsRequest();
378 request.versions = ImmutableList.of("ver1", "ver2");
379 Response response = mock(Response.class);
380 given(response.readEntity(String.class)).willReturn(expectedResponse);
382 .getVersionByInvariantId(request.versions)).willReturn(response);
385 post("/aai_get_version_by_invariant_id")
386 .content(objectMapper.writeValueAsString(request))
387 .contentType(MediaType.APPLICATION_JSON)
388 .accept(MediaType.APPLICATION_JSON))
389 .andExpect(status().isOk())
390 .andExpect(content().string(expectedResponse));
394 public void getSubscriberDetails_shouldOmitServiceInstancesFromSubscriberData_whenFeatureEnabled_andOmitFlagIsTrue()
396 boolean isFeatureActive = true;
397 boolean omitServiceInstances = true;
399 String subscriberId = "subscriberId";
400 String okResponseBody = "OK_RESPONSE";
401 AaiResponse<String> aaiResponse = new AaiResponse<>(okResponseBody, "", HttpStatus.OK.value());
402 given(featureManager.isActive(Features.FLAG_1906_AAI_SUB_DETAILS_REDUCE_DEPTH)).willReturn(isFeatureActive);
403 given(aaiService.getSubscriberData(eq(subscriberId), isA(RoleValidatorByRoles.class),
404 eq(isFeatureActive && omitServiceInstances)))
405 .willReturn(aaiResponse);
408 get("/aai_sub_details/{subscriberId}", subscriberId)
409 .param("omitServiceInstances", Boolean.toString(omitServiceInstances))
410 .contentType(MediaType.APPLICATION_JSON)
411 .accept(MediaType.APPLICATION_JSON))
412 .andExpect(status().isOk())
413 .andExpect(content().string(objectMapper.writeValueAsString(okResponseBody)));
417 public void getSubscriberDetails_shouldIncludeServiceInstancesFromSubscriberData_whenFeatureEnabled_andOmitFlagIsFalse()
419 boolean isFeatureActive = true;
420 boolean omitServiceInstances = false;
422 getSubscriberDetails_assertServiceInstancesInclusion(isFeatureActive, omitServiceInstances);
426 public void getSubscriberDetails_shouldIncludeServiceInstancesFromSubscriberData_whenFeatureDisabled_andOmitFlagIsTrue()
428 boolean isFeatureActive = false;
429 boolean omitServiceInstances = true;
431 getSubscriberDetails_assertServiceInstancesInclusion(isFeatureActive, omitServiceInstances);
435 public void getSubscriberDetails_shouldIncludeServiceInstancesFromSubscriberData_whenFeatureDisabled_andOmitFlagIsFalse()
437 boolean isFeatureActive = false;
438 boolean omitServiceInstances = false;
439 getSubscriberDetails_assertServiceInstancesInclusion(isFeatureActive, omitServiceInstances);
442 private void getSubscriberDetails_assertServiceInstancesInclusion(boolean isFeatureActive,
443 boolean omitServiceInstances) throws Exception {
444 String subscriberId = "subscriberId";
445 String okResponseBody = "OK_RESPONSE";
446 AaiResponse<String> aaiResponse = new AaiResponse<>(okResponseBody, "", HttpStatus.OK.value());
447 given(featureManager.isActive(Features.FLAG_1906_AAI_SUB_DETAILS_REDUCE_DEPTH)).willReturn(isFeatureActive);
448 given(aaiService.getSubscriberData(eq(subscriberId), isA(RoleValidatorByRoles.class),
449 eq(isFeatureActive && omitServiceInstances)))
450 .willReturn(aaiResponse);
453 get("/aai_sub_details/{subscriberId}", subscriberId)
454 .param("omitServiceInstances", Boolean.toString(omitServiceInstances))
455 .contentType(MediaType.APPLICATION_JSON)
456 .accept(MediaType.APPLICATION_JSON))
457 .andExpect(status().isOk())
458 .andExpect(content().string(objectMapper.writeValueAsString(okResponseBody)));