@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2012 - 2017 the original author or authors .
* Copyright 2012 - 2018 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -40,6 +40,7 @@ import static org.mockito.Mockito.mock;
@@ -40,6 +40,7 @@ import static org.mockito.Mockito.mock;
*
* @author Stephane Nicoll
* @author Julian Devia Serna
* @author Brian Clozel
* /
public class ElasticsearchJestHealthIndicatorTests {
@ -52,7 +53,7 @@ public class ElasticsearchJestHealthIndicatorTests {
@@ -52,7 +53,7 @@ public class ElasticsearchJestHealthIndicatorTests {
@Test
public void elasticsearchIsUp ( ) throws IOException {
given ( this . jestClient . execute ( any ( Action . class ) ) )
. willReturn ( createJestResult ( "green" , 200 , true ) ) ;
. willReturn ( createJestResult ( 200 , true , "green" ) ) ;
Health health = this . healthIndicator . health ( ) ;
assertThat ( health . getStatus ( ) ) . isEqualTo ( Status . UP ) ;
}
@ -68,42 +69,50 @@ public class ElasticsearchJestHealthIndicatorTests {
@@ -68,42 +69,50 @@ public class ElasticsearchJestHealthIndicatorTests {
@SuppressWarnings ( "unchecked" )
@Test
public void elasticsearchIsOutOfServiceByStatus ( ) throws IOException {
public void elasticsearchIsDownWhenQueryDidNotSucceed ( ) throws IOException {
given ( this . jestClient . execute ( any ( Action . class ) ) )
. willReturn ( createJestResult ( "red" , 200 , true ) ) ;
. willReturn ( createJestResult ( 200 , false , "" ) ) ;
Health health = this . healthIndicator . health ( ) ;
assertThat ( health . getStatus ( ) ) . isEqualTo ( Status . OUT_OF_SERVICE ) ;
assertThat ( health . getStatus ( ) ) . isEqualTo ( Status . DOWN ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void elasticsearchIsOutOfService ByResponseCode ( ) throws IOException {
public void elasticsearchIsDown ByResponseCode ( ) throws IOException {
given ( this . jestClient . execute ( any ( Action . class ) ) )
. willReturn ( createJestResult ( "" , 500 , true ) ) ;
. willReturn ( createJestResult ( 500 , false , "" ) ) ;
Health health = this . healthIndicator . health ( ) ;
assertThat ( health . getStatus ( ) ) . isEqualTo ( Status . OUT_OF_SERVICE ) ;
assertThat ( health . getStatus ( ) ) . isEqualTo ( Status . DOWN ) ;
}
@SuppressWarnings ( "unchecked" )
@Test
public void elasticsearchIsOutOfServiceBySucceeded ( ) throws IOException {
public void elasticsearchIsOutOfServiceByStatus ( ) throws IOException {
given ( this . jestClient . execute ( any ( Action . class ) ) )
. willReturn ( createJestResult ( "red" , 500 , false ) ) ;
. willReturn ( createJestResult ( 200 , true , "red" ) ) ;
Health health = this . healthIndicator . health ( ) ;
assertThat ( health . getStatus ( ) ) . isEqualTo ( Status . OUT_OF_SERVICE ) ;
}
private static JestResult createJestResult ( String status , int responseCode ,
boolean succeeded ) {
String json = String . format ( "{\"cluster_name\":\"docker-cluster\","
+ "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1,"
+ "\"number_of_data_nodes\":1,\"active_primary_shards\":0,"
+ "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0,"
+ "\"unassigned_shards\":0,\"delayed_unassigned_shards\":0,"
+ "\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0,"
+ "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}" ,
status ) ;
private static JestResult createJestResult ( int responseCode , boolean succeeded ,
String status ) {
SearchResult searchResult = new SearchResult ( new Gson ( ) ) ;
String json ;
if ( responseCode = = 200 ) {
json = String . format ( "{\"cluster_name\":\"elasticsearch\","
+ "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1,"
+ "\"number_of_data_nodes\":1,\"active_primary_shards\":0,"
+ "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0,"
+ "\"unassigned_shards\":0,\"delayed_unassigned_shards\":0,"
+ "\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0,"
+ "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}" ,
status ) ;
}
else {
json = "{\n" + " \"error\": \"Server Error\",\n" + " \"status\": "
+ responseCode + "\n" + "}" ;
}
searchResult . setJsonString ( json ) ;
searchResult . setJsonObject ( new JsonParser ( ) . parse ( json ) . getAsJsonObject ( ) ) ;
searchResult . setResponseCode ( responseCode ) ;