From dc234906f487167aba7e55d9a97de4b2fa2bc1dd Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 11 Mar 2019 10:48:26 +0100 Subject: [PATCH] DATAMONGO-2223 - Add test for DBRef resolution in a different database. Original pull request: #660. --- .../mongodb/core/MongoTemplateDbRefTests.java | 130 +++++++++++++++++- 1 file changed, 128 insertions(+), 2 deletions(-) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java index ae1d4444f..8b3620069 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java @@ -21,13 +21,16 @@ import static org.springframework.data.mongodb.core.query.Query.*; import lombok.Data; +import java.util.Arrays; +import java.util.List; + import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; + import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.convert.LazyLoadingTestUtils; import org.springframework.data.mongodb.core.mapping.DBRef; import org.springframework.data.mongodb.core.mapping.Document; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.mongodb.MongoClient; @@ -40,6 +43,7 @@ import com.mongodb.MongoClient; public class MongoTemplateDbRefTests { MongoTemplate template; + MongoTemplate otherDbTemplate; @Before public void setUp() { @@ -49,6 +53,13 @@ public class MongoTemplateDbRefTests { template.dropCollection(RefCycleLoadingIntoDifferentTypeRoot.class); template.dropCollection(RefCycleLoadingIntoDifferentTypeIntermediate.class); template.dropCollection(RefCycleLoadingIntoDifferentTypeRootView.class); + template.dropCollection(WithRefToAnotherDb.class); + template.dropCollection(WithLazyRefToAnotherDb.class); + template.dropCollection(WithListRefToAnotherDb.class); + template.dropCollection(WithLazyListRefToAnotherDb.class); + + otherDbTemplate = new MongoTemplate(new MongoClient(), "mongo-template-dbref-tests-other-db"); + otherDbTemplate.dropCollection(JustSomeType.class); } @Test // DATAMONGO-1703 @@ -82,6 +93,83 @@ public class MongoTemplateDbRefTests { assertThat(loaded.getRefToIntermediate().getRefToRootView().getContent()).isEqualTo("jon snow"); } + @Test // DATAMONGO-2223 + public void shouldResolveSingleDBRefToAnotherDb() { + + JustSomeType one = new JustSomeType(); + one.value = "one"; + + otherDbTemplate.insert(one); + + WithRefToAnotherDb source = new WithRefToAnotherDb(); + source.value = one; + + template.save(source); + + WithRefToAnotherDb target = template.findOne(query(where("id").is(source.id)), WithRefToAnotherDb.class); + assertThat(target.getValue()).isEqualTo(one); + } + + @Test // DATAMONGO-2223 + public void shouldResolveSingleLazyDBRefToAnotherDb() { + + JustSomeType one = new JustSomeType(); + one.value = "one"; + + otherDbTemplate.insert(one); + + WithLazyRefToAnotherDb source = new WithLazyRefToAnotherDb(); + source.value = one; + + template.save(source); + + WithLazyRefToAnotherDb target = template.findOne(query(where("id").is(source.id)), WithLazyRefToAnotherDb.class); + LazyLoadingTestUtils.assertProxyIsResolved(target.value, false); + assertThat(target.getValue()).isEqualTo(one); + } + + @Test // DATAMONGO-2223 + public void shouldResolveListDBRefToAnotherDb() { + + JustSomeType one = new JustSomeType(); + one.value = "one"; + + JustSomeType two = new JustSomeType(); + two.value = "two"; + + otherDbTemplate.insertAll(Arrays.asList(one, two)); + + WithListRefToAnotherDb source = new WithListRefToAnotherDb(); + source.value = Arrays.asList(one, two); + + template.save(source); + + WithListRefToAnotherDb target = template.findOne(query(where("id").is(source.id)), WithListRefToAnotherDb.class); + assertThat(target.getValue()).containsExactlyInAnyOrder(one, two); + } + + @Test // DATAMONGO-2223 + public void shouldResolveLazyListDBRefToAnotherDb() { + + JustSomeType one = new JustSomeType(); + one.value = "one"; + + JustSomeType two = new JustSomeType(); + two.value = "two"; + + otherDbTemplate.insertAll(Arrays.asList(one, two)); + + WithLazyListRefToAnotherDb source = new WithLazyListRefToAnotherDb(); + source.value = Arrays.asList(one, two); + + template.save(source); + + WithLazyListRefToAnotherDb target = template.findOne(query(where("id").is(source.id)), + WithLazyListRefToAnotherDb.class); + LazyLoadingTestUtils.assertProxyIsResolved(target.value, false); + assertThat(target.getValue()).containsExactlyInAnyOrder(one, two); + } + @Data @Document("cycle-with-different-type-root") static class RefCycleLoadingIntoDifferentTypeRoot { @@ -107,4 +195,42 @@ public class MongoTemplateDbRefTests { String content; } + @Data + static class WithRefToAnotherDb { + + @Id String id; + @org.springframework.data.mongodb.core.mapping.DBRef(db = "mongo-template-dbref-tests-other-db") JustSomeType value; + } + + @Data + static class WithLazyRefToAnotherDb { + + @Id String id; + @org.springframework.data.mongodb.core.mapping.DBRef(lazy = true, + db = "mongo-template-dbref-tests-other-db") JustSomeType value; + } + + @Data + static class WithListRefToAnotherDb { + + @Id String id; + @org.springframework.data.mongodb.core.mapping.DBRef( + db = "mongo-template-dbref-tests-other-db") List value; + } + + @Data + static class WithLazyListRefToAnotherDb { + + @Id String id; + @org.springframework.data.mongodb.core.mapping.DBRef(lazy = true, + db = "mongo-template-dbref-tests-other-db") List value; + } + + @Data + static class JustSomeType { + + @Id String id; + String value; + } + }