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. 
4040 * 
4141 * @author Stephane Nicoll 
4242 * @author Julian Devia Serna 
43+  * @author Brian Clozel 
4344 */ 
4445public  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