1919import java .io .IOException ;
2020
2121import 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 ;
2323import org .apache .solr .client .solrj .request .CoreAdminRequest ;
2424import org .apache .solr .client .solrj .response .SolrPingResponse ;
2525import org .apache .solr .common .util .NamedList ;
4343 * Tests for {@link SolrHealthIndicator}
4444 *
4545 * @author Andy Wilkinson
46+ * @author Markus Schuch
47+ * @author Phillip Webb
4648 */
4749public class SolrHealthIndicatorTests {
4850
@@ -56,94 +58,88 @@ public void close() {
5658 }
5759
5860 @ Test
59- public void solrIsUpWithBaseUrlPointsToRoot () throws Exception {
61+ public void healthWhenSolrStatusUpAndBaseUrlPointsToRootReturnsUp () throws Exception {
6062 SolrClient solrClient = mock (SolrClient .class );
6163 given (solrClient .request (any (CoreAdminRequest .class ), isNull ())).willReturn (mockResponse (0 ));
6264 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" );
6766 verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
6867 verifyNoMoreInteractions (solrClient );
6968 }
7069
7170 @ Test
72- public void solrIsUpWithBaseUrlPointsToParticularCore () throws Exception {
71+ public void healthWhenSolrStatusDownAndBaseUrlPointsToRootReturnsDown () throws Exception {
7372 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 ));
7774 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" );
8376 verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
84- verify (solrClient , times (1 )).ping ();
8577 verifyNoMoreInteractions (solrClient );
8678 }
8779
8880 @ Test
89- public void pathTypeIsRememberedForConsecutiveChecks () throws Exception {
81+ public void healthWhenSolrStatusUpAndBaseUrlPointsToParticularCoreReturnsUp () throws Exception {
9082 SolrClient solrClient = mock (SolrClient .class );
9183 given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
92- .willThrow (new HttpSolrClient . RemoteSolrException ("mock" , 404 , "" , null ));
84+ .willThrow (new RemoteSolrException ("mock" , 404 , "" , null ));
9385 given (solrClient .ping ()).willReturn (mockPingResponse (0 ));
9486 SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
95- healthIndicator . health ( );
87+ assertHealth ( healthIndicator , Status . UP , 0 , "particular core" );
9688 verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
9789 verify (solrClient , times (1 )).ping ();
9890 verifyNoMoreInteractions (solrClient );
99- healthIndicator .health ();
100- verify (solrClient , times (2 )).ping ();
101- verifyNoMoreInteractions (solrClient );
10291 }
10392
10493 @ Test
105- public void solrIsUpAndRequestFailedWithBaseUrlPointsToRoot () throws Exception {
94+ public void healthWhenSolrStatusDownAndBaseUrlPointsToParticularCoreReturnsDown () throws Exception {
10695 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 ));
10899 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" );
113101 verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
102+ verify (solrClient , times (1 )).ping ();
114103 verifyNoMoreInteractions (solrClient );
115104 }
116105
117106 @ Test
118- public void solrIsUpAndRequestFailedWithBaseUrlPointsToParticularCore () throws Exception {
107+ public void healthWhenSolrConnectionFailsReturnsDown () throws Exception {
119108 SolrClient solrClient = mock (SolrClient .class );
120109 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" ));
123111 SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
124112 Health health = healthIndicator .health ();
125113 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" );
129115 verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
130- verify (solrClient , times (1 )).ping ();
131116 verifyNoMoreInteractions (solrClient );
132117 }
133118
134119 @ Test
135- public void solrIsDown () throws Exception {
120+ public void healthWhenMakingMultipleCallsRemembersStatusStrategy () throws Exception {
136121 SolrClient solrClient = mock (SolrClient .class );
137122 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 ));
139125 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 ();
143127 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 ();
144132 verifyNoMoreInteractions (solrClient );
145133 }
146134
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+
147143 private NamedList <Object > mockResponse (int status ) {
148144 NamedList <Object > response = new NamedList <>();
149145 NamedList <Object > headers = new NamedList <>();
0 commit comments