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);
}
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));
}
}
}
}
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);
}
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);
}
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);
}
Aggregations