@Test public void shouldSerializeUnauthorizedEntity() { CrysonSerializer serializer = givenCrysonSerializer(); String serializedEntity = serializer.serializeUnauthorizedEntity("CrysonTestEntity", 1L); assertEquals( "{\"crysonUnauthorized\":true,\"crysonEntityClass\":\"CrysonTestEntity\",\"id\":1}", serializedEntity); }
@Test public void shouldDeserializeWithToOneAssociation() throws Exception { CrysonSerializer crysonSerializer = givenCrysonSerializer(); String serializedChildEntity = "{\"id\":100,\"parent_cryson_id\":1}"; CrysonTestChildEntity deserializedChildEntity = crysonSerializer.deserialize(serializedChildEntity, CrysonTestChildEntity.class, null); assertNotNull(deserializedChildEntity); assertEquals((Long) 100l, deserializedChildEntity.getId()); assertEquals((Long) 1l, deserializedChildEntity.getParent().getId()); }
@Test public void shouldDeserializeViaJsonElement() throws Exception { CrysonSerializer crysonSerializer = givenCrysonSerializer(); String serializedChildEntity = "{\"id\":100,\"parent_cryson_id\":1}"; JsonElement childEntityJsonElement = crysonSerializer.parse(serializedChildEntity); CrysonTestChildEntity deserializedChildEntity = crysonSerializer.deserialize(childEntityJsonElement, CrysonTestChildEntity.class, null); assertNotNull(deserializedChildEntity); assertEquals((Long) 100l, deserializedChildEntity.getId()); assertEquals((Long) 1l, deserializedChildEntity.getParent().getId()); }
@Test public void shouldDeserializeWithToManyAssociation() throws Exception { CrysonSerializer crysonSerializer = givenCrysonSerializer(); String serializedEntity = "{\"id\":1,\"childEntities_cryson_ids\":[100]}"; CrysonTestEntity deserializedEntity = crysonSerializer.deserialize(serializedEntity, CrysonTestEntity.class, null); assertNotNull(deserializedEntity); assertEquals((Long) 1l, deserializedEntity.getId()); assertEquals(1, deserializedEntity.getChildEntities().size()); assertEquals((Long) 100l, deserializedEntity.getChildEntities().iterator().next().getId()); }
@Test public void shouldSerialize() { CrysonSerializer crysonSerializer = givenCrysonSerializer(); CrysonTestChildEntity testChildEntity = new CrysonTestChildEntity(); testChildEntity.setId(100L); CrysonTestEntity testEntity = new CrysonTestEntity(); testEntity.setId(1L); testEntity.setName("test"); testEntity.setVersion(1L); testEntity.setChildEntities(Collections.singleton(testChildEntity)); testChildEntity.setParent(testEntity); String serializedChildEntity = crysonSerializer.serialize(testChildEntity); String expectedSerializedChildEntity = "{\"id\":100,\"parent\":{\"id\":1,\"name\":\"test\",\"version\":1,\"crysonEntityClass\":\"CrysonTestEntity\",\"doubleId\":2,\"childEntities_cryson_ids\":[100]},\"crysonEntityClass\":\"CrysonTestChildEntity\"}"; assertEquals(expectedSerializedChildEntity, serializedChildEntity); }
@Test public void shouldSerializeEagerFetchedToOneUnauthorizedEntity() { CrysonSerializer serializer = givenCrysonSerializer(); CrysonTestChildEntity testChildEntity = new CrysonTestChildEntity(); testChildEntity.setId(100L); CrysonTestEntity unauthorizedTestEntity = new CrysonTestEntity(); unauthorizedTestEntity.setId(1L); unauthorizedTestEntity.setVersion(1L); unauthorizedTestEntity.setName("test"); unauthorizedTestEntity.setChildEntities( Sets.newLinkedHashSet(Arrays.asList(testChildEntity))); unauthorizedTestEntity.setShouldBeReadable(false); testChildEntity.setParent(unauthorizedTestEntity); String serializedEntity = serializer.serialize(testChildEntity, Sets.newHashSet("parent")); assertEquals( "{\"id\":100,\"parent\":{\"id\":1,\"crysonEntityClass\":\"CrysonTestEntity\",\"crysonUnauthorized\":true},\"crysonEntityClass\":\"CrysonTestChildEntity\"}", serializedEntity); }
@Test public void shouldSerializeEagerFetchedToManyUnauthorizedEntities() { CrysonSerializer serializer = givenCrysonSerializer(); CrysonTestChildEntity authorizedTestChildEntity = new CrysonTestChildEntity(); authorizedTestChildEntity.setId(100L); authorizedTestChildEntity.setShouldBeReadable(true); CrysonTestChildEntity unauthorizedTestChildEntity = new CrysonTestChildEntity(); unauthorizedTestChildEntity.setId(200L); unauthorizedTestChildEntity.setShouldBeReadable(false); CrysonTestEntity testEntity = new CrysonTestEntity(); testEntity.setId(1L); testEntity.setVersion(1L); testEntity.setName("test"); testEntity.setChildEntities( Sets.newLinkedHashSet( Arrays.asList(authorizedTestChildEntity, unauthorizedTestChildEntity))); String serializedEntity = serializer.serialize(testEntity, Sets.newHashSet("childEntities")); assertEquals( "{\"id\":1,\"name\":\"test\",\"version\":1,\"crysonEntityClass\":\"CrysonTestEntity\",\"doubleId\":2,\"childEntities\":[{\"id\":100,\"parent\":null,\"crysonEntityClass\":\"CrysonTestChildEntity\"},{\"id\":200,\"crysonEntityClass\":\"CrysonTestChildEntity\",\"crysonUnauthorized\":true}]}", serializedEntity); }
private static CrysonSerializer givenCrysonSerializer() { CrysonSerializer crysonSerializer = new CrysonSerializer(); ReflectionHelper reflectionHelper = new ReflectionHelper(); LazyAssociationExclusionStrategy lazyAssociationExclusionStrategy = new LazyAssociationExclusionStrategy(); UserTypeExclusionStrategy userTypeExclusionStrategy = new UserTypeExclusionStrategy(); CrysonExcludeExclusionStrategy crysonExcludeExclusionStrategy = new CrysonExcludeExclusionStrategy(); lazyAssociationExclusionStrategy.setReflectionHelper(reflectionHelper); crysonSerializer.setReflectionHelper(reflectionHelper); crysonSerializer.setLazyAssociationExclusionStrategy(lazyAssociationExclusionStrategy); crysonSerializer.setUserTypeExclusionStrategy(userTypeExclusionStrategy); crysonSerializer.setCrysonExcludeExclusionStrategy(crysonExcludeExclusionStrategy); crysonSerializer.setupGson(); return crysonSerializer; }