Search in sources :

Example 1 with EntityProjectionIntrospector

use of org.springframework.data.projection.EntityProjectionIntrospector in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterUnitTests method projectShouldReadNestedProjection.

// GH-2860
@Test
void projectShouldReadNestedProjection() {
    org.bson.Document source = new org.bson.Document("addresses", Collections.singletonList(new org.bson.Document("s", "hwy")));
    EntityProjectionIntrospector introspector = EntityProjectionIntrospector.create(converter.getProjectionFactory(), EntityProjectionIntrospector.ProjectionPredicate.typeHierarchy().and((target, underlyingType) -> !converter.conversions.isSimpleType(target)), mappingContext);
    EntityProjection<WithNestedProjection, Person> projection = introspector.introspect(WithNestedProjection.class, Person.class);
    WithNestedProjection person = converter.project(projection, source);
    assertThat(person.getAddresses()).extracting(AddressProjection::getStreet).hasSize(1).containsOnly("hwy");
}
Also used : EntityProjectionIntrospector(org.springframework.data.projection.EntityProjectionIntrospector) BeforeEach(org.junit.jupiter.api.BeforeEach) MappingInstantiationException(org.springframework.data.mapping.model.MappingInstantiationException) EntityCallbacks(org.springframework.data.mapping.callback.EntityCallbacks) ProjectingType(org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.ProjectingType) CustomConversions(org.springframework.data.convert.CustomConversions) Metrics(org.springframework.data.geo.Metrics) NestedType(org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.NestedType) URL(java.net.URL) EntityProjectionIntrospector(org.springframework.data.projection.EntityProjectionIntrospector) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Shape(org.springframework.data.geo.Shape) AfterConvertCallback(org.springframework.data.mongodb.core.mapping.event.AfterConvertCallback) Unwrapped(org.springframework.data.mongodb.core.mapping.Unwrapped) Disabled(org.junit.jupiter.api.Disabled) Box(org.springframework.data.geo.Box) EntityProjection(org.springframework.data.projection.EntityProjection) BasicDBList(com.mongodb.BasicDBList) Document(org.springframework.data.mongodb.core.mapping.Document) BigDecimal(java.math.BigDecimal) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) DBObject(com.mongodb.DBObject) Assertions(org.assertj.core.api.Assertions) BigInteger(java.math.BigInteger) Transient(org.springframework.data.annotation.Transient) Point(org.springframework.data.geo.Point) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TypeAlias(org.springframework.data.annotation.TypeAlias) EqualsAndHashCode(lombok.EqualsAndHashCode) ZoneId(java.time.ZoneId) Test(org.junit.jupiter.api.Test) Code(org.bson.types.Code) NonNull(org.springframework.lang.NonNull) Id(org.springframework.data.annotation.Id) java.util(java.util) WritingConverter(org.springframework.data.convert.WritingConverter) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Getter(lombok.Getter) PersistenceConstructor(org.springframework.data.annotation.PersistenceConstructor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Mock(org.mockito.Mock) LocalDateTime(java.time.LocalDateTime) Distance(org.springframework.data.geo.Distance) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) Value(org.springframework.beans.factory.annotation.Value) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MappingException(org.springframework.data.mapping.MappingException) Sphere(org.springframework.data.mongodb.core.geo.Sphere) Nullable(org.springframework.lang.Nullable) TextScore(org.springframework.data.mongodb.core.mapping.TextScore) Decimal128(org.bson.types.Decimal128) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) Converter(org.springframework.core.convert.converter.Converter) Polygon(org.springframework.data.geo.Polygon) Binary(org.bson.types.Binary) FooBarEnum(org.springframework.data.mongodb.core.convert.MappingMongoConverterUnitTests.ClassWithMapUsingEnumAsKey.FooBarEnum) BasicDBObject(com.mongodb.BasicDBObject) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) ApplicationContext(org.springframework.context.ApplicationContext) ReadingConverter(org.springframework.data.convert.ReadingConverter) Field(org.springframework.data.mongodb.core.mapping.Field) LocalDate(org.joda.time.LocalDate) Mockito(org.mockito.Mockito) Circle(org.springframework.data.geo.Circle) ChronoUnit(java.time.temporal.ChronoUnit) PersonPojoStringId(org.springframework.data.mongodb.core.mapping.PersonPojoStringId) DBRef(com.mongodb.DBRef) ProxyFactory(org.springframework.aop.framework.ProxyFactory) ObjectId(org.bson.types.ObjectId) FieldType(org.springframework.data.mongodb.core.mapping.FieldType) DocumentTestUtils(org.springframework.data.mongodb.core.DocumentTestUtils) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 2 with EntityProjectionIntrospector

