1 package org.onap.vid.controller;
4 * ============LICENSE_START=======================================================
6 * ================================================================================
7 * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
8 * ================================================================================
9 * Modifications Copyright 2018 Nokia
10 * ================================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 * ============LICENSE_END=========================================================
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.common.collect.ImmutableList;
27 import com.google.common.collect.ImmutableMap;
28 import org.apache.log4j.BasicConfigurator;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.ArgumentMatcher;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.vid.category.AddCategoryOptionResponse;
36 import org.onap.vid.category.AddCategoryOptionsRequest;
37 import org.onap.vid.category.CategoryParameterOptionRep;
38 import org.onap.vid.category.CategoryParametersResponse;
39 import org.onap.vid.model.CategoryParameter;
40 import org.onap.vid.model.CategoryParameterOption;
41 import org.onap.vid.services.CategoryParameterService;
42 import org.onap.vid.services.CategoryParameterServiceImpl;
43 import org.springframework.http.MediaType;
44 import org.springframework.test.web.servlet.MockMvc;
45 import org.springframework.test.web.servlet.ResultActions;
46 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
47 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
48 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
50 import javax.ws.rs.ForbiddenException;
51 import java.util.Collections;
52 import java.util.function.BiFunction;
54 import static org.mockito.ArgumentMatchers.argThat;
55 import static org.mockito.ArgumentMatchers.eq;
56 import static org.mockito.BDDMockito.*;
57 import static org.mockito.Mockito.times;
58 import static org.onap.vid.model.CategoryParameter.Family.PARAMETER_STANDARDIZATION;
59 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
60 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
61 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
63 @RunWith(MockitoJUnitRunner.class)
64 public class MaintenanceControllerTest {
66 final private String MAINTENANCE_CATEGORY_PATH = "/maintenance/category_parameter";
67 final private String CATEGORY_PARAMETER_PATH = MAINTENANCE_CATEGORY_PATH + "/{name}";
68 final private String DELETE_CATEGORY_PATH = "/maintenance/delete_category_parameter/{name}";
71 private CategoryParameterService service;
72 private MaintenanceController maintenanceController;
73 private MockMvc mockMvc;
74 private ObjectMapper objectMapper;
78 maintenanceController = new MaintenanceController(service);
79 BasicConfigurator.configure();
80 mockMvc = MockMvcBuilders.standaloneSetup(maintenanceController).build();
81 objectMapper = new ObjectMapper();
85 public void addCategoryOptions_shouldReturnMultiStatus_whenErrorsExist() throws Exception {
86 String categoryName = "catName1";
87 AddCategoryOptionsRequest req = new AddCategoryOptionsRequest();
88 req.options = ImmutableList.of("first option", "second option");
89 AddCategoryOptionResponse addCategoryOptionResponse = new AddCategoryOptionResponse(
90 ImmutableList.of("error one", "error two"));
92 given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req))))
93 .willReturn(addCategoryOptionResponse);
95 prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName,
96 objectMapper.writeValueAsString(req))
97 .andExpect(status().isMultiStatus())
99 content().json(objectMapper
100 .writeValueAsString(addCategoryOptionResponse)));
104 public void addCategoryOptions_shouldReturnOk_whenNoErrorsExist() throws Exception {
105 String categoryName = "catName2";
106 AddCategoryOptionsRequest req = new AddCategoryOptionsRequest();
107 req.options = ImmutableList.of("first option", "second option", "third option");
108 AddCategoryOptionResponse addCategoryOptionResponse = new AddCategoryOptionResponse(Collections.emptyList());
110 given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req))))
111 .willReturn(addCategoryOptionResponse);
112 prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName,
113 objectMapper.writeValueAsString(req))
114 .andExpect(status().isOk())
115 .andExpect(content().json(
116 objectMapper.writeValueAsString(addCategoryOptionResponse)));
120 public void addCategoryOptions_shouldReturnNotFound_whenUnfoundedCategoryExceptionIsThrown() throws Exception {
121 String unfoundedMsg = "unfounded category exception message";
122 String categoryName = "catName3";
123 AddCategoryOptionsRequest req = new AddCategoryOptionsRequest();
124 req.options = ImmutableList.of("first option");
126 given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req))))
127 .willThrow(new CategoryParameterServiceImpl.UnfoundedCategoryException(unfoundedMsg));
129 prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName,
130 objectMapper.writeValueAsString(req))
131 .andExpect(status().isNotFound())
132 .andExpect(content().json(
133 objectMapper.writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of(unfoundedMsg)))));
137 public void addCategoryOptions_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception {
138 String categoryName = "catName13";
139 AddCategoryOptionsRequest req = new AddCategoryOptionsRequest();
140 req.options = ImmutableList.of("option second", "first option");
142 given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req))))
143 .willThrow(new RuntimeException());
145 prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName,
146 objectMapper.writeValueAsString(req))
147 .andExpect(status().isInternalServerError());
151 public void updateNameForOption_shouldReturnMultiStatus_whenErrorsExist() throws Exception {
152 String categoryName = "catName4";
153 CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id1", "name1");
154 AddCategoryOptionResponse addCategoryOptionResponse = new AddCategoryOptionResponse(
155 ImmutableList.of("error one", "error two"));
158 .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
159 .willReturn(addCategoryOptionResponse);
161 prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
162 objectMapper.writeValueAsString(categoryParameterOptionRep))
163 .andExpect(status().isMultiStatus())
165 content().json(objectMapper
166 .writeValueAsString(addCategoryOptionResponse)));
170 public void updateNameForOption_shouldReturnOk_whenNoErrorsExist() throws Exception {
171 String categoryName = "catName5";
172 CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id2", "name2");
173 AddCategoryOptionResponse addCategoryOptionResponse = new AddCategoryOptionResponse(Collections.emptyList());
176 .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
177 .willReturn(addCategoryOptionResponse);
178 prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
179 objectMapper.writeValueAsString(categoryParameterOptionRep))
180 .andExpect(status().isOk())
181 .andExpect(content().json(
182 objectMapper.writeValueAsString(addCategoryOptionResponse)));
186 public void updateNameForOption_shouldReturnForbidden_whenForbiddenExceptionIsThrown() throws Exception {
187 String categoryName = "catName6";
188 CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id3", "name3");
191 .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
192 .willThrow(new ForbiddenException());
193 prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
194 objectMapper.writeValueAsString(categoryParameterOptionRep))
195 .andExpect(status().isForbidden())
196 .andExpect(content().json(
198 .writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of("HTTP 403 Forbidden")))));
202 public void updateNameForOption_shouldReturnNotFound_whenUnfoundedIsThrown() throws Exception {
203 String unfoundedOptionMsg = "unfounded category option exception message";
204 String categoryName = "catName7";
205 CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id4", "name4");
208 .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
209 .willThrow(new CategoryParameterServiceImpl.UnfoundedCategoryOptionException(unfoundedOptionMsg));
211 prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
212 objectMapper.writeValueAsString(categoryParameterOptionRep))
213 .andExpect(status().isNotFound())
214 .andExpect(content().json(
215 objectMapper.writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of(unfoundedOptionMsg)))));
219 public void updateNameForOption_shouldReturnConflict_whenAlreadyExistOptionNameIsThrown() throws Exception {
220 String conflictMsg = "already exists option name exception message";
221 String categoryName = "catName8";
222 CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id5", "name5");
225 .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
226 .willThrow(new CategoryParameterServiceImpl.AlreadyExistOptionNameException(conflictMsg));
228 prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
229 objectMapper.writeValueAsString(categoryParameterOptionRep))
230 .andExpect(status().isConflict())
231 .andExpect(content().json(
232 objectMapper.writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of(conflictMsg)))));
236 public void updateNameForOption_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception {
237 String categoryName = "catName18";
238 CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id6", "name6");
241 .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
242 .willThrow(new RuntimeException());
244 prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
245 objectMapper.writeValueAsString(categoryParameterOptionRep))
246 .andExpect(status().isInternalServerError());
250 public void getCategoryParameter_shouldReturnExistingMap() throws Exception {
251 CategoryParametersResponse categoryParametersResponse =
252 new CategoryParametersResponse(
254 "key1", ImmutableList.of(
255 new CategoryParameterOptionRep("testId", "testName"))));
257 given(service.getCategoryParameters(PARAMETER_STANDARDIZATION))
258 .willReturn(categoryParametersResponse);
260 mockMvc.perform(get(MAINTENANCE_CATEGORY_PATH)
261 .param("familyName", "PARAMETER_STANDARDIZATION")
262 .accept(MediaType.APPLICATION_JSON))
263 .andExpect(status().isOk())
264 .andExpect(content().json(objectMapper.writeValueAsString(categoryParametersResponse)));
268 public void getCategoryParameter_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception {
269 given(service.getCategoryParameters(PARAMETER_STANDARDIZATION))
270 .willThrow(new RuntimeException());
272 mockMvc.perform(get(MAINTENANCE_CATEGORY_PATH)
273 .param("familyName", "PARAMETER_STANDARDIZATION")
274 .accept(MediaType.APPLICATION_JSON))
275 .andExpect(status().isInternalServerError());
279 public void deleteCategoryOption_shouldReturnOk_whenNoExceptionIsThrown() throws Exception {
280 String categoryName = "catName9";
281 CategoryParameterOption categoryParameterOption = new CategoryParameterOption("id1", "name1",
282 new CategoryParameter());
284 prepareRequestExpectations(MockMvcRequestBuilders::delete, DELETE_CATEGORY_PATH, categoryName,
285 objectMapper.writeValueAsString(categoryParameterOption))
286 .andExpect(status().isOk());
288 then(service).should(times(1))
289 .deleteCategoryOption(eq(categoryName), argThat(requestMatcher(categoryParameterOption)));
293 public void deleteCategoryOption_shouldReturnNotFound_whenUnfoundedExceptionIsThrown() throws Exception {
294 String unfoundedMsg = "unfounded category exception message";
295 String categoryName = "catName10";
296 CategoryParameterOption categoryParameterOption = new CategoryParameterOption("id2", "name2",
297 new CategoryParameter());
299 willThrow(new CategoryParameterServiceImpl.UnfoundedCategoryException(unfoundedMsg))
300 .given(service).deleteCategoryOption(eq(categoryName), argThat(requestMatcher(categoryParameterOption)));
302 prepareRequestExpectations(MockMvcRequestBuilders::delete, DELETE_CATEGORY_PATH, categoryName,
303 objectMapper.writeValueAsString(categoryParameterOption))
304 .andExpect(status().isNotFound())
305 .andExpect(content().json(
306 objectMapper.writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of(unfoundedMsg)))));
310 public void deleteCategoryOption_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception {
311 String categoryName = "catName19";
312 CategoryParameterOption categoryParameterOption = new CategoryParameterOption("id3", "name3",
313 new CategoryParameter());
315 willThrow(new RuntimeException()).given(service)
316 .deleteCategoryOption(eq(categoryName), argThat(requestMatcher(categoryParameterOption)));
318 prepareRequestExpectations(MockMvcRequestBuilders::delete, DELETE_CATEGORY_PATH, categoryName,
319 objectMapper.writeValueAsString(categoryParameterOption))
320 .andExpect(status().isInternalServerError());
323 private <T> ArgumentMatcher<T> requestMatcher(T t) {
324 return new ArgumentMatcher<T>() {
326 public boolean matches(Object o) {
332 private ResultActions prepareRequestExpectations(
333 BiFunction<String, String, MockHttpServletRequestBuilder> httpMethod,
334 String path, String name, String jsonContent) throws Exception {
335 return mockMvc.perform(httpMethod.apply(path, name)
336 .contentType(MediaType.APPLICATION_JSON)
337 .content(jsonContent));