Search in sources :

Example 36 with Token

use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.

the class TokenDataStoreTest method testQueryError.

@Test(expectedExceptions = ServerException.class)
public void testQueryError() throws Exception {
    // Given
    QueryFilter<String> query = QueryFilter.alwaysTrue();
    TokenFilter tokenFilter = mock(TokenFilter.class);
    when(adapter.toTokenQuery(query)).thenReturn(tokenFilter);
    final Token token1 = new Token("123", TokenType.GENERIC);
    final Token token2 = new Token("456", TokenType.GENERIC);
    Object o1 = new Object();
    Object o2 = new Object();
    when(adapter.fromToken(token1)).thenReturn(o1);
    when(adapter.fromToken(token2)).thenReturn(o2);
    final Task task = mock(Task.class);
    when(taskFactory.query(any(TokenFilter.class), any(ResultHandler.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((ResultHandler) invocation.getArguments()[1]).processError(new Exception());
            return task;
        }
    });
    // When
    store.query(query);
// Then - exception;
}
Also used : Task(org.forgerock.openam.sm.datalayer.api.Task) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Token(org.forgerock.openam.cts.api.tokens.Token) ResultHandler(org.forgerock.openam.sm.datalayer.api.ResultHandler) DataLayerException(org.forgerock.openam.sm.datalayer.api.DataLayerException) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Example 37 with Token

use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.

the class TokenDataStoreTest method testQuery.

@Test
public void testQuery() throws Exception {
    // Given
    QueryFilter<String> query = QueryFilter.alwaysTrue();
    TokenFilter tokenFilter = mock(TokenFilter.class);
    when(adapter.toTokenQuery(query)).thenReturn(tokenFilter);
    final Token token1 = new Token("123", TokenType.GENERIC);
    final Token token2 = new Token("456", TokenType.GENERIC);
    Object o1 = new Object();
    Object o2 = new Object();
    when(adapter.fromToken(token1)).thenReturn(o1);
    when(adapter.fromToken(token2)).thenReturn(o2);
    final Task task = mock(Task.class);
    when(taskFactory.query(any(TokenFilter.class), any(ResultHandler.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((ResultHandler) invocation.getArguments()[1]).processResults(asSet(token1, token2));
            return task;
        }
    });
    // When
    Set<Object> result = store.query(query);
    // Then
    verify(taskFactory).query(eq(tokenFilter), any(ResultHandler.class));
    verify(taskExecutor).execute(null, task);
    assertThat(result).containsOnly(o1, o2);
}
Also used : Task(org.forgerock.openam.sm.datalayer.api.Task) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Token(org.forgerock.openam.cts.api.tokens.Token) ResultHandler(org.forgerock.openam.sm.datalayer.api.ResultHandler) TokenFilter(org.forgerock.openam.cts.api.filter.TokenFilter) Test(org.testng.annotations.Test)

Example 38 with Token

use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.

the class CTSSessionBlacklistTest method shouldReturnTrueForBlacklistedSessions.

@Test
public void shouldReturnTrueForBlacklistedSessions() throws Exception {
    // Given
    given(mockCts.read(SID)).willReturn(new Token(SID, TokenType.SESSION_BLACKLIST));
    // When
    final boolean result = testBlacklist.isBlacklisted(mockSession);
    // Then
    assertThat(result).isTrue();
}
Also used : Token(org.forgerock.openam.cts.api.tokens.Token) Test(org.testng.annotations.Test)

Example 39 with Token

use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.

the class TokenDataStoreTest method testUpdateNotExisting.

@Test(expectedExceptions = NotFoundException.class)
public void testUpdateNotExisting() throws Exception {
    // Given
    final Token token = new Token("123", TokenType.GENERIC);
    final Task readTask = mock(Task.class);
    when(adapter.toToken(anyObject())).thenReturn(token);
    when(taskFactory.read(eq("123"), any(ResultHandler.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((ResultHandler) invocation.getArguments()[1]).processResults(null);
            return readTask;
        }
    });
    // When
    store.update(new Object());
// Then - exception
}
Also used : Task(org.forgerock.openam.sm.datalayer.api.Task) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Token(org.forgerock.openam.cts.api.tokens.Token) ResultHandler(org.forgerock.openam.sm.datalayer.api.ResultHandler) Test(org.testng.annotations.Test)

Example 40 with Token

use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.

the class CoreTokenResource method readInstance.

/**
     * Read the token based on its Token ID.
     *
     * If successful, the Token will be returned to the caller in serialised JSON format.
     *
     * @param serverContext Required context.
     * @param tokenId The TokenID of the Token to read.
     * @param readRequest Not used.
     */
public Promise<ResourceResponse, ResourceException> readInstance(Context serverContext, String tokenId, ReadRequest readRequest) {
    String principal = PrincipalRestUtils.getPrincipalNameFromServerContext(serverContext);
    try {
        Token token = store.read(tokenId);
        if (token == null) {
            error("READ by {0}: No token resource to read with ID: {1}", principal, tokenId);
            return generateNotFoundException(tokenId).asPromise();
        }
        String json = serialisation.serialise(token);
        ResourceResponse response = newResourceResponse(tokenId, String.valueOf(System.currentTimeMillis()), JsonValueBuilder.toJsonValue(json));
        debug("READ by {0}: Read token resource with ID: {1}", principal, tokenId);
        return newResultPromise(response);
    } catch (CoreTokenException e) {
        error(e, "READ by {0}: Error reading token resource with ID: {1}", principal, tokenId);
        return generateException(e).asPromise();
    }
}
Also used : Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) Token(org.forgerock.openam.cts.api.tokens.Token)

Aggregations

Token (org.forgerock.openam.cts.api.tokens.Token)86 Test (org.testng.annotations.Test)58 Task (org.forgerock.openam.sm.datalayer.api.Task)17 ResultHandler (org.forgerock.openam.sm.datalayer.api.ResultHandler)16 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)16 InvocationOnMock (org.mockito.invocation.InvocationOnMock)14 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)13 InternalSession (com.iplanet.dpro.session.service.InternalSession)8 DataLayerException (org.forgerock.openam.sm.datalayer.api.DataLayerException)8 Calendar (java.util.Calendar)7 JsonValue (org.forgerock.json.JsonValue)7 SessionID (com.iplanet.dpro.session.SessionID)6 TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)6 Collection (java.util.Collection)5 HashMap (java.util.HashMap)5 Entry (org.forgerock.opendj.ldap.Entry)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 SAMLToken (org.forgerock.openam.cts.api.tokens.SAMLToken)4 Connection (org.forgerock.opendj.ldap.Connection)4 LinkedHashMapEntry (org.forgerock.opendj.ldap.LinkedHashMapEntry)4