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