aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/controllers/HealthCheckControllerTest.java
blob: da9cdaa6e92f0f222333252e6ae6a07fc5364e1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package org.onap.vid.controllers;

import org.apache.log4j.BasicConfigurator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.onap.vid.dao.FnAppDoaImpl;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.sql.SQLException;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyString;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(MockitoJUnitRunner.class)
public class HealthCheckControllerTest {

    private static final String ERROR_MESSAGE = "error message";
    private HealthCheckController testSubject;
    private MockMvc mockMvc;

    @Mock
    private FnAppDoaImpl fnAppDoa;

    @Before
    public void setUp() {
        testSubject = new HealthCheckController(fnAppDoa);
        BasicConfigurator.configure();
        mockMvc = MockMvcBuilders.standaloneSetup(testSubject).build();
    }

    @Test
    public void getHealthCheckStatusForIDNS_shouldReturnSuccess_whenNoExceptionIsThrown() throws Exception {
        databaseConnectionEstablished();
        mockMvc.perform(get("/healthCheck")
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.statusCode").value(OK.value()))
                .andExpect(jsonPath("$.detailedMsg").value("health check succeeded"));
    }

    @Test
    public void getHealthCheckStatusForIDNS_shouldReturnErrorCode_whenExceptionIsThrown() throws Exception {
        databaseNotAccessible();
        mockMvc.perform(get("/healthCheck")
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.statusCode").value(INTERNAL_SERVER_ERROR.value()))
                .andExpect(jsonPath("$.detailedMsg").value("health check failed: " + ERROR_MESSAGE));
    }

    @Test
    public void getHealthCheck_shouldReturnSuccess_whenNoExceptionIsThrown() throws Exception {
        databaseConnectionEstablished();
        mockMvc.perform(get("/rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", "userAgent", "requestId")
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.statusCode").value(OK.value()))
                .andExpect(jsonPath("$.detailedMsg").value("health check succeeded"))
                .andExpect(jsonPath("$.date").isString());
    }

    @Test
    public void getHealthCheck_shouldReturnErrorCode_whenExceptionIsThrown() throws Exception {
        databaseNotAccessible();
        mockMvc.perform(get("/rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", "userAgent", "requestId")
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.statusCode").value(INTERNAL_SERVER_ERROR.value()))
                .andExpect(jsonPath("$.detailedMsg").value("health check failed: " + ERROR_MESSAGE));
    }

    @Test
    public void getCommitInfo_shouldReturnCommitData_whenCorrectPropertiesFileExists() throws Exception {
        mockMvc.perform(get("/commitInfo")
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.commitId").value("123"))
                .andExpect(jsonPath("$.commitMessageShort").value("Test short commit message"))
                .andExpect(jsonPath("$.commitTime").value("1999-09-12T13:48:55+0200"));
    }

    private void databaseConnectionEstablished() throws SQLException {
        given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString()))
                .willReturn(0);
    }

    private void databaseNotAccessible() throws SQLException {
        given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString()))
                .willThrow(new SQLException(ERROR_MESSAGE));
    }
}