use of org.forgerock.openam.cts.api.tokens.Token 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.cts.api.tokens.Token in project OpenAM by OpenRock.
the class TokenTestUtilsTest method shouldCompareDateWithMilliseconds.
@Test(expectedExceptions = AssertionError.class)
public void shouldCompareDateWithMilliseconds() {
// Given
Token expected = new Token("", TokenType.SESSION);
expected.setExpiryTimestamp(Calendar.getInstance());
Token result = new Token("", TokenType.SESSION);
Calendar resultCal = Calendar.getInstance();
resultCal.add(Calendar.MILLISECOND, 1);
result.setExpiryTimestamp(resultCal);
// When / Then
TokenTestUtils.assertTokenEquals(result, expected);
}
use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.
the class JavaBeanAdapterTest method testRoundTrip.
@Test
public void testRoundTrip() throws Exception {
//Given
DummyBean b = new DummyBean();
Map<String, Object> map = new HashMap<String, Object>();
map.put("fred", Arrays.asList("one", "two"));
b.setComplexField(map);
b.setCounter(50);
GregorianCalendar date = new GregorianCalendar(2014, 10, 30);
b.setDate(date);
b.setId("abc123");
b.setName("my token");
//When
Token t = adapter.toToken(b);
//Then
assertThat(t.getAttributeNames()).containsOnly(CoreTokenField.BLOB, CoreTokenField.TOKEN_ID, CoreTokenField.STRING_ONE, CoreTokenField.INTEGER_ONE, CoreTokenField.DATE_ONE, CoreTokenField.TOKEN_TYPE);
assertThat(t.getTokenId()).isEqualTo("abc123");
assertThat(t.<Calendar>getValue(CoreTokenField.DATE_ONE).getTimeInMillis()).isEqualTo(date.getTimeInMillis());
assertThat(t.<Integer>getValue(CoreTokenField.INTEGER_ONE)).isEqualTo(50);
assertThat(t.<String>getValue(CoreTokenField.STRING_ONE)).isEqualTo("my token");
assertThat(t.<byte[]>getValue(CoreTokenField.BLOB)).isEqualTo("{\"fred\":[\"one\",\"two\"]}".getBytes(Charset.forName("UTF-8")));
//When
DummyBean roundTrip = adapter.fromToken(t);
//Then
assertThat(roundTrip).isEqualTo(b);
}
use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.
the class SessionAdapterTest method shouldSerialiseAndDeserialiseToken.
@Test
public void shouldSerialiseAndDeserialiseToken() {
// Given
// Sessions can only measure time to the closest second.
Calendar now = Calendar.getInstance();
now.set(Calendar.MILLISECOND, 0);
long mockTimestamp = TimeUtils.toUnixTime(now);
String userId = "ferret";
String sessionId = "badger";
String sessionHandle = SessionID.SHANDLE_SCHEME_PREFIX + "weasel";
byte[] mockByteData = {};
InternalSession session = mock(InternalSession.class);
// Ensure Session ID is badger
given(tokenIdFactory.toSessionTokenId(any(InternalSession.class))).willReturn(sessionId);
// Ensure Session User is ferret
given(coreTokenConfig.getUserId(any(InternalSession.class))).willReturn(userId);
// Ensure the expiration time is set.
given(session.getExpirationTime(TimeUnit.MILLISECONDS)).willReturn(TimeUnit.SECONDS.toMillis(mockTimestamp));
SessionID mockSessionID = mock(SessionID.class);
given(mockSessionID.toString()).willReturn(sessionId);
given(session.getID()).willReturn(mockSessionID);
given(session.getSessionHandle()).willReturn(sessionHandle);
// Avoid serialisation when using mock InternalSessions
given(jsonSerialisation.deserialise(anyString(), eq(InternalSession.class))).willReturn(session);
given(jsonSerialisation.serialise(any())).willReturn(new String(mockByteData));
adapter = new SessionAdapter(tokenIdFactory, coreTokenConfig, jsonSerialisation, blobUtils);
Token token = new Token(sessionId, TokenType.SESSION);
token.setUserId(userId);
token.setExpiryTimestamp(now);
token.setBlob(mockByteData);
token.setAttribute(SessionTokenField.SESSION_ID.getField(), "badger");
token.setAttribute(SessionTokenField.SESSION_HANDLE.getField(), sessionHandle);
// When
Token result = adapter.toToken(adapter.fromToken(token));
// Then
TokenTestUtils.assertTokenEquals(result, token);
}
use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.
the class SessionAdapterTest method shouldRestoreLatestAccessTimeFromAttribute.
@Test
public void shouldRestoreLatestAccessTimeFromAttribute() {
// Given
String latestAccessTime = "12345";
Token token = new Token("badger", TokenType.SESSION);
token.setAttribute(SessionTokenField.LATEST_ACCESS_TIME.getField(), latestAccessTime);
// blob contents are missing the latestAccessTime value
token.setBlob("{\"clientDomain\":null,\"creationTime\":1376307674,\"isISStored\":true,\"maxCachingTime\":3}".getBytes());
// need a real JSONSerialisation for this test
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker().withFieldVisibility(JsonAutoDetect.Visibility.ANY).withGetterVisibility(JsonAutoDetect.Visibility.NONE).withIsGetterVisibility(JsonAutoDetect.Visibility.NONE).withSetterVisibility(JsonAutoDetect.Visibility.NONE).withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
JSONSerialisation serialisation = new JSONSerialisation(mapper);
adapter = new SessionAdapter(tokenIdFactory, coreTokenConfig, serialisation, blobUtils);
// When
InternalSession session = adapter.fromToken(token);
// Then
// if latestAccessTime was zero, this would fail
long epochedSeconds = System.currentTimeMillis() / 1000;
long idleTime = session.getIdleTime();
assertTrue(idleTime < epochedSeconds);
}
Aggregations