use of org.springframework.data.projection.EntityProjectionIntrospector in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterUnitTests method projectShouldReadSimpleDtoProjection.

// GH-2860
@Test
void projectShouldReadSimpleDtoProjection() {
    org.bson.Document source = new org.bson.Document("birthDate", new LocalDate(1999, 12, 1).toDate()).append("foo", "Walter");
    EntityProjectionIntrospector introspector = EntityProjectionIntrospector.create(converter.getProjectionFactory(), EntityProjectionIntrospector.ProjectionPredicate.typeHierarchy().and((target, underlyingType) -> !converter.conversions.isSimpleType(target)), mappingContext);
    EntityProjection<PersonDto, Person> projection = introspector.introspect(PersonDto.class, Person.class);
    PersonDto person = converter.project(projection, source);
    assertThat(person.getBirthDate()).isEqualTo(new LocalDate(1999, 12, 1));
    assertThat(person.getFirstname()).isEqualTo("Walter");
}
Also used : EntityProjectionIntrospector(org.springframework.data.projection.EntityProjectionIntrospector) BeforeEach(org.junit.jupiter.api.BeforeEach) MappingInstantiationException(org.springframework.data.mapping.model.MappingInstantiationException) EntityCallbacks(org.springframework.data.mapping.callback.EntityCallbacks) ProjectingType(org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.ProjectingType) CustomConversions(org.springframework.data.convert.CustomConversions) Metrics(org.springframework.data.geo.Metrics) NestedType(org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.NestedType) URL(java.net.URL) EntityProjectionIntrospector(org.springframework.data.projection.EntityProjectionIntrospector) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Shape(org.springframework.data.geo.Shape) AfterConvertCallback(org.springframework.data.mongodb.core.mapping.event.AfterConvertCallback) Unwrapped(org.springframework.data.mongodb.core.mapping.Unwrapped) Disabled(org.junit.jupiter.api.Disabled) Box(org.springframework.data.geo.Box) EntityProjection(org.springframework.data.projection.EntityProjection) BasicDBList(com.mongodb.BasicDBList) Document(org.springframework.data.mongodb.core.mapping.Document) BigDecimal(java.math.BigDecimal) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) DBObject(com.mongodb.DBObject) Assertions(org.assertj.core.api.Assertions) BigInteger(java.math.BigInteger) Transient(org.springframework.data.annotation.Transient) Point(org.springframework.data.geo.Point) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TypeAlias(org.springframework.data.annotation.TypeAlias) EqualsAndHashCode(lombok.EqualsAndHashCode) ZoneId(java.time.ZoneId) Test(org.junit.jupiter.api.Test) Code(org.bson.types.Code) NonNull(org.springframework.lang.NonNull) Id(org.springframework.data.annotation.Id) java.util(java.util) WritingConverter(org.springframework.data.convert.WritingConverter) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Getter(lombok.Getter) PersistenceConstructor(org.springframework.data.annotation.PersistenceConstructor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Mock(org.mockito.Mock) LocalDateTime(java.time.LocalDateTime) Distance(org.springframework.data.geo.Distance) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) Value(org.springframework.beans.factory.annotation.Value) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MappingException(org.springframework.data.mapping.MappingException) Sphere(org.springframework.data.mongodb.core.geo.Sphere) Nullable(org.springframework.lang.Nullable) TextScore(org.springframework.data.mongodb.core.mapping.TextScore) Decimal128(org.bson.types.Decimal128) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) Converter(org.springframework.core.convert.converter.Converter) Polygon(org.springframework.data.geo.Polygon) Binary(org.bson.types.Binary) FooBarEnum(org.springframework.data.mongodb.core.convert.MappingMongoConverterUnitTests.ClassWithMapUsingEnumAsKey.FooBarEnum) BasicDBObject(com.mongodb.BasicDBObject) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) ApplicationContext(org.springframework.context.ApplicationContext) ReadingConverter(org.springframework.data.convert.ReadingConverter) Field(org.springframework.data.mongodb.core.mapping.Field) LocalDate(org.joda.time.LocalDate) Mockito(org.mockito.Mockito) Circle(org.springframework.data.geo.Circle) ChronoUnit(java.time.temporal.ChronoUnit) PersonPojoStringId(org.springframework.data.mongodb.core.mapping.PersonPojoStringId) DBRef(com.mongodb.DBRef) ProxyFactory(org.springframework.aop.framework.ProxyFactory) ObjectId(org.bson.types.ObjectId) FieldType(org.springframework.data.mongodb.core.mapping.FieldType) DocumentTestUtils(org.springframework.data.mongodb.core.DocumentTestUtils) LocalDate(org.joda.time.LocalDate) Test(org.junit.jupiter.api.Test)

