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 = createPnf(pnfId);
177 AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "", HttpStatus.OK.value());
178 given(aaiService.getSpecificPnf(pnfId)).willReturn(aaiResponse);
180 mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
181 .contentType(MediaType.APPLICATION_JSON)
182 .accept(MediaType.APPLICATION_JSON))
183 .andExpect(status().isOk())
184 .andExpect(content().json(objectMapper.writeValueAsString(pnf)));
188 public void getSpecificPnf_shouldHandleAAIServiceException() throws Exception {
189 String pnfId = "MyPnfId";
190 String expectedErrorMessage = "AAI Service Error";
191 given(aaiService.getSpecificPnf(pnfId)).willThrow(new RuntimeException(expectedErrorMessage));
193 mockMvc.perform(get("/aai_get_pnfs/pnf/{pnf_id}", pnfId)
194 .contentType(MediaType.APPLICATION_JSON)
195 .accept(MediaType.APPLICATION_JSON))
196 .andExpect(status().isInternalServerError())
197 .andExpect(content().string(expectedErrorMessage));
200 private Pnf createPnf(String pnfId) {
203 pnf.setPnfName("TestPnf");
207 public void getPNFInstances_shouldReturnOKResponseFromAAIService() throws Exception {
208 String globalCustomerId = "testCustomerId";
209 String serviceType = "testServiceType";
210 String modelVersionId = UUID.nameUUIDFromBytes("modelVersionId".getBytes()).toString();
211 String modelInvariantId = UUID.nameUUIDFromBytes("modelInvariantId".getBytes()).toString();
212 String cloudRegion = "testRegion";
213 String equipVendor = "testVendor";
214 String equipModel = "model123";
215 String urlTemplate = "/aai_get_pnf_instances/{globalCustomerId}/{serviceType}/{modelVersionId}/{modelInvariantId}/{cloudRegion}/{equipVendor}/{equipModel}";
216 String expectedResponseBody = "myResponse";
217 AaiResponse<String> aaiResponse = new AaiResponse<>(expectedResponseBody, "", HttpStatus.OK.value());
220 .getPNFData(globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion, equipVendor,
221 equipModel)).willReturn(aaiResponse);
224 get(urlTemplate, globalCustomerId, serviceType, modelVersionId,
225 modelInvariantId, cloudRegion, equipVendor, equipModel)
226 .contentType(MediaType.APPLICATION_JSON)
227 .accept(MediaType.APPLICATION_JSON))
228 .andExpect(status().isOk())
229 .andExpect(content().string(expectedResponseBody));
233 public void getVersionByInvariantId_shouldReturnOKResponse() throws Exception {
234 String expectedResponse = "OKResponse";
235 VersionByInvariantIdsRequest request = new VersionByInvariantIdsRequest();
236 request.versions = ImmutableList.of("ver1", "ver2");
237 Response response = mock(Response.class);
238 given(response.readEntity(String.class)).willReturn(expectedResponse);
240 .getVersionByInvariantId(request.versions)).willReturn(response);
243 post("/aai_get_version_by_invariant_id")
244 .content(new ObjectMapper().writeValueAsString(request))
245 .contentType(MediaType.APPLICATION_JSON)
246 .accept(MediaType.APPLICATION_JSON))
247 .andExpect(status().isOk())
248 .andExpect(content().string(expectedResponse));