Skip to content

Commit 2232f7d

Browse files
committed
Polish
Closes gh-14914
1 parent 3e6a4eb commit 2232f7d

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchJestHealthIndicator.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*
3131
* @author Stephane Nicoll
3232
* @author Julian Devia Serna
33+
* @author Brian Clozel
3334
* @since 2.0.0
3435
*/
3536
public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
@@ -47,14 +48,19 @@ public ElasticsearchJestHealthIndicator(JestClient jestClient) {
4748
protected void doHealthCheck(Health.Builder builder) throws Exception {
4849
JestResult healthResult = this.jestClient
4950
.execute(new io.searchbox.cluster.Health.Builder().build());
50-
JsonElement root = this.jsonParser.parse(healthResult.getJsonString());
51-
JsonElement status = root.getAsJsonObject().get("status");
52-
if (!healthResult.isSucceeded() || healthResult.getResponseCode() != 200 || status
53-
.getAsString().equals(io.searchbox.cluster.Health.Status.RED.getKey())) {
54-
builder.outOfService();
51+
if (healthResult.getResponseCode() != 200 || !healthResult.isSucceeded()) {
52+
builder.down();
5553
}
5654
else {
57-
builder.up();
55+
JsonElement root = this.jsonParser.parse(healthResult.getJsonString());
56+
JsonElement status = root.getAsJsonObject().get("status");
57+
if (status.getAsString()
58+
.equals(io.searchbox.cluster.Health.Status.RED.getKey())) {
59+
builder.outOfService();
60+
}
61+
else {
62+
builder.up();
63+
}
5864
}
5965
}
6066

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchJestHealthIndicatorTests.java

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@
4040
*
4141
* @author Stephane Nicoll
4242
* @author Julian Devia Serna
43+
* @author Brian Clozel
4344
*/
4445
public class ElasticsearchJestHealthIndicatorTests {
4546

@@ -52,7 +53,7 @@ public class ElasticsearchJestHealthIndicatorTests {
5253
@Test
5354
public void elasticsearchIsUp() throws IOException {
5455
given(this.jestClient.execute(any(Action.class)))
55-
.willReturn(createJestResult("green", 200, true));
56+
.willReturn(createJestResult(200, true, "green"));
5657
Health health = this.healthIndicator.health();
5758
assertThat(health.getStatus()).isEqualTo(Status.UP);
5859
}
@@ -68,42 +69,50 @@ public void elasticsearchIsDown() throws IOException {
6869

6970
@SuppressWarnings("unchecked")
7071
@Test
71-
public void elasticsearchIsOutOfServiceByStatus() throws IOException {
72+
public void elasticsearchIsDownWhenQueryDidNotSucceed() throws IOException {
7273
given(this.jestClient.execute(any(Action.class)))
73-
.willReturn(createJestResult("red", 200, true));
74+
.willReturn(createJestResult(200, false, ""));
7475
Health health = this.healthIndicator.health();
75-
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
76+
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
7677
}
7778

7879
@SuppressWarnings("unchecked")
7980
@Test
80-
public void elasticsearchIsOutOfServiceByResponseCode() throws IOException {
81+
public void elasticsearchIsDownByResponseCode() throws IOException {
8182
given(this.jestClient.execute(any(Action.class)))
82-
.willReturn(createJestResult("", 500, true));
83+
.willReturn(createJestResult(500, false, ""));
8384
Health health = this.healthIndicator.health();
84-
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
85+
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
8586
}
8687

8788
@SuppressWarnings("unchecked")
8889
@Test
89-
public void elasticsearchIsOutOfServiceBySucceeded() throws IOException {
90+
public void elasticsearchIsOutOfServiceByStatus() throws IOException {
9091
given(this.jestClient.execute(any(Action.class)))
91-
.willReturn(createJestResult("red", 500, false));
92+
.willReturn(createJestResult(200, true, "red"));
9293
Health health = this.healthIndicator.health();
9394
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
9495
}
9596

96-
private static JestResult createJestResult(String status, int responseCode,
97-
boolean succeeded) {
98-
String json = String.format("{\"cluster_name\":\"docker-cluster\","
99-
+ "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1,"
100-
+ "\"number_of_data_nodes\":1,\"active_primary_shards\":0,"
101-
+ "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0,"
102-
+ "\"unassigned_shards\":0,\"delayed_unassigned_shards\":0,"
103-
+ "\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0,"
104-
+ "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}",
105-
status);
97+
private static JestResult createJestResult(int responseCode, boolean succeeded,
98+
String status) {
99+
106100
SearchResult searchResult = new SearchResult(new Gson());
101+
String json;
102+
if (responseCode == 200) {
103+
json = String.format("{\"cluster_name\":\"elasticsearch\","
104+
+ "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1,"
105+
+ "\"number_of_data_nodes\":1,\"active_primary_shards\":0,"
106+
+ "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0,"
107+
+ "\"unassigned_shards\":0,\"delayed_unassigned_shards\":0,"
108+
+ "\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0,"
109+
+ "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}",
110+
status);
111+
}
112+
else {
113+
json = "{\n" + " \"error\": \"Server Error\",\n" + " \"status\": "
114+
+ responseCode + "\n" + "}";
115+
}
107116
searchResult.setJsonString(json);
108117
searchResult.setJsonObject(new JsonParser().parse(json).getAsJsonObject());
109118
searchResult.setResponseCode(responseCode);

0 commit comments

Comments
 (0)