1919import java .io .IOException ;
2020
2121import org .apache .solr .client .solrj .SolrClient ;
22+ import org .apache .solr .client .solrj .impl .HttpSolrClient .RemoteSolrException ;
2223import org .apache .solr .client .solrj .request .CoreAdminRequest ;
24+ import org .apache .solr .client .solrj .response .SolrPingResponse ;
2325import org .apache .solr .common .util .NamedList ;
2426import org .junit .After ;
2527import org .junit .Test ;
3335import static org .mockito .ArgumentMatchers .isNull ;
3436import static org .mockito .BDDMockito .given ;
3537import 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 ;
3641
3742/**
3843 * Tests for {@link SolrHealthIndicator}
3944 *
4045 * @author Andy Wilkinson
46+ * @author Markus Schuch
47+ * @author Phillip Webb
4148 */
4249public class SolrHealthIndicatorTests {
4350
@@ -51,34 +58,86 @@ public void close() {
5158 }
5259
5360 @ Test
54- public void solrIsUp () throws Exception {
61+ public void healthWhenSolrStatusUpAndBaseUrlPointsToRootReturnsUp () throws Exception {
5562 SolrClient solrClient = mock (SolrClient .class );
5663 given (solrClient .request (any (CoreAdminRequest .class ), isNull ())).willReturn (mockResponse (0 ));
5764 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 );
6168 }
6269
6370 @ Test
64- public void solrIsUpAndRequestFailed () throws Exception {
71+ public void healthWhenSolrStatusDownAndBaseUrlPointsToRootReturnsDown () throws Exception {
6572 SolrClient solrClient = mock (SolrClient .class );
6673 given (solrClient .request (any (CoreAdminRequest .class ), isNull ())).willReturn (mockResponse (400 ));
6774 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 );
71104 }
72105
73106 @ Test
74- public void solrIsDown () throws Exception {
107+ public void healthWhenSolrConnectionFailsReturnsDown () throws Exception {
75108 SolrClient solrClient = mock (SolrClient .class );
76109 given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
77110 .willThrow (new IOException ("Connection failed" ));
78111 SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
79112 Health health = healthIndicator .health ();
80113 assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
81114 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 );
82141 }
83142
84143 private NamedList <Object > mockResponse (int status ) {
@@ -89,4 +148,10 @@ private NamedList<Object> mockResponse(int status) {
89148 return response ;
90149 }
91150
151+ private SolrPingResponse mockPingResponse (int status ) {
152+ SolrPingResponse pingResponse = new SolrPingResponse ();
153+ pingResponse .setResponse (mockResponse (status ));
154+ return pingResponse ;
155+ }
156+
92157}
0 commit comments