use of org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel in project OpenAM by OpenRock.
the class ResourceSetLabelRegistrationTest method shouldUpdateLabelsForExistingResourceSet.
@Test
public void shouldUpdateLabelsForExistingResourceSet() throws Exception {
//Given
givenLabelsForResourceSet("LABEL_ONE", "LABEL_TWO");
ResourceSetDescription resourceSet = newResourceSet("LABEL_ONE", "LABEL_THREE", "LABEL_FOUR");
givenLabelsExist("LABEL_ONE", "LABEL_TWO", "LABEL_THREE");
givenLabelsDoesNotExist("LABEL_FOUR");
//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());
verify(labelsStore).create(eq("REALM"), eq("RESOURCE_OWNER_ID"), labelCaptor.capture());
List<ResourceSetLabel> labels = labelCaptor.getAllValues();
for (ResourceSetLabel label : labels) {
if (label.getId().contains("LABEL_TWO")) {
assertThat(label.getResourceSetIds()).isEmpty();
} else if (label.getId().contains("LABEL_THREE")) {
assertThat(label.getResourceSetIds()).containsOnly("RESOURCE_SET_ID");
} else if (label.getId().contains("LABEL_FOUR")) {
assertThat(label.getResourceSetIds()).containsOnly("RESOURCE_SET_ID");
}
}
}
use of org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel in project OpenAM by OpenRock.
the class UmaLabelResourceTest method createInstance.
/**
* Should successfully create an UMA Label.
*/
@Test
public void createInstance() throws ResourceException {
//Given
JsonValue umaLabel = json(object(field(NAME_ATTRIBUTE_NAME, LABEL_NAME), field(TYPE_ATTRIBUTE_NAME, LABEL_TYPE)));
given(createRequest.getContent()).willReturn(umaLabel);
given(contextHelper.getRealm(serverContext)).willReturn(REALM_NAME);
given(contextHelper.getUserId(serverContext)).willReturn(RESOURCE_OWNER_ID);
final ResourceSetLabel resourceSetLabel = new ResourceSetLabel(null, LABEL_NAME, LabelType.valueOf(LABEL_TYPE), Collections.EMPTY_SET);
given(umaLabelsStore.create(REALM_NAME, RESOURCE_OWNER_ID, resourceSetLabel)).willReturn(resourceSetLabel);
//When
Promise<ResourceResponse, ResourceException> promise = umaLabelResource.createInstance(serverContext, createRequest);
//Then
verify(umaLabelsStore, Mockito.times(1)).create(REALM_NAME, RESOURCE_OWNER_ID, resourceSetLabel);
assertThat(promise).succeeded();
}
use of org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel in project OpenAM by OpenRock.
the class ResourceSetRegistrationEndpoint method readResourceSet.
private Representation readResourceSet(String resourceSetId) throws NotFoundException, ServerException {
ResourceSetStore store = providerSettingsFactory.get(requestFactory.create(getRequest())).getResourceSetStore();
ResourceSetDescription resourceSetDescription = store.read(resourceSetId, getResourceOwnerId());
Set<String> labels = new HashSet<String>();
try {
Set<ResourceSetLabel> labelSet = umaLabelsStore.forResourceSet(resourceSetDescription.getRealm(), resourceSetDescription.getResourceOwnerId(), resourceSetDescription.getId(), false);
for (ResourceSetLabel label : labelSet) {
labels.add(label.getName());
}
} catch (org.forgerock.json.resource.ResourceException e) {
throw new ServerException(e);
}
resourceSetDescription.getDescription().put("labels", labels);
return createJsonResponse(resourceSetDescription, true, true);
}
use of org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel in project OpenAM by OpenRock.
the class ResourceSetResource method updateInstance.
/**
* Update the none system labels on a resource set only
*
* @param context {@inheritDoc}
* @param request {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> updateInstance(Context context, String resourceId, UpdateRequest request) {
final Map<String, Object> resourceSetDescriptionAttributes;
try {
resourceSetDescriptionAttributes = validator.validate(request.getContent().asMap());
final String realm = getRealm(context);
final String userId = getUserId(context);
//remove this resource set id from all labels
Set<ResourceSetLabel> labels = umaLabelsStore.forResourceSet(realm, userId, resourceId, true);
for (ResourceSetLabel label : labels) {
if (!isSystemLabel(label)) {
label.removeResourceSetId(resourceId);
umaLabelsStore.update(realm, userId, label);
}
}
//add resource set id to new labels
for (String labelId : (List<String>) resourceSetDescriptionAttributes.get("labels")) {
ResourceSetLabel label = umaLabelsStore.read(realm, userId, labelId);
label.addResourceSetId(resourceId);
umaLabelsStore.update(realm, userId, label);
}
return resourceSetService.getResourceSet(context, realm, resourceId, userId, augmentWithPolicies(request)).thenAsync(new AsyncFunction<ResourceSetDescription, ResourceResponse, ResourceException>() {
@Override
public Promise<ResourceResponse, ResourceException> apply(ResourceSetDescription result) {
try {
JsonValue content = null;
content = getResourceSetJson(result, userId);
return newResultPromise(newResource(result.getId(), content));
} catch (ResourceException e) {
return e.asPromise();
}
}
});
} catch (ResourceException e) {
return e.asPromise();
} catch (org.forgerock.oauth2.core.exceptions.BadRequestException e) {
return new BadRequestException("Error retrieving labels.", e).asPromise();
}
}
use of org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel in project OpenAM by OpenRock.
the class UmaLabelResource method queryCollection.
@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context serverContext, QueryRequest queryRequest, QueryResourceHandler queryResultHandler) {
if (!queryRequest.getQueryFilter().toString().equals("true")) {
return new BadRequestException("Invalid query").asPromise();
}
Set<ResourceSetLabel> labels;
try {
labels = labelStore.list(getRealm(serverContext), getUserName(serverContext));
} catch (ResourceException e) {
return new BadRequestException("Error retrieving labels.").asPromise();
}
LocaleContext localeContext = localeContextProvider.get();
localeContext.setLocale(serverContext);
for (ResourceSetLabel label : labels) {
try {
label = resolveLabelName(contextHelper.getRealm(serverContext), label, localeContext, serverContext);
} catch (InternalServerErrorException e) {
debug.error("Could not resolve Resource Server label name. id: {}, name: {}", label.getId(), label.getName(), e);
}
queryResultHandler.handleResource(newResourceResponse(label.getId(), String.valueOf(label.asJson().getObject().hashCode()), label.asJson()));
}
return newResultPromise(newQueryResponse());
}
Aggregations