1
1
/*
2
- * Copyright 2012-2017 the original author or authors.
2
+ * Copyright 2012-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
40
40
*
41
41
* @author Stephane Nicoll
42
42
* @author Julian Devia Serna
43
+ * @author Brian Clozel
43
44
*/
44
45
public class ElasticsearchJestHealthIndicatorTests {
45
46
@@ -52,7 +53,7 @@ public class ElasticsearchJestHealthIndicatorTests {
52
53
@ Test
53
54
public void elasticsearchIsUp () throws IOException {
54
55
given (this .jestClient .execute (any (Action .class )))
55
- .willReturn (createJestResult ("green" , 200 , true ));
56
+ .willReturn (createJestResult (200 , true , "green" ));
56
57
Health health = this .healthIndicator .health ();
57
58
assertThat (health .getStatus ()).isEqualTo (Status .UP );
58
59
}
@@ -68,42 +69,50 @@ public void elasticsearchIsDown() throws IOException {
68
69
69
70
@ SuppressWarnings ("unchecked" )
70
71
@ Test
71
- public void elasticsearchIsOutOfServiceByStatus () throws IOException {
72
+ public void elasticsearchIsDownWhenQueryDidNotSucceed () throws IOException {
72
73
given (this .jestClient .execute (any (Action .class )))
73
- .willReturn (createJestResult ("red" , 200 , true ));
74
+ .willReturn (createJestResult (200 , false , "" ));
74
75
Health health = this .healthIndicator .health ();
75
- assertThat (health .getStatus ()).isEqualTo (Status .OUT_OF_SERVICE );
76
+ assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
76
77
}
77
78
78
79
@ SuppressWarnings ("unchecked" )
79
80
@ Test
80
- public void elasticsearchIsOutOfServiceByResponseCode () throws IOException {
81
+ public void elasticsearchIsDownByResponseCode () throws IOException {
81
82
given (this .jestClient .execute (any (Action .class )))
82
- .willReturn (createJestResult ("" , 500 , true ));
83
+ .willReturn (createJestResult (500 , false , "" ));
83
84
Health health = this .healthIndicator .health ();
84
- assertThat (health .getStatus ()).isEqualTo (Status .OUT_OF_SERVICE );
85
+ assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
85
86
}
86
87
87
88
@ SuppressWarnings ("unchecked" )
88
89
@ Test
89
- public void elasticsearchIsOutOfServiceBySucceeded () throws IOException {
90
+ public void elasticsearchIsOutOfServiceByStatus () throws IOException {
90
91
given (this .jestClient .execute (any (Action .class )))
91
- .willReturn (createJestResult ("red" , 500 , false ));
92
+ .willReturn (createJestResult (200 , true , "red" ));
92
93
Health health = this .healthIndicator .health ();
93
94
assertThat (health .getStatus ()).isEqualTo (Status .OUT_OF_SERVICE );
94
95
}
95
96
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
+
106
100
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
+ }
107
116
searchResult .setJsonString (json );
108
117
searchResult .setJsonObject (new JsonParser ().parse (json ).getAsJsonObject ());
109
118
searchResult .setResponseCode (responseCode );
0 commit comments