@Test public void shouldHandleJsonCallbacksMixedUp() { Callback callback1 = mock(Callback.class); Callback callback2 = mock(Callback.class); Callback[] callbacks = new Callback[]{callback1, callback2}; RestAuthCallbackHandler restAuthCallbackHandler1 = mock(RestAuthCallbackHandler.class); RestAuthCallbackHandler restAuthCallbackHandler2 = mock(RestAuthCallbackHandler.class); JsonValue jsonCallback1 = mock(JsonValue.class); JsonValue jsonCallback2 = mock(JsonValue.class); JsonValue jsonCallbacks = mock(JsonValue.class); JsonValue jsonCallback1Type = mock(JsonValue.class); JsonValue jsonCallback2Type = mock(JsonValue.class); given(restAuthCallbackHandlerFactory.getRestAuthCallbackHandler( Matchers.<Class<? extends Callback>>anyObject())) .willReturn(restAuthCallbackHandler1) .willReturn(restAuthCallbackHandler2); given(restAuthCallbackHandler1.getCallbackClassName()).willReturn("CALLBACK1"); given(restAuthCallbackHandler2.getCallbackClassName()).willReturn("CALLBACK2"); given(jsonCallback1.get("type")).willReturn(jsonCallback1Type); given(jsonCallback2.get("type")).willReturn(jsonCallback2Type); given(jsonCallback1Type.asString()).willReturn("CALLBACK1"); given(jsonCallback2Type.asString()).willReturn("CALLBACK2"); given(restAuthCallbackHandler1.convertFromJson(callback1, jsonCallback1)).willReturn(callback1); given(restAuthCallbackHandler2.convertFromJson(callback2, jsonCallback2)).willReturn(callback2); given(jsonCallbacks.size()).willReturn(2); given(jsonCallbacks.get(0)).willReturn(jsonCallback1); given(jsonCallbacks.get(1)).willReturn(jsonCallback2); Callback[] originalCallbacks = restAuthCallbackHandlerManager.handleJsonCallbacks(callbacks, jsonCallbacks); verify(restAuthCallbackHandler1).convertFromJson(callback1, jsonCallback1); verify(restAuthCallbackHandler2).convertFromJson(callback2, jsonCallback2); assertEquals(originalCallbacks.length, 2); assertEquals(originalCallbacks[0], callback1); assertEquals(originalCallbacks[1], callback2); }
public Callback[] handleJsonCallbacks(Callback[] originalCallbacks, JsonValue jsonCallbacks) { for (int j = 0; j < originalCallbacks.length; j++) { RestAuthCallbackHandler restAuthCallbackHandler = restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(originalCallbacks[j].getClass()); boolean foundParser = false; for (int i = 0; i < jsonCallbacks.size(); i++) { JsonValue jsonCallback = jsonCallbacks.get(i); if (restAuthCallbackHandler.getCallbackClassName().equals(jsonCallback.get("type").asString())) { foundParser = true; restAuthCallbackHandler.convertFromJson(originalCallbacks[j], jsonCallback); break; } } if (!foundParser) { logger.error("Required callback not found in JSON response"); throw new RestAuthException(Response.Status.BAD_REQUEST, "Required callback not found in JSON response"); } } return originalCallbacks; }
RestAuthCallbackHandlerManager { public Callback[] handleJsonCallbacks(Callback[] originalCallbacks, JsonValue jsonCallbacks) { for (int j = 0; j < originalCallbacks.length; j++) { RestAuthCallbackHandler restAuthCallbackHandler = restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(originalCallbacks[j].getClass()); boolean foundParser = false; for (int i = 0; i < jsonCallbacks.size(); i++) { JsonValue jsonCallback = jsonCallbacks.get(i); if (restAuthCallbackHandler.getCallbackClassName().equals(jsonCallback.get("type").asString())) { foundParser = true; restAuthCallbackHandler.convertFromJson(originalCallbacks[j], jsonCallback); break; } } if (!foundParser) { logger.error("Required callback not found in JSON response"); throw new RestAuthException(Response.Status.BAD_REQUEST, "Required callback not found in JSON response"); } } return originalCallbacks; } }
RestAuthCallbackHandlerManager { public Callback[] handleJsonCallbacks(Callback[] originalCallbacks, JsonValue jsonCallbacks) { for (int j = 0; j < originalCallbacks.length; j++) { RestAuthCallbackHandler restAuthCallbackHandler = restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(originalCallbacks[j].getClass()); boolean foundParser = false; for (int i = 0; i < jsonCallbacks.size(); i++) { JsonValue jsonCallback = jsonCallbacks.get(i); if (restAuthCallbackHandler.getCallbackClassName().equals(jsonCallback.get("type").asString())) { foundParser = true; restAuthCallbackHandler.convertFromJson(originalCallbacks[j], jsonCallback); break; } } if (!foundParser) { logger.error("Required callback not found in JSON response"); throw new RestAuthException(Response.Status.BAD_REQUEST, "Required callback not found in JSON response"); } } return originalCallbacks; } @Inject  RestAuthCallbackHandlerManager(RestAuthCallbackHandlerFactory restAuthCallbackHandlerFactory); }
RestAuthCallbackHandlerManager { public Callback[] handleJsonCallbacks(Callback[] originalCallbacks, JsonValue jsonCallbacks) { for (int j = 0; j < originalCallbacks.length; j++) { RestAuthCallbackHandler restAuthCallbackHandler = restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(originalCallbacks[j].getClass()); boolean foundParser = false; for (int i = 0; i < jsonCallbacks.size(); i++) { JsonValue jsonCallback = jsonCallbacks.get(i); if (restAuthCallbackHandler.getCallbackClassName().equals(jsonCallback.get("type").asString())) { foundParser = true; restAuthCallbackHandler.convertFromJson(originalCallbacks[j], jsonCallback); break; } } if (!foundParser) { logger.error("Required callback not found in JSON response"); throw new RestAuthException(Response.Status.BAD_REQUEST, "Required callback not found in JSON response"); } } return originalCallbacks; } @Inject  RestAuthCallbackHandlerManager(RestAuthCallbackHandlerFactory restAuthCallbackHandlerFactory); JsonValue handleCallbacks(HttpHeaders headers, HttpServletRequest request,
            HttpServletResponse response, Callback[] callbacks); Callback[] handleJsonCallbacks(Callback[] originalCallbacks, JsonValue jsonCallbacks); Callback[] handleResponseCallbacks(HttpHeaders headers, HttpServletRequest request,
            HttpServletResponse response, Callback[] originalCallbacks, JsonValue jsonRequestObject); }
