1212import junitparams .Parameters ;
1313import org .junit .Rule ;
1414import org .junit .Test ;
15- import org .junit .rules .ExpectedException ;
1615import org .junit .runner .RunWith ;
1716import org .skyscreamer .jsonassert .JSONAssert ;
1817
2221import static com .jcabi .matchers .RegexMatchers .matchesPattern ;
2322import static com .maxmind .minfraud .request .RequestTestHelper .*;
2423import static org .hamcrest .core .StringStartsWith .startsWith ;
25- import static org .junit .Assert .assertEquals ;
26- import static org .junit .Assert .assertFalse ;
27- import static org .junit .Assert .assertTrue ;
24+ import static org .junit .Assert .*;
25+ import static org .hamcrest .MatcherAssert .assertThat ;
2826
2927@ RunWith (JUnitParamsRunner .class )
3028public class WebServiceClientTest {
31-
32- @ Rule
33- public final ExpectedException thrown = ExpectedException .none ();
34-
3529 @ Rule
3630 public final WireMockRule wireMockRule = new WireMockRule (0 ); // 0 picks random port
3731
@@ -82,7 +76,7 @@ public void testFullInsightsTransaction() throws Exception {
8276 response .getIpAddress ().getRepresentedCountry ().isInEuropeanUnion ());
8377 assertEquals ("2018-04-05T15:34:40-07:00" , response .getDevice ().getLocalTime ());
8478
85- assertEquals ("81.2.69.160" , response .getIpAddress ().getTraits ().getIpAddress () );
79+ assertEquals ("81.2.69.160" , response .getIpAddress ().getTraits ().getIpAddress ());
8680 assertEquals ("81.2.69.0/24" , response .getIpAddress ().getTraits ().getNetwork ().toString ());
8781
8882 assertTrue (response .getCreditCard ().isVirtual ());
@@ -112,7 +106,7 @@ public void testFullFactorsTransaction() throws Exception {
112106 response .getIpAddress ().getRepresentedCountry ().isInEuropeanUnion ());
113107
114108
115- assertEquals ("81.2.69.160" , response .getIpAddress ().getTraits ().getIpAddress () );
109+ assertEquals ("81.2.69.160" , response .getIpAddress ().getTraits ().getIpAddress ());
116110 assertEquals ("81.2.69.0/24" , response .getIpAddress ().getTraits ().getNetwork ().toString ());
117111 }
118112 }
@@ -138,10 +132,9 @@ public void testRequestEncoding() throws Exception {
138132 public void test200WithNoBody () throws Exception {
139133 try (WebServiceClient client = createSuccessClient ("insights" , "" )) {
140134 Transaction request = fullTransaction ();
135+ Exception ex = assertThrows (MinFraudException .class , () -> client .insights (request ));
141136
142- thrown .expect (MinFraudException .class );
143- thrown .expectMessage (matchesPattern ("Received a 200 response but could not decode it as JSON" ));
144- client .insights (request );
137+ assertThat (ex .getMessage (), matchesPattern ("Received a 200 response but could not decode it as JSON" ));
145138 }
146139 }
147140
@@ -150,21 +143,23 @@ public void test200WithInvalidJson() throws Exception {
150143 try (WebServiceClient client = createSuccessClient ("insights" , "{" )) {
151144 Transaction request = fullTransaction ();
152145
153- thrown . expect (MinFraudException .class );
154- thrown . expectMessage ( "Received a 200 response but could not decode it as JSON" );
155- client . insights ( request );
146+ Exception ex = assertThrows (MinFraudException .class , () -> client . insights ( request ) );
147+
148+ assertEquals ( "Received a 200 response but could not decode it as JSON" , ex . getMessage () );
156149 }
157150 }
158151
159152 @ Test
160153 public void testInsufficientCredit () throws Exception {
161- thrown .expect (InsufficientFundsException .class );
162- thrown .expectMessage ("out of credit" );
163- createInsightsError (
164- 402 ,
165- "application/json" ,
166- "{\" code\" :\" INSUFFICIENT_FUNDS\" ,\" error\" :\" out of credit\" }"
167- );
154+ Exception ex = assertThrows (InsufficientFundsException .class , () -> {
155+ createInsightsError (
156+ 402 ,
157+ "application/json" ,
158+ "{\" code\" :\" INSUFFICIENT_FUNDS\" ,\" error\" :\" out of credit\" }"
159+ );
160+ });
161+ assertEquals ("out of credit" , ex .getMessage ());
162+
168163 }
169164
170165 @ Test
@@ -173,101 +168,112 @@ public void testInsufficientCredit() throws Exception {
173168 "LICENSE_KEY_REQUIRED" ,
174169 "USER_ID_REQUIRED" })
175170 public void testInvalidAuth (String code ) throws Exception {
176- thrown . expect (AuthenticationException .class );
177- thrown . expectMessage ( "Invalid auth" );
178- createInsightsError (
179- 401 ,
180- "application/json" ,
181- "{ \" code \" : \" " + code + " \" , \" error \" : \" Invalid auth \" }"
171+ Exception ex = assertThrows (AuthenticationException .class , () ->
172+ createInsightsError (
173+ 401 ,
174+ "application/json" ,
175+ "{ \" code \" : \" " + code + " \" , \" error \" : \" Invalid auth \" }"
176+ )
182177 );
178+ assertEquals ("Invalid auth" , ex .getMessage ());
183179 }
184180
185181 @ Test
186182 public void testPermissionRequired () throws Exception {
187- thrown .expect (PermissionRequiredException .class );
188- thrown .expectMessage ("Permission required" );
189- createInsightsError (
190- 403 ,
191- "application/json" ,
192- "{\" code\" :\" PERMISSION_REQUIRED\" ,\" error\" :\" Permission required\" }"
193- );
183+ Exception ex = assertThrows (PermissionRequiredException .class , () -> {
184+ createInsightsError (
185+ 403 ,
186+ "application/json" ,
187+ "{\" code\" :\" PERMISSION_REQUIRED\" ,\" error\" :\" Permission required\" }"
188+ );
189+ });
190+ assertEquals ("Permission required" , ex .getMessage ());
194191 }
195192
196193 @ Test
197194 public void testInvalidRequest () throws Exception {
198- thrown . expect (InvalidRequestException .class );
199- thrown . expectMessage ( "IP invalid" );
200- createInsightsError (
201- 400 ,
202- "application/json" ,
203- "{ \" code \" : \" IP_ADDRESS_INVALID \" , \" error \" : \" IP invalid \" }"
195+ Exception ex = assertThrows (InvalidRequestException .class , () ->
196+ createInsightsError (
197+ 400 ,
198+ "application/json" ,
199+ "{ \" code \" : \" IP_ADDRESS_INVALID \" , \" error \" : \" IP invalid \" }"
200+ )
204201 );
202+ assertEquals ("IP invalid" , ex .getMessage ());
205203 }
206204
207205 @ Test
208206 public void test400WithInvalidJson () throws Exception {
209- thrown . expect (HttpException .class );
210- thrown . expectMessage ( matchesPattern ( "Received a 400 error for .*/minfraud/v2.0/insights but it did not include the expected JSON body: \\ {blah \\ }" ));
211- createInsightsError (
212- 400 ,
213- "application/json" ,
214- "{blah}"
207+ Exception ex = assertThrows (HttpException .class , () ->
208+ createInsightsError (
209+ 400 ,
210+ "application/json" ,
211+ "{blah}"
212+ )
215213 );
214+ assertThat (ex .getMessage (), matchesPattern ("Received a 400 error for .*/minfraud/v2.0/insights but it did not include the expected JSON body: \\ {blah\\ }" ));
216215 }
217216
218217 @ Test
219218 public void test400WithNoBody () throws Exception {
220- thrown . expect (HttpException .class );
221- thrown . expectMessage ( matchesPattern ( "Received a 400 error for .*/minfraud/v2.0/insights but it did not include the expected JSON body:.*" ));
222- createInsightsError (
223- 400 ,
224- "application/json" ,
225- ""
219+ Exception ex = assertThrows (HttpException .class , () ->
220+ createInsightsError (
221+ 400 ,
222+ "application/json" ,
223+ ""
224+ )
226225 );
226+ assertThat (ex .getMessage (), matchesPattern ("Received a 400 error for .*/minfraud/v2.0/insights but it did not include the expected JSON body:.*" ));
227227 }
228228
229229 @ Test
230230 public void test400WithUnexpectedContentType () throws Exception {
231- thrown . expect (HttpException .class );
232- thrown . expectMessage ( matchesPattern ( "Received a 400 error for .*/minfraud/v2.0/insights but it did not include the expected JSON body: text" ));
233- createInsightsError (
234- 400 ,
235- "text/plain" ,
236- "text"
231+ Exception ex = assertThrows (HttpException .class , () ->
232+ createInsightsError (
233+ 400 ,
234+ "text/plain" ,
235+ "text"
236+ )
237237 );
238+ assertThat (ex .getMessage (), matchesPattern ("Received a 400 error for .*/minfraud/v2.0/insights but it did not include the expected JSON body: text" ));
239+
238240 }
239241
240242 @ Test
241243 public void test400WithUnexpectedJson () throws Exception {
242- thrown . expect (HttpException .class );
243- thrown . expectMessage ( "Error response contains JSON but it does not specify code or error keys: { \" not \" : \" expected \" }" );
244- createInsightsError (
245- 400 ,
246- "application/json" ,
247- "{ \" not \" : \" expected \" }"
244+ Exception ex = assertThrows (HttpException .class , () ->
245+ createInsightsError (
246+ 400 ,
247+ "application/json" ,
248+ "{ \" not \" : \" expected \" }"
249+ )
248250 );
251+ assertEquals ("Error response contains JSON but it does not specify code or error keys: {\" not\" :\" expected\" }" , ex .getMessage ());
249252 }
250253
251254 @ Test
252255 public void test300 () throws Exception {
253- thrown . expect (HttpException .class );
254- thrown . expectMessage ( startsWith ( "Received an unexpected HTTP status (300)" ));
255- createInsightsError (
256- 300 ,
257- "application/json" ,
258- ""
256+ Exception ex = assertThrows (HttpException .class , () ->
257+ createInsightsError (
258+ 300 ,
259+ "application/json" ,
260+ ""
261+ )
259262 );
263+ assertThat (ex .getMessage (), startsWith ("Received an unexpected HTTP status (300)" ));
264+
260265 }
261266
262267 @ Test
263268 public void test500 () throws Exception {
264- thrown . expect (HttpException .class );
265- thrown . expectMessage ( startsWith ( "Received a server error (500)" ));
266- createInsightsError (
267- 500 ,
268- "application/json" ,
269- ""
269+ Exception ex = assertThrows (HttpException .class , () ->
270+ createInsightsError (
271+ 500 ,
272+ "application/json" ,
273+ ""
274+ )
270275 );
276+ assertThat (ex .getMessage (), startsWith ("Received a server error (500)" ));
271277 }
272278
273279 private WebServiceClient createSuccessClient (String service , String responseContent ) {
0 commit comments