- private PropertyController createTestSubject() {
- return new PropertyController();
- }
-
- @Test
- public void testWelcome() throws Exception {
- PropertyController testSubject;
- HttpServletRequest request = null;
- ModelAndView result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.welcome(request);
- }
-
-
- @Test
- public void testGetProperty() throws Exception {
- PropertyController testSubject;
- String name = "";
- String defaultvalue = "";
- HttpServletRequest request = null;
- ResponseEntity<String> result;
-
- // default test
- testSubject = createTestSubject();
- result = testSubject.getProperty(name, defaultvalue, request);
- }
+ private static final String GET_PROPERTY = "/get_property/{name}/{defaultvalue}";
+ private static final String CATEGORY_PARAMETER = "/category_parameter";
+
+ private static final String ERROR_MSG = "Internal error occurred: ";
+ private static final String FAMILY_NAME = "familyName";
+
+ private PropertyController propertyController;
+ private MockMvc mockMvc;
+ private ObjectMapper objectMapper;
+
+ @Mock
+ private CategoryParameterService service;
+ @Mock
+ private SystemPropertiesWrapper systemPropertiesWrapper;
+
+ @Before
+ public void setUp() {
+ propertyController = new PropertyController(service, systemPropertiesWrapper);
+ BasicConfigurator.configure();
+ mockMvc = MockMvcBuilders.standaloneSetup(propertyController).build();
+ objectMapper = new ObjectMapper();
+ }
+
+ @Test
+ public void shouldReturnInputJson_whenPropertyIsNotFound() throws Exception {
+ String inputJson = "{key1: val1}";
+ given(systemPropertiesWrapper.getOrDefault("name.1", inputJson)).willReturn(inputJson);
+
+ mockMvc.perform(get(GET_PROPERTY, "name_1", inputJson)
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(content().json(inputJson));
+ }
+
+ @Test
+ public void shouldReturnGivenJson_whenPropertyIsFound() throws Exception {
+ String propertyJson = "{key1: val1}";
+ String inputJson = "{key2: val2}";
+ given(systemPropertiesWrapper.getOrDefault("name.1", inputJson)).willReturn(propertyJson);
+
+ mockMvc.perform(get(GET_PROPERTY, "name_1", inputJson)
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(content().json(propertyJson));
+ }
+
+ @Test
+ public void shouldReturnInternalServerError_whenExceptionIsThrownFromSystemProperties() throws Exception {
+ String exceptionMessage = "Test exception message from system properties";
+ String inputJson = "{key1: val1}";
+ given(systemPropertiesWrapper.getOrDefault("name.1", inputJson)).willThrow(new RuntimeException(exceptionMessage));
+
+ mockMvc.perform(get(GET_PROPERTY, "name_1", inputJson)
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isInternalServerError())
+ .andExpect(content().string(ERROR_MSG + exceptionMessage));
+ }
+
+ @Test
+ public void shouldReturnResponse_whenResponseIsFound() throws Exception {
+
+ CategoryParametersResponse categoryParametersResponse =
+ new CategoryParametersResponse(
+ ImmutableMap.of(
+ "key1", ImmutableList.of(
+ new CategoryParameterOptionRep("testId", "testName"))));
+
+ given(service.getCategoryParameters(Family.PARAMETER_STANDARDIZATION)).willReturn(categoryParametersResponse);
+
+ mockMvc.perform(get(CATEGORY_PARAMETER)
+ .param(FAMILY_NAME, "PARAMETER_STANDARDIZATION")
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(content().json(objectMapper.writeValueAsString(categoryParametersResponse)));
+ }
+
+ @Test
+ public void shouldReturnInternalServerError_whenExceptionIsThrownFromService() throws Exception {
+ String exceptionMessage = "Test exception message from category parameter service";
+ given(service.getCategoryParameters(Family.PARAMETER_STANDARDIZATION)).willThrow(new RuntimeException(
+ exceptionMessage));
+
+ mockMvc.perform(get(CATEGORY_PARAMETER)
+ .param(FAMILY_NAME, "PARAMETER_STANDARDIZATION")
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isInternalServerError())
+ .andExpect(content().string(ERROR_MSG + exceptionMessage));
+ }