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;
53 @RunWith(SpringRunner.class)
54 @ContextConfiguration(classes = {GenericStringConverter.class, HealthCheckConverter.class},
55 initializers = {ConfigFileApplicationContextInitializer.class})
56 @ActiveProfiles("test")
57 @EnableConfigurationProperties({HealthCheckConfig.class})
58 public class GlobalHealthcheckHandlerTest {
60 private RestTemplate restTemplate;
63 private ContainerRequestContext requestContext;
66 private GlobalHealthcheckHandler globalhealth;
69 public void testQuerySubsystemHealthNullResult() {
70 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
72 Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
73 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(null);
75 HealthCheckSubsystem result = globalhealth
76 .querySubsystemHealth(new Endpoint(SoSubsystems.BPMN, UriBuilder.fromPath("http://localhost").build()));
77 assertEquals(HealthCheckStatus.DOWN, result.getStatus());
81 public void testQuerySubsystemHealthNotNullResult() {
82 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
84 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
85 subSystemResponse.setStatus("UP");
86 ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
88 Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
89 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(r);
91 HealthCheckSubsystem result = globalhealth
92 .querySubsystemHealth(new Endpoint(SoSubsystems.ASDC, UriBuilder.fromPath("http://localhost").build()));
93 assertEquals(HealthCheckStatus.UP, result.getStatus());
96 private Response globalHealthcheck(String status) {
97 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
99 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
101 subSystemResponse.setStatus(status);
102 ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
103 Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
104 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(r);
106 Mockito.when(requestContext.getProperty(anyString())).thenReturn("1234567890");
107 Response response = globalhealth.globalHealthcheck(true, requestContext);
112 public void globalHealthcheckAllUPTest() throws JSONException {
114 HealthCheckResponse expected = new HealthCheckResponse();
116 for (Subsystem system : SoSubsystems.values()) {
117 expected.getSubsystems().add(new HealthCheckSubsystem(system,
118 UriBuilder.fromUri("http://localhost").build(), HealthCheckStatus.UP));
120 expected.setMessage("HttpStatus: 200");
121 Response response = globalHealthcheck("UP");
122 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
123 HealthCheckResponse root;
124 root = (HealthCheckResponse) response.getEntity();
125 assertThat(root, sameBeanAs(expected).ignoring("subsystems.uri").ignoring("subsystems.subsystem"));
130 public void globalHealthcheckAllDOWNTest() throws JSONException {
131 HealthCheckResponse expected = new HealthCheckResponse();
133 for (Subsystem system : SoSubsystems.values()) {
134 expected.getSubsystems().add(new HealthCheckSubsystem(system,
135 UriBuilder.fromUri("http://localhost").build(), HealthCheckStatus.DOWN));
137 expected.setMessage("HttpStatus: 200");
138 Response response = globalHealthcheck("DOWN");
139 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
140 HealthCheckResponse root;
141 root = (HealthCheckResponse) response.getEntity();
142 assertThat(root, sameBeanAs(expected).ignoring("subsystems.uri").ignoring("subsystems.subsystem"));
146 public void buildHttpEntityForRequestTest() {
147 HttpEntity<String> he = globalhealth.buildHttpEntityForRequest();
148 assertEquals(MediaType.APPLICATION_JSON, he.getHeaders().getAccept().get(0));
149 assertEquals(MediaType.APPLICATION_JSON, he.getHeaders().getContentType());
154 public void processResponseFromSubsystemTest() {
155 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
156 subSystemResponse.setStatus("UP");
157 ResponseEntity<SubsystemHealthcheckResponse> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
158 Endpoint endpoint = new Endpoint(SoSubsystems.BPMN, UriBuilder.fromUri("http://localhost").build());
159 HealthCheckStatus result = globalhealth.processResponseFromSubsystem(r, endpoint);
160 assertEquals(HealthCheckStatus.UP, result);