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