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);
}
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));
}
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);
}
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"));
}
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);
}
Aggregations