Search in sources :

Example 21 with DBRef

use of com.mongodb.DBRef in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterUnitTests method readsDBRefMap.

// DATAMONGO-424
@Test
void readsDBRefMap() {
    DBRef dbRef = mock(DBRef.class);
    org.bson.Document refMap = new org.bson.Document("foo", dbRef);
    org.bson.Document document = new org.bson.Document("refMap", refMap);
    DBRefWrapper result = converter.read(DBRefWrapper.class, document);
    assertThat(result.refMap.entrySet()).hasSize(1);
    assertThat(result.refMap.values()).contains(dbRef);
}
Also used : DBRef(com.mongodb.DBRef) Document(org.springframework.data.mongodb.core.mapping.Document) Test(org.junit.jupiter.api.Test)

Example 22 with DBRef

use of com.mongodb.DBRef in project spring-data-mongodb by spring-projects.

the class DbRefMappingMongoConverterUnitTests method createsDBRefWithClientSpecCorrectly.

// DATAMONGO-347
@Test
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()).isEqualTo("foo");
    assertThat(dbRef.getCollectionName()).isEqualTo("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.jupiter.api.Test)

Example 23 with DBRef

use of com.mongodb.DBRef in project spring-data-mongodb by spring-projects.

the class DbRefMappingMongoConverterUnitTests method shouldNotEagerlyResolveIdPropertyWithPropertyAccess.

// DATAMONGO-1012
@Test
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")).isNull();
    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.jupiter.api.Test)

Example 24 with DBRef

use of com.mongodb.DBRef in project spring-data-mongodb by spring-projects.

the class DbRefMappingMongoConverterUnitTests method shouldEagerlyResolveIdPropertyWithFieldAccess.

// DATAMONGO-1012
@Test
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)).isNotNull();
    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.jupiter.api.Test)

Example 25 with DBRef

use of com.mongodb.DBRef in project spring-data-mongodb by spring-projects.

the class UpdateMapperUnitTests method updateOnDbrefPropertyOfInterfaceTypeWithoutExplicitGetterForIdShouldBeMappedCorrectly.

// DATAMONGO-897
@Test
void updateOnDbrefPropertyOfInterfaceTypeWithoutExplicitGetterForIdShouldBeMappedCorrectly() {
    Update update = new Update().set("referencedDocument", new InterfaceDocumentDefinitionImpl("1", "Foo"));
    Document mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(DocumentWithReferenceToInterfaceImpl.class));
    Document $set = DocumentTestUtils.getAsDocument(mappedObject, "$set");
    Object model = $set.get("referencedDocument");
    DBRef expectedDBRef = new DBRef("interfaceDocumentDefinitionImpl", "1");
    assertThat(model).isInstanceOf(DBRef.class).isEqualTo(expectedDBRef);
}
Also used : DBRef(com.mongodb.DBRef) Update(org.springframework.data.mongodb.core.query.Update) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Aggregations

DBRef (com.mongodb.DBRef)68 Document (org.bson.Document)26 Test (org.junit.jupiter.api.Test)24 BasicDBObject (com.mongodb.BasicDBObject)22 ObjectId (org.bson.types.ObjectId)20 DBObject (com.mongodb.DBObject)17 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)13 BasicDBList (com.mongodb.BasicDBList)11 ArrayList (java.util.ArrayList)7 Binary (org.bson.types.Binary)7 Test (org.junit.Test)6 Document (org.springframework.data.mongodb.core.mapping.Document)6 DBCollection (com.mongodb.DBCollection)5 Map (java.util.Map)5 BasicBSONObject (org.bson.BasicBSONObject)5 Date (java.util.Date)4 List (java.util.List)4 UUID (java.util.UUID)4 Bson (org.bson.conversions.Bson)4 MaxKey (org.bson.types.MaxKey)4