1616
1717package org .springframework .boot .actuate .elasticsearch ;
1818
19- import java .io .IOException ;
20-
2119import com .google .gson .Gson ;
2220import com .google .gson .JsonParser ;
2321import io .searchbox .action .Action ;
2624import io .searchbox .client .config .exception .CouldNotConnectException ;
2725import io .searchbox .core .SearchResult ;
2826import org .junit .Test ;
29-
3027import org .springframework .boot .actuate .health .Health ;
3128import org .springframework .boot .actuate .health .Status ;
3229
30+ import java .io .IOException ;
31+
3332import static org .assertj .core .api .Assertions .assertThat ;
3433import static org .mockito .ArgumentMatchers .any ;
3534import static org .mockito .BDDMockito .given ;
@@ -51,7 +50,7 @@ public class ElasticsearchJestHealthIndicatorTests {
5150 @ Test
5251 public void elasticsearchIsUp () throws IOException {
5352 given (this .jestClient .execute (any (Action .class )))
54- .willReturn (createJestResult (4 , 0 ));
53+ .willReturn (createJestResult ("green" , 200 , true ));
5554 Health health = this .healthIndicator .health ();
5655 assertThat (health .getStatus ()).isEqualTo (Status .UP );
5756 }
@@ -67,19 +66,44 @@ public void elasticsearchIsDown() throws IOException {
6766
6867 @ SuppressWarnings ("unchecked" )
6968 @ Test
70- public void elasticsearchIsOutOfService () throws IOException {
69+ public void elasticsearchIsOutOfServiceByStatus () throws IOException {
70+ given (this .jestClient .execute (any (Action .class )))
71+ .willReturn (createJestResult ("red" , 200 , true ));
72+ Health health = this .healthIndicator .health ();
73+ assertThat (health .getStatus ()).isEqualTo (Status .OUT_OF_SERVICE );
74+ }
75+
76+ @ SuppressWarnings ("unchecked" )
77+ @ Test
78+ public void elasticsearchIsOutOfServiceByResponseCode () throws IOException {
79+ given (this .jestClient .execute (any (Action .class )))
80+ .willReturn (createJestResult ("" , 500 , true ));
81+ Health health = this .healthIndicator .health ();
82+ assertThat (health .getStatus ()).isEqualTo (Status .OUT_OF_SERVICE );
83+ }
84+
85+ @ SuppressWarnings ("unchecked" )
86+ @ Test
87+ public void elasticsearchIsOutOfServiceBySucceeded () throws IOException {
7188 given (this .jestClient .execute (any (Action .class )))
72- .willReturn (createJestResult (4 , 1 ));
89+ .willReturn (createJestResult ("red" , 500 , false ));
7390 Health health = this .healthIndicator .health ();
7491 assertThat (health .getStatus ()).isEqualTo (Status .OUT_OF_SERVICE );
7592 }
7693
77- private static JestResult createJestResult (int shards , int failedShards ) {
78- String json = String .format ("{_shards: {\n " + "total: %s,\n " + "successful: %s,\n "
79- + "failed: %s\n " + "}}" , shards , shards - failedShards , failedShards );
94+ private static JestResult createJestResult (String status , int responseCode , boolean succeeded ) {
95+ String json = String .format ("{\" cluster_name\" :\" docker-cluster\" ," +
96+ "\" status\" :\" %s\" ,\" timed_out\" :false,\" number_of_nodes\" :1," +
97+ "\" number_of_data_nodes\" :1,\" active_primary_shards\" :0," +
98+ "\" active_shards\" :0,\" relocating_shards\" :0,\" initializing_shards\" :0," +
99+ "\" unassigned_shards\" :0,\" delayed_unassigned_shards\" :0," +
100+ "\" number_of_pending_tasks\" :0,\" number_of_in_flight_fetch\" :0," +
101+ "\" task_max_waiting_in_queue_millis\" :0,\" active_shards_percent_as_number\" :100.0}" , status );
80102 SearchResult searchResult = new SearchResult (new Gson ());
81103 searchResult .setJsonString (json );
82104 searchResult .setJsonObject (new JsonParser ().parse (json ).getAsJsonObject ());
105+ searchResult .setResponseCode (responseCode );
106+ searchResult .setSucceeded (succeeded );
83107 return searchResult ;
84108 }
85109
0 commit comments