Search in sources :

Example 11 with RestAuthCallbackHandler

use of org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler in project OpenAM by OpenRock.

the class RestAuthCallbackHandlerManagerTest method shouldHandleCallbacksIntoJson.

@Test
public void shouldHandleCallbacksIntoJson() throws RestAuthResponseException, RestAuthException {
    //Given
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    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 = new JsonValue(new LinkedHashMap<String, String>());
    jsonCallback1.put("KEY1", "VALUE1");
    JsonValue jsonCallback2 = new JsonValue(new LinkedHashMap<String, String>());
    jsonCallback2.put("KEY2", "VALUE2");
    given(restAuthCallbackHandlerFactory.getRestAuthCallbackHandler(Matchers.<Class<? extends Callback>>anyObject())).willReturn(restAuthCallbackHandler1).willReturn(restAuthCallbackHandler1).willReturn(restAuthCallbackHandler2);
    given(restAuthCallbackHandler1.updateCallbackFromRequest(request, response, callback1)).willReturn(false);
    given(restAuthCallbackHandler2.updateCallbackFromRequest(request, response, callback2)).willReturn(false);
    given(restAuthCallbackHandler1.convertToJson(callback1, 1)).willReturn(jsonCallback1);
    given(restAuthCallbackHandler2.convertToJson(callback2, 2)).willReturn(jsonCallback2);
    //When
    JsonValue jsonCallbacks = restAuthCallbackHandlerManager.handleCallbacks(request, response, callbacks);
    //Then
    verify(restAuthCallbackHandler1).updateCallbackFromRequest(request, response, callback1);
    verify(restAuthCallbackHandler2, never()).updateCallbackFromRequest(request, response, callback2);
    verify(restAuthCallbackHandler1).convertToJson(callback1, 1);
    verify(restAuthCallbackHandler2).convertToJson(callback2, 2);
    assertEquals(jsonCallbacks.size(), 2);
    assertEquals(jsonCallbacks.get(0).get("KEY1").asString(), "VALUE1");
    assertEquals(jsonCallbacks.get(1).get("KEY2").asString(), "VALUE2");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Callback(javax.security.auth.callback.Callback) JsonValue(org.forgerock.json.JsonValue) HttpServletResponse(javax.servlet.http.HttpServletResponse) RestAuthCallbackHandler(org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler) Test(org.testng.annotations.Test)

Aggregations

RestAuthCallbackHandler (org.forgerock.openam.core.rest.authn.callbackhandlers.RestAuthCallbackHandler)11 Callback (javax.security.auth.callback.Callback)9 JsonValue (org.forgerock.json.JsonValue)8 Test (org.testng.annotations.Test)8 BeforeClass (org.testng.annotations.BeforeClass)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 HttpHeaders (javax.ws.rs.core.HttpHeaders)2 ArrayList (java.util.ArrayList)1 RestAuthException (org.forgerock.openam.core.rest.authn.exceptions.RestAuthException)1