Merge "Add memory to Cypress"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / PropertyControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nokia. All rights reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.onap.vid.controller;
23
24 import static org.mockito.BDDMockito.given;
25 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
26 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
28
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.google.common.collect.ImmutableList;
31 import com.google.common.collect.ImmutableMap;
32 import org.apache.log4j.BasicConfigurator;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.onap.vid.category.CategoryParameterOptionRep;
39 import org.onap.vid.category.CategoryParametersResponse;
40 import org.onap.vid.model.CategoryParameter.Family;
41 import org.onap.vid.services.CategoryParameterService;
42 import org.onap.vid.utils.SystemPropertiesWrapper;
43 import org.springframework.http.MediaType;
44 import org.springframework.test.web.servlet.MockMvc;
45 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class PropertyControllerTest {
49
50     private static final String GET_PROPERTY = "/get_property/{name}/{defaultvalue}";
51     private static final String CATEGORY_PARAMETER = "/category_parameter";
52
53     private static final String ERROR_MSG = "Internal error occurred: ";
54     private static final String FAMILY_NAME = "familyName";
55
56     private PropertyController propertyController;
57     private MockMvc mockMvc;
58     private ObjectMapper objectMapper;
59
60     @Mock
61     private CategoryParameterService service;
62     @Mock
63     private SystemPropertiesWrapper systemPropertiesWrapper;
64
65     @Before
66     public void setUp() {
67         propertyController = new PropertyController(service, systemPropertiesWrapper);
68         BasicConfigurator.configure();
69         mockMvc = MockMvcBuilders.standaloneSetup(propertyController).build();
70         objectMapper = new ObjectMapper();
71     }
72
73     @Test
74     public void shouldReturnInputJson_whenPropertyIsNotFound() throws Exception {
75         String inputJson = "{key1: val1}";
76         given(systemPropertiesWrapper.getOrDefault("name.1", inputJson)).willReturn(inputJson);
77
78         mockMvc.perform(get(GET_PROPERTY, "name_1", inputJson)
79             .contentType(MediaType.APPLICATION_JSON))
80             .andExpect(status().isOk())
81             .andExpect(content().json(inputJson));
82     }
83
84     @Test
85     public void shouldReturnGivenJson_whenPropertyIsFound() throws Exception {
86         String propertyJson = "{key1: val1}";
87         String inputJson = "{key2: val2}";
88         given(systemPropertiesWrapper.getOrDefault("name.1", inputJson)).willReturn(propertyJson);
89
90         mockMvc.perform(get(GET_PROPERTY, "name_1", inputJson)
91             .contentType(MediaType.APPLICATION_JSON))
92             .andExpect(status().isOk())
93             .andExpect(content().json(propertyJson));
94     }
95
96     @Test
97     public void shouldReturnInternalServerError_whenExceptionIsThrownFromSystemProperties() throws Exception {
98         String exceptionMessage = "Test exception message from system properties";
99         String inputJson = "{key1: val1}";
100         given(systemPropertiesWrapper.getOrDefault("name.1", inputJson)).willThrow(new RuntimeException(exceptionMessage));
101
102         mockMvc.perform(get(GET_PROPERTY, "name_1", inputJson)
103             .contentType(MediaType.APPLICATION_JSON))
104             .andExpect(status().isInternalServerError())
105             .andExpect(content().string(ERROR_MSG + exceptionMessage));
106     }
107
108     @Test
109     public void shouldReturnResponse_whenResponseIsFound() throws Exception {
110
111         CategoryParametersResponse categoryParametersResponse =
112             new CategoryParametersResponse(
113                 ImmutableMap.of(
114                     "key1", ImmutableList.of(
115                         new CategoryParameterOptionRep("testId", "testName"))));
116
117         given(service.getCategoryParameters(Family.PARAMETER_STANDARDIZATION)).willReturn(categoryParametersResponse);
118
119         mockMvc.perform(get(CATEGORY_PARAMETER)
120             .param(FAMILY_NAME, "PARAMETER_STANDARDIZATION")
121             .contentType(MediaType.APPLICATION_JSON))
122             .andExpect(status().isOk())
123             .andExpect(content().json(objectMapper.writeValueAsString(categoryParametersResponse)));
124     }
125
126     @Test
127     public void shouldReturnInternalServerError_whenExceptionIsThrownFromService() throws Exception {
128         String exceptionMessage = "Test exception message from category parameter service";
129         given(service.getCategoryParameters(Family.PARAMETER_STANDARDIZATION)).willThrow(new RuntimeException(
130             exceptionMessage));
131
132         mockMvc.perform(get(CATEGORY_PARAMETER)
133             .param(FAMILY_NAME, "PARAMETER_STANDARDIZATION")
134             .contentType(MediaType.APPLICATION_JSON))
135             .andExpect(status().isInternalServerError())
136             .andExpect(content().string(ERROR_MSG + exceptionMessage));
137     }
138 }