RestAuthCallbackHandlerManager { public Callback[] handleJsonCallbacks(Callback[] originalCallbacks, JsonValue jsonCallbacks) { for (int j = 0; j < originalCallbacks.length; j++) { RestAuthCallbackHandler restAuthCallbackHandler = restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(originalCallbacks[j].getClass()); boolean foundParser = false; for (int i = 0; i < jsonCallbacks.size(); i++) { JsonValue jsonCallback = jsonCallbacks.get(i); if (restAuthCallbackHandler.getCallbackClassName().equals(jsonCallback.get("type").asString())) { foundParser = true; restAuthCallbackHandler.convertFromJson(originalCallbacks[j], jsonCallback); break; } } if (!foundParser) { logger.error("Required callback not found in JSON response"); throw new RestAuthException(Response.Status.BAD_REQUEST, "Required callback not found in JSON response"); } } return originalCallbacks; } @Inject  RestAuthCallbackHandlerManager(RestAuthCallbackHandlerFactory restAuthCallbackHandlerFactory); JsonValue handleCallbacks(HttpHeaders headers, HttpServletRequest request,
            HttpServletResponse response, Callback[] callbacks); Callback[] handleJsonCallbacks(Callback[] originalCallbacks, JsonValue jsonCallbacks); Callback[] handleResponseCallbacks(HttpHeaders headers, HttpServletRequest request,
            HttpServletResponse response, Callback[] originalCallbacks, JsonValue jsonRequestObject);  }