Merge from ECOMP's repository
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / HealthCheckControllerTest.java
1 package org.onap.vid.controller;
2
3 import org.apache.log4j.BasicConfigurator;
4 import org.junit.Before;
5 import org.junit.Test;
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;
13
14 import java.sql.SQLException;
15
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;
23
24 @RunWith(MockitoJUnitRunner.class)
25 public class HealthCheckControllerTest {
26
27     private static final String ERROR_MESSAGE = "error message";
28     private HealthCheckController testSubject;
29     private MockMvc mockMvc;
30
31     @Mock
32     private FnAppDoaImpl fnAppDoa;
33
34     @Before
35     public void setUp() {
36         testSubject = new HealthCheckController(fnAppDoa);
37         BasicConfigurator.configure();
38         mockMvc = MockMvcBuilders.standaloneSetup(testSubject).build();
39     }
40
41     @Test
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"));
49     }
50
51     @Test
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));
59     }
60
61     @Test
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());
70     }
71
72     @Test
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));
80     }
81
82     @Test
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"));
90     }
91
92     private void databaseConnectionEstablished() throws SQLException {
93         given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString()))
94                 .willReturn(0);
95     }
96
97     private void databaseNotAccessible() throws SQLException {
98         given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString()))
99                 .willThrow(new SQLException(ERROR_MESSAGE));
100     }
101 }