Skip to content

Commit

Permalink
DATAREST-662 - Polishing.
Browse files Browse the repository at this point in the history
Simplified test case. Added author tags.
  • Loading branch information
odrotbohm committed Aug 29, 2015
1 parent b7e650a commit 71cfe23
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
Expand Up @@ -22,7 +22,6 @@
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.CollectionFactory;
Expand Down Expand Up @@ -68,6 +67,7 @@
import com.fasterxml.jackson.databind.deser.std.CollectionDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerBuilder;
Expand Down Expand Up @@ -366,6 +366,7 @@ public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanD
* {@link UriToEntityConverter}.
*
* @author Oliver Gierke
* @author Valentin Rentschler
*/
static class UriStringDeserializer extends StdDeserializer<Object> {

Expand Down Expand Up @@ -414,10 +415,16 @@ public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOE
}

/**
* Deserialize by ignoring typeDeserializer, as URI will either resolve to null or concrete instance
* Deserialize by ignoring the {@link TypeDeserializer}, as URIs will either resolve to {@literal null} or a
* concrete instance anyway.
*
* @see com.fasterxml.jackson.databind.deser.std.StdDeserializer#deserializeWithType(com.fasterxml.jackson.core.JsonParser,
* com.fasterxml.jackson.databind.DeserializationContext,
* com.fasterxml.jackson.databind.jsontype.TypeDeserializer)
*/
@Override
public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt, TypeDeserializer typeDeserializer)
throws IOException {
return deserialize(jp, ctxt);
}
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,14 +23,12 @@
import java.net.URI;
import java.util.Arrays;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
Expand All @@ -40,25 +38,27 @@
import org.springframework.data.rest.core.config.ProjectionDefinitionConfiguration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.mapping.AssociationLinks;
import org.springframework.hateoas.UriTemplate;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.jayway.jsonpath.JsonPath;
import org.springframework.hateoas.UriTemplate;

/**
* Unit tests for {@link PersistentEntityJackson2Module}.
*
* @author Oliver Gierke
* @author Valentin Rentschler
*/
@RunWith(MockitoJUnitRunner.class)
public class PersistentEntityJackson2ModuleUnitTests {

@Mock AssociationLinks associationLinks;
@Mock PersistentEntities repositories;
@Mock UriToEntityConverter converter;

PersistentEntities persistentEntities;
ObjectMapper mapper;

@Before
Expand All @@ -67,19 +67,21 @@ public void setUp() {
MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.getPersistentEntity(Sample.class);
mappingContext.getPersistentEntity(SampleWithAdditionalGetters.class);
mappingContext.getPersistentEntity(PersistentEntityJackson2ModuleUnitTests.PetOwner.class);

PersistentEntities persistentEntities = new PersistentEntities(Arrays.asList(mappingContext));
this.persistentEntities = new PersistentEntities(Arrays.asList(mappingContext));

SimpleModule module = new SimpleModule();
module.setSerializerModifier(new PersistentEntityJackson2Module.AssociationOmittingSerializerModifier(
persistentEntities, associationLinks, new RepositoryRestConfiguration(new ProjectionDefinitionConfiguration(),
new MetadataConfiguration(), mock(EnumTranslationConfiguration.class))));

module.setDeserializerModifier(new PersistentEntityJackson2Module.
AssociationUriResolvingDeserializerModifier(repositories, converter, associationLinks));
module.setDeserializerModifier(new PersistentEntityJackson2Module.AssociationUriResolvingDeserializerModifier(
persistentEntities, converter, associationLinks));

this.mapper = new ObjectMapper();
this.mapper.registerModule(module);

}

/**
Expand Down Expand Up @@ -112,41 +114,34 @@ public void rendersAdditionalJsonPropertiesNotBackedByAPersistentField() throws
* @see DATAREST-662
*/
@Test
public void isAbleToResolveSubclassedProperty() throws IOException {
PersistentEntity petOwnerPersistentEntity = mock(PersistentEntity.class);
PersistentProperty petProperty = mock(PersistentProperty.class);
when(petProperty.isCollectionLike()).thenReturn(false);
when(petProperty.getActualType()).thenReturn(Pet.class);
when(petOwnerPersistentEntity.getPersistentProperty("pet")).thenReturn(petProperty);
when(repositories.getPersistentEntity(PetOwner.class)).thenReturn(petOwnerPersistentEntity);
when(associationLinks.isLinkableAssociation(petProperty)).thenReturn(true);
public void resolvesReferenceToSubtypeCorrectly() throws IOException {

PersistentProperty<?> property = persistentEntities.getPersistentEntity(PetOwner.class)
.getPersistentProperty("pet");

when(associationLinks.isLinkableAssociation(property)).thenReturn(true);
when(converter.convert(new UriTemplate("/pets/1").expand(), TypeDescriptor.valueOf(URI.class),
TypeDescriptor.valueOf(Pet.class))).thenReturn(new Cat());

PetOwner petOwner = mapper.readValue("{\"pet\":\"/pets/1\"}", PetOwner.class);

assertNotNull(petOwner);
assertNotNull(petOwner.getPet());
assertThat(petOwner, is(notNullValue()));
assertThat(petOwner.getPet(), is(notNullValue()));
}

static class PetOwner {

private Pet pet;
Pet pet;

public Pet getPet() {
return pet;
}

}

@JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.MINIMAL_CLASS)
static class Pet {

}

static class Cat extends Pet {
static class Pet {}

}
static class Cat extends Pet {}

static class Sample {
public @JsonProperty("foo") String name;
Expand Down

0 comments on commit 71cfe23

Please sign in to comment.