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 .RemoteSolrException ;
22
23
import org .apache .solr .client .solrj .request .CoreAdminRequest ;
24
+ import org .apache .solr .client .solrj .response .SolrPingResponse ;
23
25
import org .apache .solr .common .util .NamedList ;
24
26
import org .junit .After ;
25
27
import org .junit .Test ;
33
35
import static org .mockito .ArgumentMatchers .isNull ;
34
36
import static org .mockito .BDDMockito .given ;
35
37
import static org .mockito .Mockito .mock ;
38
+ import static org .mockito .Mockito .times ;
39
+ import static org .mockito .Mockito .verify ;
40
+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
36
41
37
42
/**
38
43
* Tests for {@link SolrHealthIndicator}
39
44
*
40
45
* @author Andy Wilkinson
46
+ * @author Markus Schuch
47
+ * @author Phillip Webb
41
48
*/
42
49
public class SolrHealthIndicatorTests {
43
50
@@ -51,34 +58,86 @@ public void close() {
51
58
}
52
59
53
60
@ Test
54
- public void solrIsUp () throws Exception {
61
+ public void healthWhenSolrStatusUpAndBaseUrlPointsToRootReturnsUp () throws Exception {
55
62
SolrClient solrClient = mock (SolrClient .class );
56
63
given (solrClient .request (any (CoreAdminRequest .class ), isNull ())).willReturn (mockResponse (0 ));
57
64
SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
58
- Health health = healthIndicator . health ( );
59
- assertThat ( health . getStatus ( )).isEqualTo ( Status . UP );
60
- assertThat ( health . getDetails (). get ( "status" )). isEqualTo ( 0 );
65
+ assertHealth ( healthIndicator , Status . UP , 0 , "root" );
66
+ verify ( solrClient , times ( 1 )).request ( any ( CoreAdminRequest . class ), isNull () );
67
+ verifyNoMoreInteractions ( solrClient );
61
68
}
62
69
63
70
@ Test
64
- public void solrIsUpAndRequestFailed () throws Exception {
71
+ public void healthWhenSolrStatusDownAndBaseUrlPointsToRootReturnsDown () throws Exception {
65
72
SolrClient solrClient = mock (SolrClient .class );
66
73
given (solrClient .request (any (CoreAdminRequest .class ), isNull ())).willReturn (mockResponse (400 ));
67
74
SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
68
- Health health = healthIndicator .health ();
69
- assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
70
- assertThat (health .getDetails ().get ("status" )).isEqualTo (400 );
75
+ assertHealth (healthIndicator , Status .DOWN , 400 , "root" );
76
+ verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
77
+ verifyNoMoreInteractions (solrClient );
78
+ }
79
+
80
+ @ Test
81
+ public void healthWhenSolrStatusUpAndBaseUrlPointsToParticularCoreReturnsUp () throws Exception {
82
+ SolrClient solrClient = mock (SolrClient .class );
83
+ given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
84
+ .willThrow (new RemoteSolrException ("mock" , 404 , "" , null ));
85
+ given (solrClient .ping ()).willReturn (mockPingResponse (0 ));
86
+ SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
87
+ assertHealth (healthIndicator , Status .UP , 0 , "particular core" );
88
+ verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
89
+ verify (solrClient , times (1 )).ping ();
90
+ verifyNoMoreInteractions (solrClient );
91
+ }
92
+
93
+ @ Test
94
+ public void healthWhenSolrStatusDownAndBaseUrlPointsToParticularCoreReturnsDown () throws Exception {
95
+ SolrClient solrClient = mock (SolrClient .class );
96
+ given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
97
+ .willThrow (new RemoteSolrException ("mock" , 404 , "" , null ));
98
+ given (solrClient .ping ()).willReturn (mockPingResponse (400 ));
99
+ SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
100
+ assertHealth (healthIndicator , Status .DOWN , 400 , "particular core" );
101
+ verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
102
+ verify (solrClient , times (1 )).ping ();
103
+ verifyNoMoreInteractions (solrClient );
71
104
}
72
105
73
106
@ Test
74
- public void solrIsDown () throws Exception {
107
+ public void healthWhenSolrConnectionFailsReturnsDown () throws Exception {
75
108
SolrClient solrClient = mock (SolrClient .class );
76
109
given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
77
110
.willThrow (new IOException ("Connection failed" ));
78
111
SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
79
112
Health health = healthIndicator .health ();
80
113
assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
81
114
assertThat ((String ) health .getDetails ().get ("error" )).contains ("Connection failed" );
115
+ verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
116
+ verifyNoMoreInteractions (solrClient );
117
+ }
118
+
119
+ @ Test
120
+ public void healthWhenMakingMultipleCallsRemembersStatusStrategy () throws Exception {
121
+ SolrClient solrClient = mock (SolrClient .class );
122
+ given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
123
+ .willThrow (new RemoteSolrException ("mock" , 404 , "" , null ));
124
+ given (solrClient .ping ()).willReturn (mockPingResponse (0 ));
125
+ SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
126
+ healthIndicator .health ();
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 ();
132
+ verifyNoMoreInteractions (solrClient );
133
+ }
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 );
82
141
}
83
142
84
143
private NamedList <Object > mockResponse (int status ) {
@@ -89,4 +148,10 @@ private NamedList<Object> mockResponse(int status) {
89
148
return response ;
90
149
}
91
150
151
+ private SolrPingResponse mockPingResponse (int status ) {
152
+ SolrPingResponse pingResponse = new SolrPingResponse ();
153
+ pingResponse .setResponse (mockResponse (status ));
154
+ return pingResponse ;
155
+ }
156
+
92
157
}
0 commit comments