2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.controller.open;
23 import org.apache.log4j.BasicConfigurator;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.vid.controller.open.HealthCheckController;
30 import org.onap.vid.dao.FnAppDoaImpl;
31 import org.springframework.http.MediaType;
32 import org.springframework.test.web.servlet.MockMvc;
33 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
35 import java.sql.SQLException;
37 import static org.mockito.ArgumentMatchers.anyString;
38 import static org.mockito.BDDMockito.given;
39 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
40 import static org.springframework.http.HttpStatus.OK;
41 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
42 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
43 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
45 @RunWith(MockitoJUnitRunner.class)
46 public class HealthCheckControllerTest {
48 private static final String ERROR_MESSAGE = "error message";
49 private HealthCheckController testSubject;
50 private MockMvc mockMvc;
53 private FnAppDoaImpl fnAppDoa;
57 testSubject = new HealthCheckController(fnAppDoa);
58 BasicConfigurator.configure();
59 mockMvc = MockMvcBuilders.standaloneSetup(testSubject).build();
63 public void getHealthCheckStatusForIDNS_shouldReturnSuccess_whenNoExceptionIsThrown() throws Exception {
64 databaseConnectionEstablished();
65 mockMvc.perform(get("/healthCheck")
66 .accept(MediaType.APPLICATION_JSON))
67 .andExpect(status().isOk())
68 .andExpect(jsonPath("$.statusCode").value(OK.value()))
69 .andExpect(jsonPath("$.detailedMsg").value("health check succeeded"));
73 public void getHealthCheckStatusForIDNS_shouldReturnErrorCode_whenExceptionIsThrown() throws Exception {
74 databaseNotAccessible();
75 mockMvc.perform(get("/healthCheck")
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 getHealthCheck_shouldReturnSuccess_whenNoExceptionIsThrown() throws Exception {
84 databaseConnectionEstablished();
85 mockMvc.perform(get("/rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", "userAgent", "requestId")
86 .accept(MediaType.APPLICATION_JSON))
87 .andExpect(status().isOk())
88 .andExpect(jsonPath("$.statusCode").value(OK.value()))
89 .andExpect(jsonPath("$.detailedMsg").value("health check succeeded"))
90 .andExpect(jsonPath("$.date").isString());
94 public void getHealthCheck_shouldReturnErrorCode_whenExceptionIsThrown() throws Exception {
95 databaseNotAccessible();
96 mockMvc.perform(get("/rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", "userAgent", "requestId")
97 .accept(MediaType.APPLICATION_JSON))
98 .andExpect(status().isOk())
99 .andExpect(jsonPath("$.statusCode").value(INTERNAL_SERVER_ERROR.value()))
100 .andExpect(jsonPath("$.detailedMsg").value("health check failed: " + ERROR_MESSAGE));
104 public void getCommitInfo_shouldReturnCommitData_whenCorrectPropertiesFileExists() throws Exception {
105 mockMvc.perform(get("/commitInfo")
106 .accept(MediaType.APPLICATION_JSON))
107 .andExpect(status().isOk())
108 .andExpect(jsonPath("$.commitId").value("123"))
109 .andExpect(jsonPath("$.commitMessageShort").value("Test short commit message"))
110 .andExpect(jsonPath("$.commitTime").value("1999-09-12T13:48:55+0200"));
113 private void databaseConnectionEstablished() throws SQLException {
114 given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString()))
118 private void databaseNotAccessible() throws SQLException {
119 given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString()))
120 .willThrow(new SQLException(ERROR_MESSAGE));