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.mockito.Mockito.mock;
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
28 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
29 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.google.common.collect.ImmutableList;
33 import com.google.common.collect.ImmutableMap;
34 import com.google.common.collect.ImmutableMultimap;
35 import com.google.common.collect.Lists;
36 import com.google.common.collect.Multimap;
38 import java.util.UUID;
39 import javax.ws.rs.core.Response;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.vid.aai.AaiResponse;
46 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
47 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
48 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
49 import org.onap.vid.aai.model.AaiGetAicZone.AicZones;
50 import org.onap.vid.aai.model.AaiGetAicZone.Zone;
51 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
52 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetails;
53 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsError;
54 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsOk;
55 import org.onap.vid.aai.util.AAIRestInterface;
56 import org.onap.vid.model.VersionByInvariantIdsRequest;
57 import org.onap.vid.roles.RoleProvider;
58 import org.onap.vid.services.AaiService;
59 import org.onap.vid.utils.SystemPropertiesWrapper;
60 import org.springframework.http.HttpStatus;
61 import org.springframework.http.MediaType;
62 import org.springframework.test.web.servlet.MockMvc;
63 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
65 @RunWith(MockitoJUnitRunner.class)
66 public class AaiControllerTest {
68 private static final String ID_1 = "id1";
69 private static final String ID_2 = "id2";
70 private final ObjectMapper objectMapper = new ObjectMapper();
72 private AaiService aaiService;
74 private AAIRestInterface aaiRestInterface;
76 private RoleProvider roleProvider;
78 private SystemPropertiesWrapper systemPropertiesWrapper;
79 private MockMvc mockMvc;
80 private AaiController aaiController;
84 aaiController = new AaiController(aaiService, aaiRestInterface, roleProvider, systemPropertiesWrapper);
85 mockMvc = MockMvcBuilders.standaloneSetup(aaiController).build();
89 public void getPortMirroringConfigData_givenIds_shouldReturnConfigDataMappedById() throws Exception {
90 PortMirroringConfigDataOk okConfigData = new PortMirroringConfigDataOk("foo");
91 PortMirroringConfigDataError errorConfigData = new PortMirroringConfigDataError("bar", "{ baz: qux }");
92 Map<String, PortMirroringConfigData> expectedJson = ImmutableMap.of(
94 ID_2, errorConfigData);
95 given(aaiService.getPortMirroringConfigData(ID_1)).willReturn(okConfigData);
96 given(aaiService.getPortMirroringConfigData(ID_2)).willReturn(errorConfigData);
99 .perform(get("/aai_getPortMirroringConfigsData")
100 .param("configurationIds", ID_1, ID_2)
101 .contentType(MediaType.APPLICATION_JSON)
102 .accept(MediaType.APPLICATION_JSON))
103 .andExpect(status().isOk())
104 .andExpect(content().json(objectMapper.writeValueAsString(expectedJson)));
108 public void getPortMirroringSourcePorts_givenIds_shouldReturnPortDetailsMappedById() throws Exception {
109 PortDetailsOk portDetailsOk = new PortDetailsOk("foo", "testInterface", true);
110 PortDetailsError portDetailsError = new PortDetailsError("bar", "{ baz: qux }");
111 Multimap<String, PortDetails> expectedJson = ImmutableMultimap.of(
113 ID_2, portDetailsError);
114 given(aaiService.getPortMirroringSourcePorts(ID_1)).willReturn(Lists.newArrayList(portDetailsOk));
115 given(aaiService.getPortMirroringSourcePorts(ID_2)).willReturn(Lists.newArrayList(portDetailsError));
118 .perform(get("/aai_getPortMirroringSourcePorts")
119 .param("configurationIds", ID_1, ID_2)
120 .contentType(MediaType.APPLICATION_JSON)
121 .accept(MediaType.APPLICATION_JSON))
122 .andExpect(status().isOk())
123 .andExpect(content().json(objectMapper.writeValueAsString(expectedJson.asMap())));
127 public void getNodeTemplateInstances_givenParams_shouldReturnCorrectResponse() throws Exception {
128 String globalCustomerId = "testCustomerId";
129 String serviceType = "testServiceType";
130 String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
131 String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
132 String cloudRegion = "testRegion";
133 String urlTemplate = "/aai_get_vnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}";
134 String expectedResponseBody = "myResponse";
135 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());
137 .getNodeTemplateInstances(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion))
138 .willReturn(aaiResponse);
141 .perform(get(urlTemplate, globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion)
142 .contentType(MediaType.APPLICATION_JSON)
143 .accept(MediaType.APPLICATION_JSON))
144 .andExpect(status().isOk())
145 .andExpect(content().string(expectedResponseBody));
149 public void getAicZones_shouldReturnAaiZones_whenAaiHttpStatusIsOK() throws Exception {
150 AicZones aicZones = new AicZones(ImmutableList.of(new Zone("TEST_ZONE_ID", "TEST_ZONE_NAME")));
151 given(aaiService.getAaiZones()).willReturn(new AaiResponse(aicZones, "", HttpStatus.OK.value()));
153 mockMvc.perform(get("/aai_get_aic_zones")
154 .contentType(MediaType.APPLICATION_JSON)
155 .accept(MediaType.APPLICATION_JSON))
156 .andExpect(status().isOk())
157 .andExpect(content().json(objectMapper.writeValueAsString(aicZones)));
161 public void getAicZones_shouldReturnErrorResponse_whenAaiHttpStatusOtherThanOK() throws Exception {
162 String expectedErrorMessage = "Calling AAI Failed";
163 given(aaiService.getAaiZones())
164 .willReturn(new AaiResponse(null, expectedErrorMessage, HttpStatus.INTERNAL_SERVER_ERROR.value()));
166 mockMvc.perform(get("/aai_get_aic_zones")
167 .contentType(MediaType.APPLICATION_JSON)
168 .accept(MediaType.APPLICATION_JSON))
169 .andExpect(status().isInternalServerError())
170 .andExpect(content().string(expectedErrorMessage));
174 public void getSpecificPnf_shouldReturnPnfObjectForPnfId() throws Exception {
175 String pnfId = "MyPnfId";
176 Pnf pnf = Pnf.builder()
178 .withPnfName("TestPnf")
179 .withPnfName2("pnfName2")
180 .withPnfName2Source("pnfNameSource")
181 .withEquipModel("model")
182 .withEquipType("type")
183 .withEquipVendor("vendor")
185 AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "", HttpStatus.OK.value());
186 given(aaiService.getSpecificPnf(pnfId)).willReturn(aaiResponse);
188 mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
189 .contentType(MediaType.APPLICATION_JSON)
190 .accept(MediaType.APPLICATION_JSON))
191 .andExpect(status().isOk())
192 .andExpect(content().json(objectMapper.writeValueAsString(pnf)));
196 public void getSpecificPnf_shouldHandleAAIServiceException() throws Exception {
197 String pnfId = "MyPnfId";
198 String expectedErrorMessage = "AAI Service Error";
199 given(aaiService.getSpecificPnf(pnfId)).willThrow(new RuntimeException(expectedErrorMessage));
201 mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
202 .contentType(MediaType.APPLICATION_JSON)
203 .accept(MediaType.APPLICATION_JSON))
204 .andExpect(status().isInternalServerError())
205 .andExpect(content().string(expectedErrorMessage));
208 public void getPNFInstances_shouldReturnOKResponseFromAAIService() throws Exception {
209 String globalCustomerId = "testCustomerId";
210 String serviceType = "testServiceType";
211 String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
212 String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
213 String cloudRegion = "testRegion";
214 String equipVendor = "testVendor";
215 String equipModel = "model123";
216 String urlTemplate = "/aai_get_pnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}/{equipVendor}/{equipModel}";
217 String expectedResponseBody = "myResponse";
218 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());
221 .getPNFData(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion, equipVendor,
222 equipModel)).willReturn(aaiResponse);
225 get(urlTemplate, globalCustomerId, serviceType, modelVersionId,
226 modelInvariantId, cloudRegion, equipVendor, equipModel)
227 .contentType(MediaType.APPLICATION_JSON)
228 .accept(MediaType.APPLICATION_JSON))
229 .andExpect(status().isOk())
230 .andExpect(content().string(expectedResponseBody));
234 public void getVersionByInvariantId_shouldReturnOKResponse() throws Exception {
235 String expectedResponse = "OKResponse";
236 VersionByInvariantIdsRequest request = new VersionByInvariantIdsRequest();
237 request.versions = ImmutableList.of("ver1", "ver2");
238 Response response = mock(Response.class);
239 given(response.readEntity(String.class)).willReturn(expectedResponse);
241 .getVersionByInvariantId(request.versions)).willReturn(response);
244 post("/aai_get_version_by_invariant_id")
245 .content(new ObjectMapper().writeValueAsString(request))
246 .contentType(MediaType.APPLICATION_JSON)
247 .accept(MediaType.APPLICATION_JSON))
248 .andExpect(status().isOk())
249 .andExpect(content().string(expectedResponse));