Search in sources :

Example 11 with ResourceSetLabel

use of org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel in project OpenAM by OpenRock.

the class ResourceSetLabelRegistrationTest method shouldUpdateLabelsForExistingResourceSetWithAllLabelsRemoved.

@Test
public void shouldUpdateLabelsForExistingResourceSetWithAllLabelsRemoved() throws Exception {
    //Given
    givenLabelsForResourceSet("LABEL_ONE", "LABEL_TWO");
    ResourceSetDescription resourceSet = newResourceSet();
    givenLabelsExist("LABEL_ONE", "LABEL_TWO");
    //When
    labelRegistration.updateLabelsForExistingResourceSet(resourceSet);
    //Then
    ArgumentCaptor<ResourceSetLabel> labelCaptor = ArgumentCaptor.forClass(ResourceSetLabel.class);
    verify(labelsStore, times(2)).update(eq("REALM"), eq("RESOURCE_OWNER_ID"), labelCaptor.capture());
    List<ResourceSetLabel> labels = labelCaptor.getAllValues();
    for (ResourceSetLabel label : labels) {
        assertThat(label.getResourceSetIds()).isEmpty();
    }
}
Also used : ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel) Test(org.testng.annotations.Test)

Example 12 with ResourceSetLabel

use of org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel in project OpenAM by OpenRock.

the class ResourceSetResource method getResourceSetJson.

private JsonValue getResourceSetJson(ResourceSetDescription resourceSet, String userId) throws ResourceException {
    HashMap<String, Object> content = new HashMap<String, Object>(resourceSet.asMap());
    content.put(ID, resourceSet.getId());
    content.put("resourceServer", resourceSet.getClientId());
    content.put("resourceOwnerId", resourceSet.getResourceOwnerId());
    Set<ResourceSetLabel> labels = umaLabelsStore.forResourceSet(resourceSet.getRealm(), resourceSet.getResourceOwnerId(), resourceSet.getId(), false);
    Set<String> labelIds = new HashSet<>();
    boolean filterOutSystemLabels = userId.equals(resourceSet.getResourceOwnerId());
    for (ResourceSetLabel label : labels) {
        if (filterOutSystemLabels && label.getType().equals(LabelType.SYSTEM)) {
            continue;
        } else {
            labelIds.add(label.getId());
        }
    }
    content.put("labels", labelIds);
    if (resourceSet.getPolicy() != null) {
        JsonValue policy = new JsonValue(new HashMap<String, Object>(resourceSet.getPolicy().asMap()));
        policy.remove("policyId");
        policy.remove("name");
        content.put("policy", policy.getObject());
    }
    return new JsonValue(content);
}
Also used : HashMap(java.util.HashMap) JsonValue(org.forgerock.json.JsonValue) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel) HashSet(java.util.HashSet)

Example 13 with ResourceSetLabel

use of org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel in project OpenAM by OpenRock.

the class UmaLabelResource method createInstance.

@Override
public Promise<ResourceResponse, ResourceException> createInstance(Context serverContext, CreateRequest createRequest) {
    final JsonValue umaLabel = createRequest.getContent();
    try {
        validate(umaLabel);
    } catch (BadRequestException e) {
        return e.asPromise();
    }
    final String realm = getRealm(serverContext);
    final String userName = getUserName(serverContext);
    final String labelName = umaLabel.get(NAME_LABEL).asString();
    final String labelType = umaLabel.get(TYPE_LABEL).asString();
    final ResourceSetLabel label;
    try {
        label = labelStore.create(realm, userName, new ResourceSetLabel(null, labelName, LabelType.valueOf(labelType), Collections.EMPTY_SET));
        return newResultPromise(newResourceResponse(label.getId(), String.valueOf(label.hashCode()), label.asJson()));
    } catch (ResourceException e) {
        return e.asPromise();
    }
}
Also used : JsonValue(org.forgerock.json.JsonValue) BadRequestException(org.forgerock.json.resource.BadRequestException) ResourceException(org.forgerock.json.resource.ResourceException) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel)

Example 14 with ResourceSetLabel

use of org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel in project OpenAM by OpenRock.

the class UmaLabelResource method deleteInstance.

@Override
public Promise<ResourceResponse, ResourceException> deleteInstance(Context serverContext, String labelId, DeleteRequest deleteRequest) {
    try {
        ResourceSetLabel resourceSetLabel = labelStore.read(getRealm(serverContext), getUserName(serverContext), labelId);
        if (!isSameRevision(deleteRequest, resourceSetLabel)) {
            throw new BadRequestException("Revision number doesn't match latest revision.");
        }
        labelStore.delete(getRealm(serverContext), getUserName(serverContext), labelId);
        return newResultPromise(newResourceResponse(labelId, null, resourceSetLabel.asJson()));
    } catch (ResourceException e) {
        return new BadRequestException("Error deleting label.").asPromise();
    }
}
Also used : BadRequestException(org.forgerock.json.resource.BadRequestException) ResourceException(org.forgerock.json.resource.ResourceException) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel)

Example 15 with ResourceSetLabel

use of org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel in project OpenAM by OpenRock.

the class UmaLabelResourceTest method deleteLabel.

/**
     * Should successfully delete a label.
     */
@Test
public void deleteLabel() throws ResourceException {
    //Given
    final ResourceSetLabel resourceSetLabel = new ResourceSetLabel(LABEL_ID, LABEL_NAME, LabelType.valueOf(LABEL_TYPE), Collections.EMPTY_SET);
    given(contextHelper.getRealm(serverContext)).willReturn(REALM_NAME);
    given(contextHelper.getUserId(serverContext)).willReturn(RESOURCE_OWNER_ID);
    given(umaLabelsStore.read(REALM_NAME, RESOURCE_OWNER_ID, LABEL_ID)).willReturn(resourceSetLabel);
    given(deleteRequest.getRevision()).willReturn(String.valueOf(resourceSetLabel.hashCode()));
    //When
    Promise<ResourceResponse, ResourceException> promise = umaLabelResource.deleteInstance(serverContext, LABEL_ID, deleteRequest);
    //Then
    verify(umaLabelsStore, Mockito.times(1)).delete(REALM_NAME, RESOURCE_OWNER_ID, LABEL_ID);
    assertThat(promise).succeeded();
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel) Test(org.testng.annotations.Test)

Aggregations

ResourceSetLabel (org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel)15 ResourceException (org.forgerock.json.resource.ResourceException)8 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)7 Test (org.testng.annotations.Test)7 JsonValue (org.forgerock.json.JsonValue)5 HashSet (java.util.HashSet)4 BadRequestException (org.forgerock.json.resource.BadRequestException)4 ResourceResponse (org.forgerock.json.resource.ResourceResponse)3 LocaleContext (com.sun.identity.common.LocaleContext)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)1 Responses.newResourceResponse (org.forgerock.json.resource.Responses.newResourceResponse)1 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)1 ResourceSetStore (org.forgerock.oauth2.resources.ResourceSetStore)1 Promise (org.forgerock.util.promise.Promise)1 Promises.newResultPromise (org.forgerock.util.promise.Promises.newResultPromise)1