2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.so.apihandlerinfra;
23 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
26 import static org.mockito.ArgumentMatchers.anyString;
28 import javax.ws.rs.container.ContainerRequestContext;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.UriBuilder;
31 import org.json.JSONException;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.ArgumentMatchers;
35 import org.mockito.Mockito;
36 import org.onap.so.apihandlerinfra.HealthCheckConfig.Endpoint;
37 import org.springframework.boot.context.properties.EnableConfigurationProperties;
38 import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
39 import org.springframework.boot.test.mock.mockito.MockBean;
40 import org.springframework.boot.test.mock.mockito.SpyBean;
41 import org.springframework.http.HttpEntity;
42 import org.springframework.http.HttpMethod;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.MediaType;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.test.context.ActiveProfiles;
47 import org.springframework.test.context.ContextConfiguration;
48 import org.springframework.test.context.junit4.SpringRunner;
49 import org.springframework.test.util.ReflectionTestUtils;
50 import org.springframework.web.client.RestTemplate;
51 import com.fasterxml.jackson.core.JsonProcessingException;
54 @RunWith(SpringRunner.class)
55 @ContextConfiguration(classes = {GenericStringConverter.class, HealthCheckConverter.class},
56 initializers = {ConfigFileApplicationContextInitializer.class})
57 @ActiveProfiles("test")
58 @EnableConfigurationProperties({HealthCheckConfig.class})
59 public class GlobalHealthcheckHandlerTest {
61 private RestTemplate restTemplate;
64 private ContainerRequestContext requestContext;
67 private GlobalHealthcheckHandler globalhealth;
70 public void testQuerySubsystemHealthNullResult() {
71 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
73 Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
74 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(null);
76 HealthCheckSubsystem result = globalhealth
77 .querySubsystemHealth(new Endpoint(SoSubsystems.BPMN, UriBuilder.fromPath("http://localhost").build()));
78 assertEquals(HealthCheckStatus.DOWN, result.getStatus());
82 public void testQuerySubsystemHealthNotNullResult() {
83 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
85 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
86 subSystemResponse.setStatus("UP");
87 ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
89 Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
90 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(r);
92 HealthCheckSubsystem result = globalhealth
93 .querySubsystemHealth(new Endpoint(SoSubsystems.ASDC, UriBuilder.fromPath("http://localhost").build()));
94 assertEquals(HealthCheckStatus.UP, result.getStatus());
97 private Response globalHealthcheck(String status) {
98 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
100 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
102 subSystemResponse.setStatus(status);
103 ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
104 Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
105 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(r);
107 Mockito.when(requestContext.getProperty(anyString())).thenReturn("1234567890");
108 Response response = globalhealth.globalHealthcheck(true, requestContext);
113 public void globalHealthcheckAllUPTest() throws JSONException, JsonProcessingException {
115 HealthCheckResponse expected = new HealthCheckResponse();
117 for (Subsystem system : SoSubsystems.values()) {
118 expected.getSubsystems().add(new HealthCheckSubsystem(system,
119 UriBuilder.fromUri("http://localhost").build(), HealthCheckStatus.UP));
121 expected.setMessage("HttpStatus: 200");
122 Response response = globalHealthcheck("UP");
123 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
124 HealthCheckResponse root;
125 root = (HealthCheckResponse) response.getEntity();
126 assertThat(root, sameBeanAs(expected).ignoring("subsystems.uri").ignoring("subsystems.subsystem"));
131 public void globalHealthcheckAllDOWNTest() throws JSONException {
132 HealthCheckResponse expected = new HealthCheckResponse();
134 for (Subsystem system : SoSubsystems.values()) {
135 expected.getSubsystems().add(new HealthCheckSubsystem(system,
136 UriBuilder.fromUri("http://localhost").build(), HealthCheckStatus.DOWN));
138 expected.setMessage("HttpStatus: 200");
139 Response response = globalHealthcheck("DOWN");
140 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
141 HealthCheckResponse root;
142 root = (HealthCheckResponse) response.getEntity();
143 assertThat(root, sameBeanAs(expected).ignoring("subsystems.uri").ignoring("subsystems.subsystem"));
147 public void buildHttpEntityForRequestTest() {
148 HttpEntity<String> he = globalhealth.buildHttpEntityForRequest();
149 assertEquals(MediaType.APPLICATION_JSON, he.getHeaders().getAccept().get(0));
150 assertEquals(MediaType.APPLICATION_JSON, he.getHeaders().getContentType());
155 public void processResponseFromSubsystemTest() {
156 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
157 subSystemResponse.setStatus("UP");
158 ResponseEntity<SubsystemHealthcheckResponse> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
159 Endpoint endpoint = new Endpoint(SoSubsystems.BPMN, UriBuilder.fromUri("http://localhost").build());
160 HealthCheckStatus result = globalhealth.processResponseFromSubsystem(r, endpoint);
161 assertEquals(HealthCheckStatus.UP, result);