Search in sources :

Example 1 with NotFoundException

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>");
}
Also used : Response(org.forgerock.http.protocol.Response) AuthenticationException(org.forgerock.caf.authentication.api.AuthenticationException) NotFoundException(org.forgerock.json.resource.NotFoundException) ResourceException(org.forgerock.json.resource.ResourceException) MessageContext(org.forgerock.caf.authentication.api.MessageContext) AuditTrail(org.forgerock.caf.authentication.framework.AuditTrail) Test(org.testng.annotations.Test)

Example 2 with NotFoundException

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());
        }
    });
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) NotFoundException(org.forgerock.json.resource.NotFoundException) Promise(org.forgerock.util.promise.Promise) ResourceResponse(org.forgerock.json.resource.ResourceResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) List(java.util.List) ArrayList(java.util.ArrayList) ResourceException(org.forgerock.json.resource.ResourceException) QueryResourceHandler(org.forgerock.json.resource.QueryResourceHandler) Pair(org.forgerock.util.Pair)

Example 3 with NotFoundException

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
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) NotFoundException(org.forgerock.json.resource.NotFoundException) ResourceException(org.forgerock.json.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 4 with NotFoundException

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");
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) InOrder(org.mockito.InOrder) JsonValue(org.forgerock.json.JsonValue) ArrayList(java.util.ArrayList) NotFoundException(org.forgerock.json.resource.NotFoundException) ResourceResponse(org.forgerock.json.resource.ResourceResponse) ResourceException(org.forgerock.json.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 5 with NotFoundException

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);
    }
}
Also used : Attribute(org.forgerock.opendj.ldap.Attribute) ByteString(org.forgerock.opendj.ldap.ByteString) Connection(org.forgerock.opendj.ldap.Connection) NotFoundException(org.forgerock.json.resource.NotFoundException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ByteString(org.forgerock.opendj.ldap.ByteString) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry) HashSet(java.util.HashSet)

Aggregations

NotFoundException (org.forgerock.json.resource.NotFoundException)69 ResourceException (org.forgerock.json.resource.ResourceException)43 SSOException (com.iplanet.sso.SSOException)42 JsonValue (org.forgerock.json.JsonValue)39 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)39 BadRequestException (org.forgerock.json.resource.BadRequestException)38 SMSException (com.sun.identity.sm.SMSException)34 ResourceResponse (org.forgerock.json.resource.ResourceResponse)27 ForbiddenException (org.forgerock.json.resource.ForbiddenException)26 IdRepoException (com.sun.identity.idm.IdRepoException)24 PermanentException (org.forgerock.json.resource.PermanentException)24 ConflictException (org.forgerock.json.resource.ConflictException)22 ServiceNotFoundException (com.sun.identity.sm.ServiceNotFoundException)21 NotSupportedException (org.forgerock.json.resource.NotSupportedException)18 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)17 IdentityDetails (com.sun.identity.idsvcs.IdentityDetails)16 RealmContext (org.forgerock.openam.rest.RealmContext)16 SSOToken (com.iplanet.sso.SSOToken)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 MessagingException (javax.mail.MessagingException)14