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;
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.dao.FnAppDoaImpl;
30 import org.springframework.http.MediaType;
31 import org.springframework.test.web.servlet.MockMvc;
32 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
34 import java.sql.SQLException;
36 import static org.mockito.ArgumentMatchers.anyString;
37 import static org.mockito.BDDMockito.given;
38 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
39 import static org.springframework.http.HttpStatus.OK;
40 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
41 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
42 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
44 @RunWith(MockitoJUnitRunner.class)
45 public class HealthCheckControllerTest {
47 private static final String ERROR_MESSAGE = "error message";
48 private HealthCheckController testSubject;
49 private MockMvc mockMvc;
52 private FnAppDoaImpl fnAppDoa;
56 testSubject = new HealthCheckController(fnAppDoa);
57 BasicConfigurator.configure();
58 mockMvc = MockMvcBuilders.standaloneSetup(testSubject).build();
62 public void getHealthCheckStatusForIDNS_shouldReturnSuccess_whenNoExceptionIsThrown() throws Exception {
63 databaseConnectionEstablished();
64 mockMvc.perform(get("/healthCheck")
65 .accept(MediaType.APPLICATION_JSON))
66 .andExpect(status().isOk())
67 .andExpect(jsonPath("$.statusCode").value(OK.value()))
68 .andExpect(jsonPath("$.detailedMsg").value("health check succeeded"));
72 public void getHealthCheckStatusForIDNS_shouldReturnErrorCode_whenExceptionIsThrown() throws Exception {
73 databaseNotAccessible();
74 mockMvc.perform(get("/healthCheck")
75 .accept(MediaType.APPLICATION_JSON))
76 .andExpect(status().isOk())
77 .andExpect(jsonPath("$.statusCode").value(INTERNAL_SERVER_ERROR.value()))
78 .andExpect(jsonPath("$.detailedMsg").value("health check failed: " + ERROR_MESSAGE));
82 public void getHealthCheck_shouldReturnSuccess_whenNoExceptionIsThrown() throws Exception {
83 databaseConnectionEstablished();
84 mockMvc.perform(get("/rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", "userAgent", "requestId")
85 .accept(MediaType.APPLICATION_JSON))
86 .andExpect(status().isOk())
87 .andExpect(jsonPath("$.statusCode").value(OK.value()))
88 .andExpect(jsonPath("$.detailedMsg").value("health check succeeded"))
89 .andExpect(jsonPath("$.date").isString());
93 public void getHealthCheck_shouldReturnErrorCode_whenExceptionIsThrown() throws Exception {
94 databaseNotAccessible();
95 mockMvc.perform(get("/rest/healthCheck/{User-Agent}/{X-ECOMP-RequestID}", "userAgent", "requestId")
96 .accept(MediaType.APPLICATION_JSON))
97 .andExpect(status().isOk())
98 .andExpect(jsonPath("$.statusCode").value(INTERNAL_SERVER_ERROR.value()))
99 .andExpect(jsonPath("$.detailedMsg").value("health check failed: " + ERROR_MESSAGE));
103 public void getCommitInfo_shouldReturnCommitData_whenCorrectPropertiesFileExists() throws Exception {
104 mockMvc.perform(get("/commitInfo")
105 .accept(MediaType.APPLICATION_JSON))
106 .andExpect(status().isOk())
107 .andExpect(jsonPath("$.commitId").value("123"))
108 .andExpect(jsonPath("$.commitMessageShort").value("Test short commit message"))
109 .andExpect(jsonPath("$.commitTime").value("1999-09-12T13:48:55+0200"));
112 private void databaseConnectionEstablished() throws SQLException {
113 given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString()))
117 private void databaseNotAccessible() throws SQLException {
118 given(fnAppDoa.getProfileCount(anyString(), anyString(), anyString()))
119 .willThrow(new SQLException(ERROR_MESSAGE));