From d88e4c0e3ec5467127da1b164689a22e85ed5a77 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Thu, 20 Feb 2014 10:46:45 +0100 Subject: [PATCH] DATAMONGO-468 - Verify that one can use a domain object in DbRef field updates. Added test case to demonstrate that using a domain object as a value for a DbRef field update is already supported. Original pull request: #127. --- .../data/mongodb/core/MongoTemplateTests.java | 39 ++++++++++++++++--- .../core/convert/UpdateMapperUnitTests.java | 20 ++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java index 0a834dc3f..475573963 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java @@ -185,6 +185,7 @@ public class MongoTemplateTests { template.dropCollection(DocumentWithCollection.class); template.dropCollection(DocumentWithCollectionOfSimpleType.class); template.dropCollection(DocumentWithMultipleCollections.class); + template.dropCollection(DocumentWithDBRefCollection.class); } @Test @@ -2336,8 +2337,6 @@ public class MongoTemplateTests { @Test public void updateWithPullShouldRemoveNestedItemFromDbRefAnnotatedCollection() { - template.dropCollection(DocumentWithDBRefCollection.class); - Sample sample1 = new Sample("1", "A"); Sample sample2 = new Sample("2", "B"); template.save(sample1); @@ -2370,8 +2369,6 @@ public class MongoTemplateTests { @Test public void updateWithPullShouldRemoveNestedItemFromDbRefAnnotatedCollectionWhenGivenAnIdValueOfComponentTypeEntity() { - template.dropCollection(DocumentWithDBRefCollection.class); - Sample sample1 = new Sample("1", "A"); Sample sample2 = new Sample("2", "B"); template.save(sample1); @@ -2397,7 +2394,7 @@ public class MongoTemplateTests { assertThat(result.dbRefAnnotatedList.get(0), is(notNullValue())); assertThat(result.dbRefAnnotatedList.get(0).id, is((Object) "1")); } - + /** * @see DATAMONGO-852 */ @@ -2421,12 +2418,44 @@ public class MongoTemplateTests { assertThat(personAfterUpdateFirst.lastname, is("Bubu")); } + /** + * @see DATAMONGO-468 + */ + @Test + public void shouldBeAbleToUpdateDbRefPropertyWithDomainObject(){ + + Sample sample1 = new Sample("1", "A"); + Sample sample2 = new Sample("2", "B"); + template.save(sample1); + template.save(sample2); + + DocumentWithDBRefCollection doc = new DocumentWithDBRefCollection(); + doc.id = "1"; + doc.dbRefProperty = sample1; + template.save(doc); + + Update update = new Update().set("dbRefProperty",sample2); + + Query qry = query(where("id").is("1")); + template.updateFirst(qry, update, DocumentWithDBRefCollection.class); + + DocumentWithDBRefCollection updatedDoc = template.findOne(qry, DocumentWithDBRefCollection.class); + + assertThat(updatedDoc,is(notNullValue())); + assertThat(updatedDoc.dbRefProperty,is(notNullValue())); + assertThat(updatedDoc.dbRefProperty.id,is(sample2.id)); + assertThat(updatedDoc.dbRefProperty.field,is(sample2.field)); + } + static class DocumentWithDBRefCollection { @Id public String id; @org.springframework.data.mongodb.core.mapping.DBRef// public List dbRefAnnotatedList; + + @org.springframework.data.mongodb.core.mapping.DBRef + public Sample dbRefProperty; } static class DocumentWithCollection { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java index 1412b7aef..81dd80824 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java @@ -359,6 +359,23 @@ public class UpdateMapperUnitTests { assertThat(pullClause.containsField("mapped.dbRefAnnotatedList"), is(true)); } + /** + * @see DATAMONGO-468 + */ + @Test + public void rendersUpdateOfDbRefPropertyWithDomainObjectCorrectly() { + + Entity entity = new Entity(); + entity.id = "5"; + + Update update = new Update().set("dbRefProperty", entity); + DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), + context.getPersistentEntity(DocumentWithDBRefCollection.class)); + + DBObject setClause = getAsDBObject(mappedObject, "$set"); + assertThat(setClause.get("dbRefProperty"), is((Object) new DBRef(null, "entity", entity.id))); + } + static interface Model {} static class ModelImpl implements Model { @@ -451,6 +468,9 @@ public class UpdateMapperUnitTests { @org.springframework.data.mongodb.core.mapping.DBRef// public List dbRefAnnotatedList; + + @org.springframework.data.mongodb.core.mapping.DBRef// + public Entity dbRefProperty; } static class Entity {