1 package org.onap.vid.controller;
3 import org.apache.log4j.BasicConfigurator;
4 import org.junit.Before;
6 import org.junit.runner.RunWith;
7 import org.mockito.Mock;
8 import org.mockito.junit.MockitoJUnitRunner;
9 import org.onap.vid.dao.FnAppDoaImpl;
10 import org.springframework.http.MediaType;
11 import org.springframework.test.web.servlet.MockMvc;
12 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
14 import java.sql.SQLException;
16 import static org.mockito.ArgumentMatchers.anyString;
17 import static org.mockito.BDDMockito.given;
18 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
19 import static org.springframework.http.HttpStatus.OK;
20 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
21 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
22 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
24 @RunWith(MockitoJUnitRunner.class)
25 public class HealthCheckControllerTest {
27 private static final String ERROR_MESSAGE = "error message";
28 private HealthCheckController testSubject;
29 private MockMvc mockMvc;
32 private FnAppDoaImpl fnAppDoa;
36 testSubject = new HealthCheckController(fnAppDoa);
37 BasicConfigurator.configure();
38 mockMvc = MockMvcBuilders.standaloneSetup(testSubject).build();
42 public void getHealthCheckStatusForIDNS_shouldReturnSuccess_whenNoExceptionIsThrown() throws Exception {
43 databaseConnectionEstablished();
44 mockMvc.perform(get("/healthCheck")
45 .accept(MediaType.APPLICATION_JSON))
46 .andExpect(status().isOk())
47 .andExpect(jsonPath("$.statusCode").value(OK.value()))
48 .andExpect(jsonPath("$.detailedMsg").value("health check succeeded"));
52 public void getHealthCheckStatusForIDNS_shouldReturnErrorCode_whenExceptionIsThrown() throws Exception {
53 databaseNotAccessible();
54 mockMvc.perform(get("/healthCheck")
55 .accept(MediaType.APPLICATION_JSON))
56 .andExpect(status().isOk())
57 .andExpect(jsonPath("$.statusCode").value(INTERNAL_SERVER_ERROR.value()))
58 .andExpect(jsonPath("$.detailedMsg").value("health check failed: " + ERROR_MESSAGE));
62 public void getHealthCheck_shouldReturnSuccess_whenNoExceptionIsThrown() throws Exception {
63 databaseConnectionEstablished();
64 mockMvc.perform(get("/rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", "userAgent", "requestId")
65 .accept(MediaType.APPLICATION_JSON))
66 .andExpect(status().isOk())
67 .andExpect(jsonPath("$.statusCode").value(OK.value()))
68 .andExpect(jsonPath("$.detailedMsg").value("health check succeeded"))
69 .andExpect(jsonPath("$.date").isString());
73 public void getHealthCheck_shouldReturnErrorCode_whenExceptionIsThrown() throws Exception {
74 databaseNotAccessible();
75 mockMvc.perform(get("/rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", "userAgent", "requestId")
76 .accept(MediaType.APPLICATION_JSON))
77 .andExpect(status().isOk())
78 .andExpect(jsonPath("$.statusCode").value(INTERNAL_SERVER_ERROR.value()))
79 .andExpect(jsonPath("$.detailedMsg").value("health check failed: " + ERROR_MESSAGE));
83 public void getCommitInfo_shouldReturnCommitData_whenCorrectPropertiesFileExists() throws Exception {
84 mockMvc.perform(get("/commitInfo")
85 .accept(MediaType.APPLICATION_JSON))
86 .andExpect(status().isOk())
87 .andExpect(jsonPath("$.commitId").value("123"))
88 .andExpect(jsonPath("$.commitMessageShort").value("Test short commit message"))
89 .andExpect(jsonPath("$.commitTime").value("1999-09-12T13:48:55+0200"));
92 private void databaseConnectionEstablished() throws SQLException {
93 given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString()))
97 private void databaseNotAccessible() throws SQLException {
98 given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString()))
99 .willThrow(new SQLException(ERROR_MESSAGE));