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
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}
@@ -51,23 +56,79 @@ public void close() {
51
56
}
52
57
53
58
@ Test
54
- public void solrIsUp () throws Exception {
59
+ public void solrIsUpWithBaseUrlPointsToRoot () throws Exception {
55
60
SolrClient solrClient = mock (SolrClient .class );
56
61
given (solrClient .request (any (CoreAdminRequest .class ), isNull ())).willReturn (mockResponse (0 ));
57
62
SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
58
63
Health health = healthIndicator .health ();
59
64
assertThat (health .getStatus ()).isEqualTo (Status .UP );
60
65
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 );
61
69
}
62
70
63
71
@ 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 {
65
106
SolrClient solrClient = mock (SolrClient .class );
66
107
given (solrClient .request (any (CoreAdminRequest .class ), isNull ())).willReturn (mockResponse (400 ));
67
108
SolrHealthIndicator healthIndicator = new SolrHealthIndicator (solrClient );
68
109
Health health = healthIndicator .health ();
69
110
assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
70
111
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 );
71
132
}
72
133
73
134
@ Test
@@ -79,6 +140,8 @@ public void solrIsDown() throws Exception {
79
140
Health health = healthIndicator .health ();
80
141
assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
81
142
assertThat ((String ) health .getDetails ().get ("error" )).contains ("Connection failed" );
143
+ verify (solrClient , times (1 )).request (any (CoreAdminRequest .class ), isNull ());
144
+ verifyNoMoreInteractions (solrClient );
82
145
}
83
146
84
147
private NamedList <Object > mockResponse (int status ) {
@@ -89,4 +152,10 @@ private NamedList<Object> mockResponse(int status) {
89
152
return response ;
90
153
}
91
154
155
+ private SolrPingResponse mockPingResponse (int status ) {
156
+ SolrPingResponse pingResponse = new SolrPingResponse ();
157
+ pingResponse .setResponse (mockResponse (status ));
158
+ return pingResponse ;
159
+ }
160
+
92
161
}
0 commit comments