Merge "Fix and improvement for MsoBusinessLogic tests"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controllers / MaintenanceControllerTest.java
1 package org.onap.vid.controllers;
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 static org.mockito.BDDMockito.given;
26 import static org.mockito.BDDMockito.then;
27 import static org.mockito.BDDMockito.willThrow;
28 import static org.mockito.Matchers.argThat;
29 import static org.mockito.Matchers.eq;
30 import static org.mockito.Mockito.times;
31 import static org.onap.vid.model.CategoryParameter.Family.PARAMETER_STANDARDIZATION;
32 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
33 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
34 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
35
36 import com.google.common.collect.ImmutableList;
37 import com.google.common.collect.ImmutableMap;
38 import java.util.Collections;
39 import java.util.function.BiFunction;
40 import javax.ws.rs.ForbiddenException;
41 import org.apache.log4j.BasicConfigurator;
42 import org.codehaus.jackson.map.ObjectMapper;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.ArgumentMatcher;
47 import org.mockito.Mock;
48 import org.mockito.runners.MockitoJUnitRunner;
49 import org.onap.vid.category.AddCategoryOptionResponse;
50 import org.onap.vid.category.AddCategoryOptionsRequest;
51 import org.onap.vid.category.CategoryParameterOptionRep;
52 import org.onap.vid.category.CategoryParametersResponse;
53 import org.onap.vid.model.CategoryParameter;
54 import org.onap.vid.model.CategoryParameterOption;
55 import org.onap.vid.services.CategoryParameterService;
56 import org.onap.vid.services.CategoryParameterServiceImpl;
57 import org.springframework.http.MediaType;
58 import org.springframework.test.web.servlet.MockMvc;
59 import org.springframework.test.web.servlet.ResultActions;
60 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
61 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
62 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
63
64 @RunWith(MockitoJUnitRunner.class)
65 public class MaintenanceControllerTest {
66
67     final private String MAINTENANCE_CATEGORY_PATH = "/maintenance/category_parameter";
68     final private String CATEGORY_PARAMETER_PATH = MAINTENANCE_CATEGORY_PATH + "/{name}";
69     final private String DELETE_CATEGORY_PATH = "/maintenance/delete_category_parameter/{name}";
70
71     @Mock
72     private CategoryParameterService service;
73     private MaintenanceController maintenanceController;
74     private MockMvc mockMvc;
75     private ObjectMapper objectMapper;
76
77     @Before
78     public void setUp() {
79         maintenanceController = new MaintenanceController(service);
80         BasicConfigurator.configure();
81         mockMvc = MockMvcBuilders.standaloneSetup(maintenanceController).build();
82         objectMapper = new ObjectMapper();
83     }
84
85     @Test
86     public void addCategoryOptions_shouldReturnMultiStatus_whenErrorsExist() throws Exception {
87         String categoryName = "catName1";
88         AddCategoryOptionsRequest req = new AddCategoryOptionsRequest();
89         req.options = ImmutableList.of("first option", "second option");
90         AddCategoryOptionResponse addCategoryOptionResponse = new AddCategoryOptionResponse(
91             ImmutableList.of("error one", "error two"));
92
93         given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req))))
94             .willReturn(addCategoryOptionResponse);
95
96         prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName,
97             objectMapper.writeValueAsString(req))
98             .andExpect(status().isMultiStatus())
99             .andExpect(
100                 content().json(objectMapper
101                     .writeValueAsString(addCategoryOptionResponse)));
102     }
103
104     @Test
105     public void addCategoryOptions_shouldReturnOk_whenNoErrorsExist() throws Exception {
106         String categoryName = "catName2";
107         AddCategoryOptionsRequest req = new AddCategoryOptionsRequest();
108         req.options = ImmutableList.of("first option", "second option", "third option");
109         AddCategoryOptionResponse addCategoryOptionResponse = new AddCategoryOptionResponse(Collections.emptyList());
110
111         given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req))))
112             .willReturn(addCategoryOptionResponse);
113         prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName,
114             objectMapper.writeValueAsString(req))
115             .andExpect(status().isOk())
116             .andExpect(content().json(
117                 objectMapper.writeValueAsString(addCategoryOptionResponse)));
118     }
119
120     @Test
121     public void addCategoryOptions_shouldReturnNotFound_whenUnfoundedCategoryExceptionIsThrown() throws Exception {
122         String unfoundedMsg = "unfounded category exception message";
123         String categoryName = "catName3";
124         AddCategoryOptionsRequest req = new AddCategoryOptionsRequest();
125         req.options = ImmutableList.of("first option");
126
127         given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req))))
128             .willThrow(new CategoryParameterServiceImpl.UnfoundedCategoryException(unfoundedMsg));
129
130         prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName,
131             objectMapper.writeValueAsString(req))
132             .andExpect(status().isNotFound())
133             .andExpect(content().json(
134                 objectMapper.writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of(unfoundedMsg)))));
135     }
136
137     @Test
138     public void addCategoryOptions_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception {
139         String categoryName = "catName13";
140         AddCategoryOptionsRequest req = new AddCategoryOptionsRequest();
141         req.options = ImmutableList.of("option second", "first option");
142
143         given(service.createCategoryParameterOptions(eq(categoryName), argThat(requestMatcher(req))))
144             .willThrow(new RuntimeException());
145
146         prepareRequestExpectations(MockMvcRequestBuilders::post, CATEGORY_PARAMETER_PATH, categoryName,
147             objectMapper.writeValueAsString(req))
148             .andExpect(status().isInternalServerError());
149     }
150
151     @Test
152     public void updateNameForOption_shouldReturnMultiStatus_whenErrorsExist() throws Exception {
153         String categoryName = "catName4";
154         CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id1", "name1");
155         AddCategoryOptionResponse addCategoryOptionResponse = new AddCategoryOptionResponse(
156             ImmutableList.of("error one", "error two"));
157
158         given(service
159             .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
160             .willReturn(addCategoryOptionResponse);
161
162         prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
163             objectMapper.writeValueAsString(categoryParameterOptionRep))
164             .andExpect(status().isMultiStatus())
165             .andExpect(
166                 content().json(objectMapper
167                     .writeValueAsString(addCategoryOptionResponse)));
168     }
169
170     @Test
171     public void updateNameForOption_shouldReturnOk_whenNoErrorsExist() throws Exception {
172         String categoryName = "catName5";
173         CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id2", "name2");
174         AddCategoryOptionResponse addCategoryOptionResponse = new AddCategoryOptionResponse(Collections.emptyList());
175
176         given(service
177             .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
178             .willReturn(addCategoryOptionResponse);
179         prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
180             objectMapper.writeValueAsString(categoryParameterOptionRep))
181             .andExpect(status().isOk())
182             .andExpect(content().json(
183                 objectMapper.writeValueAsString(addCategoryOptionResponse)));
184     }
185
186     @Test
187     public void updateNameForOption_shouldReturnForbidden_whenForbiddenExceptionIsThrown() throws Exception {
188         String categoryName = "catName6";
189         CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id3", "name3");
190
191         given(service
192             .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
193             .willThrow(new ForbiddenException());
194         prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
195             objectMapper.writeValueAsString(categoryParameterOptionRep))
196             .andExpect(status().isForbidden())
197             .andExpect(content().json(
198                 objectMapper
199                     .writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of("HTTP 403 Forbidden")))));
200     }
201
202     @Test
203     public void updateNameForOption_shouldReturnNotFound_whenUnfoundedIsThrown() throws Exception {
204         String unfoundedOptionMsg = "unfounded category option exception message";
205         String categoryName = "catName7";
206         CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id4", "name4");
207
208         given(service
209             .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
210             .willThrow(new CategoryParameterServiceImpl.UnfoundedCategoryOptionException(unfoundedOptionMsg));
211
212         prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
213             objectMapper.writeValueAsString(categoryParameterOptionRep))
214             .andExpect(status().isNotFound())
215             .andExpect(content().json(
216                 objectMapper.writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of(unfoundedOptionMsg)))));
217     }
218
219     @Test
220     public void updateNameForOption_shouldReturnConflict_whenAlreadyExistOptionNameIsThrown() throws Exception {
221         String conflictMsg = "already exists option name exception message";
222         String categoryName = "catName8";
223         CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id5", "name5");
224
225         given(service
226             .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
227             .willThrow(new CategoryParameterServiceImpl.AlreadyExistOptionNameException(conflictMsg));
228
229         prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
230             objectMapper.writeValueAsString(categoryParameterOptionRep))
231             .andExpect(status().isConflict())
232             .andExpect(content().json(
233                 objectMapper.writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of(conflictMsg)))));
234     }
235
236     @Test
237     public void updateNameForOption_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception {
238         String categoryName = "catName18";
239         CategoryParameterOptionRep categoryParameterOptionRep = new CategoryParameterOptionRep("id6", "name6");
240
241         given(service
242             .updateCategoryParameterOption(eq(categoryName), argThat(requestMatcher(categoryParameterOptionRep))))
243             .willThrow(new RuntimeException());
244
245         prepareRequestExpectations(MockMvcRequestBuilders::put, CATEGORY_PARAMETER_PATH, categoryName,
246             objectMapper.writeValueAsString(categoryParameterOptionRep))
247             .andExpect(status().isInternalServerError());
248     }
249
250     @Test
251     public void getCategoryParameter_shouldReturnExistingMap() throws Exception {
252         CategoryParametersResponse categoryParametersResponse =
253             new CategoryParametersResponse(
254                 ImmutableMap.of(
255                     "key1", ImmutableList.of(
256                         new CategoryParameterOptionRep("testId", "testName"))));
257
258         given(service.getCategoryParameters(PARAMETER_STANDARDIZATION))
259             .willReturn(categoryParametersResponse);
260
261         mockMvc.perform(get(MAINTENANCE_CATEGORY_PATH)
262             .param("familyName", "PARAMETER_STANDARDIZATION")
263             .accept(MediaType.APPLICATION_JSON))
264             .andExpect(status().isOk())
265             .andExpect(content().json(objectMapper.writeValueAsString(categoryParametersResponse)));
266     }
267
268     @Test
269     public void getCategoryParameter_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception {
270         given(service.getCategoryParameters(PARAMETER_STANDARDIZATION))
271             .willThrow(new RuntimeException());
272
273         mockMvc.perform(get(MAINTENANCE_CATEGORY_PATH)
274             .param("familyName", "PARAMETER_STANDARDIZATION")
275             .accept(MediaType.APPLICATION_JSON))
276             .andExpect(status().isInternalServerError());
277     }
278
279     @Test
280     public void deleteCategoryOption_shouldReturnOk_whenNoExceptionIsThrown() throws Exception {
281         String categoryName = "catName9";
282         CategoryParameterOption categoryParameterOption = new CategoryParameterOption("id1", "name1",
283             new CategoryParameter());
284
285         prepareRequestExpectations(MockMvcRequestBuilders::delete, DELETE_CATEGORY_PATH, categoryName,
286             objectMapper.writeValueAsString(categoryParameterOption))
287             .andExpect(status().isOk());
288
289         then(service).should(times(1))
290             .deleteCategoryOption(eq(categoryName), argThat(requestMatcher(categoryParameterOption)));
291     }
292
293     @Test
294     public void deleteCategoryOption_shouldReturnNotFound_whenUnfoundedExceptionIsThrown() throws Exception {
295         String unfoundedMsg = "unfounded category exception message";
296         String categoryName = "catName10";
297         CategoryParameterOption categoryParameterOption = new CategoryParameterOption("id2", "name2",
298             new CategoryParameter());
299
300         willThrow(new CategoryParameterServiceImpl.UnfoundedCategoryException(unfoundedMsg))
301             .given(service).deleteCategoryOption(eq(categoryName), argThat(requestMatcher(categoryParameterOption)));
302
303         prepareRequestExpectations(MockMvcRequestBuilders::delete, DELETE_CATEGORY_PATH, categoryName,
304             objectMapper.writeValueAsString(categoryParameterOption))
305             .andExpect(status().isNotFound())
306             .andExpect(content().json(
307                 objectMapper.writeValueAsString(new AddCategoryOptionResponse(ImmutableList.of(unfoundedMsg)))));
308     }
309
310     @Test
311     public void deleteCategoryOption_shouldReturnInternalServerError_whenExceptionIsThrown() throws Exception {
312         String categoryName = "catName19";
313         CategoryParameterOption categoryParameterOption = new CategoryParameterOption("id3", "name3",
314             new CategoryParameter());
315
316         willThrow(new RuntimeException()).given(service)
317             .deleteCategoryOption(eq(categoryName), argThat(requestMatcher(categoryParameterOption)));
318
319         prepareRequestExpectations(MockMvcRequestBuilders::delete, DELETE_CATEGORY_PATH, categoryName,
320             objectMapper.writeValueAsString(categoryParameterOption))
321             .andExpect(status().isInternalServerError());
322     }
323
324     private <T> ArgumentMatcher<T> requestMatcher(T t) {
325         return new ArgumentMatcher<T>() {
326             @Override
327             public boolean matches(Object o) {
328                 return t.equals(o);
329             }
330         };
331     }
332
333     private ResultActions prepareRequestExpectations(
334         BiFunction<String, String, MockHttpServletRequestBuilder> httpMethod,
335         String path, String name, String jsonContent) throws Exception {
336         return mockMvc.perform(httpMethod.apply(path, name)
337             .contentType(MediaType.APPLICATION_JSON)
338             .content(jsonContent));
339     }
340 }