19
19
import java .io .IOException ;
20
20
21
21
import org .apache .solr .client .solrj .SolrClient ;
22
- import org .apache .solr .client .solrj .impl .HttpSolrClient ;
22
+ import org .apache .solr .client .solrj .impl .HttpSolrClient . RemoteSolrException ;
23
23
import org .apache .solr .client .solrj .request .CoreAdminRequest ;
24
24
import org .apache .solr .client .solrj .response .SolrPingResponse ;
25
25
import org .apache .solr .common .util .NamedList ;
43
43
* Tests for {@link SolrHealthIndicator}
44
44
*
45
45
* @author Andy Wilkinson
46
+ * @author Markus Schuch
47
+ * @author Phillip Webb
46
48
*/
47
49
public class SolrHealthIndicatorTests {
48
50
@@ -56,94 +58,88 @@ public void close() {
56
58
}
57
59
58
60
@ Test
59
- public void solrIsUpWithBaseUrlPointsToRoot () throws Exception {
61
+ public void healthWhenSolrStatusUpAndBaseUrlPointsToRootReturnsUp () throws Exception {
60
62
SolrClient solrClient = mock (SolrClient .class );
61
63
given (solrClient .request (any (CoreAdminRequest .class ), isNull ())).willReturn (mockResponse (0 ));
62
64
SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
63
- Health health = healthIndicator .health ();
64
- assertThat (health .getStatus ()).isEqualTo (Status .UP );
65
- assertThat (health .getDetails ().get ("status" )).isEqualTo (0 );
66
- assertThat (health .getDetails ().get ("detectedPathType" )).isEqualTo (SolrHealthIndicator .PathType .ROOT .toString ());
65
+ assertHealth (healthIndicator , Status .UP , 0 , "root" );
67
66
verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
68
67
verifyNoMoreInteractions (solrClient );
69
68
}
70
69
71
70
@ Test
72
- public void solrIsUpWithBaseUrlPointsToParticularCore () throws Exception {
71
+ public void healthWhenSolrStatusDownAndBaseUrlPointsToRootReturnsDown () throws Exception {
73
72
SolrClient solrClient = mock (SolrClient .class );
74
- given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
75
- .willThrow (new HttpSolrClient .RemoteSolrException ("mock" , 404 , "" , null ));
76
- given (solrClient .ping ()).willReturn (mockPingResponse (0 ));
73
+ given (solrClient .request (any (CoreAdminRequest .class ), isNull ())).willReturn (mockResponse (400 ));
77
74
SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
78
- Health health = healthIndicator .health ();
79
- assertThat (health .getStatus ()).isEqualTo (Status .UP );
80
- assertThat (health .getDetails ().get ("status" )).isEqualTo (0 );
81
- assertThat (health .getDetails ().get ("detectedPathType" ))
82
- .isEqualTo (SolrHealthIndicator .PathType .PARTICULAR_CORE .toString ());
75
+ assertHealth (healthIndicator , Status .DOWN , 400 , "root" );
83
76
verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
84
- verify (solrClient , times (1 )).ping ();
85
77
verifyNoMoreInteractions (solrClient );
86
78
}
87
79
88
80
@ Test
89
- public void pathTypeIsRememberedForConsecutiveChecks () throws Exception {
81
+ public void healthWhenSolrStatusUpAndBaseUrlPointsToParticularCoreReturnsUp () throws Exception {
90
82
SolrClient solrClient = mock (SolrClient .class );
91
83
given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
92
- .willThrow (new HttpSolrClient . RemoteSolrException ("mock" , 404 , "" , null ));
84
+ .willThrow (new RemoteSolrException ("mock" , 404 , "" , null ));
93
85
given (solrClient .ping ()).willReturn (mockPingResponse (0 ));
94
86
SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
95
- healthIndicator . health ( );
87
+ assertHealth ( healthIndicator , Status . UP , 0 , "particular core" );
96
88
verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
97
89
verify (solrClient , times (1 )).ping ();
98
90
verifyNoMoreInteractions (solrClient );
99
- healthIndicator .health ();
100
- verify (solrClient , times (2 )).ping ();
101
- verifyNoMoreInteractions (solrClient );
102
91
}
103
92
104
93
@ Test
105
- public void solrIsUpAndRequestFailedWithBaseUrlPointsToRoot () throws Exception {
94
+ public void healthWhenSolrStatusDownAndBaseUrlPointsToParticularCoreReturnsDown () throws Exception {
106
95
SolrClient solrClient = mock (SolrClient .class );
107
- given (solrClient .request (any (CoreAdminRequest .class ), isNull ())).willReturn (mockResponse (400 ));
96
+ given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
97
+ .willThrow (new RemoteSolrException ("mock" , 404 , "" , null ));
98
+ given (solrClient .ping ()).willReturn (mockPingResponse (400 ));
108
99
SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
109
- Health health = healthIndicator .health ();
110
- assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
111
- assertThat (health .getDetails ().get ("status" )).isEqualTo (400 );
112
- assertThat (health .getDetails ().get ("detectedPathType" )).isEqualTo (SolrHealthIndicator .PathType .ROOT .toString ());
100
+ assertHealth (healthIndicator , Status .DOWN , 400 , "particular core" );
113
101
verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
102
+ verify (solrClient , times (1 )).ping ();
114
103
verifyNoMoreInteractions (solrClient );
115
104
}
116
105
117
106
@ Test
118
- public void solrIsUpAndRequestFailedWithBaseUrlPointsToParticularCore () throws Exception {
107
+ public void healthWhenSolrConnectionFailsReturnsDown () throws Exception {
119
108
SolrClient solrClient = mock (SolrClient .class );
120
109
given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
121
- .willThrow (new HttpSolrClient .RemoteSolrException ("mock" , 404 , "" , null ));
122
- given (solrClient .ping ()).willReturn (mockPingResponse (400 ));
110
+ .willThrow (new IOException ("Connection failed" ));
123
111
SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
124
112
Health health = healthIndicator .health ();
125
113
assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
126
- assertThat (health .getDetails ().get ("status" )).isEqualTo (400 );
127
- assertThat (health .getDetails ().get ("detectedPathType" ))
128
- .isEqualTo (SolrHealthIndicator .PathType .PARTICULAR_CORE .toString ());
114
+ assertThat ((String ) health .getDetails ().get ("error" )).contains ("Connection failed" );
129
115
verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
130
- verify (solrClient , times (1 )).ping ();
131
116
verifyNoMoreInteractions (solrClient );
132
117
}
133
118
134
119
@ Test
135
- public void solrIsDown () throws Exception {
120
+ public void healthWhenMakingMultipleCallsRemembersStatusStrategy () throws Exception {
136
121
SolrClient solrClient = mock (SolrClient .class );
137
122
given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
138
- .willThrow (new IOException ("Connection failed" ));
123
+ .willThrow (new RemoteSolrException ("mock" , 404 , "" , null ));
124
+ given (solrClient .ping ()).willReturn (mockPingResponse (0 ));
139
125
SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
140
- Health health = healthIndicator .health ();
141
- assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
142
- assertThat ((String ) health .getDetails ().get ("error" )).contains ("Connection failed" );
126
+ healthIndicator .health ();
143
127
verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
128
+ verify (solrClient , times (1 )).ping ();
129
+ verifyNoMoreInteractions (solrClient );
130
+ healthIndicator .health ();
131
+ verify (solrClient , times (2 )).ping ();
144
132
verifyNoMoreInteractions (solrClient );
145
133
}
146
134
135
+ private void assertHealth (SolrHealthIndicator healthIndicator , Status expectedStatus , int expectedStatusCode ,
136
+ String expectedPathType ) {
137
+ Health health = healthIndicator .health ();
138
+ assertThat (health .getStatus ()).isEqualTo (expectedStatus );
139
+ assertThat (health .getDetails ().get ("status" )).isEqualTo (expectedStatusCode );
140
+ assertThat (health .getDetails ().get ("detectedPathType" )).isEqualTo (expectedPathType );
141
+ }
142
+
147
143
private NamedList <Object > mockResponse (int status ) {
148
144
NamedList <Object > response = new NamedList <>();
149
145
NamedList <Object > headers = new NamedList <>();
0 commit comments