Example 3 with EntityProjectionIntrospector

use of org.springframework.data.projection.EntityProjectionIntrospector in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterUnitTests method projectShouldReadProjectionWithNestedEntity.

// GH-2860
@Test
void projectShouldReadProjectionWithNestedEntity() {
    org.bson.Document source = new org.bson.Document("addresses", Collections.singletonList(new org.bson.Document("s", "hwy")));
    EntityProjectionIntrospector introspector = EntityProjectionIntrospector.create(converter.getProjectionFactory(), EntityProjectionIntrospector.ProjectionPredicate.typeHierarchy().and((target, underlyingType) -> !converter.conversions.isSimpleType(target)), mappingContext);
    EntityProjection<ProjectionWithNestedEntity, Person> projection = introspector.introspect(ProjectionWithNestedEntity.class, Person.class);
    ProjectionWithNestedEntity person = converter.project(projection, source);
    assertThat(person.getAddresses()).extracting(Address::getStreet).hasSize(1).containsOnly("hwy");
}
Also used : EntityProjectionIntrospector(org.springframework.data.projection.EntityProjectionIntrospector) BeforeEach(org.junit.jupiter.api.BeforeEach) MappingInstantiationException(org.springframework.data.mapping.model.MappingInstantiationException) EntityCallbacks(org.springframework.data.mapping.callback.EntityCallbacks) ProjectingType(org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.ProjectingType) CustomConversions(org.springframework.data.convert.CustomConversions) Metrics(org.springframework.data.geo.Metrics) NestedType(org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.NestedType) URL(java.net.URL) EntityProjectionIntrospector(org.springframework.data.projection.EntityProjectionIntrospector) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Shape(org.springframework.data.geo.Shape) AfterConvertCallback(org.springframework.data.mongodb.core.mapping.event.AfterConvertCallback) Unwrapped(org.springframework.data.mongodb.core.mapping.Unwrapped) Disabled(org.junit.jupiter.api.Disabled) Box(org.springframework.data.geo.Box) EntityProjection(org.springframework.data.projection.EntityProjection) BasicDBList(com.mongodb.BasicDBList) Document(org.springframework.data.mongodb.core.mapping.Document) BigDecimal(java.math.BigDecimal) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) DBObject(com.mongodb.DBObject) Assertions(org.assertj.core.api.Assertions) BigInteger(java.math.BigInteger) Transient(org.springframework.data.annotation.Transient) Point(org.springframework.data.geo.Point) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TypeAlias(org.springframework.data.annotation.TypeAlias) EqualsAndHashCode(lombok.EqualsAndHashCode) ZoneId(java.time.ZoneId) Test(org.junit.jupiter.api.Test) Code(org.bson.types.Code) NonNull(org.springframework.lang.NonNull) Id(org.springframework.data.annotation.Id) java.util(java.util) WritingConverter(org.springframework.data.convert.WritingConverter) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Getter(lombok.Getter) PersistenceConstructor(org.springframework.data.annotation.PersistenceConstructor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Mock(org.mockito.Mock) LocalDateTime(java.time.LocalDateTime) Distance(org.springframework.data.geo.Distance) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) Value(org.springframework.beans.factory.annotation.Value) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MappingException(org.springframework.data.mapping.MappingException) Sphere(org.springframework.data.mongodb.core.geo.Sphere) Nullable(org.springframework.lang.Nullable) TextScore(org.springframework.data.mongodb.core.mapping.TextScore) Decimal128(org.bson.types.Decimal128) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) Converter(org.springframework.core.convert.converter.Converter) Polygon(org.springframework.data.geo.Polygon) Binary(org.bson.types.Binary) FooBarEnum(org.springframework.data.mongodb.core.convert.MappingMongoConverterUnitTests.ClassWithMapUsingEnumAsKey.FooBarEnum) BasicDBObject(com.mongodb.BasicDBObject) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) ApplicationContext(org.springframework.context.ApplicationContext) ReadingConverter(org.springframework.data.convert.ReadingConverter) Field(org.springframework.data.mongodb.core.mapping.Field) LocalDate(org.joda.time.LocalDate) Mockito(org.mockito.Mockito) Circle(org.springframework.data.geo.Circle) ChronoUnit(java.time.temporal.ChronoUnit) PersonPojoStringId(org.springframework.data.mongodb.core.mapping.PersonPojoStringId) DBRef(com.mongodb.DBRef) ProxyFactory(org.springframework.aop.framework.ProxyFactory) ObjectId(org.bson.types.ObjectId) FieldType(org.springframework.data.mongodb.core.mapping.FieldType) DocumentTestUtils(org.springframework.data.mongodb.core.DocumentTestUtils) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 4 with EntityProjectionIntrospector

