Search in sources :

Example 21 with Token

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);
}
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 22 with Token

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);
}
Also used : Calendar(java.util.Calendar) Token(org.forgerock.openam.cts.api.tokens.Token) LDAPDataConversionTest(org.forgerock.openam.cts.utils.LDAPDataConversionTest) Test(org.testng.annotations.Test)

Example 23 with Token

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);
}
Also used : HashMap(java.util.HashMap) GregorianCalendar(java.util.GregorianCalendar) Token(org.forgerock.openam.cts.api.tokens.Token) Test(org.testng.annotations.Test)

Example 24 with Token

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);
}
Also used : InternalSession(com.iplanet.dpro.session.service.InternalSession) Calendar(java.util.Calendar) Token(org.forgerock.openam.cts.api.tokens.Token) SessionID(com.iplanet.dpro.session.SessionID) Test(org.testng.annotations.Test)

Example 25 with 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);
}
Also used : JSONSerialisation(org.forgerock.openam.cts.utils.JSONSerialisation) InternalSession(com.iplanet.dpro.session.service.InternalSession) Token(org.forgerock.openam.cts.api.tokens.Token) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

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