|
|
@ -33,8 +33,8 @@ import org.springframework.boot.actuate.health.Status;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.assertj.core.api.Assertions.entry;
|
|
|
|
import static org.assertj.core.api.Assertions.entry;
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
import static org.mockito.BDDMockito.mock;
|
|
|
|
import static org.mockito.BDDMockito.given;
|
|
|
|
import static org.mockito.BDDMockito.when;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Tests for {@link ElasticsearchRestHealthIndicator}.
|
|
|
|
* Tests for {@link ElasticsearchRestHealthIndicator}.
|
|
|
@ -54,15 +54,12 @@ public class ElasticsearchRestHealthIndicatorTest {
|
|
|
|
BasicHttpEntity httpEntity = new BasicHttpEntity();
|
|
|
|
BasicHttpEntity httpEntity = new BasicHttpEntity();
|
|
|
|
httpEntity.setContent(
|
|
|
|
httpEntity.setContent(
|
|
|
|
new ByteArrayInputStream(createJsonResult(200, "green").getBytes()));
|
|
|
|
new ByteArrayInputStream(createJsonResult(200, "green").getBytes()));
|
|
|
|
|
|
|
|
|
|
|
|
Response response = mock(Response.class);
|
|
|
|
Response response = mock(Response.class);
|
|
|
|
StatusLine statusLine = mock(StatusLine.class);
|
|
|
|
StatusLine statusLine = mock(StatusLine.class);
|
|
|
|
|
|
|
|
given(statusLine.getStatusCode()).willReturn(200);
|
|
|
|
when(statusLine.getStatusCode()).thenReturn(200);
|
|
|
|
given(response.getStatusLine()).willReturn(statusLine);
|
|
|
|
when(response.getStatusLine()).thenReturn(statusLine);
|
|
|
|
given(response.getEntity()).willReturn(httpEntity);
|
|
|
|
when(response.getEntity()).thenReturn(httpEntity);
|
|
|
|
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
|
|
|
|
when(this.restClient.performRequest(any(Request.class))).thenReturn(response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Health health = this.elasticsearchRestHealthIndicator.health();
|
|
|
|
Health health = this.elasticsearchRestHealthIndicator.health();
|
|
|
|
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
|
|
|
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
|
|
|
assertHealthDetailsWithStatus(health.getDetails(), "green");
|
|
|
|
assertHealthDetailsWithStatus(health.getDetails(), "green");
|
|
|
@ -73,15 +70,12 @@ public class ElasticsearchRestHealthIndicatorTest {
|
|
|
|
BasicHttpEntity httpEntity = new BasicHttpEntity();
|
|
|
|
BasicHttpEntity httpEntity = new BasicHttpEntity();
|
|
|
|
httpEntity.setContent(
|
|
|
|
httpEntity.setContent(
|
|
|
|
new ByteArrayInputStream(createJsonResult(200, "yellow").getBytes()));
|
|
|
|
new ByteArrayInputStream(createJsonResult(200, "yellow").getBytes()));
|
|
|
|
|
|
|
|
|
|
|
|
Response response = mock(Response.class);
|
|
|
|
Response response = mock(Response.class);
|
|
|
|
StatusLine statusLine = mock(StatusLine.class);
|
|
|
|
StatusLine statusLine = mock(StatusLine.class);
|
|
|
|
|
|
|
|
given(statusLine.getStatusCode()).willReturn(200);
|
|
|
|
when(statusLine.getStatusCode()).thenReturn(200);
|
|
|
|
given(response.getStatusLine()).willReturn(statusLine);
|
|
|
|
when(response.getStatusLine()).thenReturn(statusLine);
|
|
|
|
given(response.getEntity()).willReturn(httpEntity);
|
|
|
|
when(response.getEntity()).thenReturn(httpEntity);
|
|
|
|
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
|
|
|
|
when(this.restClient.performRequest(any(Request.class))).thenReturn(response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Health health = this.elasticsearchRestHealthIndicator.health();
|
|
|
|
Health health = this.elasticsearchRestHealthIndicator.health();
|
|
|
|
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
|
|
|
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
|
|
|
assertHealthDetailsWithStatus(health.getDetails(), "yellow");
|
|
|
|
assertHealthDetailsWithStatus(health.getDetails(), "yellow");
|
|
|
@ -89,9 +83,8 @@ public class ElasticsearchRestHealthIndicatorTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void elasticsearchIsDown() throws IOException {
|
|
|
|
public void elasticsearchIsDown() throws IOException {
|
|
|
|
when(this.restClient.performRequest(any(Request.class)))
|
|
|
|
given(this.restClient.performRequest(any(Request.class)))
|
|
|
|
.thenThrow(new IOException("Couldn't connect"));
|
|
|
|
.willThrow(new IOException("Couldn't connect"));
|
|
|
|
|
|
|
|
|
|
|
|
Health health = this.elasticsearchRestHealthIndicator.health();
|
|
|
|
Health health = this.elasticsearchRestHealthIndicator.health();
|
|
|
|
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
|
|
|
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
|
|
|
assertThat(health.getDetails())
|
|
|
|
assertThat(health.getDetails())
|
|
|
@ -102,12 +95,10 @@ public class ElasticsearchRestHealthIndicatorTest {
|
|
|
|
public void elasticsearchIsDownByResponseCode() throws IOException {
|
|
|
|
public void elasticsearchIsDownByResponseCode() throws IOException {
|
|
|
|
Response response = mock(Response.class);
|
|
|
|
Response response = mock(Response.class);
|
|
|
|
StatusLine statusLine = mock(StatusLine.class);
|
|
|
|
StatusLine statusLine = mock(StatusLine.class);
|
|
|
|
|
|
|
|
given(statusLine.getStatusCode()).willReturn(500);
|
|
|
|
when(statusLine.getStatusCode()).thenReturn(500);
|
|
|
|
given(statusLine.getReasonPhrase()).willReturn("Internal server error");
|
|
|
|
when(statusLine.getReasonPhrase()).thenReturn("Internal server error");
|
|
|
|
given(response.getStatusLine()).willReturn(statusLine);
|
|
|
|
when(response.getStatusLine()).thenReturn(statusLine);
|
|
|
|
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
|
|
|
|
when(this.restClient.performRequest(any(Request.class))).thenReturn(response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Health health = this.elasticsearchRestHealthIndicator.health();
|
|
|
|
Health health = this.elasticsearchRestHealthIndicator.health();
|
|
|
|
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
|
|
|
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
|
|
|
assertThat(health.getDetails()).contains(entry("statusCode", 500),
|
|
|
|
assertThat(health.getDetails()).contains(entry("statusCode", 500),
|
|
|
@ -119,15 +110,12 @@ public class ElasticsearchRestHealthIndicatorTest {
|
|
|
|
BasicHttpEntity httpEntity = new BasicHttpEntity();
|
|
|
|
BasicHttpEntity httpEntity = new BasicHttpEntity();
|
|
|
|
httpEntity.setContent(
|
|
|
|
httpEntity.setContent(
|
|
|
|
new ByteArrayInputStream(createJsonResult(200, "red").getBytes()));
|
|
|
|
new ByteArrayInputStream(createJsonResult(200, "red").getBytes()));
|
|
|
|
|
|
|
|
|
|
|
|
Response response = mock(Response.class);
|
|
|
|
Response response = mock(Response.class);
|
|
|
|
StatusLine statusLine = mock(StatusLine.class);
|
|
|
|
StatusLine statusLine = mock(StatusLine.class);
|
|
|
|
|
|
|
|
given(statusLine.getStatusCode()).willReturn(200);
|
|
|
|
when(statusLine.getStatusCode()).thenReturn(200);
|
|
|
|
given(response.getStatusLine()).willReturn(statusLine);
|
|
|
|
when(response.getStatusLine()).thenReturn(statusLine);
|
|
|
|
given(response.getEntity()).willReturn(httpEntity);
|
|
|
|
when(response.getEntity()).thenReturn(httpEntity);
|
|
|
|
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
|
|
|
|
when(this.restClient.performRequest(any(Request.class))).thenReturn(response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Health health = this.elasticsearchRestHealthIndicator.health();
|
|
|
|
Health health = this.elasticsearchRestHealthIndicator.health();
|
|
|
|
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
|
|
|
|
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
|
|
|
|
assertHealthDetailsWithStatus(health.getDetails(), "red");
|
|
|
|
assertHealthDetailsWithStatus(health.getDetails(), "red");
|
|
|
@ -148,9 +136,8 @@ public class ElasticsearchRestHealthIndicatorTest {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String createJsonResult(int responseCode, String status) {
|
|
|
|
private String createJsonResult(int responseCode, String status) {
|
|
|
|
String json;
|
|
|
|
|
|
|
|
if (responseCode == 200) {
|
|
|
|
if (responseCode == 200) {
|
|
|
|
json = String.format("{\"cluster_name\":\"elasticsearch\","
|
|
|
|
return String.format("{\"cluster_name\":\"elasticsearch\","
|
|
|
|
+ "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1,"
|
|
|
|
+ "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1,"
|
|
|
|
+ "\"number_of_data_nodes\":1,\"active_primary_shards\":0,"
|
|
|
|
+ "\"number_of_data_nodes\":1,\"active_primary_shards\":0,"
|
|
|
|
+ "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0,"
|
|
|
|
+ "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0,"
|
|
|
@ -159,12 +146,8 @@ public class ElasticsearchRestHealthIndicatorTest {
|
|
|
|
+ "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}",
|
|
|
|
+ "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}",
|
|
|
|
status);
|
|
|
|
status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "{\n" + " \"error\": \"Server Error\",\n" + " \"status\": "
|
|
|
|
json = "{\n" + " \"error\": \"Server Error\",\n" + " \"status\": "
|
|
|
|
|
|
|
|
+ responseCode + "\n" + "}";
|
|
|
|
+ responseCode + "\n" + "}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return json;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|