UmaPolicyServiceImplTest.java revision a35224ef1ee8c02d389ffeeb676b4de432294fb6
/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2015 ForgeRock AS.
*/
public class UmaPolicyServiceImplTest {
private UmaPolicyServiceImpl policyService;
private ResourceSetStore resourceSetStore;
private ResourceSetDescription resourceSet;
private UmaAuditLogger auditLogger;
private ContextHelper contextHelper;
private Evaluator policyEvaluator;
private CoreServicesWrapper coreServicesWrapper;
private UmaSettings umaSettings;
given(policyEvaluatorFactory.getEvaluator(any(Subject.class), anyString())).willReturn(policyEvaluator);
policyService = new UmaPolicyServiceImpl(policyResourceDelegate, resourceSetStoreFactory, lazyAuditLogger,
resourceSet.setDescription(json(object(field("name", "NAME"), field("scopes", array("SCOPE_A", "SCOPE_B", "SCOPE_C")))));
doThrow(org.forgerock.oauth2.core.exceptions.NotFoundException.class).when(resourceSetStore).read("OTHER_ID");
doThrow(org.forgerock.oauth2.core.exceptions.ServerException.class).when(resourceSetStore).read("FAILING_ID");
doThrow(org.forgerock.oauth2.core.exceptions.ServerException.class).when(resourceSetStore).query(QueryFilter.and(
}
}
}
))
));
}
}
static JsonValue createBackendSubjectOnePolicyJson() {
field("active", true),
field("SCOPE_A", true),
field("SCOPE_B", true))),
))
));
}
static JsonValue createBackendSubjectTwoPolicyJson() {
field("active", true),
field("SCOPE_A", true))),
))
));
}
field("active", true),
field("SCOPE_A", true),
field("SCOPE_B", true),
field("SCOPE_C", true))),
))
));
}
@Test
@SuppressWarnings("unchecked")
public void shouldCreateUmaPolicy() throws Exception {
//Given
ResourceResponse createdPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
ResourceResponse createdPolicy2 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectTwoPolicyJson());
Promise<List<ResourceResponse>, ResourceException> createPolicyPromise = newResultPromise(createdPolicies);
//When
//Then
}
@SuppressWarnings("unchecked")
public void shouldNotCreateUmaPolicyIfAlreadyExists() throws Exception {
//Given
ResourceResponse policyResource = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
//When
try {
} catch (ResourceException e) {
//Then
throw e;
}
}
public void shouldHandleFailureToCreateUnderlyingPolicies() throws Exception {
//Given
Promise<List<ResourceResponse>, ResourceException> createPoliciesPromise = Promises.newExceptionPromise(exception);
//When
//Then
//Expected ResourceException
}
public void shouldNotCreateUmaPolicyIfContainsInvalidRequestedScope() throws Exception {
//Given
//When
//Then
//Expected ResourceException
}
public void shouldNotCreateUmaPolicyIfResourceSetNotFound() throws Exception {
//Given
//When
//Then
//Expected ResourceException
}
public void shouldNotCreateUmaPolicyWhenFailToReadResourceSet() throws Exception {
//Given
//When
//Then
//Expected ResourceException
}
@Test
@SuppressWarnings("unchecked")
public void shouldReadUmaPolicy() throws Exception {
//Given
ResourceResponse readPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
ResourceResponse readPolicy2 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectTwoPolicyJson());
//When
UmaPolicy umaPolicy = policyService.readPolicy(context, "RESOURCE_SET_ID").getOrThrowUninterruptibly();
//Then
}
@SuppressWarnings("unchecked")
public void shouldHandleReadFailureToQueryUnderlyingPolicies() throws Exception {
//Given
//When
//Then
}
@Test
public void shouldUpdateUmaPolicy() throws Exception {
//Given
ResourceResponse updatedPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOneUpdatedPolicyJson());
ResourceResponse updatedPolicy2 = newResourceResponse("ID_2", "REVISION_1", createBackendSubjectTwoPolicyJson());
Promise<List<ResourceResponse>, ResourceException> updatePolicyPromise = newResultPromise(updatedPolicies);
ResourceResponse currentPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
ResourceResponse currentPolicy2 = newResourceResponse("ID_2", "REVISION_1", createBackendSubjectTwoPolicyJson());
//When
//Then
}
public void shouldHandleFailureToUpdateUnderlyingPolicies() throws Exception {
//Given
Promise<List<ResourceResponse>, ResourceException> updatePoliciesPromise = newExceptionPromise(exception);
//When
//Then
//Expected ResourceException
}
public void shouldNotUpdateUmaPolicyIfContainsInvalidRequestedScope() throws Exception {
//Given
//When
//Then
//Expected ResourceException
}
public void shouldNotUpdateUmaPolicyIfResourceSetNotFound() throws Exception {
//Given
//When
//Then
//Expected ResourceException
}
public void shouldNotUpdateUmaPolicyWhenFailToReadResourceSet() throws Exception {
//Given
//When
//Then
//Expected ResourceException
}
@Test
@SuppressWarnings("unchecked")
public void shouldDeleteUmaPolicy() throws Exception {
//Given
ResourceResponse readPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
ResourceResponse readPolicy2 = newResourceResponse("ID_2", "REVISION_2", createBackendSubjectTwoPolicyJson());
Promise<List<ResourceResponse>, ResourceException> deletePoliciesPromise = newResultPromise(readPolicies);
//When
//Then
}
@SuppressWarnings("unchecked")
public void shouldHandleDeleteFailureToQueryUnderlyingPolicies() throws Exception {
//Given
//When
try {
} catch (ResourceException e) {
//Then
throw e;
}
}
@SuppressWarnings("unchecked")
public void shouldHandleFailureToDeleteUnderlyingPolicies() throws Exception {
//Given
ResourceResponse readPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
ResourceResponse readPolicy2 = newResourceResponse("ID_2", "REVISION_2", createBackendSubjectTwoPolicyJson());
Promise<List<ResourceResponse>, ResourceException> deletePoliciesPromise = newExceptionPromise(exception);
//When
try {
} catch (ResourceException e) {
//Then
throw e;
}
}
private void setupQueries(Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> initialQuery,
final ResourceResponse... updatedPolicies) {
any(QueryResourceHandler.class))).willAnswer(new Answer<Promise<QueryResponse, ResourceException>>() {
public Promise<QueryResponse, ResourceException> answer(InvocationOnMock invocation) throws Throwable {
for (ResourceResponse r : updatedPolicies) {
}
return newResultPromise(newQueryResponse());
}
});
}
}
}
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesBySubject() throws Exception {
//Given
mockBackendQuery(context, createBackendSubjectOnePolicyJson(), createBackendSubjectTwoPolicyJson());
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request)
//Then
}
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesByUnknownSubject() throws Exception {
//Given
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request)
//Then
}
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesByResourceServer() throws Exception {
//Given
mockBackendQuery(context, createBackendSubjectOnePolicyJson(), createBackendSubjectTwoPolicyJson());
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request)
//Then
}
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesByUnknownResourceServer() throws Exception {
//Given
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request)
//Then
}
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesBySubjectAndResourceServer() throws Exception {
//Given
));
mockBackendQuery(context, createBackendSubjectOnePolicyJson(), createBackendSubjectTwoPolicyJson());
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request)
//Then
}
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesByUnknownSubjectAndResourceServer() throws Exception {
//Given
));
mockBackendQuery(context, createBackendSubjectOnePolicyJson(), createBackendSubjectTwoPolicyJson());
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request)
//Then
}
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesBySubjectAndUnknownResourceServer() throws Exception {
//Given
));
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request)
//Then
}
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesByUnknownSubjectAndUnknownResourceServer() throws Exception {
//Given
));
mockBackendQuery(context, createBackendSubjectOnePolicyJson(), createBackendSubjectTwoPolicyJson());
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request)
//Then
}
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesBySubjectOrResourceServer() throws Exception {
//Given
));
mockBackendQuery(context, createBackendSubjectOnePolicyJson(), createBackendSubjectTwoPolicyJson());
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request)
//Then
}
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesByUnknownSubjectOrResourceServer() throws Exception {
//Given
));
mockBackendQuery(context, createBackendSubjectOnePolicyJson(), createBackendSubjectTwoPolicyJson());
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request)
//Then
}
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesBySubjectOrUnknownResourceServer() throws Exception {
//Given
));
mockBackendQuery(context, createBackendSubjectOnePolicyJson(), createBackendSubjectTwoPolicyJson());
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request)
//Then
}
@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesByUnknownSubjectOrUnknownResourceServer() throws Exception {
//Given
));
//When
Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request)
//Then
}
}