Search in sources :

Example 46 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class ResourceSetServiceTest method getResourceSetsShouldReturnEmptySetWhenNoResourceSetsExist.

@Test
public void getResourceSetsShouldReturnEmptySetWhenNoResourceSetsExist() throws Exception {
    //Given
    String realm = "REALM";
    Context context = mockContext(realm);
    ResourceSetWithPolicyQuery query = new ResourceSetWithPolicyQuery();
    String resourceOwnerId = "RESOURCE_OWNER_ID";
    boolean augmentWithPolicies = false;
    QueryFilter<String> resourceSetQuery = mock(QueryFilter.class);
    QueryFilter<JsonPointer> policyQuery = QueryFilter.alwaysFalse();
    Set<ResourceSetDescription> queriedResourceSets = new HashSet<>();
    Collection<UmaPolicy> queriedPolicies = new HashSet<>();
    Pair<QueryResponse, Collection<UmaPolicy>> queriedPoliciesPair = Pair.of(newQueryResponse(), queriedPolicies);
    Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> queriedPoliciesPromise = Promises.newResultPromise(queriedPoliciesPair);
    query.setResourceSetQuery(resourceSetQuery);
    query.setPolicyQuery(policyQuery);
    given(resourceSetStore.query(any(QueryFilter.class))).willReturn(queriedResourceSets);
    given(policyService.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(queriedPoliciesPromise);
    mockResourceOwnerIdentity(resourceOwnerId, realm);
    mockPolicyEvaluator("RS_CLIENT_ID");
    mockFilteredResourceSetsQueryVisitor(resourceSetQuery, queriedResourceSets);
    //When
    Collection<ResourceSetDescription> resourceSets = service.getResourceSets(context, realm, query, resourceOwnerId, augmentWithPolicies).getOrThrowUninterruptibly();
    //Then
    assertThat(resourceSets).isEmpty();
}
Also used : RootContext(org.forgerock.services.context.RootContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) JsonPointer(org.forgerock.json.JsonPointer) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) QueryFilter(org.forgerock.util.query.QueryFilter) Responses.newQueryResponse(org.forgerock.json.resource.Responses.newQueryResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) Collection(java.util.Collection) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) HashSet(java.util.HashSet) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 47 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class ResourceSetServiceTest method shouldGetResourceWithPolicy.

@Test
public void shouldGetResourceWithPolicy() throws Exception {
    //Given
    Context context = mock(Context.class);
    String realm = "REALM";
    String resourceSetId = "RESOURCE_SET_ID";
    String resourceOwnerId = "RESOURCE_OWNER_ID";
    boolean augmentWithPolicy = true;
    ResourceSetDescription resourceSetDescription = mock(ResourceSetDescription.class);
    UmaPolicy policy = mock(UmaPolicy.class);
    Promise<UmaPolicy, ResourceException> policyPromise = Promises.newResultPromise(policy);
    JsonValue policyJson = mock(JsonValue.class);
    given(resourceSetStore.read(eq(resourceSetId), any(ResourceSetSharedFilter.class))).willReturn(resourceSetDescription);
    given(policyService.readPolicy(context, resourceSetId)).willReturn(policyPromise);
    given(policy.asJson()).willReturn(policyJson);
    //When
    ResourceSetDescription resourceSet = service.getResourceSet(context, realm, resourceSetId, resourceOwnerId, augmentWithPolicy).getOrThrowUninterruptibly();
    //Then
    assertThat(resourceSet).isEqualTo(resourceSetDescription);
    verify(policyService).readPolicy(context, resourceSetId);
    verify(resourceSet).setPolicy(policyJson);
}
Also used : RootContext(org.forgerock.services.context.RootContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) ResourceSetSharedFilter(org.forgerock.openam.uma.ResourceSetSharedFilter) Test(org.testng.annotations.Test)

Example 48 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class ResourceSetServiceTest method getResourceSetsShouldReturnSetWhenResourceSetsExistWithNoPolicyQueryWithPolicies.

