Merge "Logging improvements"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / MaintenanceControllerTest.java
1 package org.onap.vid.controller;
2
3 /*-
4  * ============LICENSE_START=======================================================
5  * VID
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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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=========================================================
23  */
24
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;
49
50 import javax.ws.rs.ForbiddenException;
51 import java.util.Collections;
52 import java.util.function.BiFunction;
53
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;
62
63 @RunWith(MockitoJUnitRunner.class)
64 public class MaintenanceControllerTest {
65
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}";
69
70     @Mock
71     private CategoryParameterService service;
72     private MaintenanceController maintenanceController;
73     private MockMvc mockMvc;
74     private ObjectMapper objectMapper;
75
76     @Before
77     public void setUp() {
78         maintenanceController = new MaintenanceController(service);
79         BasicConfigurator.configure();
80         mockMvc = MockMvcBuilders.standaloneSetup(maintenanceController).build();
81         objectMapper = new ObjectMapper();
82     }
83
84     @Test
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"));
91
92         given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req))))
93             .willReturn(addCategoryOptionResponse);
94
95         prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName,
96             objectMapper.writeValueAsString(req))
97             .andExpect(status().isMultiStatus())
98             .andExpect(
99                 content().json(objectMapper
100                     .writeValueAsString(addCategoryOptionResponse)));
101     }
102
103     @Test
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());
109
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)));
117     }
118
119     @Test
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");
125
126         given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req))))
127             .willThrow(new CategoryParameterServiceImpl.UnfoundedCategoryException(unfoundedMsg));
128
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)))));
134     }
135
136     @Test
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");
141
142         given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req))))
143             .willThrow(new RuntimeException());
144
145         prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName,
146             objectMapper.writeValueAsString(req))
147             .andExpect(status().isInternalServerError());
148     }
149
150     @Test
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"));
156
157         given(service
158             .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
159             .willReturn(addCategoryOptionResponse);
160
161         prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
162             objectMapper.writeValueAsString(categoryParameterOptionRep))
163             .andExpect(status().isMultiStatus())
164             .andExpect(
165                 content().json(objectMapper
166                     .writeValueAsString(addCategoryOptionResponse)));
167     }
168
169     @Test
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());
174
175         given(service
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)));
183     }
184
185     @Test
186     public void updateNameForOption_shouldReturnForbidden_whenForbiddenExceptionIsThrown() throws Exception {
187         String categoryName = "catName6";
188         CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id3", "name3");
189
190         given(service
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(
197                 objectMapper
198                     .writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of("HTTP 403 Forbidden")))));
199     }
200
201     @Test
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");
206
207         given(service
208             .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
209             .willThrow(new CategoryParameterServiceImpl.UnfoundedCategoryOptionException(unfoundedOptionMsg));
210
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)))));
216     }
217
218     @Test
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");
223
224         given(service
225             .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
226             .willThrow(new CategoryParameterServiceImpl.AlreadyExistOptionNameException(conflictMsg));
227
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)))));
233     }
234
235     @Test
236     public void updateNameForOption_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception {
237         String categoryName = "catName18";
238         CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id6", "name6");
239
240         given(service
241             .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
242             .willThrow(new RuntimeException());
243
244         prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
245             objectMapper.writeValueAsString(categoryParameterOptionRep))
246             .andExpect(status().isInternalServerError());
247     }
248
249     @Test
250     public void getCategoryParameter_shouldReturnExistingMap() throws Exception {
251         CategoryParametersResponse categoryParametersResponse =
252             new CategoryParametersResponse(
253                 ImmutableMap.of(
254                     "key1", ImmutableList.of(
255                         new CategoryParameterOptionRep("testId", "testName"))));
256
257         given(service.getCategoryParameters(PARAMETER_STANDARDIZATION))
258             .willReturn(categoryParametersResponse);
259
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)));
265     }
266
267     @Test
268     public void getCategoryParameter_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception {
269         given(service.getCategoryParameters(PARAMETER_STANDARDIZATION))
270             .willThrow(new RuntimeException());
271
272         mockMvc.perform(get(MAINTENANCE_CATEGORY_PATH)
273             .param("familyName", "PARAMETER_STANDARDIZATION")
274             .accept(MediaType.APPLICATION_JSON))
275             .andExpect(status().isInternalServerError());
276     }
277
278     @Test
279     public void deleteCategoryOption_shouldReturnOk_whenNoExceptionIsThrown() throws Exception {
280         String categoryName = "catName9";
281         CategoryParameterOption categoryParameterOption = new CategoryParameterOption("id1", "name1",
282             new CategoryParameter());
283
284         prepareRequestExpectations(MockMvcRequestBuilders::delete, DELETE_CATEGORY_PATH, categoryName,
285             objectMapper.writeValueAsString(categoryParameterOption))
286             .andExpect(status().isOk());
287
288         then(service).should(times(1))
289             .deleteCategoryOption(eq(categoryName), argThat(requestMatcher(categoryParameterOption)));
290     }
291
292     @Test
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());
298
299         willThrow(new CategoryParameterServiceImpl.UnfoundedCategoryException(unfoundedMsg))
300             .given(service).deleteCategoryOption(eq(categoryName), argThat(requestMatcher(categoryParameterOption)));
301
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)))));
307     }
308
309     @Test
310     public void deleteCategoryOption_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception {
311         String categoryName = "catName19";
312         CategoryParameterOption categoryParameterOption = new CategoryParameterOption("id3", "name3",
313             new CategoryParameter());
314
315         willThrow(new RuntimeException()).given(service)
316             .deleteCategoryOption(eq(categoryName), argThat(requestMatcher(categoryParameterOption)));
317
318         prepareRequestExpectations(MockMvcRequestBuilders::delete, DELETE_CATEGORY_PATH, categoryName,
319             objectMapper.writeValueAsString(categoryParameterOption))
320             .andExpect(status().isInternalServerError());
321     }
322
323     private <T> ArgumentMatcher<T> requestMatcher(T t) {
324         return new ArgumentMatcher<T>() {
325             @Override
326             public boolean matches(Object o) {
327                 return t.equals(o);
328             }
329         };
330     }
331
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));
338     }
339 }