|
33 | 33 | import java.util.Collections;
|
34 | 34 | import java.util.HashSet;
|
35 | 35 | import java.util.List;
|
| 36 | +import java.util.Map; |
36 | 37 |
|
37 | 38 | import org.bson.Document;
|
38 | 39 | import org.junit.jupiter.api.BeforeEach;
|
@@ -110,6 +111,98 @@ void resolvesLazyDBRefOnAccess() {
|
110 | 111 | verify(dbRefResolver).bulkFetch(any());
|
111 | 112 | }
|
112 | 113 |
|
| 114 | + @Test // GH-4312 |
| 115 | + void conversionShouldAllowReadingAlreadyResolvedReferences() { |
| 116 | + |
| 117 | + Document sampleSource = new Document("_id", "sample-1").append("value", "one"); |
| 118 | + Document source = new Document("_id", "id-1").append("sample", sampleSource); |
| 119 | + |
| 120 | + WithSingleValueDbRef read = converter.read(WithSingleValueDbRef.class, source); |
| 121 | + |
| 122 | + assertThat(read.sample).isEqualTo(converter.read(Sample.class, sampleSource)); |
| 123 | + verifyNoInteractions(dbRefResolver); |
| 124 | + } |
| 125 | + |
| 126 | + @Test // GH-4312 |
| 127 | + void conversionShouldAllowReadingAlreadyResolvedListOfReferences() { |
| 128 | + |
| 129 | + Document sample1Source = new Document("_id", "sample-1").append("value", "one"); |
| 130 | + Document sample2Source = new Document("_id", "sample-2").append("value", "two"); |
| 131 | + Document source = new Document("_id", "id-1").append("lazyList", List.of(sample1Source, sample2Source)); |
| 132 | + |
| 133 | + WithLazyDBRef read = converter.read(WithLazyDBRef.class, source); |
| 134 | + |
| 135 | + assertThat(read.lazyList).containsExactly(converter.read(Sample.class, sample1Source), |
| 136 | + converter.read(Sample.class, sample2Source)); |
| 137 | + verifyNoInteractions(dbRefResolver); |
| 138 | + } |
| 139 | + |
| 140 | + @Test // GH-4312 |
| 141 | + void conversionShouldAllowReadingAlreadyResolvedMapOfReferences() { |
| 142 | + |
| 143 | + Document sample1Source = new Document("_id", "sample-1").append("value", "one"); |
| 144 | + Document sample2Source = new Document("_id", "sample-2").append("value", "two"); |
| 145 | + Document source = new Document("_id", "id-1").append("sampleMap", |
| 146 | + new Document("s1", sample1Source).append("s2", sample2Source)); |
| 147 | + |
| 148 | + WithMapValueDbRef read = converter.read(WithMapValueDbRef.class, source); |
| 149 | + |
| 150 | + assertThat(read.sampleMap) // |
| 151 | + .containsEntry("s1", converter.read(Sample.class, sample1Source)) // |
| 152 | + .containsEntry("s2", converter.read(Sample.class, sample2Source)); |
| 153 | + verifyNoInteractions(dbRefResolver); |
| 154 | + } |
| 155 | + |
| 156 | + @Test // GH-4312 |
| 157 | + void conversionShouldAllowReadingAlreadyResolvedMapOfLazyReferences() { |
| 158 | + |
| 159 | + Document sample1Source = new Document("_id", "sample-1").append("value", "one"); |
| 160 | + Document sample2Source = new Document("_id", "sample-2").append("value", "two"); |
| 161 | + Document source = new Document("_id", "id-1").append("sampleMapLazy", |
| 162 | + new Document("s1", sample1Source).append("s2", sample2Source)); |
| 163 | + |
| 164 | + WithMapValueDbRef read = converter.read(WithMapValueDbRef.class, source); |
| 165 | + |
| 166 | + assertThat(read.sampleMapLazy) // |
| 167 | + .containsEntry("s1", converter.read(Sample.class, sample1Source)) // |
| 168 | + .containsEntry("s2", converter.read(Sample.class, sample2Source)); |
| 169 | + verifyNoInteractions(dbRefResolver); |
| 170 | + } |
| 171 | + |
| 172 | + @Test // GH-4312 |
| 173 | + void resolvesLazyDBRefMapOnAccess() { |
| 174 | + |
| 175 | + client.getDatabase(DATABASE).getCollection("samples") |
| 176 | + .insertMany(Arrays.asList(new Document("_id", "sample-1").append("value", "one"), |
| 177 | + new Document("_id", "sample-2").append("value", "two"))); |
| 178 | + |
| 179 | + Document source = new Document("_id", "id-1").append("sampleMapLazy", |
| 180 | + new Document("s1", new com.mongodb.DBRef("samples", "sample-1")).append("s2", |
| 181 | + new com.mongodb.DBRef("samples", "sample-2"))); |
| 182 | + |
| 183 | + WithMapValueDbRef target = converter.read(WithMapValueDbRef.class, source); |
| 184 | + |
| 185 | + verify(dbRefResolver).resolveDbRef(any(), isNull(), any(), any()); |
| 186 | + |
| 187 | + assertThat(target.sampleMapLazy).isInstanceOf(LazyLoadingProxy.class); |
| 188 | + assertThat(target.getSampleMapLazy()).containsEntry("s1", new Sample("sample-1", "one")).containsEntry("s2", |
| 189 | + new Sample("sample-2", "two")); |
| 190 | + |
| 191 | + verify(dbRefResolver).bulkFetch(any()); |
| 192 | + } |
| 193 | + |
| 194 | + @Test // GH-4312 |
| 195 | + void conversionShouldAllowReadingAlreadyResolvedLazyReferences() { |
| 196 | + |
| 197 | + Document sampleSource = new Document("_id", "sample-1").append("value", "one"); |
| 198 | + Document source = new Document("_id", "id-1").append("sampleLazy", sampleSource); |
| 199 | + |
| 200 | + WithSingleValueDbRef read = converter.read(WithSingleValueDbRef.class, source); |
| 201 | + |
| 202 | + assertThat(read.sampleLazy).isEqualTo(converter.read(Sample.class, sampleSource)); |
| 203 | + verifyNoInteractions(dbRefResolver); |
| 204 | + } |
| 205 | + |
113 | 206 | @Test // DATAMONGO-2004
|
114 | 207 | void resolvesLazyDBRefConstructorArgOnAccess() {
|
115 | 208 |
|
@@ -164,6 +257,31 @@ List<Sample> getLazyList() {
|
164 | 257 | }
|
165 | 258 | }
|
166 | 259 |
|
| 260 | + @Data |
| 261 | + public static class WithSingleValueDbRef { |
| 262 | + |
| 263 | + @Id // |
| 264 | + String id; |
| 265 | + |
| 266 | + @DBRef // |
| 267 | + Sample sample; |
| 268 | + |
| 269 | + @DBRef(lazy = true) // |
| 270 | + Sample sampleLazy; |
| 271 | + } |
| 272 | + |
| 273 | + @Data |
| 274 | + public static class WithMapValueDbRef { |
| 275 | + |
| 276 | + @Id String id; |
| 277 | + |
| 278 | + @DBRef // |
| 279 | + Map<String, Sample> sampleMap; |
| 280 | + |
| 281 | + @DBRef(lazy = true) // |
| 282 | + Map<String, Sample> sampleMapLazy; |
| 283 | + } |
| 284 | + |
167 | 285 | public static class WithLazyDBRefAsConstructorArg {
|
168 | 286 |
|
169 | 287 | @Id String id;
|
|
0 commit comments