1919import java .io .IOException ;
2020
2121import org .apache .solr .client .solrj .SolrClient ;
22+ import org .apache .solr .client .solrj .impl .HttpSolrClient ;
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}
@@ -51,23 +56,79 @@ public void close() {
5156 }
5257
5358 @ Test
54- public void solrIsUp () throws Exception {
59+ public void solrIsUpWithBaseUrlPointsToRoot () throws Exception {
5560 SolrClient solrClient = mock (SolrClient .class );
5661 given (solrClient .request (any (CoreAdminRequest .class ), isNull ())).willReturn (mockResponse (0 ));
5762 SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
5863 Health health = healthIndicator .health ();
5964 assertThat (health .getStatus ()).isEqualTo (Status .UP );
6065 assertThat (health .getDetails ().get ("status" )).isEqualTo (0 );
66+ assertThat (health .getDetails ().get ("detectedPathType" )).isEqualTo (SolrHealthIndicator .PathType .ROOT .toString ());
67+ verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
68+ verifyNoMoreInteractions (solrClient );
6169 }
6270
6371 @ Test
64- public void solrIsUpAndRequestFailed () throws Exception {
72+ public void solrIsUpWithBaseUrlPointsToParticularCore () throws Exception {
73+ 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 ));
77+ 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 ());
83+ verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
84+ verify (solrClient , times (1 )).ping ();
85+ verifyNoMoreInteractions (solrClient );
86+ }
87+
88+ @ Test
89+ public void pathTypeIsRememberedForConsecutiveChecks () throws Exception {
90+ SolrClient solrClient = mock (SolrClient .class );
91+ given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
92+ .willThrow (new HttpSolrClient .RemoteSolrException ("mock" , 404 , "" , null ));
93+ given (solrClient .ping ()).willReturn (mockPingResponse (0 ));
94+ SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
95+ healthIndicator .health ();
96+ verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
97+ verify (solrClient , times (1 )).ping ();
98+ verifyNoMoreInteractions (solrClient );
99+ healthIndicator .health ();
100+ verify (solrClient , times (2 )).ping ();
101+ verifyNoMoreInteractions (solrClient );
102+ }
103+
104+ @ Test
105+ public void solrIsUpAndRequestFailedWithBaseUrlPointsToRoot () throws Exception {
65106 SolrClient solrClient = mock (SolrClient .class );
66107 given (solrClient .request (any (CoreAdminRequest .class ), isNull ())).willReturn (mockResponse (400 ));
67108 SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
68109 Health health = healthIndicator .health ();
69110 assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
70111 assertThat (health .getDetails ().get ("status" )).isEqualTo (400 );
112+ assertThat (health .getDetails ().get ("detectedPathType" )).isEqualTo (SolrHealthIndicator .PathType .ROOT .toString ());
113+ verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
114+ verifyNoMoreInteractions (solrClient );
115+ }
116+
117+ @ Test
118+ public void solrIsUpAndRequestFailedWithBaseUrlPointsToParticularCore () throws Exception {
119+ SolrClient solrClient = mock (SolrClient .class );
120+ given (solrClient .request (any (CoreAdminRequest .class ), isNull ()))
121+ .willThrow (new HttpSolrClient .RemoteSolrException ("mock" , 404 , "" , null ));
122+ given (solrClient .ping ()).willReturn (mockPingResponse (400 ));
123+ SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
124+ Health health = healthIndicator .health ();
125+ 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 ());
129+ verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
130+ verify (solrClient , times (1 )).ping ();
131+ verifyNoMoreInteractions (solrClient );
71132 }
72133
73134 @ Test
@@ -79,6 +140,8 @@ public void solrIsDown() throws Exception {
79140 Health health = healthIndicator .health ();
80141 assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
81142 assertThat ((String ) health .getDetails ().get ("error" )).contains ("Connection failed" );
143+ verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
144+ verifyNoMoreInteractions (solrClient );
82145 }
83146
84147 private NamedList <Object > mockResponse (int status ) {
@@ -89,4 +152,10 @@ private NamedList<Object> mockResponse(int status) {
89152 return response ;
90153 }
91154
155+ private SolrPingResponse mockPingResponse (int status ) {
156+ SolrPingResponse pingResponse = new SolrPingResponse ();
157+ pingResponse .setResponse (mockResponse (status ));
158+ return pingResponse ;
159+ }
160+
92161}
0 commit comments