use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.
the class UmaPolicyServiceImplTest method mockBackendQuery.
private void mockBackendQuery(Context context, JsonValue... policies) {
QueryResponse queryResult = newQueryResponse();
List<ResourceResponse> policyResources = new ArrayList<>();
for (JsonValue policy : policies) {
policyResources.add(newResourceResponse("ID_1", "REVISION_1", policy));
}
Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> backendQueryPromise = newResultPromise(Pair.of(queryResult, policyResources));
given(policyResourceDelegate.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(backendQueryPromise);
}
use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.
the class UmaPolicyServiceImplTest method shouldHandleFailureToCreateUnderlyingPolicies.
@Test(expectedExceptions = ResourceException.class)
public void shouldHandleFailureToCreateUnderlyingPolicies() throws Exception {
//Given
Context context = createContext();
JsonValue policy = createUmaPolicyJson("RESOURCE_SET_ID");
ResourceException exception = mock(ResourceException.class);
Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> queryPromise = Promises.newExceptionPromise((ResourceException) new NotFoundException());
Promise<List<ResourceResponse>, ResourceException> createPoliciesPromise = Promises.newExceptionPromise(exception);
given(policyResourceDelegate.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(queryPromise);
given(policyResourceDelegate.createPolicies(eq(context), Matchers.<Set<JsonValue>>anyObject())).willReturn(createPoliciesPromise);
//When
policyService.createPolicy(context, policy).getOrThrowUninterruptibly();
//Then
//Expected ResourceException
}
use of org.forgerock.json.JsonValue 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.json.JsonValue 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.json.JsonValue in project OpenAM by OpenRock.
the class UmaLabelResourceTest method createInstanceFailsWhenTypePropertyIsMissing.
@Test
public void createInstanceFailsWhenTypePropertyIsMissing() {
//Given
JsonValue umaLabel = json(object(field(NAME_ATTRIBUTE_NAME, LABEL_NAME)));
given(createRequest.getContent()).willReturn(umaLabel);
given(contextHelper.getRealm(serverContext)).willReturn(REALM_NAME);
given(contextHelper.getUserId(serverContext)).willReturn(RESOURCE_OWNER_ID);
//When
Promise<ResourceResponse, ResourceException> promise = umaLabelResource.createInstance(serverContext, createRequest);
//Then
verifyZeroInteractions(umaLabelsStore);
assertThat(promise).failedWithResourceException().isInstanceOf(BadRequestException.class);
}
Aggregations