Search in sources :

Example 6 with ResourceSetLabel

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

the class ResourceSetLabelRegistration method updateLabels.

private void updateLabels(ResourceSetDescription resourceSet, Collection<String> addedLabels, Collection<String> removedLabels) {
    Collection<String> updatedLabels = new HashSet<>(addedLabels);
    updatedLabels.addAll(removedLabels);
    for (String label : updatedLabels) {
        try {
            String labelId = getLabelId(resourceSet.getClientId(), label);
            try {
                ResourceSetLabel resourceSetLabel = labelsStore.read(resourceSet.getRealm(), resourceSet.getResourceOwnerId(), labelId);
                if (addedLabels.contains(label)) {
                    resourceSetLabel.addResourceSetId(resourceSet.getId());
                } else if (removedLabels.contains(label)) {
                    resourceSetLabel.removeResourceSetId(resourceSet.getId());
                }
                labelsStore.update(resourceSet.getRealm(), resourceSet.getResourceOwnerId(), resourceSetLabel);
                if (removedLabels.contains(label)) {
                    if (!labelsStore.isLabelInUse(resourceSet.getRealm(), resourceSet.getResourceOwnerId(), labelId)) {
                        labelsStore.delete(resourceSet.getRealm(), resourceSet.getResourceOwnerId(), getLabelId(resourceSet.getClientId(), label));
                    }
                }
            } catch (org.forgerock.json.resource.NotFoundException e) {
                if (addedLabels.contains(label)) {
                    labelsStore.create(resourceSet.getRealm(), resourceSet.getResourceOwnerId(), new ResourceSetLabel(labelId, label, LabelType.SYSTEM, Collections.singleton(resourceSet.getId())));
                }
            }
        } catch (ResourceException e) {
            logger.error("Failed to update label, {}, on resource set: {}", getLabelId(resourceSet.getClientId(), label), resourceSet.getId(), e);
        }
    }
}
Also used : ResourceException(org.forgerock.json.resource.ResourceException) HashSet(java.util.HashSet) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel)

Example 7 with ResourceSetLabel

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

the class ResourceSetLabelRegistration method updateLabelsForExistingResourceSet.

/**
     * Adds and removes labels on the updated resource set, creating the label
     * if required and deleting labels which are no longer used.
     *
     * @param resourceSet The updated resource set.
     */
void updateLabelsForExistingResourceSet(ResourceSetDescription resourceSet) {
    JsonValue newLabels = resourceSet.getDescription().get(OAuth2Constants.ResourceSets.LABELS);
    if (newLabels.isNull()) {
        newLabels = json(array());
    }
    Collection<String> addedLabels = new HashSet<>(newLabels.asSet(String.class));
    try {
        Set<ResourceSetLabel> labels = labelsStore.forResourceSet(resourceSet.getRealm(), resourceSet.getResourceOwnerId(), resourceSet.getId(), true);
        Collection<String> removedLabels = new HashSet<>();
        for (ResourceSetLabel label : labels) {
            String labelName = label.getName().substring(label.getName().lastIndexOf("/") + 1);
            if (!addedLabels.remove(labelName)) {
                removedLabels.add(labelName);
            }
        }
        updateLabels(resourceSet, addedLabels, removedLabels);
    } catch (ResourceException e) {
        logger.error("Failed to find current labels on resource set: {}", resourceSet.getId(), e);
    }
}
Also used : JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) HashSet(java.util.HashSet) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel)

Example 8 with ResourceSetLabel

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

the class ResourceSetLabelRegistrationTest method shouldUpdateLabelUsingClientIdIfClientDisplayNameIsNull.

@Test
public void shouldUpdateLabelUsingClientIdIfClientDisplayNameIsNull() throws Exception {
    //Given
    ResourceSetDescription resourceSet = newResourceSet("LABEL_ONE");
    givenLabelsDoesNotExist("LABEL_ONE");
    //When
    labelRegistration.updateLabelsForNewResourceSet(resourceSet);
    //Then
    ArgumentCaptor<ResourceSetLabel> labelCaptor = ArgumentCaptor.forClass(ResourceSetLabel.class);
    verify(labelsStore).create(eq("REALM"), eq("RESOURCE_OWNER_ID"), labelCaptor.capture());
    assertThat(labelCaptor.getValue().getId()).isEqualTo("CLIENT_ID/LABEL_ONE");
    assertThat(labelCaptor.getValue().getName()).isEqualTo("LABEL_ONE");
}
Also used : ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel) Test(org.testng.annotations.Test)

Example 9 with ResourceSetLabel

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

the class ResourceSetLabelRegistrationTest method shouldUpdateLabelsForDeletedResourceSet.

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

Example 10 with ResourceSetLabel

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

the class ResourceSetLabelRegistrationTest method shouldUpdateLabelsForNewResourceSet.

@Test
public void shouldUpdateLabelsForNewResourceSet() throws Exception {
    //Given
    ResourceSetDescription resourceSet = newResourceSet("LABEL_ONE");
    givenLabelsDoesNotExist("LABEL_ONE");
    //When
    labelRegistration.updateLabelsForNewResourceSet(resourceSet);
    //Then
    ArgumentCaptor<ResourceSetLabel> labelCaptor = ArgumentCaptor.forClass(ResourceSetLabel.class);
    verify(labelsStore).create(eq("REALM"), eq("RESOURCE_OWNER_ID"), labelCaptor.capture());
    assertThat(labelCaptor.getValue().getId()).isEqualTo("CLIENT_ID/LABEL_ONE");
    assertThat(labelCaptor.getValue().getName()).isEqualTo("LABEL_ONE");
    assertThat(labelCaptor.getValue().getType()).isEqualTo(LabelType.SYSTEM);
    assertThat(labelCaptor.getValue().getResourceSetIds()).containsOnly("RESOURCE_SET_ID");
}
Also used : ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) 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