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

@@ -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);