27
27
import java .net .URI ;
28
28
import java .util .Collections ;
29
29
import java .util .concurrent .CountDownLatch ;
30
+ import java .util .concurrent .TimeUnit ;
30
31
31
32
import static org .elasticsearch .client .RestClientTestUtil .getHttpMethods ;
32
33
import static org .hamcrest .Matchers .instanceOf ;
33
34
import static org .junit .Assert .assertEquals ;
34
35
import static org .junit .Assert .assertThat ;
36
+ import static org .junit .Assert .assertTrue ;
35
37
import static org .junit .Assert .fail ;
36
38
import static org .mockito .Mockito .mock ;
37
39
import static org .mockito .Mockito .times ;
@@ -57,17 +59,20 @@ public void testPerformAsyncWithUnsupportedMethod() throws Exception {
57
59
restClient .performRequestAsync (new Request ("unsupported" , randomAsciiLettersOfLength (5 )), new ResponseListener () {
58
60
@ Override
59
61
public void onSuccess (Response response ) {
60
- fail ( "should have failed because of unsupported method " );
62
+ throw new UnsupportedOperationException ( "onSuccess cannot be called when using a mocked http client " );
61
63
}
62
64
63
65
@ Override
64
66
public void onFailure (Exception exception ) {
65
- assertThat (exception , instanceOf (UnsupportedOperationException .class ));
66
- assertEquals ("http method not supported: unsupported" , exception .getMessage ());
67
- latch .countDown ();
67
+ try {
68
+ assertThat (exception , instanceOf (UnsupportedOperationException .class ));
69
+ assertEquals ("http method not supported: unsupported" , exception .getMessage ());
70
+ } finally {
71
+ latch .countDown ();
72
+ }
68
73
}
69
74
});
70
- latch .await ();
75
+ assertTrue ( "time out waiting for request to return" , latch .await (1000 , TimeUnit . MILLISECONDS ) );
71
76
}
72
77
}
73
78
@@ -81,17 +86,20 @@ public void testPerformAsyncOldStyleWithUnsupportedMethod() throws Exception {
81
86
restClient .performRequestAsync ("unsupported" , randomAsciiLettersOfLength (5 ), new ResponseListener () {
82
87
@ Override
83
88
public void onSuccess (Response response ) {
84
- fail ( "should have failed because of unsupported method " );
89
+ throw new UnsupportedOperationException ( "onSuccess cannot be called when using a mocked http client " );
85
90
}
86
91
87
92
@ Override
88
93
public void onFailure (Exception exception ) {
89
- assertThat (exception , instanceOf (UnsupportedOperationException .class ));
90
- assertEquals ("http method not supported: unsupported" , exception .getMessage ());
91
- latch .countDown ();
94
+ try {
95
+ assertThat (exception , instanceOf (UnsupportedOperationException .class ));
96
+ assertEquals ("http method not supported: unsupported" , exception .getMessage ());
97
+ } finally {
98
+ latch .countDown ();
99
+ }
92
100
}
93
101
});
94
- latch .await ();
102
+ assertTrue ( "time out waiting for request to return" , latch .await (1000 , TimeUnit . MILLISECONDS ) );
95
103
}
96
104
}
97
105
@@ -105,17 +113,20 @@ public void testPerformOldStyleAsyncWithNullParams() throws Exception {
105
113
restClient .performRequestAsync (randomAsciiLettersOfLength (5 ), randomAsciiLettersOfLength (5 ), null , new ResponseListener () {
106
114
@ Override
107
115
public void onSuccess (Response response ) {
108
- fail ( "should have failed because of null parameters " );
116
+ throw new UnsupportedOperationException ( "onSuccess cannot be called when using a mocked http client " );
109
117
}
110
118
111
119
@ Override
112
120
public void onFailure (Exception exception ) {
113
- assertThat (exception , instanceOf (NullPointerException .class ));
114
- assertEquals ("parameters cannot be null" , exception .getMessage ());
115
- latch .countDown ();
121
+ try {
122
+ assertThat (exception , instanceOf (NullPointerException .class ));
123
+ assertEquals ("parameters cannot be null" , exception .getMessage ());
124
+ } finally {
125
+ latch .countDown ();
126
+ }
116
127
}
117
128
});
118
- latch .await ();
129
+ assertTrue ( "time out waiting for request to return" , latch .await (1000 , TimeUnit . MILLISECONDS ) );
119
130
}
120
131
}
121
132
@@ -129,18 +140,21 @@ public void testPerformOldStyleAsyncWithNullHeaders() throws Exception {
129
140
ResponseListener listener = new ResponseListener () {
130
141
@ Override
131
142
public void onSuccess (Response response ) {
132
- fail ( "should have failed because of null headers " );
143
+ throw new UnsupportedOperationException ( "onSuccess cannot be called when using a mocked http client " );
133
144
}
134
145
135
146
@ Override
136
147
public void onFailure (Exception exception ) {
137
- assertThat (exception , instanceOf (NullPointerException .class ));
138
- assertEquals ("header cannot be null" , exception .getMessage ());
139
- latch .countDown ();
148
+ try {
149
+ assertThat (exception , instanceOf (NullPointerException .class ));
150
+ assertEquals ("header cannot be null" , exception .getMessage ());
151
+ } finally {
152
+ latch .countDown ();
153
+ }
140
154
}
141
155
};
142
156
restClient .performRequestAsync ("GET" , randomAsciiLettersOfLength (5 ), listener , (Header ) null );
143
- latch .await ();
157
+ assertTrue ( "time out waiting for request to return" , latch .await (1000 , TimeUnit . MILLISECONDS ) );
144
158
}
145
159
}
146
160
@@ -150,17 +164,20 @@ public void testPerformAsyncWithWrongEndpoint() throws Exception {
150
164
restClient .performRequestAsync (new Request ("GET" , "::http:///" ), new ResponseListener () {
151
165
@ Override
152
166
public void onSuccess (Response response ) {
153
- fail ( "should have failed because of wrong endpoint " );
167
+ throw new UnsupportedOperationException ( "onSuccess cannot be called when using a mocked http client " );
154
168
}
155
169
156
170
@ Override
157
171
public void onFailure (Exception exception ) {
158
- assertThat (exception , instanceOf (IllegalArgumentException .class ));
159
- assertEquals ("Expected scheme name at index 0: ::http:///" , exception .getMessage ());
160
- latch .countDown ();
172
+ try {
173
+ assertThat (exception , instanceOf (IllegalArgumentException .class ));
174
+ assertEquals ("Expected scheme name at index 0: ::http:///" , exception .getMessage ());
175
+ } finally {
176
+ latch .countDown ();
177
+ }
161
178
}
162
179
});
163
- latch .await ();
180
+ assertTrue ( "time out waiting for request to return" , latch .await (1000 , TimeUnit . MILLISECONDS ) );
164
181
}
165
182
}
166
183
@@ -174,17 +191,20 @@ public void testPerformAsyncOldStyleWithWrongEndpoint() throws Exception {
174
191
restClient .performRequestAsync ("GET" , "::http:///" , new ResponseListener () {
175
192
@ Override
176
193
public void onSuccess (Response response ) {
177
- fail ( "should have failed because of wrong endpoint " );
194
+ throw new UnsupportedOperationException ( "onSuccess cannot be called when using a mocked http client " );
178
195
}
179
196
180
197
@ Override
181
198
public void onFailure (Exception exception ) {
182
- assertThat (exception , instanceOf (IllegalArgumentException .class ));
183
- assertEquals ("Expected scheme name at index 0: ::http:///" , exception .getMessage ());
184
- latch .countDown ();
199
+ try {
200
+ assertThat (exception , instanceOf (IllegalArgumentException .class ));
201
+ assertEquals ("Expected scheme name at index 0: ::http:///" , exception .getMessage ());
202
+ } finally {
203
+ latch .countDown ();
204
+ }
185
205
}
186
206
});
187
- latch .await ();
207
+ assertTrue ( "time out waiting for request to return" , latch .await (1000 , TimeUnit . MILLISECONDS ) );
188
208
}
189
209
}
190
210
0 commit comments