Search in sources :

Example 11 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class PathUnitTests method shouldIdentifyCycle.

// DATAMONGO-962, DATAMONGO-1782
@Test
public void shouldIdentifyCycle() {
    MongoPersistentProperty foo = createPersistentPropertyMock(entityMock, "foo");
    MongoPersistentProperty bar = createPersistentPropertyMock(entityMock, "bar");
    Path path = Path.of(foo).append(bar).append(bar);
    assertThat(path.isCycle(), is(true));
    assertThat(path.toCyclePath(), is(equalTo("bar -> bar")));
    assertThat(path.toString(), is(equalTo("foo -> bar -> bar")));
}
Also used : Path(org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.CycleGuard.Path) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Test(org.junit.Test)

Example 12 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class PathUnitTests method isCycleShouldReturnFalseCycleForNonEqualProperties.

// DATAMONGO-1782
@Test
public void isCycleShouldReturnFalseCycleForNonEqualProperties() {
    MongoPersistentProperty foo = createPersistentPropertyMock(entityMock, "foo");
    MongoPersistentProperty bar = createPersistentPropertyMock(entityMock, "bar");
    MongoPersistentProperty bar2 = createPersistentPropertyMock(mock(MongoPersistentEntity.class), "bar");
    assertThat(Path.of(foo).append(bar).append(bar2).isCycle(), is(false));
}
Also used : MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Test(org.junit.Test)

Example 13 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class DbRefMappingMongoConverterUnitTests method shouldEagerlyResolveIdPropertyWithFieldAccess.

// DATAMONGO-1012
@Test
public void shouldEagerlyResolveIdPropertyWithFieldAccess() {
    MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(ClassWithLazyDbRefs.class);
    MongoPersistentProperty property = entity.getRequiredPersistentProperty("dbRefToConcreteType");
    MongoPersistentEntity<?> propertyEntity = mappingContext.getRequiredPersistentEntity(property);
    String idValue = new ObjectId().toString();
    DBRef dbRef = converter.toDBRef(new LazyDbRefTarget(idValue), property);
    Document object = new Document("dbRefToConcreteType", dbRef);
    ClassWithLazyDbRefs result = converter.read(ClassWithLazyDbRefs.class, object);
    PersistentPropertyAccessor accessor = propertyEntity.getPropertyAccessor(result.dbRefToConcreteType);
    MongoPersistentProperty idProperty = mappingContext.getRequiredPersistentEntity(LazyDbRefTarget.class).getIdProperty();
    assertThat(accessor.getProperty(idProperty), is(notNullValue()));
    assertProxyIsResolved(result.dbRefToConcreteType, false);
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) ObjectId(org.bson.types.ObjectId) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) DBRef(com.mongodb.DBRef) Document(org.bson.Document) Test(org.junit.Test)

Example 14 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class DbRefMappingMongoConverterUnitTests method createsDBRefWithClientSpecCorrectly.

// DATAMONGO-347
@Test
public void createsDBRefWithClientSpecCorrectly() {
    PropertyPath path = PropertyPath.from("person", PersonClient.class);
    MongoPersistentProperty property = mappingContext.getPersistentPropertyPath(path).getLeafProperty();
    Person person = new Person();
    person.id = "foo";
    DBRef dbRef = converter.toDBRef(person, property);
    assertThat(dbRef.getId(), is("foo"));
    assertThat(dbRef.getCollectionName(), is("person"));
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) PropertyPath(org.springframework.data.mapping.PropertyPath) DBRef(com.mongodb.DBRef) Person(org.springframework.data.mongodb.core.convert.MappingMongoConverterUnitTests.Person) Test(org.junit.Test)

Example 15 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class DbRefMappingMongoConverterUnitTests method shouldNotEagerlyResolveIdPropertyWithPropertyAccess.

// DATAMONGO-1012
@Test
public void shouldNotEagerlyResolveIdPropertyWithPropertyAccess() {
    MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(ClassWithLazyDbRefs.class);
    MongoPersistentProperty property = entity.getRequiredPersistentProperty("dbRefToConcreteTypeWithPropertyAccess");
    String idValue = new ObjectId().toString();
    DBRef dbRef = converter.toDBRef(new LazyDbRefTargetPropertyAccess(idValue), property);
    Document object = new Document("dbRefToConcreteTypeWithPropertyAccess", dbRef);
    ClassWithLazyDbRefs result = converter.read(ClassWithLazyDbRefs.class, object);
    LazyDbRefTargetPropertyAccess proxy = result.dbRefToConcreteTypeWithPropertyAccess;
    assertThat(ReflectionTestUtils.getField(proxy, "id"), is(nullValue()));
    assertProxyIsResolved(proxy, false);
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) ObjectId(org.bson.types.ObjectId) DBRef(com.mongodb.DBRef) Document(org.bson.Document) Test(org.junit.Test)

Aggregations

MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)40 Document (org.bson.Document)13 DBRef (com.mongodb.DBRef)10 Test (org.junit.Test)9 MappingException (org.springframework.data.mapping.MappingException)6 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)6 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)6 MongoPersistentEntity (org.springframework.data.mongodb.core.mapping.MongoPersistentEntity)6 BasicDBObject (com.mongodb.BasicDBObject)5 DBObject (com.mongodb.DBObject)4 ReturnDocument (com.mongodb.client.model.ReturnDocument)4 FullDocument (com.mongodb.client.model.changestream.FullDocument)4 Arrays (java.util.Arrays)3 Iterator (java.util.Iterator)3 ObjectId (org.bson.types.ObjectId)3 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 List (java.util.List)2