@Test
public void getResourceSetsShouldReturnSetWhenResourceSetsExistWithNoPolicyQueryWithPolicies() throws Exception {
    //Given
    Context context = createContext();
    String realm = "REALM";
    ResourceSetWithPolicyQuery query = new ResourceSetWithPolicyQuery();
    String resourceOwnerId = "RESOURCE_OWNER_ID";
    boolean augmentWithPolicies = true;
    QueryFilter<String> resourceSetQuery = mock(QueryFilter.class);
    Set<ResourceSetDescription> queriedResourceSets = new HashSet<>();
    ResourceSetDescription resourceSetOne = new ResourceSetDescription("RS_ID_ONE", "CLIENT_ID_ONE", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
    ResourceSetDescription resourceSetTwo = new ResourceSetDescription("RS_ID_TWO", "CLIENT_ID_TWO", "RESOURCE_OWNER_ID", Collections.<String, Object>emptyMap());
    UmaPolicy policyOne = mock(UmaPolicy.class);
    UmaPolicy policyTwo = mock(UmaPolicy.class);
    JsonValue policyOneJson = mock(JsonValue.class);
    JsonValue policyTwoJson = mock(JsonValue.class);
    Promise<UmaPolicy, ResourceException> policyOnePromise = Promises.newResultPromise(policyOne);
    Promise<UmaPolicy, ResourceException> policyTwoPromise = Promises.newResultPromise(policyTwo);
    query.setResourceSetQuery(resourceSetQuery);
    queriedResourceSets.add(resourceSetOne);
    queriedResourceSets.add(resourceSetTwo);
    given(resourceSetStore.query(resourceSetQuery)).willReturn(queriedResourceSets);
    given(policyOne.asJson()).willReturn(policyOneJson);
    given(policyTwo.asJson()).willReturn(policyTwoJson);
    given(policyOne.getId()).willReturn("RS_ID_ONE");
    given(policyTwo.getId()).willReturn("RS_ID_TWO");
    given(resourceSetStore.query(QueryFilter.and(resourceSetQuery, equalTo(ResourceSetTokenField.RESOURCE_OWNER_ID, "RESOURCE_OWNER_ID")))).willReturn(queriedResourceSets);
    given(policyService.readPolicy(context, "RS_ID_ONE")).willReturn(policyOnePromise);
    given(policyService.readPolicy(context, "RS_ID_TWO")).willReturn(policyTwoPromise);
    given(policyService.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(Promises.<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException>newResultPromise(Pair.<QueryResponse, Collection<UmaPolicy>>of(newQueryResponse(), new HashSet<UmaPolicy>())));
    mockResourceOwnerIdentity(resourceOwnerId, realm);
    mockFilteredResourceSetsQueryVisitor(resourceSetQuery, queriedResourceSets);
    //When
    Collection<ResourceSetDescription> resourceSets = service.getResourceSets(context, realm, query, resourceOwnerId, augmentWithPolicies).getOrThrowUninterruptibly();
    //Then
    assertThat(resourceSets).hasSize(2).contains(resourceSetOne, resourceSetTwo);
    assertThat(resourceSetOne.getPolicy()).isEqualTo(policyOneJson);
    assertThat(resourceSetTwo.getPolicy()).isEqualTo(policyTwoJson);
}
Also used : RootContext(org.forgerock.services.context.RootContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) JsonValue(org.forgerock.json.JsonValue) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Responses.newQueryResponse(org.forgerock.json.resource.Responses.newQueryResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) Collection(java.util.Collection) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 49 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class UmaPolicyServiceImplDelegationTest method registerResourceSet.

private String registerResourceSet(String resourceOwner) throws Exception {
    setupIdentityForUser(resourceOwner, loggedInRealm);
    String resourceSetId = UUID.randomUUID().toString();
    ResourceSetDescription resourceSet = new ResourceSetDescription(resourceSetId, "CLIENT_ID", resourceOwner, json(object(field("name", "RESOURCE_SET_NAME"), field("scopes", array("SCOPE_A", "SCOPE_B")))).asMap());
    given(resourceSetStore.read(resourceSetId, resourceOwner)).willReturn(resourceSet);
    given(resourceSetStore.query(QueryFilter.and(QueryFilter.equalTo(ResourceSetTokenField.RESOURCE_SET_ID, resourceSetId)))).willReturn(Collections.singleton(resourceSet));
    return resourceSetId;
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription)

Example 50 with ResourceSetDescription

use of org.forgerock.oauth2.resources.ResourceSetDescription in project OpenAM by OpenRock.

the class UmaPolicyServiceImplTest method setup.

@BeforeMethod
public void setup() throws Exception {
    policyResourceDelegate = mock(PolicyResourceDelegate.class);
    final ResourceSetStoreFactory resourceSetStoreFactory = mock(ResourceSetStoreFactory.class);
    Config<UmaAuditLogger> lazyAuditLogger = mock(Config.class);
    auditLogger = mock(UmaAuditLogger.class);
    contextHelper = mock(ContextHelper.class);
    UmaPolicyEvaluatorFactory policyEvaluatorFactory = mock(UmaPolicyEvaluatorFactory.class);
    policyEvaluator = mock(Evaluator.class);
    given(policyEvaluatorFactory.getEvaluator(any(Subject.class), anyString())).willReturn(policyEvaluator);
    coreServicesWrapper = mock(CoreServicesWrapper.class);
    Debug debug = mock(Debug.class);
    UmaSettingsFactory umaSettingsFactory = mock(UmaSettingsFactory.class);
    UmaSettings umaSettings = mock(UmaSettings.class);
    given(umaSettingsFactory.create(anyString())).willReturn(umaSettings);
    ExtensionFilterManager extensionFilterManager = mock(ExtensionFilterManager.class);
    resourceDelegationFilter = mock(ResourceDelegationFilter.class);
    given(extensionFilterManager.getFilters(ResourceDelegationFilter.class)).willReturn(Collections.singleton(resourceDelegationFilter));
    policyService = new UmaPolicyServiceImpl(policyResourceDelegate, resourceSetStoreFactory, lazyAuditLogger, contextHelper, policyEvaluatorFactory, coreServicesWrapper, debug, umaSettingsFactory, extensionFilterManager);
    given(contextHelper.getRealm(Matchers.<Context>anyObject())).willReturn("REALM");
    given(contextHelper.getUserId(Matchers.<Context>anyObject())).willReturn(RESOURCE_OWNER_ID);
    given(contextHelper.getUserUid(Matchers.<Context>anyObject())).willReturn("RESOURCE_OWNER_UID");
    resourceSetStore = mock(ResourceSetStore.class);
    resourceSet = new ResourceSetDescription("RESOURCE_SET_ID", "CLIENT_ID", RESOURCE_OWNER_ID, Collections.<String, Object>emptyMap());
    resourceSet.setDescription(json(object(field("name", "NAME"), field("scopes", array("SCOPE_A", "SCOPE_B", "SCOPE_C")))));
    given(resourceSetStoreFactory.create(anyString())).willReturn(resourceSetStore);
    given(resourceSetStore.read("RESOURCE_SET_ID", RESOURCE_OWNER_ID)).willReturn(resourceSet);
    given(resourceSetStore.read(eq("RESOURCE_SET_ID"), any(ResourceSetAcceptAllFilter.class))).willReturn(resourceSet);
    given(resourceSetStore.query(QueryFilter.and(QueryFilter.equalTo(ResourceSetTokenField.RESOURCE_SET_ID, "RESOURCE_SET_ID")))).willReturn(Collections.singleton(resourceSet));
    doThrow(org.forgerock.oauth2.core.exceptions.NotFoundException.class).when(resourceSetStore).read("OTHER_ID", RESOURCE_OWNER_ID);
    doThrow(org.forgerock.oauth2.core.exceptions.ServerException.class).when(resourceSetStore).read("FAILING_ID", RESOURCE_OWNER_ID);
    doThrow(org.forgerock.oauth2.core.exceptions.ServerException.class).when(resourceSetStore).query(QueryFilter.and(QueryFilter.equalTo(ResourceSetTokenField.RESOURCE_SET_ID, "FAILING_ID")));
    given(lazyAuditLogger.get()).willReturn(auditLogger);
    AMIdentity identity = mock(AMIdentity.class);
    given(identity.getUniversalId()).willReturn("uid=RESOURCE_OWNER_ID,ou=REALM,dc=forgerock,dc=org");
    given(coreServicesWrapper.getIdentity(RESOURCE_OWNER_ID, "REALM")).willReturn(identity);
}
Also used : CoreServicesWrapper(org.forgerock.openam.core.CoreServicesWrapper) ContextHelper(org.forgerock.openam.rest.resource.ContextHelper) UmaSettingsFactory(org.forgerock.openam.uma.UmaSettingsFactory) UmaAuditLogger(org.forgerock.openam.uma.audit.UmaAuditLogger) ResourceDelegationFilter(org.forgerock.openam.uma.extensions.ResourceDelegationFilter) Matchers.anyString(org.mockito.Matchers.anyString) Evaluator(com.sun.identity.entitlement.Evaluator) ResourceSetDescription(org.forgerock.oauth2.resources.ResourceSetDescription) Subject(javax.security.auth.Subject) ResourceSetAcceptAllFilter(org.forgerock.openam.uma.ResourceSetAcceptAllFilter) UmaSettings(org.forgerock.openam.uma.UmaSettings) ResourceSetStore(org.forgerock.oauth2.resources.ResourceSetStore) AMIdentity(com.sun.identity.idm.AMIdentity) ResourceSetStoreFactory(org.forgerock.openam.oauth2.resources.ResourceSetStoreFactory) Debug(com.sun.identity.shared.debug.Debug) ExtensionFilterManager(org.forgerock.openam.oauth2.extensions.ExtensionFilterManager) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)59 Test (org.testng.annotations.Test)33 ResourceException (org.forgerock.json.resource.ResourceException)19 HashSet (java.util.HashSet)15 UmaPolicy (org.forgerock.openam.uma.UmaPolicy)15 Context (org.forgerock.services.context.Context)14 JsonValue (org.forgerock.json.JsonValue)12 QueryResponse (org.forgerock.json.resource.QueryResponse)12 Collection (java.util.Collection)11 ResourceSetStore (org.forgerock.oauth2.resources.ResourceSetStore)11 RealmContext (org.forgerock.openam.rest.RealmContext)11 HashMap (java.util.HashMap)10 Responses.newQueryResponse (org.forgerock.json.resource.Responses.newQueryResponse)10 RootContext (org.forgerock.services.context.RootContext)10 Pair (org.forgerock.util.Pair)10 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)9 QueryFilter (org.forgerock.util.query.QueryFilter)9 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)9 List (java.util.List)8 ResourceSetLabel (org.forgerock.openam.oauth2.resources.labels.ResourceSetLabel)8