Search in sources :

Example 1 with CoreTokenField

use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.

the class TokenTestUtilsTest method shouldCompareValue.

@Test(expectedExceptions = AssertionError.class)
public void shouldCompareValue() {
    // Given
    Token expected = new Token("", TokenType.SESSION);
    Token result = new Token("", TokenType.SESSION);
    CoreTokenField field = CoreTokenField.STRING_ONE;
    expected.setAttribute(field, "badger");
    result.setAttribute(field, "ferret");
    // When / Then
    TokenTestUtils.assertTokenEquals(result, expected);
}
Also used : Token(org.forgerock.openam.cts.api.tokens.Token) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) LDAPDataConversionTest(org.forgerock.openam.cts.utils.LDAPDataConversionTest) Test(org.testng.annotations.Test)

Example 2 with CoreTokenField

use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.

the class TokenTestUtils method assertTokenEquals.

/**
     * Logic for comparing two tokens. It might be useful to move this to a .equals method at some point.
     *
     * @param result Non null.
     * @param expected Non null.
     */
public static void assertTokenEquals(Token result, Token expected) {
    assertThat(result.getAttributeNames()).isEqualTo(expected.getAttributeNames());
    for (CoreTokenField field : result.getAttributeNames()) {
        if (CoreTokenFieldTypes.isCalendar(field)) {
            Calendar resultCal = result.getValue(field);
            Calendar expectedCal = expected.getValue(field);
            if (resultCal.getTimeInMillis() != expectedCal.getTimeInMillis()) {
                throw new AssertionError(MessageFormat.format("Milliseconds did not match for date field {0}:\n" + "Expected: {1}\n" + "  Result: {2}", field.toString(), expectedCal.getTimeInMillis(), resultCal.getTimeInMillis()));
            }
            int resultOffset = getTotalTimeZoneOffset(resultCal.getTimeZone());
            int expectedOffset = getTotalTimeZoneOffset(expectedCal.getTimeZone());
            if (resultOffset != expectedOffset) {
                throw new AssertionError(MessageFormat.format("TimeZone offset did not match for date field {0}:\n" + "Expected: {1}\n" + "  Result: {2}", field.toString(), expectedOffset, resultOffset));
            }
        } else if (CoreTokenFieldTypes.isByteArray(field)) {
            byte[] resultValue = result.getValue(field);
            byte[] expectedValue = expected.getValue(field);
            if (!ArrayUtils.isEquals(resultValue, expectedValue)) {
                throw new AssertionError(MessageFormat.format("Value did not match for byte[] field {0}:\n" + "Expected: {1} bytes\n" + "  Result: {2} bytes", field.toString(), expectedValue.length, resultValue.length));
            }
        } else {
            Object resultValue = result.getValue(field);
            Object expectedValue = expected.getValue(field);
            if (!compareValue(resultValue, expectedValue)) {
                throw new AssertionError(MessageFormat.format("Value did not match for field {0}:\n" + "Expected: {1}\n" + "  Result: {2}", field.toString(), expectedValue, resultValue));
            }
        }
    }
}
Also used : Calendar(java.util.Calendar) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField)

Example 3 with CoreTokenField

use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.

the class CoreTokenFieldTypesTest method shouldValidateDateField.

@Test
public void shouldValidateDateField() throws CoreTokenException {
    // Given
    CoreTokenField key = CoreTokenField.DATE_ONE;
    Calendar value = Calendar.getInstance();
    // When / Then
    CoreTokenFieldTypes.validateType(key, value);
}
Also used : Calendar(java.util.Calendar) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) Test(org.testng.annotations.Test)

Example 4 with CoreTokenField

use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.

the class CoreTokenFieldTypesTest method shouldValidateInvalidType.

@Test(expectedExceptions = CoreTokenException.class)
public void shouldValidateInvalidType() throws CoreTokenException {
    // Given
    CoreTokenField key = CoreTokenField.BLOB;
    Integer value = 1234;
    // When / Then
    CoreTokenFieldTypes.validateType(key, value);
}
Also used : CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) Test(org.testng.annotations.Test)

Example 5 with CoreTokenField

use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.

the class CoreTokenFieldTypesTest method shouldValidateIntegerField.

@Test
public void shouldValidateIntegerField() throws CoreTokenException {
    // Given
    CoreTokenField key = CoreTokenField.INTEGER_ONE;
    Integer value = 1234;
    // When / Then
    CoreTokenFieldTypes.validateType(key, value);
}
Also used : CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) Test(org.testng.annotations.Test)

Aggregations

CoreTokenField (org.forgerock.openam.tokens.CoreTokenField)33 Test (org.testng.annotations.Test)17 Calendar (java.util.Calendar)5 PartialToken (org.forgerock.openam.sm.datalayer.api.query.PartialToken)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 TokenFilterBuilder (org.forgerock.openam.cts.api.filter.TokenFilterBuilder)4 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)4 QueryFilter (org.forgerock.util.query.QueryFilter)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 JsonValue (org.forgerock.json.JsonValue)3 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)3 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)3 TokenFilter (org.forgerock.openam.cts.api.filter.TokenFilter)3 Token (org.forgerock.openam.cts.api.tokens.Token)3 BadRequestException (org.forgerock.json.resource.BadRequestException)2 InvalidClientException (org.forgerock.oauth2.core.exceptions.InvalidClientException)2 NotFoundException (org.forgerock.oauth2.core.exceptions.NotFoundException)2 QueryBuilder (org.forgerock.openam.sm.datalayer.api.query.QueryBuilder)2