DATAGEODE-17 - Adapt to API changes in mapping subsystem.

This commit is contained in:
John Blum
2017-07-09 12:57:47 -07:00
parent 1d5597afb3
commit 0907ecbe44
14 changed files with 122 additions and 115 deletions

View File

@@ -22,7 +22,6 @@ import static org.springframework.data.gemfire.util.ArrayUtils.defaultIfEmpty;
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeMap;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
import java.lang.annotation.Annotation;
import java.util.Collections;
@@ -242,9 +241,7 @@ public class EntityDefinedRegionsConfiguration
/* (non-Javadoc) */
protected GemfirePersistentEntity<?> getPersistentEntity(Class<?> persistentEntityType) {
return resolveMappingContext().getPersistentEntity(persistentEntityType).orElseThrow(
() -> newIllegalStateException("PersistentEntity for type [%s] not found", persistentEntityType));
return resolveMappingContext().getPersistentEntity(persistentEntityType);
}
/* (non-Javadoc) */
@@ -523,16 +520,16 @@ public class EntityDefinedRegionsConfiguration
/* (non-Javadoc) */
protected Class<?> resolveDomainType(GemfirePersistentEntity persistentEntity) {
return Optional.of(persistentEntity.getType()).orElse(Object.class);
return Optional.ofNullable(persistentEntity.getType()).orElse(Object.class);
}
/* (non-Javadoc) */
@SuppressWarnings("unchecked")
protected Class<?> resolveIdType(GemfirePersistentEntity persistentEntity) {
return (Class<?>) persistentEntity.getIdProperty()
return Optional.ofNullable(persistentEntity.getIdProperty())
.map(idProperty -> ((GemfirePersistentProperty) idProperty).getActualType())
.orElse(Object.class);
.orElse((Class) Object.class);
}
/* (non-Javadoc) */

View File

