use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.
the class PolicyResourceDelegateTest method shouldUpdatePolicies.
@Test
public void shouldUpdatePolicies() throws ResourceException {
//Given
Context context = mock(Context.class);
Set<JsonValue> policies = new HashSet<JsonValue>();
JsonValue policyOne = json(object(field("name", "POLICY_ONE")));
JsonValue policyTwo = json(object(field("name", "POLICY_TWO")));
policies.add(policyOne);
policies.add(policyTwo);
List<ResourceResponse> updatedPolicies = new ArrayList<ResourceResponse>();
ResourceResponse updatedPolicyOne = newResourceResponse("ID_1", "REVISION_1", json(object()));
ResourceResponse updatedPolicyTwo = newResourceResponse("ID_1", "REVISION_1", json(object()));
updatedPolicies.add(updatedPolicyOne);
updatedPolicies.add(updatedPolicyTwo);
Promise<ResourceResponse, ResourceException> updatePolicyOnePromise = Promises.newResultPromise(updatedPolicyOne);
Promise<ResourceResponse, ResourceException> updatePolicyTwoPromise = Promises.newResultPromise(updatedPolicyTwo);
given(policyResource.handleUpdate(eq(context), Matchers.<UpdateRequest>anyObject())).willReturn(updatePolicyOnePromise).willReturn(updatePolicyTwoPromise);
//When
List<ResourceResponse> returnedPolicies = delegate.updatePolicies(context, policies).getOrThrowUninterruptibly();
//Then
assertThat(returnedPolicies).isEqualTo(updatedPolicies);
}
use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.
the class ResourceSetServiceTest method shouldGetResourceSetsWhenResourceSetsExistQueryingByOrWithPolicies.
@Test
public void shouldGetResourceSetsWhenResourceSetsExistQueryingByOrWithPolicies() throws Exception {
//Given
Context context = createContext();
String realm = "REALM";
ResourceSetWithPolicyQuery query = new ResourceSetWithPolicyQuery();
String resourceOwnerId = "RESOURCE_OWNER_ID";
boolean augmentWithPolicies = true;
QueryFilter<String> resourceSetQuery = QueryFilter.contains("name", "RS_THREE");
QueryFilter policyQuery = QueryFilter.alwaysFalse();
Set<ResourceSetDescription> queriedResourceSets = new HashSet<>();
ResourceSetDescription resourceSetOne = new ResourceSetDescription("RS_ID_ONE", "CLIENT_ID_ONE", "RESOURCE_OWNER_ID", singletonMap("name", (Object) "RS_ONE"));
ResourceSetDescription resourceSetTwo = new ResourceSetDescription("RS_ID_TWO", "CLIENT_ID_TWO", "RESOURCE_OWNER_ID", singletonMap("name", (Object) "RS_TWO"));
ResourceSetDescription resourceSetThree = new ResourceSetDescription("RS_ID_THREE", "CLIENT_ID_TWO", "RESOURCE_OWNER_ID", singletonMap("name", (Object) "RS_THREE"));
Collection<UmaPolicy> queriedPolicies = new HashSet<>();
UmaPolicy policyOne = mock(UmaPolicy.class);
UmaPolicy policyTwo = mock(UmaPolicy.class);
UmaPolicy policyThree = mock(UmaPolicy.class);
JsonValue policyOneJson = mock(JsonValue.class);
JsonValue policyTwoJson = mock(JsonValue.class);
JsonValue policyThreeJson = mock(JsonValue.class);
Pair<QueryResponse, Collection<UmaPolicy>> queriedPoliciesPair = Pair.of(newQueryResponse(), queriedPolicies);
Promise<Pair<QueryResponse, Collection<UmaPolicy>>, ResourceException> queriedPoliciesPromise = Promises.newResultPromise(queriedPoliciesPair);
Promise<UmaPolicy, ResourceException> policyOnePromise = Promises.newResultPromise(policyOne);
Promise<UmaPolicy, ResourceException> policyTwoPromise = Promises.newResultPromise(policyTwo);
mockResourceOwnerIdentity(resourceOwnerId, realm);
query.setResourceSetQuery(resourceSetQuery);
query.setPolicyQuery(policyQuery);
queriedResourceSets.add(resourceSetOne);
queriedResourceSets.add(resourceSetTwo);
queriedPolicies.add(policyOne);
queriedPolicies.add(policyThree);
given(policyOne.getId()).willReturn("RS_ID_ONE");
given(policyOne.getResourceSet()).willReturn(resourceSetOne);
given(policyTwo.getId()).willReturn("RS_ID_TWO");
given(policyTwo.getResourceSet()).willReturn(resourceSetTwo);
given(policyThree.getId()).willReturn("RS_ID_THREE");
given(policyThree.getResourceSet()).willReturn(resourceSetThree);
given(policyOne.asJson()).willReturn(policyOneJson);
given(policyTwo.asJson()).willReturn(policyTwoJson);
given(policyThree.asJson()).willReturn(policyThreeJson);
given(resourceSetStore.query(QueryFilter.and(resourceSetQuery, equalTo(ResourceSetTokenField.RESOURCE_OWNER_ID, "RESOURCE_OWNER_ID")))).willReturn(queriedResourceSets);
given(policyService.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(queriedPoliciesPromise);
given(resourceSetStore.read("RS_ID_ONE", resourceOwnerId)).willReturn(resourceSetOne);
given(resourceSetStore.read("RS_ID_THREE", resourceOwnerId)).willReturn(resourceSetThree);
given(policyService.readPolicy(context, "RS_ID_ONE")).willReturn(policyOnePromise);
given(policyService.readPolicy(context, "RS_ID_TWO")).willReturn(policyTwoPromise);
Entitlement entitlement = new Entitlement();
Map<String, Boolean> actionValues = new HashMap();
actionValues.put("actionValueKey", true);
entitlement.setActionValues(actionValues);
Evaluator evaluator = mock(Evaluator.class);
given(umaProviderSettings.getPolicyEvaluator(any(Subject.class), anyString())).willReturn(evaluator);
given(evaluator.evaluate(eq(realm), any(Subject.class), eq("RS_ONE"), isNull(Map.class), eq(false))).willReturn(singletonList(entitlement));
given(evaluator.evaluate(eq(realm), any(Subject.class), eq("RS_TWO"), isNull(Map.class), eq(false))).willReturn(singletonList(entitlement));
given(evaluator.evaluate(eq(realm), any(Subject.class), eq("RS_THREE"), isNull(Map.class), eq(false))).willReturn(Collections.<Entitlement>emptyList());
//When
Collection<ResourceSetDescription> resourceSets = service.getResourceSets(context, realm, query, resourceOwnerId, augmentWithPolicies).getOrThrowUninterruptibly();
//Then
assertThat(resourceSets).hasSize(2).contains(resourceSetOne, resourceSetThree);
assertThat(resourceSetOne.getPolicy()).isEqualTo(policyOneJson);
assertThat(resourceSetThree.getPolicy()).isEqualTo(policyThreeJson);
}
use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.
the class UmaPolicyTest method shouldFailForMissingIdOfPolicy.
@Test(expectedExceptions = BadRequestException.class)
public void shouldFailForMissingIdOfPolicy() throws Exception {
//Given
JsonValue policy = json(object());
//When
String policyId = UmaPolicy.idOf(policy);
//Then - exception
}
use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.
the class UmaPolicyTest method shouldGetIdOfPolicy.
@Test
public void shouldGetIdOfPolicy() throws Exception {
//Given
JsonValue policy = createUmaPolicyJson();
//When
String policyId = UmaPolicy.idOf(policy);
//Then
assertThat(policyId).isEqualTo("POLICY_ID");
}
use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.
the class UmaPolicyTest method shouldConvertUmaPolicyToUnderlyingPolicies.
@Test
public void shouldConvertUmaPolicyToUnderlyingPolicies() throws BadRequestException {
//Given
UmaPolicy umaPolicy = UmaPolicy.valueOf(resourceSet, createUmaPolicyJson());
//When
Set<JsonValue> underlyingPolicies = umaPolicy.asUnderlyingPolicies("RESOURCE_OWNER_ID");
//Then
boolean foundScopeAPolicy = false;
boolean foundScopeBPolicy = false;
for (JsonValue policy : underlyingPolicies) {
if (policy.contains("NAME - RESOURCE_OWNER_ID - RESOURCE_SET_ID-" + "SUBJECT_ONE".hashCode())) {
assertThat(policy.asMap()).isEqualTo(createUnderlyingSubjectOnePolicyJson("RESOURCE_OWNER_ID", "RESOURCE_SET_ID").asMap());
foundScopeAPolicy = true;
} else if (policy.contains("NAME - RESOURCE_OWNER_ID - RESOURCE_SET_ID-" + "SUBJECT_TWO".hashCode())) {
assertThat(policy.asMap()).isEqualTo(createUnderlyingSubjectTwoPolicyJson("RESOURCE_OWNER_ID", "RESOURCE_SET_ID").asMap());
foundScopeBPolicy = true;
}
}
assertThat(foundScopeAPolicy).isTrue();
assertThat(foundScopeBPolicy).isTrue();
}
Aggregations