use of org.forgerock.openam.cts.utils.JSONSerialisation in project OpenAM by OpenRock.
the class SAMLAdapterTest method shouldSerialiseAndDeserialiseToken.
@Test
public void shouldSerialiseAndDeserialiseToken() {
// Given
// Need real delegates for this test.
serialisation = new JSONSerialisation(new ObjectMapper());
adapter = new SAMLAdapter(new TokenIdFactory(encoding), new JSONSerialisation(new ObjectMapper()), new TokenBlobUtils());
String tokenId = encoding.encodeKey("badger");
Token token = new Token(tokenId, TokenType.SAML2);
// SAML tokens only store time to seconds resolution
Calendar now = Calendar.getInstance();
now.set(Calendar.MILLISECOND, 0);
token.setExpiryTimestamp(now);
// SAML implementation detail around stored object
String blob = "woodland forrest";
token.setBlob(serialisation.serialise(blob).getBytes());
token.setAttribute(SAMLTokenField.OBJECT_CLASS.getField(), String.class.getName());
// SAML detail for secondary key
String secondaryKey = encoding.encodeKey("weasel");
token.setAttribute(SAMLTokenField.SECONDARY_KEY.getField(), secondaryKey);
// When
Token result = adapter.toToken(adapter.fromToken(token));
// Then
TokenTestUtils.assertTokenEquals(result, token);
}
use of org.forgerock.openam.cts.utils.JSONSerialisation in project OpenAM by OpenRock.
the class OAuthAdapterTest method shouldDeserialiseSerialisedToken.
@Test
public void shouldDeserialiseSerialisedToken() {
// Given
String[] id = { "badger" };
List<String> list = new ArrayList<String>(Arrays.asList(id));
OAuthTokenField field = OAuthTokenField.ID;
JSONSerialisation serialisation = new JSONSerialisation(new ObjectMapper());
OAuthAdapter adapter = generateOAuthAdapter();
// Populate a map for serialisation.
Map<String, Object> values = new HashMap<String, Object>();
values.put(field.getOAuthField(), list);
//Map<String, Object> map = new HashMap<String, Object>();
//map.put(OAuthAdapter.VALUE, values);
String serialisedObject = serialisation.serialise(values);
Token token = new Token(id[0], TokenType.OAUTH);
// Set the serialised binary data
token.setBlob(serialisedObject.getBytes());
// When
JsonValue result = adapter.fromToken(token);
// Then
assertNotNull(result);
assert (result.asMap().get(field.getOAuthField()).toString().contains(id[0]));
}
use of org.forgerock.openam.cts.utils.JSONSerialisation in project OpenAM by OpenRock.
the class OAuthAdapterTest method generateOAuthAdapter.
/**
* @return Makes a standard OAuthAdapter with real dependencies.
*/
private OAuthAdapter generateOAuthAdapter() {
JSONSerialisation serialisation = new JSONSerialisation(new ObjectMapper());
KeyConversion keyConversion = new KeyConversion();
OAuthValues oAuthValues = new OAuthValues();
TokenBlobUtils blobUtils = new TokenBlobUtils();
return new OAuthAdapter(new TokenIdFactory(keyConversion), serialisation, oAuthValues, blobUtils);
}
use of org.forgerock.openam.cts.utils.JSONSerialisation 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);
}
use of org.forgerock.openam.cts.utils.JSONSerialisation in project OpenAM by OpenRock.
the class OAuthAdapterTest method shouldNotDeserialiseATokenWhichDoesntContainAMap.
@Test
public void shouldNotDeserialiseATokenWhichDoesntContainAMap() {
// Given
JSONSerialisation serialisation = new JSONSerialisation(new ObjectMapper());
OAuthAdapter adapter = generateOAuthAdapter();
Token token = new Token("", TokenType.OAUTH);
token.setBlob(serialisation.serialise("badger").getBytes());
// When
JsonValue fromToken = adapter.fromToken(token);
// Then
assertThat(fromToken).isNull();
}
Aggregations