OAuth1aService extends OAuthService { Callback<ResponseBody> getCallbackWrapper(final Callback<OAuthResponse> callback) { return new Callback<ResponseBody>() { @Override public void success(Result<ResponseBody> result) { BufferedReader reader = null; final StringBuilder sb = new StringBuilder(); try { try { reader = new BufferedReader( new InputStreamReader(result.data.byteStream())); String line; while ((line = reader.readLine()) != null) { sb.append(line); } } finally { if (reader != null) { reader.close(); } } final String responseAsStr = sb.toString(); final OAuthResponse authResponse = parseAuthResponse(responseAsStr); if (authResponse == null) { callback.failure(new TwitterAuthException( "Failed to parse auth response: " + responseAsStr)); } else { callback.success(new Result<>(authResponse, null)); } } catch (IOException e) { callback.failure(new TwitterAuthException(e.getMessage(), e)); } } @Override public void failure(TwitterException exception) { callback.failure(exception); } }; } OAuth1aService(TwitterCore twitterCore, TwitterApi api); void requestTempToken(final Callback<OAuthResponse> callback); String buildCallbackUrl(TwitterAuthConfig authConfig); void requestAccessToken(final Callback<OAuthResponse> callback,
            TwitterAuthToken requestToken, String verifier); String getAuthorizeUrl(TwitterAuthToken requestToken); static OAuthResponse parseAuthResponse(String response);  }
@Test public void testCallbackWrapperSuccess_iOException() throws IOException { final Callback<OAuthResponse> callback = new Callback<OAuthResponse>() { @Override public void success(Result<OAuthResponse> result) { fail(); } @Override public void failure(TwitterException exception) { assertNotNull(exception); } }; final Callback<ResponseBody> callbackWrapper = service.getCallbackWrapper(callback); final ResponseBody responseBody = ResponseBody.create(MediaType.parse("application/json"), ""); callbackWrapper.success(new Result<>(responseBody, Response.success(responseBody))); }
@Test public void testCallbackWrapperFailure() { final Callback<OAuthResponse> authResponseCallback = mock(Callback.class); final Callback<ResponseBody> callbackWrapper = service.getCallbackWrapper(authResponseCallback); final TwitterException mockException = mock(TwitterException.class); callbackWrapper.failure(mockException); verify(authResponseCallback).failure(eq(mockException)); }