Search in sources :

Example 1 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpointTest method createCreateRequestRepresentation.

private JsonRepresentation createCreateRequestRepresentation() throws JSONException, JsonProcessingException, BadRequestException {
    JsonRepresentation entity = mock(JsonRepresentation.class);
    JSONObject jsonObject = mock(JSONObject.class);
    String jsonString = new ObjectMapper().writeValueAsString(RESOURCE_SET_DESCRIPTION_CONTENT.asMap());
    given(entity.getJsonObject()).willReturn(jsonObject);
    given(jsonObject.toString()).willReturn(jsonString);
    given(validator.validate(anyMapOf(String.class, Object.class))).willReturn(RESOURCE_SET_DESCRIPTION_CONTENT.asMap());
    return entity;
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpointTest method shouldNotCreateExistingResourceSetDescription.

@Test
@SuppressWarnings("unchecked")
public void shouldNotCreateExistingResourceSetDescription() throws Exception {
    //Given
    JsonRepresentation entity = createCreateRequestRepresentation();
    when(store.query(any(QueryFilter.class))).thenReturn(asSet(new ResourceSetDescription("id", "CLIENT_ID", "RESOURCE_OWNER_ID", RESOURCE_SET_DESCRIPTION_CONTENT.asMap())));
    noConditions();
    //When
    Representation result = endpoint.createResourceSet(entity);
    //Then
    ArgumentCaptor<QueryFilter> queryCaptor = ArgumentCaptor.forClass(QueryFilter.class);
    verify(store).query(queryCaptor.capture());
    verifyZeroInteractions(resourceRegistrationFilter);
    String queryString = queryCaptor.getValue().toString();
    assertThat(queryString).contains("name eq \"NAME\"").contains("clientId eq \"CLIENT_ID\"").contains("resourceOwnerId eq \"RESOURCE_OWNER_ID\"").doesNotContain(" or ");
    verify(response).setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
    assertThat(result).isInstanceOf(JsonRepresentation.class);
    assertThat(((JsonRepresentation) result).getJsonObject().get("error")).isEqualTo("Bad Request");
    assertThat(((JsonRepresentation) result).getJsonObject().getString("error_description")).contains("'NAME' already exists");
}
Also used : QueryFilter(org.forgerock.util.query.QueryFilter) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Representation(org.restlet.representation.Representation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Test(org.testng.annotations.Test)

Example 3 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class PermissionRequestEndpointTest method shouldThrowInvalidResourceSetIdExceptionWhenResourceSetStoreThrowsServerException.

@Test(expectedExceptions = UmaException.class)
public void shouldThrowInvalidResourceSetIdExceptionWhenResourceSetStoreThrowsServerException() throws Exception {
    //Given
    JsonRepresentation entity = mock(JsonRepresentation.class);
    JSONObject requestBody = mock(JSONObject.class);
    given(entity.getJsonObject()).willReturn(requestBody);
    given(requestBody.toString()).willReturn("{\"resource_set_id\":\"RESOURCE_SET_ID\", " + "\"scopes\":[\"SCOPE_A\", \"SCOPE_C\"]}");
    doThrow(ServerException.class).when(resourceSetStore).read("RESOURCE_SET_ID", "RESOURCE_OWNER_ID");
    //When
    try {
        endpoint.registerPermissionRequest(entity);
    } catch (UmaException e) {
        //Then
        assertThat(e.getStatusCode()).isEqualTo(400);
        assertThat(e.getError()).isEqualTo("invalid_resource_set_id");
        throw e;
    }
}
Also used : JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Test(org.testng.annotations.Test)

Example 4 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class PermissionRequestEndpointTest method shouldThrowInvalidResourceSetIdExceptionWhenResourceSetNotFound.

@Test(expectedExceptions = UmaException.class)
public void shouldThrowInvalidResourceSetIdExceptionWhenResourceSetNotFound() throws Exception {
    //Given
    JsonRepresentation entity = mock(JsonRepresentation.class);
    JSONObject requestBody = mock(JSONObject.class);
    given(entity.getJsonObject()).willReturn(requestBody);
    given(requestBody.toString()).willReturn("{\"resource_set_id\":\"RESOURCE_SET_ID\", " + "\"scopes\":[\"SCOPE_A\", \"SCOPE_C\"]}");
    doThrow(NotFoundException.class).when(resourceSetStore).read("RESOURCE_SET_ID", "RESOURCE_OWNER_ID");
    //When
    try {
        endpoint.registerPermissionRequest(entity);
    } catch (UmaException e) {
        //Then
        assertThat(e.getStatusCode()).isEqualTo(400);
        assertThat(e.getError()).isEqualTo("invalid_resource_set_id");
        throw e;
    }
}
Also used : JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Test(org.testng.annotations.Test)

Example 5 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project OpenAM by OpenRock.

the class PermissionRequestEndpointTest method shouldThrowInvalidScopeExceptionWhenRequestedScopeNotInResourceScope.

@Test(expectedExceptions = UmaException.class)
public void shouldThrowInvalidScopeExceptionWhenRequestedScopeNotInResourceScope() throws Exception {
    //Given
    JsonRepresentation entity = mock(JsonRepresentation.class);
    JSONObject requestBody = mock(JSONObject.class);
    given(entity.getJsonObject()).willReturn(requestBody);
    given(requestBody.toString()).willReturn("{\"resource_set_id\":\"RESOURCE_SET_ID\", " + "\"scopes\":[\"SCOPE_A\", \"SCOPE_C\"]}");
    setupResourceSetStore();
    //When
    try {
        endpoint.registerPermissionRequest(entity);
    } catch (UmaException e) {
        //Then
        assertThat(e.getStatusCode()).isEqualTo(400);
        assertThat(e.getError()).isEqualTo("invalid_scope");
        assertThat(e.getMessage()).contains("Requested scopes are not in allowed scopes");
        throw e;
    }
}
Also used : JSONObject(org.json.JSONObject) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Test(org.testng.annotations.Test)

Aggregations

JsonRepresentation (org.restlet.ext.json.JsonRepresentation)22 JSONObject (org.json.JSONObject)16 Test (org.testng.annotations.Test)13 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)7 Representation (org.restlet.representation.Representation)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Map (java.util.Map)5 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)4 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 Date (java.util.Date)2 AuditEvent (org.forgerock.audit.events.AuditEvent)2 JsonValue (org.forgerock.json.JsonValue)2 OAuth2Exception (org.forgerock.oauth2.core.exceptions.OAuth2Exception)2 OAuth2RestletException (org.forgerock.oauth2.restlet.OAuth2RestletException)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 TextMatch (org.opensextant.extraction.TextMatch)2