@@ -137,19 +137,21 @@ public class IndexConfiguration extends EntityDefinedRegionsConfiguration {
localPersistentEntity.doWithProperties((PropertyHandler<GemfirePersistentProperty>) persistentProperty -> {
Optional<Id> idAnnotation = persistentProperty.findAnnotation(Id.class);
Optional<Id> idAnnotation = Optional.ofNullable(persistentProperty.findAnnotation(Id.class));
idAnnotation.ifPresent(id ->
registerIndexBeanDefinition(enableIndexingAttributes, localPersistentEntity, persistentProperty,
IndexType.KEY, id, registry));
Optional<Indexed> indexedAnnotation = persistentProperty.findAnnotation(Indexed.class);
Optional<Indexed> indexedAnnotation =
Optional.ofNullable(persistentProperty.findAnnotation(Indexed.class));
indexedAnnotation.ifPresent(indexed ->
registerIndexBeanDefinition(enableIndexingAttributes, localPersistentEntity, persistentProperty,
indexed.type(), indexed, registry));
Optional<LuceneIndexed> luceneIndexedAnnotation = persistentProperty.findAnnotation(LuceneIndexed.class);
Optional<LuceneIndexed> luceneIndexedAnnotation =
Optional.ofNullable(persistentProperty.findAnnotation(LuceneIndexed.class));
luceneIndexedAnnotation.ifPresent(luceneIndexed ->
registerLuceneIndexBeanDefinition(enableIndexingAttributes, localPersistentEntity,

View File

@@ -25,10 +25,10 @@ import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.util.TypeInformation;
/**
@@ -149,11 +149,10 @@ public class GemfirePersistentEntity<T> extends BasicPersistentEntity<T, Gemfire
GemfirePersistentProperty property) {
if (property.isIdProperty()) {
Optional<GemfirePersistentProperty> optionalIdProperty = getIdProperty();
if (optionalIdProperty.isPresent()) {
GemfirePersistentProperty idProperty = getIdProperty().get();
GemfirePersistentProperty idProperty = getIdProperty();
if (idProperty != null) {
if (idProperty.isExplicitIdProperty()) {
if (property.isExplicitIdProperty()) {
throw new MappingException(String.format(

View File

@@ -16,8 +16,6 @@
package org.springframework.data.gemfire.mapping;
import java.util.Optional;
import org.apache.geode.pdx.PdxReader;
import org.springframework.data.mapping.model.PropertyValueProvider;
import org.springframework.util.Assert;
@@ -49,7 +47,7 @@ class GemfirePropertyValueProvider implements PropertyValueProvider<GemfirePersi
*/
@Override
@SuppressWarnings("unchecked")
public <T> Optional<T> getPropertyValue(GemfirePersistentProperty property) {
return Optional.ofNullable((T) reader.readField(property.getName()));
public <T> T getPropertyValue(GemfirePersistentProperty property) {
return (T) reader.readField(property.getName());
}
}

View File

@@ -15,11 +15,8 @@
*/
package org.springframework.data.gemfire.mapping;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -33,11 +30,11 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.convert.EntityInstantiator;
import org.springframework.data.convert.EntityInstantiators;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider;
import org.springframework.data.mapping.model.SpELContext;
import org.springframework.util.Assert;
@@ -185,21 +182,22 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw
GemfirePersistentEntity<?> entity = getPersistentEntity(type);
Object instance = getInstantiatorFor(entity).createInstance(entity,
new PersistentEntityParameterValueProvider<>(entity, new GemfirePropertyValueProvider(reader),
Optional.empty()));
new PersistentEntityParameterValueProvider<>(entity, new GemfirePropertyValueProvider(reader), null));
PersistentPropertyAccessor propertyAccessor =
new ConvertingPropertyAccessor(entity.getPropertyAccessor(instance), getConversionService());
entity.doWithProperties((PropertyHandler<GemfirePersistentProperty>) persistentProperty -> {
if (!entity.isConstructorArgument(persistentProperty)) {
PdxSerializer customSerializer = getCustomSerializer(persistentProperty.getType());
Object value = null;
try {
if (log.isDebugEnabled()) {
log.debug(String.format("setting property [%1$s] for entity [%2$s] of type [%3$s] from PDX%4$s",
log.debug(String.format("Setting property [%1$s] for entity [%2$s] of type [%3$s] from PDX%4$s",
persistentProperty.getName(), instance, type, (customSerializer != null ?
String.format(" using custom PdxSerializer [%1$s]", customSerializer) : "")));
}
@@ -209,10 +207,10 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw
: reader.readField(persistentProperty.getName()));
if (log.isDebugEnabled()) {
log.debug(String.format("with value [%1$s]", value));
log.debug(String.format("... with value [%s]", value));
}
propertyAccessor.setProperty(persistentProperty, Optional.ofNullable(value));
propertyAccessor.setProperty(persistentProperty, value);
}
catch (Exception e) {
throw new MappingException(String.format(
@@ -239,9 +237,10 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw
new ConvertingPropertyAccessor(entity.getPropertyAccessor(value), getConversionService());
entity.doWithProperties((PropertyHandler<GemfirePersistentProperty>) persistentProperty -> {
PdxSerializer customSerializer = getCustomSerializer(persistentProperty.getType());
Optional<Object> propertyValue = null;
Object propertyValue = null;
try {
propertyValue = propertyAccessor.getProperty(persistentProperty);
@@ -253,28 +252,27 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw
}
if (customSerializer != null) {
customSerializer.toData(propertyValue.orElse(null), writer);
customSerializer.toData(propertyValue, writer);
}
else {
writer.writeField(persistentProperty.getName(), propertyValue.orElse(null),
writer.writeField(persistentProperty.getName(), propertyValue,
(Class<Object>) persistentProperty.getType());
}
}
catch (Exception e) {
Object resolvedPropertyValue = (propertyValue != null ? propertyValue.orElse(null) : null);
throw new MappingException(String.format(
"While serializing entity property [%1$s] value [%2$s] of type [%3$s] to PDX%4$s",
persistentProperty.getName(), resolvedPropertyValue, value.getClass(),
persistentProperty.getName(), propertyValue, value.getClass(),
(customSerializer != null ? String.format(" using custom PdxSerializer [%1$s].",
customSerializer.getClass().getName()) : ".")), e);
}
});
Optional<GemfirePersistentProperty> idProperty = entity.getIdProperty();
GemfirePersistentProperty idProperty = entity.getIdProperty();
idProperty.ifPresent(gemfirePersistentProperty ->
writer.markIdentityField(gemfirePersistentProperty.getName()));
if (idProperty != null) {
writer.markIdentityField(idProperty.getName());
}
return true;
}
@@ -326,7 +324,6 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw
* @see #getMappingContext()
*/
protected GemfirePersistentEntity<?> getPersistentEntity(Class<?> entityType) {
return getMappingContext().getPersistentEntity(entityType).orElseThrow(
() -> newIllegalArgumentException("Unable to resolve PersistentEntity for type [%]", entityType));
return getMappingContext().getPersistentEntity(entityType);
}
}

View File

@@ -16,12 +16,11 @@
package org.springframework.data.gemfire.mapping;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import org.apache.geode.cache.Region;
import org.springframework.data.mapping.context.MappingContext;
@@ -74,10 +73,11 @@ public class Regions implements Iterable<Region<?, ?>> {
*/
@SuppressWarnings("unchecked")
public <T> Region<?, T> getRegion(Class<T> entityType) {
Assert.notNull(entityType, "Entity type must not be null");
String regionName = this.mappingContext.getPersistentEntity(entityType).map((entity) -> entity.getRegionName())
.orElseGet(entityType::getSimpleName);
String regionName = Optional.ofNullable(this.mappingContext.getPersistentEntity(entityType))
.map(entity -> entity.getRegionName()).orElseGet(entityType::getSimpleName);
return (Region<?, T>) this.regions.get(regionName);
}
@@ -92,6 +92,7 @@ public class Regions implements Iterable<Region<?, ?>> {
*/
@SuppressWarnings("unchecked")
public <S, T> Region<S, T> getRegion(String namePath) {
Assert.hasText(namePath, "Region name/path is required");
return (Region<S, T>) this.regions.get(namePath);

View File

@@ -67,9 +67,8 @@ public class GemfireQueryMethod extends QueryMethod {
assertNonPagingQueryMethod(method);
this.method = method;
this.entity = mappingContext.getPersistentEntity(getDomainClass()).orElseThrow(
() -> new IllegalArgumentException(String.format("Failed to resolve PersistentEntity for type [%s]",
getDomainClass())));
this.entity = mappingContext.getPersistentEntity(getDomainClass());
}
/**

View File

@@ -16,7 +16,6 @@
package org.springframework.data.gemfire.repository.support;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
import java.io.Serializable;
@@ -84,9 +83,8 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
@SuppressWarnings("unchecked")
public <T, ID> GemfireEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
GemfirePersistentEntity<T> entity = (GemfirePersistentEntity<T>) mappingContext.getPersistentEntity(domainClass)
.orElseThrow(() -> newIllegalArgumentException("Unable to resolve PersistentEntity for type [%s]",
domainClass));
GemfirePersistentEntity<T> entity =
(GemfirePersistentEntity<T>) mappingContext.getPersistentEntity(domainClass);
return new DefaultGemfireEntityInformation<>(entity);
}
@@ -108,9 +106,7 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
GemfireTemplate getTemplate(RepositoryMetadata metadata) {
GemfirePersistentEntity<?> entity = mappingContext.getPersistentEntity(metadata.getDomainType())
.orElseThrow(() -> newIllegalArgumentException("Unable to resolve PersistentEntity for type [%s]",
metadata.getDomainType()));
GemfirePersistentEntity<?> entity = mappingContext.getPersistentEntity(metadata.getDomainType());
String entityRegionName = entity.getRegionName();
String repositoryRegionName = getRepositoryRegionName(metadata.getRepositoryInterface());

View File

@@ -16,18 +16,16 @@
package org.springframework.data.gemfire.mapping;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.*;
import lombok.Data;
import static org.assertj.core.api.Assertions.assertThat;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Optional;
import org.junit.Test;
import org.springframework.data.gemfire.mapping.annotation.Region;
import lombok.Data;
/**
* Unit tests for {@link GemfireMappingContext} class.
*
@@ -43,32 +41,29 @@ public class GemfireMappingContextUnitTests {
@Test
@SuppressWarnings("unchecked")
public void getPersistentEntityForPerson() throws Exception {
GemfirePersistentEntity<Person> personPersistentEntity =
(GemfirePersistentEntity<Person>) mappingContext.getPersistentEntity(Person.class).orElseThrow(
() -> newIllegalStateException("Unable to resolve PersistentEntity for type [%s]",
Person.class.getName()));
(GemfirePersistentEntity<Person>) mappingContext.getPersistentEntity(Person.class);
assertThat(personPersistentEntity).isNotNull();
assertThat(personPersistentEntity.getRegionName()).isEqualTo("People");
Optional<GemfirePersistentProperty> namePersistentProperty =
personPersistentEntity.getPersistentProperty("name");
GemfirePersistentProperty namePersistentProperty = personPersistentEntity.getPersistentProperty("name");
assertThat(namePersistentProperty.isPresent()).isTrue();
assertThat(namePersistentProperty.get().isEntity()).isFalse();
assertThat(namePersistentProperty.get().getName()).isEqualTo("name");
assertThat(namePersistentProperty.get().getOwner()).isEqualTo(personPersistentEntity);
assertThat(namePersistentProperty).isNotNull();
assertThat(namePersistentProperty.isEntity()).isFalse();
assertThat(namePersistentProperty.getName()).isEqualTo("name");
assertThat(namePersistentProperty.getOwner()).isEqualTo(personPersistentEntity);
}
@Test
public void getPersistentEntityForBigDecimal() {
assertThat(mappingContext.getPersistentEntity(BigDecimal.class).isPresent()).isFalse();
assertThat(mappingContext.getPersistentEntity(BigDecimal.class)).isNull();
}
@Test
public void getPersistentEntityForBigInteger() {
assertThat(mappingContext.getPersistentEntity(BigInteger.class).isPresent()).isFalse();
assertThat(mappingContext.getPersistentEntity(BigInteger.class)).isNull();
}
@Data

View File

@@ -19,11 +19,9 @@ package org.springframework.data.gemfire.mapping;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Optional;
import org.junit.Rule;
import org.junit.Test;
@@ -31,7 +29,7 @@ import org.junit.rules.ExpectedException;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.annotation.Region;
import org.springframework.data.mapping.IdentifierAccessor;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.util.ClassTypeInformation;
/**
@@ -62,8 +60,7 @@ public class GemfirePersistentEntityUnitTests {
@SuppressWarnings("unchecked")
protected <T> GemfirePersistentEntity<T> getMappingContextPersistentEntity(Class<T> type) {
return (GemfirePersistentEntity<T>) this.mappingContext.getPersistentEntity(type).orElseThrow(
() -> newIllegalStateException("Unable to resolve PersistentEntity for type [%s]", type));
return (GemfirePersistentEntity<T>) this.mappingContext.getPersistentEntity(type);
}
protected <T> GemfirePersistentEntity<T> newPersistentEntity(Class<T> type) {
@@ -96,25 +93,26 @@ public class GemfirePersistentEntityUnitTests {
assertThat(entity).isNotNull();
assertThat(entity.getRegionName()).isEqualTo("Example");
Optional<GemfirePersistentProperty> currency = entity.getPersistentProperty("currency");
GemfirePersistentProperty currency = entity.getPersistentProperty("currency");
assertThat(currency.isPresent()).isTrue();
assertThat(currency.get().isEntity()).isFalse();
assertThat(currency).isNotNull();
assertThat(currency.isEntity()).isFalse();
}
@Test
@SuppressWarnings("unchecked")
public void bigIntegerPersistentPropertyIsNotAnEntity() {
GemfirePersistentEntity<ExampleDomainObject> entity =
getMappingContextPersistentEntity(ExampleDomainObject.class);
assertThat(entity).isNotNull();
assertThat(entity.getRegionName()).isEqualTo("Example");
Optional<GemfirePersistentProperty> bigNumber = entity.getPersistentProperty("bigNumber");
GemfirePersistentProperty bigNumber = entity.getPersistentProperty("bigNumber");
assertThat(bigNumber.isPresent()).isTrue();
assertThat(bigNumber.get().isEntity()).isFalse();
assertThat(bigNumber).isNotNull();
assertThat(bigNumber.isEntity()).isFalse();
}
/**
@@ -122,9 +120,10 @@ public class GemfirePersistentEntityUnitTests {
*/
@Test
public void identifierForNonIdAnnotatedEntityWithNoIdFieldOrPropertyIsNull() {
IdentifierAccessor identifierAccessor = getIdentifierAccessor(new NonRegionAnnotatedEntity());
assertThat(identifierAccessor.getIdentifier().orElse(null)).isNull();
assertThat(identifierAccessor.getIdentifier()).isNull();
}
/**
@@ -132,9 +131,10 @@ public class GemfirePersistentEntityUnitTests {
*/
@Test
public void identifierForNonIdAnnotatedEntityWithIdFieldIsNotNull() {
IdentifierAccessor identifierAccessor = getIdentifierAccessor(new NonIdAnnotatedIdFieldEntity());
assertThat(identifierAccessor.getIdentifier().orElse(null)).isEqualTo(123L);
assertThat(identifierAccessor.getIdentifier()).isEqualTo(123L);
}
/**
@@ -142,22 +142,25 @@ public class GemfirePersistentEntityUnitTests {
*/
@Test
public void identifierForNonIdAnnotatedEntityWithIdPropertyIsNotNull() {
IdentifierAccessor identifierAccessor = getIdentifierAccessor(new NonIdAnnotatedIdGetterEntity());
assertThat(identifierAccessor.getIdentifier().orElse(null)).isEqualTo(456L);
assertThat(identifierAccessor.getIdentifier()).isEqualTo(456L);
}
@Test
public void identifierForIdAnnotatedFieldAndPropertyEntityShouldNotConflict() {
IdentifierAccessor identifierAccessor = getIdentifierAccessor(new IdAnnotatedFieldAndPropertyEntity());
assertThat(identifierAccessor.getIdentifier().orElse(null)).isEqualTo(1L);
assertThat(identifierAccessor.getIdentifier()).isEqualTo(1L);
}
@Test
public void identifierForAmbiguousIdAnnotatedFieldAndIdAnnotatedPropertyEntityThrowsMappingException() {
AmbiguousIdAnnotatedFieldAndIdAnnotatedPropertyEntity entity
= new AmbiguousIdAnnotatedFieldAndIdAnnotatedPropertyEntity();
AmbiguousIdAnnotatedFieldAndIdAnnotatedPropertyEntity entity =
new AmbiguousIdAnnotatedFieldAndIdAnnotatedPropertyEntity();
String expectedMessage = String.format("Attempt to add explicit id property [ssn] but already have id property [id] registered as explicit;"
+ " Please check your object [%s] mapping configuration", entity.getClass().getName());

View File

@@ -28,7 +28,6 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
import java.util.Collections;
@@ -47,7 +46,7 @@ import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.EntityInstantiator;
import org.springframework.data.gemfire.repository.sample.Address;
import org.springframework.data.gemfire.repository.sample.Person;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.model.ParameterValueProvider;
/**
@@ -78,7 +77,7 @@ public class MappingPdxSerializerUnitTests {
MappingPdxSerializer serializer;
@Rule
public ExpectedException expectedException = ExpectedException.none();
public ExpectedException exception = ExpectedException.none();
@Mock
EntityInstantiator mockInstantiator;
@@ -102,6 +101,7 @@ public class MappingPdxSerializerUnitTests {
@Test
public void createFullyInitialized() {
ConversionService mockConversionService = mock(ConversionService.class);
GemfireMappingContext mockMappingContext = mock(GemfireMappingContext.class);
@@ -114,6 +114,7 @@ public class MappingPdxSerializerUnitTests {
@Test
public void createWithNullConversionService() {
GemfireMappingContext mockMappingContext = mock(GemfireMappingContext.class);
MappingPdxSerializer pdxSerializer = MappingPdxSerializer.create(mockMappingContext, null);
@@ -125,6 +126,7 @@ public class MappingPdxSerializerUnitTests {
@Test
public void createWithNullMappingContext() {
ConversionService mockConversionService = mock(ConversionService.class);
MappingPdxSerializer pdxSerializer = MappingPdxSerializer.create(null, mockConversionService);
@@ -136,6 +138,7 @@ public class MappingPdxSerializerUnitTests {
@Test
public void createWithNullConversionServiceAndNullMappingContext() {
MappingPdxSerializer pdxSerializer = MappingPdxSerializer.create(null, null);
assertThat(pdxSerializer).isNotNull();
@@ -146,11 +149,14 @@ public class MappingPdxSerializerUnitTests {
@Test
@SuppressWarnings("unchecked")
public void usesRegisteredInstantiator() {
Address address = new Address();
address.city = "London";
address.zipCode = "01234";
Person person = new Person(1L, "Oliver", "Gierke");
person.address = address;
when(mockInstantiator.createInstance(any(GemfirePersistentEntity.class), any(ParameterValueProvider.class)))
@@ -160,18 +166,18 @@ public class MappingPdxSerializerUnitTests {
serializer.fromData(Person.class, mockReader);
GemfirePersistentEntity<?> persistentEntity = context.getPersistentEntity(Person.class)
.orElseThrow(() -> newIllegalStateException("Unable to resolve PersistentEntity for type [%s]",
Person.class.getName()));
GemfirePersistentEntity<?> persistentEntity = context.getPersistentEntity(Person.class);
verify(mockInstantiator, times(1)).createInstance(eq(persistentEntity),
any(ParameterValueProvider.class));
verify(mockAddressSerializer, times(1)).fromData(eq(Address.class), any(PdxReader.class));
}
@Test
@SuppressWarnings("unchecked")
public void fromDataMapsPdxDataToApplicationDomainObject() {
Address expectedAddress = new Address();
expectedAddress.city = "Portland";
@@ -207,6 +213,7 @@ public class MappingPdxSerializerUnitTests {
@Test
@SuppressWarnings("unchecked")
public void fromDataHandlesExceptionProperly() {
when(mockInstantiator.createInstance(any(GemfirePersistentEntity.class), any(ParameterValueProvider.class)))
.thenReturn(new Person(null, null, null));
@@ -215,9 +222,9 @@ public class MappingPdxSerializerUnitTests {
serializer.setGemfireInstantiators(Collections.singletonMap(Person.class, mockInstantiator));
try {
expectedException.expect(MappingException.class);
expectedException.expectCause(isA(IllegalArgumentException.class));
expectedException.expectMessage(String.format(
exception.expect(MappingException.class);
exception.expectCause(isA(IllegalArgumentException.class));
exception.expectMessage(String.format(
"While setting value [null] of property [id] for entity of type [%s] from PDX", Person.class));
serializer.fromData(Person.class, mockReader);
@@ -225,17 +232,21 @@ public class MappingPdxSerializerUnitTests {
finally {
verify(mockInstantiator, times(1))
.createInstance(any(GemfirePersistentEntity.class), any(ParameterValueProvider.class));
verify(mockReader, times(1)).readField(eq("id"));
}
}
@Test
public void toDataSerializesApplicationDomainObjectToPdx() {
Address address = new Address();
address.city = "Portland";
address.zipCode = "12345";
Person jonDoe = new Person(1L, "Jon", "Doe");
jonDoe.address = address;
serializer.setCustomSerializers(Collections.singletonMap(Address.class, mockAddressSerializer));
@@ -251,20 +262,23 @@ public class MappingPdxSerializerUnitTests {
@Test
public void toDataHandlesExceptionProperly() {
Address address = new Address();
address.city = "Portland";
address.zipCode = "12345";
Person jonDoe = new Person(1L, "Jon", "Doe");
jonDoe.address = address;
when(mockWriter.writeField(eq("address"), eq(address), eq(Address.class)))
.thenThrow(newIllegalArgumentException("test"));
try {
expectedException.expect(MappingException.class);
expectedException.expectCause(isA(IllegalArgumentException.class));
expectedException.expectMessage(String.format(
exception.expect(MappingException.class);
exception.expectCause(isA(IllegalArgumentException.class));
exception.expectMessage(String.format(
"While serializing entity property [address] value [Portland, 12345] of type [%s] to PDX",
Person.class));

View File

@@ -27,7 +27,6 @@ import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.apache.geode.cache.Region;
import org.junit.After;
@@ -101,30 +100,34 @@ public class RegionsTest {
@Test
public void getRegionByEntityTypeReturnsRegionForEntityRegionName() {
GemfirePersistentEntity<User> mockPersistentEntity = mock(GemfirePersistentEntity.class);
when(mockPersistentEntity.getRegionName()).thenReturn("Users");
when(mockMappingContext.getPersistentEntity(eq(User.class))).thenReturn(Optional.of(mockPersistentEntity));
when(mockMappingContext.getPersistentEntity(eq(User.class))).thenReturn(mockPersistentEntity);
assertThat(regions.getRegion(User.class)).isEqualTo(mockUsers);
}
@Test
public void getRegionByEntityTypeReturnsRegionForEntityTypeSimpleName() {
when(mockMappingContext.getPersistentEntity(any(Class.class))).thenReturn(Optional.empty());
when(mockMappingContext.getPersistentEntity(any(Class.class))).thenReturn(null);
assertThat(regions.getRegion(Users.class)).isEqualTo(mockUsers);
}
@Test
public void getRegionByEntityTypeReturnsNull() {
when(mockMappingContext.getPersistentEntity(any(Class.class))).thenReturn(Optional.empty());
when(mockMappingContext.getPersistentEntity(any(Class.class))).thenReturn(null);
assertThat(regions.getRegion(Object.class)).isNull();
}
@Test
public void getRegionWithNullEntityTypeThrowsIllegalArgumentException() {
exception.expect(IllegalArgumentException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage("Entity type must not be null");
@@ -158,6 +161,7 @@ public class RegionsTest {
@Test
public void getRegionWithNullNameNullPathThrowsIllegalArgumentException() {
exception.expect(IllegalArgumentException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage("Region name/path is required");
@@ -167,6 +171,7 @@ public class RegionsTest {
@Test
public void iterateRegions() {
List<Region> actualRegions = new ArrayList<>(3);
for (Region region : regions) {

View File

@@ -20,7 +20,6 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
import java.io.Serializable;
@@ -69,12 +68,12 @@ public class DefaultGemfireEntityInformationTest {
@SuppressWarnings("unchecked")
protected <T> GemfirePersistentEntity<T> newPersistentEntity(Class<T> entityType) {
return (GemfirePersistentEntity<T>) mappingContext.getPersistentEntity(entityType).orElseThrow(
() -> newIllegalStateException("Unable to resolve PersistentEntity for type [%s]", entityType));
return (GemfirePersistentEntity<T>) mappingContext.getPersistentEntity(entityType);
}
@Test
public void interfaceBasedEntity() {
GemfireEntityInformation<Algorithm, String> entityInfo =
newEntityInformation(newPersistentEntity(Algorithm.class));
@@ -82,12 +81,13 @@ public class DefaultGemfireEntityInformationTest {
assertEquals("Algorithms", entityInfo.getRegionName());
assertTrue(Algorithm.class.isAssignableFrom(entityInfo.getJavaType()));
assertEquals(String.class, entityInfo.getIdType());
assertThat(entityInfo.getId(new QuickSort()).orElse(null)).isEqualTo("QuickSort");
assertThat(entityInfo.getId(newAlgorithm("Quick Sort")).orElse(null)).isEqualTo("Quick Sort");
assertThat(entityInfo.getId(new QuickSort())).isEqualTo("QuickSort");
assertThat(entityInfo.getId(newAlgorithm("Quick Sort"))).isEqualTo("Quick Sort");
}
@Test
public void classBasedEntity() {
GemfireEntityInformation<Animal, Long> entityInfo =
newEntityInformation(newPersistentEntity(Animal.class));
@@ -95,11 +95,12 @@ public class DefaultGemfireEntityInformationTest {
assertEquals("Animal", entityInfo.getRegionName());
assertEquals(Animal.class, entityInfo.getJavaType());
assertEquals(Long.class, entityInfo.getIdType());
assertThat(entityInfo.getId(newAnimal(1L, "Tyger")).orElse(null)).isEqualTo(1L);
assertThat(entityInfo.getId(newAnimal(1L, "Tyger"))).isEqualTo(1L);
}
@Test
public void confusedDomainEntityTypedWithLongId() {
GemfireEntityInformation<ConfusedDomainEntity, Long> entityInfo =
newEntityInformation(newPersistentEntity(ConfusedDomainEntity.class));
@@ -107,12 +108,13 @@ public class DefaultGemfireEntityInformationTest {
assertEquals("ConfusedDomainEntity", entityInfo.getRegionName());
assertEquals(ConfusedDomainEntity.class, entityInfo.getJavaType());
assertEquals(Long.class, entityInfo.getIdType());
assertThat(entityInfo.getId(new ConfusedDomainEntity(123L)).orElse(null)).isEqualTo(123L);
assertThat(entityInfo.getId(new ConfusedDomainEntity(123L))).isEqualTo(123L);
}
@Test
@SuppressWarnings("all")
public void confusedDomainEntityTypedStringId() {
GemfireEntityInformation<ConfusedDomainEntity, ?> entityInfo =
newEntityInformation(newPersistentEntity(ConfusedDomainEntity.class));
@@ -121,8 +123,8 @@ public class DefaultGemfireEntityInformationTest {
assertEquals(ConfusedDomainEntity.class, entityInfo.getJavaType());
//assertEquals(String.class, entityInfo.getIdType());
assertTrue(Long.class.equals(entityInfo.getIdType()));
assertThat(entityInfo.getId(new ConfusedDomainEntity(123L)).orElse(null)).isEqualTo(123L);
assertThat(entityInfo.getId(new ConfusedDomainEntity("248")).orElse(null)).isEqualTo(248L);
assertThat(entityInfo.getId(new ConfusedDomainEntity(123L))).isEqualTo(123L);
assertThat(entityInfo.getId(new ConfusedDomainEntity("248"))).isEqualTo(248L);
}
@SuppressWarnings("unused")

View File

@@ -18,7 +18,6 @@ package org.springframework.data.gemfire.repository.query;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
import org.junit.Before;
import org.junit.Test;
@@ -40,13 +39,12 @@ public class GemfireQueryCreatorUnitTests {
@Before
@SuppressWarnings("unchecked")
public void setUp() {
entity = (GemfirePersistentEntity<Person>) new GemfireMappingContext().getPersistentEntity(Person.class)
.orElseThrow(() -> newIllegalStateException("Unable to resolve PersistentEntity for type [%s]",
Person.class));
entity = (GemfirePersistentEntity<Person>) new GemfireMappingContext().getPersistentEntity(Person.class);
}
@Test
public void createsQueryForSimplePropertyReferenceCorrectly() {
PartTree partTree = new PartTree("findByLastname", Person.class);
GemfireQueryCreator queryCreator = new GemfireQueryCreator(partTree, entity);
@@ -58,6 +56,7 @@ public class GemfireQueryCreatorUnitTests {
@Test
public void createsQueryForNestedPropertyReferenceCorrectly() {
PartTree partTree = new PartTree("findPersonByAddressCity", Person.class);
GemfireQueryCreator queryCreator = new GemfireQueryCreator(partTree, entity);