use of org.springframework.data.projection.EntityProjectionIntrospector in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterUnitTests method projectShouldReadSimpleInterfaceProjection.

// GH-2860
@Test
void projectShouldReadSimpleInterfaceProjection() {
    org.bson.Document source = new org.bson.Document("birthDate", new LocalDate(1999, 12, 1).toDate()).append("foo", "Walter");
    EntityProjectionIntrospector discoverer = EntityProjectionIntrospector.create(converter.getProjectionFactory(), EntityProjectionIntrospector.ProjectionPredicate.typeHierarchy().and((target, underlyingType) -> !converter.conversions.isSimpleType(target)), mappingContext);
    EntityProjection<PersonProjection, Person> projection = discoverer.introspect(PersonProjection.class, Person.class);
    PersonProjection person = converter.project(projection, source);
    assertThat(person.getBirthDate()).isEqualTo(new LocalDate(1999, 12, 1));
    assertThat(person.getFirstname()).isEqualTo("Walter");
}
Also used : EntityProjectionIntrospector(org.springframework.data.projection.EntityProjectionIntrospector) BeforeEach(org.junit.jupiter.api.BeforeEach) MappingInstantiationException(org.springframework.data.mapping.model.MappingInstantiationException) EntityCallbacks(org.springframework.data.mapping.callback.EntityCallbacks) ProjectingType(org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.ProjectingType) CustomConversions(org.springframework.data.convert.CustomConversions) Metrics(org.springframework.data.geo.Metrics) NestedType(org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.NestedType) URL(java.net.URL) EntityProjectionIntrospector(org.springframework.data.projection.EntityProjectionIntrospector) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Shape(org.springframework.data.geo.Shape) AfterConvertCallback(org.springframework.data.mongodb.core.mapping.event.AfterConvertCallback) Unwrapped(org.springframework.data.mongodb.core.mapping.Unwrapped) Disabled(org.junit.jupiter.api.Disabled) Box(org.springframework.data.geo.Box) EntityProjection(org.springframework.data.projection.EntityProjection) BasicDBList(com.mongodb.BasicDBList) Document(org.springframework.data.mongodb.core.mapping.Document) BigDecimal(java.math.BigDecimal) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) DBObject(com.mongodb.DBObject) Assertions(org.assertj.core.api.Assertions) BigInteger(java.math.BigInteger) Transient(org.springframework.data.annotation.Transient) Point(org.springframework.data.geo.Point) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TypeAlias(org.springframework.data.annotation.TypeAlias) EqualsAndHashCode(lombok.EqualsAndHashCode) ZoneId(java.time.ZoneId) Test(org.junit.jupiter.api.Test) Code(org.bson.types.Code) NonNull(org.springframework.lang.NonNull) Id(org.springframework.data.annotation.Id) java.util(java.util) WritingConverter(org.springframework.data.convert.WritingConverter) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) Getter(lombok.Getter) PersistenceConstructor(org.springframework.data.annotation.PersistenceConstructor) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Mock(org.mockito.Mock) LocalDateTime(java.time.LocalDateTime) Distance(org.springframework.data.geo.Distance) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) Value(org.springframework.beans.factory.annotation.Value) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MappingException(org.springframework.data.mapping.MappingException) Sphere(org.springframework.data.mongodb.core.geo.Sphere) Nullable(org.springframework.lang.Nullable) TextScore(org.springframework.data.mongodb.core.mapping.TextScore) Decimal128(org.bson.types.Decimal128) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) Converter(org.springframework.core.convert.converter.Converter) Polygon(org.springframework.data.geo.Polygon) Binary(org.bson.types.Binary) FooBarEnum(org.springframework.data.mongodb.core.convert.MappingMongoConverterUnitTests.ClassWithMapUsingEnumAsKey.FooBarEnum) BasicDBObject(com.mongodb.BasicDBObject) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) ApplicationContext(org.springframework.context.ApplicationContext) ReadingConverter(org.springframework.data.convert.ReadingConverter) Field(org.springframework.data.mongodb.core.mapping.Field) LocalDate(org.joda.time.LocalDate) Mockito(org.mockito.Mockito) Circle(org.springframework.data.geo.Circle) ChronoUnit(java.time.temporal.ChronoUnit) PersonPojoStringId(org.springframework.data.mongodb.core.mapping.PersonPojoStringId) DBRef(com.mongodb.DBRef) ProxyFactory(org.springframework.aop.framework.ProxyFactory) ObjectId(org.bson.types.ObjectId) FieldType(org.springframework.data.mongodb.core.mapping.FieldType) DocumentTestUtils(org.springframework.data.mongodb.core.DocumentTestUtils) LocalDate(org.joda.time.LocalDate) Test(org.junit.jupiter.api.Test)

Aggregations

BasicDBList (com.mongodb.BasicDBList)4 BasicDBObject (com.mongodb.BasicDBObject)4 DBObject (com.mongodb.DBObject)4 DBRef (com.mongodb.DBRef)4 BigDecimal (java.math.BigDecimal)4 BigInteger (java.math.BigInteger)4 URL (java.net.URL)4 LocalDateTime (java.time.LocalDateTime)4 ZoneId (java.time.ZoneId)4 ChronoUnit (java.time.temporal.ChronoUnit)4 java.util (java.util)4 EqualsAndHashCode (lombok.EqualsAndHashCode)4 Getter (lombok.Getter)4 RequiredArgsConstructor (lombok.RequiredArgsConstructor)4 Assertions (org.assertj.core.api.Assertions)4 Binary (org.bson.types.Binary)4 Code (org.bson.types.Code)4 Decimal128 (org.bson.types.Decimal128)4 ObjectId (org.bson.types.ObjectId)4 LocalDate (org.joda.time.LocalDate)4