Search in sources :

Example 1 with Response

use of org.restlet.Response in project OpenAM by OpenRock.

the class UmaExceptionHandlerTest method shouldSetUmaExceptionResponse.

@Test
@SuppressWarnings("unchecked")
public void shouldSetUmaExceptionResponse() throws IOException {
    //Given
    Response response = mock(Response.class);
    Throwable throwable = mock(Throwable.class);
    Exception exception = new UmaException(444, "ERROR", "DESCRIPTION");
    given(throwable.getCause()).willReturn(exception);
    Status status = new Status(444, exception);
    given(response.getStatus()).willReturn(status);
    //When
    exceptionFilter.handleException(response, throwable);
    //Then
    ArgumentCaptor<JacksonRepresentation> exceptionResponseCaptor = ArgumentCaptor.forClass(JacksonRepresentation.class);
    verify(response).setEntity(exceptionResponseCaptor.capture());
    Map<String, String> responseBody = (Map<String, String>) exceptionResponseCaptor.getValue().getObject();
    assertThat(responseBody).containsOnly(entry("error", "ERROR"), entry("error_description", "DESCRIPTION"));
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(response).setStatus(statusCaptor.capture());
    assertThat(statusCaptor.getValue().getCode()).isEqualTo(444);
    assertThat(statusCaptor.getValue().getThrowable()).isEqualTo(exception);
}
Also used : Response(org.restlet.Response) Status(org.restlet.data.Status) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Map(java.util.Map) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 2 with Response

use of org.restlet.Response in project OpenAM by OpenRock.

the class AbstractRestletAccessAuditFilterTest method shouldCallHandleOnRestlet.

@Test
public void shouldCallHandleOnRestlet() {
    // Given
    Request request = mock(Request.class);
    Response response = new Response(request);
    Representation representation = mock(Representation.class);
    when(request.getEntity()).thenReturn(representation);
    when(request.getAttributes()).thenReturn(new ConcurrentHashMap<String, Object>());
    when(representation.isTransient()).thenReturn(false);
    when(eventPublisher.isAuditing(anyString(), anyString(), any(EventName.class))).thenReturn(false);
    // When
    auditFilter.handle(request, response);
    // Then
    verify(restlet, times(1)).handle(any(Request.class), any(Response.class));
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Representation(org.restlet.representation.Representation) Test(org.testng.annotations.Test)

Example 3 with Response

use of org.restlet.Response in project OpenAM by OpenRock.

the class AccessTokenProtectionFilterTest method testBeforeHandleWithoutNeedingScope.

@Test
public void testBeforeHandleWithoutNeedingScope() throws Exception {
    //Given
    filter = new AccessTokenProtectionFilter(null, tokenStore, requestFactory, null);
    Request req = mock(Request.class);
    Response resp = mock(Response.class);
    OAuth2Request oAuth2Request = mock(OAuth2Request.class);
    when(requestFactory.create(req)).thenReturn(oAuth2Request);
    ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_BASIC);
    challengeResponse.setRawValue("tokenId");
    when(req.getChallengeResponse()).thenReturn(challengeResponse);
    AccessToken accessToken = new AccessToken(json(object(field("id", "tokenId"), field("tokenName", "access_token"), field("scope", asSet("a")), field("expireTime", System.currentTimeMillis() + 5000))));
    when(tokenStore.readAccessToken(oAuth2Request, "tokenId")).thenReturn(accessToken);
    //When
    int result = filter.beforeHandle(req, resp);
    //Then
    assertThat(result).isEqualTo(Filter.CONTINUE);
}
Also used : ChallengeResponse(org.restlet.data.ChallengeResponse) Response(org.restlet.Response) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) AccessToken(org.forgerock.oauth2.core.AccessToken) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) ChallengeResponse(org.restlet.data.ChallengeResponse) Test(org.testng.annotations.Test)

Example 4 with Response

use of org.restlet.Response in project OpenAM by OpenRock.

the class ResourceSetRegistrationExceptionFilterTest method shouldSet405ExceptionResponse.

@Test
@SuppressWarnings("unchecked")
public void shouldSet405ExceptionResponse() throws Exception {
    //Given
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    Status status = new Status(405);
    given(response.getStatus()).willReturn(status);
    //When
    exceptionFilter.afterHandle(request, response);
    //Then
    ArgumentCaptor<JacksonRepresentation> exceptionResponseCaptor = ArgumentCaptor.forClass(JacksonRepresentation.class);
    verify(response).setEntity(exceptionResponseCaptor.capture());
    Map<String, String> responseBody = (Map<String, String>) exceptionResponseCaptor.getValue().getObject();
    assertThat(responseBody).containsOnly(entry("error", "unsupported_method_type"));
}
Also used : Response(org.restlet.Response) Status(org.restlet.data.Status) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Request(org.restlet.Request) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 5 with Response

use of org.restlet.Response in project OpenAM by OpenRock.

the class ResourceSetRegistrationExceptionFilterTest method shouldSetBadRequestExceptionResponse.

@Test
@SuppressWarnings("unchecked")
public void shouldSetBadRequestExceptionResponse() throws Exception {
    //Given
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    Exception exception = new BadRequestException("MESSAGE");
    Status status = new Status(444, exception);
    given(response.getStatus()).willReturn(status);
    //When
    exceptionFilter.afterHandle(request, response);
    //Then
    ArgumentCaptor<JacksonRepresentation> exceptionResponseCaptor = ArgumentCaptor.forClass(JacksonRepresentation.class);
    verify(response).setEntity(exceptionResponseCaptor.capture());
    Map<String, String> responseBody = (Map<String, String>) exceptionResponseCaptor.getValue().getObject();
    assertThat(responseBody).containsOnly(entry("error", "bad_request"), entry("error_description", "MESSAGE"));
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(response).setStatus(statusCaptor.capture());
    assertThat(statusCaptor.getValue().getCode()).isEqualTo(400);
    assertThat(statusCaptor.getValue().getThrowable()).isEqualTo(exception);
}
Also used : Response(org.restlet.Response) Status(org.restlet.data.Status) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Request(org.restlet.Request) BadRequestException(org.forgerock.oauth2.core.exceptions.BadRequestException) Map(java.util.Map) BadRequestException(org.forgerock.oauth2.core.exceptions.BadRequestException) Test(org.testng.annotations.Test)

Aggregations

Response (org.restlet.Response)82 Request (org.restlet.Request)64 Test (org.testng.annotations.Test)28 Reference (org.restlet.data.Reference)26 Representation (org.restlet.representation.Representation)24 Status (org.restlet.data.Status)17 StringWriter (java.io.StringWriter)16 ChallengeResponse (org.restlet.data.ChallengeResponse)15 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)12 ResponseHandler (org.qi4j.library.rest.client.spi.ResponseHandler)12 HashMap (java.util.HashMap)11 ZNRecord (org.apache.helix.ZNRecord)11 StringReader (java.io.StringReader)10 Map (java.util.Map)10 TypeReference (org.codehaus.jackson.type.TypeReference)10 Test (org.junit.Test)10 Client (org.restlet.Client)10 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)9 ContextResourceClient (org.qi4j.library.rest.client.api.ContextResourceClient)7 HandlerCommand (org.qi4j.library.rest.client.api.HandlerCommand)7