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.
This commit is contained in:
committed by
Oliver Gierke
parent
57d1449008
commit
d88e4c0e3e
@@ -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<Sample> dbRefAnnotatedList;
|
||||
|
||||
@org.springframework.data.mongodb.core.mapping.DBRef
|
||||
public Sample dbRefProperty;
|
||||
}
|
||||
|
||||
static class DocumentWithCollection {
|
||||
|
||||
@@ -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<Entity> dbRefAnnotatedList;
|
||||
|
||||
@org.springframework.data.mongodb.core.mapping.DBRef//
|
||||
public Entity dbRefProperty;
|
||||
}
|
||||
|
||||
static class Entity {
|
||||
|
||||
Reference in New Issue
Block a user