1+ package io .getstream .client .apache .repo .handlers ;
2+
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import io .getstream .client .exception .AuthenticationFailedException ;
5+ import io .getstream .client .exception .InternalServerException ;
6+ import io .getstream .client .exception .InvalidOrMissingInputException ;
7+ import io .getstream .client .exception .RateLimitExceededException ;
8+ import io .getstream .client .exception .ResourceNotFoundException ;
9+ import io .getstream .client .exception .StreamClientException ;
10+ import org .apache .http .ProtocolVersion ;
11+ import org .apache .http .client .methods .CloseableHttpResponse ;
12+ import org .apache .http .entity .StringEntity ;
13+ import org .apache .http .message .BasicStatusLine ;
14+ import org .junit .Test ;
15+
16+ import java .io .IOException ;
17+
18+ import static org .mockito .Mockito .mock ;
19+ import static org .mockito .Mockito .when ;
20+
21+ public class StreamExceptionHandlerTest {
22+
23+ private final StreamExceptionHandler exceptionHandler ;
24+
25+ public StreamExceptionHandlerTest () {
26+ exceptionHandler = new StreamExceptionHandler (mock (ObjectMapper .class ));
27+ }
28+
29+ @ Test (expected = RateLimitExceededException .class )
30+ public void testError429 () throws IOException , StreamClientException {
31+ CloseableHttpResponse response = mock (CloseableHttpResponse .class );
32+ when (response .getStatusLine ()).thenReturn (new BasicStatusLine (new ProtocolVersion ("HTTP" , 1 , 1 ), 429 , "" ));
33+ when (response .getEntity ()).thenReturn (new StringEntity ("" ));
34+ exceptionHandler .handleResponseCode (response );
35+ }
36+
37+ @ Test (expected = InvalidOrMissingInputException .class )
38+ public void testError400 () throws IOException , StreamClientException {
39+ CloseableHttpResponse response = mock (CloseableHttpResponse .class );
40+ when (response .getStatusLine ()).thenReturn (new BasicStatusLine (new ProtocolVersion ("HTTP" , 1 , 1 ), 400 , "" ));
41+ when (response .getEntity ()).thenReturn (new StringEntity ("" ));
42+ exceptionHandler .handleResponseCode (response );
43+ }
44+
45+ @ Test (expected = AuthenticationFailedException .class )
46+ public void testError401 () throws IOException , StreamClientException {
47+ CloseableHttpResponse response = mock (CloseableHttpResponse .class );
48+ when (response .getStatusLine ()).thenReturn (new BasicStatusLine (new ProtocolVersion ("HTTP" , 1 , 1 ), 401 , "" ));
49+ when (response .getEntity ()).thenReturn (new StringEntity ("" ));
50+ exceptionHandler .handleResponseCode (response );
51+ }
52+
53+ @ Test (expected = AuthenticationFailedException .class )
54+ public void testError403 () throws IOException , StreamClientException {
55+ CloseableHttpResponse response = mock (CloseableHttpResponse .class );
56+ when (response .getStatusLine ()).thenReturn (new BasicStatusLine (new ProtocolVersion ("HTTP" , 1 , 1 ), 403 , "" ));
57+ when (response .getEntity ()).thenReturn (new StringEntity ("" ));
58+ exceptionHandler .handleResponseCode (response );
59+ }
60+
61+ @ Test (expected = ResourceNotFoundException .class )
62+ public void testError404 () throws IOException , StreamClientException {
63+ CloseableHttpResponse response = mock (CloseableHttpResponse .class );
64+ when (response .getStatusLine ()).thenReturn (new BasicStatusLine (new ProtocolVersion ("HTTP" , 1 , 1 ), 404 , "" ));
65+ when (response .getEntity ()).thenReturn (new StringEntity ("" ));
66+ exceptionHandler .handleResponseCode (response );
67+ }
68+
69+ @ Test (expected = InternalServerException .class )
70+ public void testError500 () throws IOException , StreamClientException {
71+ CloseableHttpResponse response = mock (CloseableHttpResponse .class );
72+ when (response .getStatusLine ()).thenReturn (new BasicStatusLine (new ProtocolVersion ("HTTP" , 1 , 1 ), 500 , "" ));
73+ when (response .getEntity ()).thenReturn (new StringEntity ("" ));
74+ exceptionHandler .handleResponseCode (response );
75+ }
76+
77+ @ Test (expected = StreamClientException .class )
78+ public void testError503 () throws IOException , StreamClientException {
79+ CloseableHttpResponse response = mock (CloseableHttpResponse .class );
80+ when (response .getStatusLine ()).thenReturn (new BasicStatusLine (new ProtocolVersion ("HTTP" , 1 , 1 ), 503 , "" ));
81+ when (response .getEntity ()).thenReturn (new StringEntity ("" ));
82+ exceptionHandler .handleResponseCode (response );
83+ }
84+
85+ @ Test
86+ public void testOk () throws IOException , StreamClientException {
87+ CloseableHttpResponse response = mock (CloseableHttpResponse .class );
88+ when (response .getStatusLine ()).thenReturn (new BasicStatusLine (new ProtocolVersion ("HTTP" , 1 , 1 ), 201 , "" ));
89+ when (response .getEntity ()).thenReturn (new StringEntity ("" ));
90+ exceptionHandler .handleResponseCode (response );
91+ }
92+ }
0 commit comments