55
66import com .google .api .client .http .HttpHeaders ;
77import com .google .api .client .http .HttpResponseException ;
8+ import org .junit .After ;
89import org .junit .Before ;
910import org .junit .Rule ;
1011import org .junit .Test ;
1516
1617public class XeroExceptionsTest {
1718
19+ private AutoCloseable closeable ;
1820 @ InjectMocks
1921 private static XeroApiExceptionHandler xeroApiExceptionHandler ;
2022 @ Rule
@@ -32,7 +34,12 @@ public class XeroExceptionsTest {
3234
3335 @ Before
3436 public void setUp () {
35- MockitoAnnotations .initMocks (this );
37+ closeable = MockitoAnnotations .openMocks (this );
38+ }
39+
40+ @ After
41+ public void tearDown () throws Exception {
42+ closeable .close ();
3643 }
3744
3845 @ Test
@@ -102,7 +109,7 @@ public void testXeroNotFoundException() {
102109 @ Test
103110 public void testXeroRateLimitException () {
104111 int statusCode = 429 ;
105- String message = "You've exceeded the per 100 rate limit" ;
112+ String message = "You've exceeded the per [any] rate limit" ;
106113
107114 // XeroRateLimitException extends XeroException so we can catch either
108115 expectedException .expect (XeroException .class );
@@ -111,7 +118,67 @@ public void testXeroRateLimitException() {
111118
112119 when (httpResponseException .getStatusCode ()).thenReturn (statusCode );
113120 when (httpResponseException .getHeaders ()).thenReturn (httpHeaders );
114- when (httpHeaders .get ("x-rate-limit-problem" )).thenReturn (100 );
121+ when (httpHeaders .getFirstHeaderStringValue ("X-Rate-Limit-Problem" )).thenReturn ("[any]" );
122+ xeroApiExceptionHandler .execute (httpResponseException );
123+ }
124+
125+ @ Test
126+ public void testXeroMinuteRateLimitException () {
127+ int statusCode = 429 ;
128+ String message = "You've exceeded the per [minute] rate limit" ;
129+
130+ // XeroMinuteRateLimitException extends XeroRateLimitException so we can catch either
131+ expectedException .expect (XeroException .class );
132+ expectedException .expect (XeroMinuteRateLimitException .class );
133+ expectedException .expect (XeroRateLimitException .class );
134+ expectedException .expectMessage (message );
135+
136+ when (httpResponseException .getStatusCode ()).thenReturn (statusCode );
137+ when (httpResponseException .getHeaders ()).thenReturn (httpHeaders );
138+ when (httpHeaders .getFirstHeaderStringValue ("X-Rate-Limit-Problem" )).thenReturn ("[minute]" );
139+ when (httpHeaders .getFirstHeaderStringValue ("X-MinLimit-Remaining" )).thenReturn ("0" );
140+ when (httpHeaders .getFirstHeaderStringValue ("X-DayLimit-Remaining" )).thenReturn ("10" );
141+ when (httpHeaders .getFirstHeaderStringValue ("X-AppMinLimit-Remaining" )).thenReturn ("10" );
142+ xeroApiExceptionHandler .execute (httpResponseException );
143+ }
144+
145+ @ Test
146+ public void testXeroDailyRateLimitException () {
147+ int statusCode = 429 ;
148+ String message = "You've exceeded the per [day] rate limit" ;
149+
150+ // XeroDailyRateLimitException extends XeroRateLimitException so we can catch either
151+ expectedException .expect (XeroException .class );
152+ expectedException .expect (XeroDailyRateLimitException .class );
153+ expectedException .expect (XeroRateLimitException .class );
154+ expectedException .expectMessage (message );
155+
156+ when (httpResponseException .getStatusCode ()).thenReturn (statusCode );
157+ when (httpResponseException .getHeaders ()).thenReturn (httpHeaders );
158+ when (httpHeaders .getFirstHeaderStringValue ("X-Rate-Limit-Problem" )).thenReturn ("[day]" );
159+ when (httpHeaders .getFirstHeaderStringValue ("X-MinLimit-Remaining" )).thenReturn ("10" );
160+ when (httpHeaders .getFirstHeaderStringValue ("X-DayLimit-Remaining" )).thenReturn ("0" );
161+ when (httpHeaders .getFirstHeaderStringValue ("X-AppMinLimit-Remaining" )).thenReturn ("10" );
162+ xeroApiExceptionHandler .execute (httpResponseException );
163+ }
164+
165+ @ Test
166+ public void testXeroAppMinuteRateLimitException () {
167+ int statusCode = 429 ;
168+ String message = "You've exceeded the per [app] rate limit" ;
169+
170+ // XeroAppMinuteRateLimitException extends XeroRateLimitException so we can catch either
171+ expectedException .expect (XeroException .class );
172+ expectedException .expect (XeroAppMinuteRateLimitException .class );
173+ expectedException .expect (XeroRateLimitException .class );
174+ expectedException .expectMessage (message );
175+
176+ when (httpResponseException .getStatusCode ()).thenReturn (statusCode );
177+ when (httpResponseException .getHeaders ()).thenReturn (httpHeaders );
178+ when (httpHeaders .getFirstHeaderStringValue ("X-Rate-Limit-Problem" )).thenReturn ("[app]" );
179+ when (httpHeaders .getFirstHeaderStringValue ("X-MinLimit-Remaining" )).thenReturn ("10" );
180+ when (httpHeaders .getFirstHeaderStringValue ("X-DayLimit-Remaining" )).thenReturn ("10" );
181+ when (httpHeaders .getFirstHeaderStringValue ("X-AppMinLimit-Remaining" )).thenReturn ("0" );
115182 xeroApiExceptionHandler .execute (httpResponseException );
116183 }
117184
0 commit comments