Search in sources :

Example 1 with ResourceSetLabel

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");
        }
    }
}
Also used : ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel) Test(org.testng.annotations.Test)

Example 2 with ResourceSetLabel

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();
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel) Test(org.testng.annotations.Test)

Example 3 with ResourceSetLabel

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);
}
Also used : ServerException(org.forgerock.oauth2.core.exceptions.ServerException) ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) HashSet(java.util.HashSet) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel)

Example 4 with ResourceSetLabel

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();
    }
}
Also used : JsonValue(org.forgerock.json.JsonValue) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel) Promises.newResultPromise(org.forgerock.util.promise.Promises.newResultPromise) Promise(org.forgerock.util.promise.Promise) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) BadRequestException(org.forgerock.json.resource.BadRequestException) ArrayList(java.util.ArrayList) List(java.util.List) ResourceException(org.forgerock.json.resource.ResourceException)

Example 5 with ResourceSetLabel

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());
}
Also used : LocaleContext(com.sun.identity.common.LocaleContext) BadRequestException(org.forgerock.json.resource.BadRequestException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ResourceException(org.forgerock.json.resource.ResourceException) ResourceSetLabel(org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel)

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