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.BDDMockito.given;
25 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
26 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.google.common.collect.ImmutableList;
31 import com.google.common.collect.ImmutableMap;
32 import com.google.common.collect.ImmutableMultimap;
33 import com.google.common.collect.Lists;
34 import com.google.common.collect.Multimap;
36 import java.util.UUID;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.vid.aai.AaiResponse;
43 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
44 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
45 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
46 import org.onap.vid.aai.model.AaiGetAicZone.AicZones;
47 import org.onap.vid.aai.model.AaiGetAicZone.Zone;
48 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
49 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetails;
50 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsError;
51 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsOk;
52 import org.onap.vid.aai.util.AAIRestInterface;
53 import org.onap.vid.roles.RoleProvider;
54 import org.onap.vid.services.AaiService;
55 import org.onap.vid.utils.SystemPropertiesWrapper;
56 import org.springframework.http.HttpStatus;
57 import org.springframework.http.MediaType;
58 import org.springframework.test.web.servlet.MockMvc;
59 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
61 @RunWith(MockitoJUnitRunner.class)
62 public class AaiControllerTest {
64 private static final String ID_1 = "id1";
65 private static final String ID_2 = "id2";
66 private final ObjectMapper objectMapper = new ObjectMapper();
68 private AaiService aaiService;
70 private AAIRestInterface aaiRestInterface;
72 private RoleProvider roleProvider;
74 private SystemPropertiesWrapper systemPropertiesWrapper;
75 private MockMvc mockMvc;
76 private AaiController aaiController;
80 aaiController = new AaiController(aaiService, aaiRestInterface, roleProvider, systemPropertiesWrapper);
81 mockMvc = MockMvcBuilders.standaloneSetup(aaiController).build();
85 public void getPortMirroringConfigData_givenIds_shouldReturnConfigDataMappedById() throws Exception {
86 PortMirroringConfigDataOk okConfigData = new PortMirroringConfigDataOk("foo");
87 PortMirroringConfigDataError errorConfigData = new PortMirroringConfigDataError("bar", "{ baz: qux }");
88 Map<String, PortMirroringConfigData> expectedJson = ImmutableMap.of(
90 ID_2, errorConfigData);
91 given(aaiService.getPortMirroringConfigData(ID_1)).willReturn(okConfigData);
92 given(aaiService.getPortMirroringConfigData(ID_2)).willReturn(errorConfigData);
95 .perform(get("/aai_getPortMirroringConfigsData")
96 .param("configurationIds", ID_1, ID_2)
97 .contentType(MediaType.APPLICATION_JSON)
98 .accept(MediaType.APPLICATION_JSON))
99 .andExpect(status().isOk())
100 .andExpect(content().json(objectMapper.writeValueAsString(expectedJson)));
104 public void getPortMirroringSourcePorts_givenIds_shouldReturnPortDetailsMappedById() throws Exception {
105 PortDetailsOk portDetailsOk = new PortDetailsOk("foo", "testInterface", true);
106 PortDetailsError portDetailsError = new PortDetailsError("bar", "{ baz: qux }");
107 Multimap<String, PortDetails> expectedJson = ImmutableMultimap.of(
109 ID_2, portDetailsError);
110 given(aaiService.getPortMirroringSourcePorts(ID_1)).willReturn(Lists.newArrayList(portDetailsOk));
111 given(aaiService.getPortMirroringSourcePorts(ID_2)).willReturn(Lists.newArrayList(portDetailsError));
114 .perform(get("/aai_getPortMirroringSourcePorts")
115 .param("configurationIds", ID_1, ID_2)
116 .contentType(MediaType.APPLICATION_JSON)
117 .accept(MediaType.APPLICATION_JSON))
118 .andExpect(status().isOk())
119 .andExpect(content().json(objectMapper.writeValueAsString(expectedJson.asMap())));
123 public void getNodeTemplateInstances_givenParams_shouldReturnCorrectResponse() throws Exception {
124 String globalCustomerId = "testCustomerId";
125 String serviceType = "testServiceType";
126 String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
127 String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
128 String cloudRegion = "testRegion";
129 String urlTemplate = "/aai_get_vnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}";
130 String expectedResponseBody = "myResponse";
131 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());
133 .getNodeTemplateInstances(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion))
134 .willReturn(aaiResponse);
137 .perform(get(urlTemplate, globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion)
138 .contentType(MediaType.APPLICATION_JSON)
139 .accept(MediaType.APPLICATION_JSON))
140 .andExpect(status().isOk())
141 .andExpect(content().string(expectedResponseBody));
145 public void getAicZones_shouldReturnAaiZones_whenAaiHttpStatusIsOK() throws Exception {
146 AicZones aicZones = new AicZones(ImmutableList.of(new Zone("TEST_ZONE_ID", "TEST_ZONE_NAME")));
147 given(aaiService.getAaiZones()).willReturn(new AaiResponse(aicZones, "", HttpStatus.OK.value()));
149 mockMvc.perform(get("/aai_get_aic_zones")
150 .contentType(MediaType.APPLICATION_JSON)
151 .accept(MediaType.APPLICATION_JSON))
152 .andExpect(status().isOk())
153 .andExpect(content().json(objectMapper.writeValueAsString(aicZones)));
157 public void getAicZones_shouldReturnErrorResponse_whenAaiHttpStatusOtherThanOK() throws Exception {
158 String expectedErrorMessage = "Calling AAI Failed";
159 given(aaiService.getAaiZones())
160 .willReturn(new AaiResponse(null, expectedErrorMessage, HttpStatus.INTERNAL_SERVER_ERROR.value()));
162 mockMvc.perform(get("/aai_get_aic_zones")
163 .contentType(MediaType.APPLICATION_JSON)
164 .accept(MediaType.APPLICATION_JSON))
165 .andExpect(status().isInternalServerError())
166 .andExpect(content().string(expectedErrorMessage));
170 public void getSpecificPnf_shouldReturnPnfObjectForPnfId() throws Exception {
171 String pnfId = "MyPnfId";
172 Pnf pnf = createPnf(pnfId);
173 AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "", HttpStatus.OK.value());
174 given(aaiService.getSpecificPnf(pnfId)).willReturn(aaiResponse);
176 mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
177 .contentType(MediaType.APPLICATION_JSON)
178 .accept(MediaType.APPLICATION_JSON))
179 .andExpect(status().isOk())
180 .andExpect(content().json(objectMapper.writeValueAsString(pnf)));
184 public void getSpecificPnf_shouldHandleAAIServiceException() throws Exception {
185 String pnfId = "MyPnfId";
186 String expectedErrorMessage = "AAI Service Error";
187 given(aaiService.getSpecificPnf(pnfId)).willThrow(new RuntimeException(expectedErrorMessage));
189 mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
190 .contentType(MediaType.APPLICATION_JSON)
191 .accept(MediaType.APPLICATION_JSON))
192 .andExpect(status().isInternalServerError())
193 .andExpect(content().string(expectedErrorMessage));
196 private Pnf createPnf(String pnfId) {
199 pnf.setPnfName("TestPnf");
203 public void getPNFInstances_shouldReturnOKResponseFromAAIService() throws Exception {
204 String globalCustomerId = "testCustomerId";
205 String serviceType = "testServiceType";
206 String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
207 String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
208 String cloudRegion = "testRegion";
209 String equipVendor = "testVendor";
210 String equipModel = "model123";
211 String urlTemplate = "/aai_get_pnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}/{equipVendor}/{equipModel}";
212 String expectedResponseBody = "myResponse";
213 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());
216 .getPNFData(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion, equipVendor,
217 equipModel)).willReturn(aaiResponse);
220 get(urlTemplate, globalCustomerId, serviceType, modelVersionId,
221 modelInvariantId, cloudRegion, equipVendor, equipModel)
222 .contentType(MediaType.APPLICATION_JSON)
223 .accept(MediaType.APPLICATION_JSON))
224 .andExpect(status().isOk())
225 .andExpect(content().string(expectedResponseBody));