use of org.forgerock.json.resource.NotFoundException in project OpenAM by OpenRock.
the class XMLResourceExceptionHandlerTest method testWrite.
@Test
public void testWrite() throws Exception {
//given
MessageContext context = mock(MessageContext.class);
AuditTrail mockAudit = mock(AuditTrail.class);
Response response = new Response();
doReturn(mockAudit).when(context).getAuditTrail();
doReturn(response).when(context).getResponse();
String message = "I don't know where it is";
ResourceException ex = new NotFoundException(message);
AuthenticationException ex2 = new AuthenticationException(ex);
//when
handler.write(context, ex2);
//then
assertThat(response.getStatus()).isEqualTo(Status.NOT_FOUND);
String text = response.getEntity().getString();
assertThat(text).contains("<message>" + message + "</message>");
assertThat(text).contains("<code>404</code>");
}
use of org.forgerock.json.resource.NotFoundException in project OpenAM by OpenRock.
the class UmaPolicyServiceImplDelegationTest method mockPolicyResourceDelegateForNewPolicy.
private void mockPolicyResourceDelegateForNewPolicy() {
final 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<List<ResourceResponse>, ResourceException> createPolicyPromise = newResultPromise(createdPolicies);
given(policyResourceDelegate.createPolicies(any(Context.class), anySetOf(JsonValue.class))).willReturn(createPolicyPromise);
Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> queryPromise = new NotFoundException().asPromise();
given(policyResourceDelegate.queryPolicies(any(Context.class), any(QueryRequest.class))).willReturn(queryPromise);
given(policyResourceDelegate.queryPolicies(any(Context.class), any(QueryRequest.class), any(QueryResourceHandler.class))).willAnswer(new Answer<Promise<QueryResponse, ResourceException>>() {
@Override
public Promise<QueryResponse, ResourceException> answer(InvocationOnMock invocation) throws Throwable {
final PolicyGraph policyGraph = (PolicyGraph) invocation.getArguments()[2];
for (ResourceResponse r : createdPolicies) {
policyGraph.handleResource(r);
}
policyGraph.handleResult(newQueryResponse());
return newResultPromise(newQueryResponse());
}
});
}
use of org.forgerock.json.resource.NotFoundException 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.resource.NotFoundException 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.resource.NotFoundException in project OpenAM by OpenRock.
the class UmaLabelsStore method read.
/**
* Reads a label from the underlying database.
* @param realm The current realm.
* @param username The user that owns the label.
* @param id The id of the label.
* @return The retrieved label details.
* @throws ResourceException Thrown if the label cannot be read.
*/
public ResourceSetLabel read(String realm, String username, String id) throws ResourceException {
try (Connection connection = getConnection()) {
SearchResultEntry entry = connection.searchSingleEntry(LDAPRequests.newSingleEntrySearchRequest(getLabelDn(realm, username, id)));
Set<String> resourceSets = new HashSet<>();
final Attribute resourceSetAttribute = entry.getAttribute(RESOURCE_SET_ATTR);
if (resourceSetAttribute != null) {
for (ByteString resourceSetId : resourceSetAttribute) {
resourceSets.add(resourceSetId.toString());
}
}
return getResourceSetLabel(entry, resourceSets);
} catch (LdapException e) {
final ResultCode resultCode = e.getResult().getResultCode();
if (resultCode.equals(ResultCode.NO_SUCH_OBJECT)) {
throw new NotFoundException();
}
throw new InternalServerErrorException("Could not read", e);
}
}
Aggregations