2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright 2019 Nokia
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.vid.controller;
25 import static org.mockito.BDDMockito.given;
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
28 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.google.common.collect.ImmutableList;
32 import com.google.common.collect.ImmutableMap;
33 import org.apache.log4j.BasicConfigurator;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.vid.category.CategoryParameterOptionRep;
40 import org.onap.vid.category.CategoryParametersResponse;
41 import org.onap.vid.model.CategoryParameter.Family;
42 import org.onap.vid.services.CategoryParameterService;
43 import org.onap.vid.utils.SystemPropertiesWrapper;
44 import org.springframework.http.MediaType;
45 import org.springframework.test.web.servlet.MockMvc;
46 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
48 @RunWith(MockitoJUnitRunner.class)
49 public class PropertyControllerTest {
51 private static final String GET_PROPERTY = "/get_property/{name}/{defaultvalue}";
52 private static final String CATEGORY_PARAMETER = "/category_parameter";
54 private static final String ERROR_MSG = "Internal error occurred: ";
55 private static final String FAMILY_NAME = "familyName";
57 private PropertyController propertyController;
58 private MockMvc mockMvc;
59 private ObjectMapper objectMapper;
62 private CategoryParameterService service;
64 private SystemPropertiesWrapper systemPropertiesWrapper;
68 propertyController = new PropertyController(service, systemPropertiesWrapper);
69 BasicConfigurator.configure();
70 mockMvc = MockMvcBuilders.standaloneSetup(propertyController).build();
71 objectMapper = new ObjectMapper();
75 public void shouldReturnInputJson_whenPropertyIsNotFound() throws Exception {
76 String inputJson = "{key1: val1}";
77 given(systemPropertiesWrapper.getOrDefault("name.1", inputJson)).willReturn(inputJson);
79 mockMvc.perform(get(GET_PROPERTY, "name_1", inputJson)
80 .contentType(MediaType.APPLICATION_JSON))
81 .andExpect(status().isOk())
82 .andExpect(content().json(inputJson));
86 public void shouldReturnGivenJson_whenPropertyIsFound() throws Exception {
87 String propertyJson = "{key1: val1}";
88 String inputJson = "{key2: val2}";
89 given(systemPropertiesWrapper.getOrDefault("name.1", inputJson)).willReturn(propertyJson);
91 mockMvc.perform(get(GET_PROPERTY, "name_1", inputJson)
92 .contentType(MediaType.APPLICATION_JSON))
93 .andExpect(status().isOk())
94 .andExpect(content().json(propertyJson));
98 public void shouldReturnInternalServerError_whenExceptionIsThrownFromSystemProperties() throws Exception {
99 String exceptionMessage = "Test exception message from system properties";
100 String inputJson = "{key1: val1}";
101 given(systemPropertiesWrapper.getOrDefault("name.1", inputJson)).willThrow(new RuntimeException(exceptionMessage));
103 mockMvc.perform(get(GET_PROPERTY, "name_1", inputJson)
104 .contentType(MediaType.APPLICATION_JSON))
105 .andExpect(status().isInternalServerError())
106 .andExpect(content().string(ERROR_MSG + exceptionMessage));
110 public void shouldReturnResponse_whenResponseIsFound() throws Exception {
112 CategoryParametersResponse categoryParametersResponse =
113 new CategoryParametersResponse(
115 "key1", ImmutableList.of(
116 new CategoryParameterOptionRep("testId", "testName"))));
118 given(service.getCategoryParameters(Family.PARAMETER_STANDARDIZATION)).willReturn(categoryParametersResponse);
120 mockMvc.perform(get(CATEGORY_PARAMETER)
121 .param(FAMILY_NAME, "PARAMETER_STANDARDIZATION")
122 .contentType(MediaType.APPLICATION_JSON))
123 .andExpect(status().isOk())
124 .andExpect(content().json(objectMapper.writeValueAsString(categoryParametersResponse)));
128 public void shouldReturnInternalServerError_whenExceptionIsThrownFromService() throws Exception {
129 String exceptionMessage = "Test exception message from category parameter service";
130 given(service.getCategoryParameters(Family.PARAMETER_STANDARDIZATION)).willThrow(new RuntimeException(
133 mockMvc.perform(get(CATEGORY_PARAMETER)
134 .param(FAMILY_NAME, "PARAMETER_STANDARDIZATION")
135 .contentType(MediaType.APPLICATION_JSON))
136 .andExpect(status().isInternalServerError())
137 .andExpect(content().string(ERROR_MSG + exceptionMessage));