use of org.forgerock.services.context.Context in project OpenAM by OpenRock.
the class UmaPolicyServiceImplTest method shouldCreateUmaPolicy.
@Test
@SuppressWarnings("unchecked")
public void shouldCreateUmaPolicy() throws Exception {
//Given
Context context = createContext();
JsonValue policy = createUmaPolicyJson("RESOURCE_SET_ID");
List<ResourceResponse> createdPolicies = new ArrayList<>();
ResourceResponse createdPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
ResourceResponse createdPolicy2 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectTwoPolicyJson());
createdPolicies.add(createdPolicy1);
createdPolicies.add(createdPolicy2);
Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> queryPromise = Promises.newExceptionPromise((ResourceException) new NotFoundException());
setupQueries(queryPromise, createdPolicy1, createdPolicy2);
Promise<List<ResourceResponse>, ResourceException> createPolicyPromise = newResultPromise(createdPolicies);
given(policyResourceDelegate.createPolicies(eq(context), Matchers.<Set<JsonValue>>anyObject())).willReturn(createPolicyPromise);
//When
UmaPolicy umaPolicy = policyService.createPolicy(context, policy).getOrThrowUninterruptibly();
//Then
InOrder inOrder = inOrder(resourceDelegationFilter, policyResourceDelegate, resourceDelegationFilter);
inOrder.verify(resourceDelegationFilter).beforeResourceShared(any(UmaPolicy.class));
inOrder.verify(policyResourceDelegate).createPolicies(eq(context), anySetOf(JsonValue.class));
inOrder.verify(resourceDelegationFilter).afterResourceShared(any(UmaPolicy.class));
assertThat(umaPolicy.getId()).isEqualTo("RESOURCE_SET_ID");
assertThat(umaPolicy.getRevision()).isNotNull();
assertThat(umaPolicy.asJson().asMap()).hasSize(3).contains(entry("policyId", "RESOURCE_SET_ID"), entry("name", "NAME"));
JsonValue permissions = umaPolicy.asJson().get("permissions");
assertThat(permissions.asList()).hasSize(2);
assertThat(permissions.get(0).asMap()).contains(entry("subject", "SUBJECT_ONE"));
assertThat(permissions.get(0).get("scopes").asList()).containsOnly("SCOPE_A", "SCOPE_B");
assertThat(permissions.get(1).asMap()).contains(entry("subject", "SUBJECT_TWO"));
assertThat(permissions.get(1).get("scopes").asList()).containsOnly("SCOPE_A");
}
use of org.forgerock.services.context.Context in project OpenAM by OpenRock.
the class UmaPolicyServiceImplTest method shouldQueryUmaPoliciesBySubjectAndUnknownResourceServer.
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesBySubjectAndUnknownResourceServer() throws Exception {
//Given
Context context = createContext();
QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.and(QueryFilter.equalTo(new JsonPointer("permissions/subject"), "SUBJECT_ONE"), QueryFilter.equalTo(new JsonPointer("resourceServer"), "OTHER_CLIENT_ID")));
mockBackendQuery(context);
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request).getOrThrowUninterruptibly();
//Then
assertThat(queryResult.getSecond()).hasSize(0);
}
use of org.forgerock.services.context.Context in project OpenAM by OpenRock.
the class UmaPolicyServiceImplTest method shouldHandleDeleteFailureToQueryUnderlyingPolicies.
@Test(expectedExceptions = ResourceException.class)
@SuppressWarnings("unchecked")
public void shouldHandleDeleteFailureToQueryUnderlyingPolicies() throws Exception {
//Given
Context context = createContext();
ResourceException exception = mock(ResourceException.class);
Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> readPoliciesPromise = Promises.newExceptionPromise(exception);
given(policyResourceDelegate.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(readPoliciesPromise);
//When
try {
policyService.deletePolicy(context, "RESOURCE_SET_ID").getOrThrowUninterruptibly();
} catch (ResourceException e) {
//Then
verify(policyResourceDelegate, never()).deletePolicies(eq(context), anyListOf(String.class));
throw e;
}
}
use of org.forgerock.services.context.Context in project OpenAM by OpenRock.
the class UmaPolicyServiceImplTest method shouldNotCreateUmaPolicyIfAlreadyExists.
@Test(expectedExceptions = ConflictException.class)
@SuppressWarnings("unchecked")
public void shouldNotCreateUmaPolicyIfAlreadyExists() throws Exception {
//Given
Context context = createContext();
JsonValue policy = createUmaPolicyJson("RESOURCE_SET_ID");
ResourceResponse policyResource = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> queryPromise = newResultPromise(Pair.of(newQueryResponse(), Collections.singletonList(policyResource)));
given(policyResourceDelegate.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(queryPromise);
//When
try {
policyService.createPolicy(context, policy).getOrThrowUninterruptibly();
} catch (ResourceException e) {
//Then
verify(policyResourceDelegate, never()).createPolicies(eq(context), anySetOf(JsonValue.class));
throw e;
}
}
use of org.forgerock.services.context.Context in project OpenAM by OpenRock.
the class UmaPolicyServiceImplDelegationTest method aliceShouldBeAbleToUpdatePolicyForResource.
@Test
public void aliceShouldBeAbleToUpdatePolicyForResource() throws Exception {
//Given
userIsLoggedIn("alice", "REALM");
accessingUriForUser("alice");
String resourceSetId = registerResourceSet("alice");
createPolicyFor("bob", resourceSetId, "SCOPE_A", "SCOPE_B");
JsonValue policy = policyToUpdate(resourceSetId);
Context context = getContext();
//When
Promise<UmaPolicy, ResourceException> promise = policyService.updatePolicy(context, resourceSetId, policy);
//Then
assertThat(promise).succeeded();
}
Aggregations