|
21 | 21 | import static org.springframework.data.mongodb.core.query.Query.*;
|
22 | 22 |
|
23 | 23 | import java.net.UnknownHostException;
|
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
24 | 27 |
|
| 28 | +import org.bson.Document; |
25 | 29 | import org.junit.After;
|
26 | 30 | import org.junit.Assert;
|
27 | 31 | import org.junit.Before;
|
|
33 | 37 | import org.springframework.data.mongodb.core.mapping.PersonPojoStringId;
|
34 | 38 |
|
35 | 39 | import com.mongodb.DB;
|
36 |
| -import org.bson.Document; |
37 | 40 | import com.mongodb.Mongo;
|
38 | 41 | import com.mongodb.MongoClient;
|
39 | 42 | import com.mongodb.WriteConcern;
|
|
43 | 46 | *
|
44 | 47 | * @author Mark Pollack
|
45 | 48 | * @author Christoph Strobl
|
| 49 | + * @author Jordi Llach |
46 | 50 | */
|
47 | 51 | public class ApplicationContextEventTests {
|
48 | 52 |
|
49 | 53 | private static final String COLLECTION_NAME = "personPojoStringId";
|
| 54 | + private static final String ROOT_COLLECTION_NAME = "root"; |
| 55 | + private static final String RELATED_COLLECTION_NAME = "related"; |
50 | 56 |
|
51 |
| - private final String[] collectionsToDrop = new String[] { COLLECTION_NAME }; |
| 57 | + private final String[] collectionsToDrop = new String[] { COLLECTION_NAME, ROOT_COLLECTION_NAME, |
| 58 | + RELATED_COLLECTION_NAME }; |
52 | 59 |
|
53 | 60 | private ApplicationContext applicationContext;
|
54 | 61 | private MongoTemplate template;
|
@@ -188,6 +195,154 @@ public void deleteEvents() {
|
188 | 195 | assertThat(simpleMappingEventListener.onAfterDeleteEvents.get(0).getCollectionName(), is(COLLECTION_NAME));
|
189 | 196 | }
|
190 | 197 |
|
| 198 | + /** |
| 199 | + * DATAMONGO-1271 DATAMONGO-1287 |
| 200 | + */ |
| 201 | + @Test |
| 202 | + public void loadAndConvertEventsInInnerSimpleDBRef() throws Exception { |
| 203 | + ParentMappingEventListener simpleMappingEventListener = applicationContext |
| 204 | + .getBean(ParentMappingEventListener.class); |
| 205 | + Related embed = new Related(1L, "embed desc"); |
| 206 | + Related ref1 = new Related(2L, "related desc1"); |
| 207 | + Related ref2 = new Related(3L, "related desc2"); |
| 208 | + template.insert(embed); |
| 209 | + template.insert(ref1); |
| 210 | + template.insert(ref2); |
| 211 | + |
| 212 | + Root root = new Root(1L, embed, ref1, ref2, null, null, null, null); |
| 213 | + template.insert(root); |
| 214 | + |
| 215 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(0)); |
| 216 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(0)); |
| 217 | + |
| 218 | + // initially fetching ROOT document and also eagerly fetching 1 DBRef |
| 219 | + Root rootR = template.findOne(query(where("id").is(root.getId())), Root.class); |
| 220 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(2)); |
| 221 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(2)); |
| 222 | + |
| 223 | + // checking that no event is fired because those documents were previously eagerly fetched |
| 224 | + rootR.getRef().getDescription(); |
| 225 | + rootR.getEmbed().getDescription(); |
| 226 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(2)); |
| 227 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(2)); |
| 228 | + // checking that accessing lazy DBRef fires 1 more event of each type |
| 229 | + rootR.getLazyRef().getDescription(); |
| 230 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(3)); |
| 231 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(3)); |
| 232 | + |
| 233 | + // checking collectionNames fired |
| 234 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(0).getCollectionName(), is(ROOT_COLLECTION_NAME)); |
| 235 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(0).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 236 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(1).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 237 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(1).getCollectionName(), is(ROOT_COLLECTION_NAME)); |
| 238 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(2).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 239 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(2).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * DATAMONGO-1271 DATAMONGO-1287 |
| 244 | + */ |
| 245 | + @Test |
| 246 | + public void loadAndConvertEventsInInnerListDBRef() throws Exception { |
| 247 | + ParentMappingEventListener simpleMappingEventListener = applicationContext |
| 248 | + .getBean(ParentMappingEventListener.class); |
| 249 | + Related embed = new Related(1L, "embed desc"); |
| 250 | + Related ref1 = new Related(2L, "related desc1"); |
| 251 | + Related ref2 = new Related(3L, "related desc2"); |
| 252 | + template.insert(embed); |
| 253 | + template.insert(ref1); |
| 254 | + template.insert(ref2); |
| 255 | + |
| 256 | + Root root = new Root(1L, embed, null, null, Arrays.asList(ref1, ref2), Arrays.asList(ref1, ref2), null, null); |
| 257 | + template.insert(root); |
| 258 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(0)); |
| 259 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(0)); |
| 260 | + |
| 261 | + // initially fetching ROOT document and also eagerly fetching 2 DBRef |
| 262 | + Root rootR = template.findOne(query(where("id").is(root.getId())), Root.class); |
| 263 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(3)); |
| 264 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(3)); |
| 265 | + |
| 266 | + // checking that no event is fired because those documents were previously eagerly fetched |
| 267 | + rootR.getListRef().get(0).getDescription(); |
| 268 | + rootR.getListRef().get(1).getDescription(); |
| 269 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(3)); |
| 270 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(3)); |
| 271 | + |
| 272 | + // fetching lazily dbref |
| 273 | + rootR.getListLazy().get(0).getDescription(); |
| 274 | + rootR.getListLazy().get(1).getDescription(); |
| 275 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(5)); |
| 276 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(5)); |
| 277 | + |
| 278 | + // checking collectionNames fired |
| 279 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(0).getCollectionName(), is(ROOT_COLLECTION_NAME)); |
| 280 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(0).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 281 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(1).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 282 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(1).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 283 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(2).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 284 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(2).getCollectionName(), is(ROOT_COLLECTION_NAME)); |
| 285 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(3).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 286 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(3).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 287 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(4).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 288 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(4).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 289 | + } |
| 290 | + |
| 291 | + /** |
| 292 | + * DATAMONGO-1271 DATAMONGO-1287 |
| 293 | + */ |
| 294 | + @Test |
| 295 | + public void loadAndConvertEventsInInnerMapDBRef() throws Exception { |
| 296 | + ParentMappingEventListener simpleMappingEventListener = applicationContext |
| 297 | + .getBean(ParentMappingEventListener.class); |
| 298 | + Related embed = new Related(1L, "embed desc"); |
| 299 | + Related ref1 = new Related(2L, "related desc1"); |
| 300 | + Related ref2 = new Related(3L, "related desc2"); |
| 301 | + template.insert(embed); |
| 302 | + template.insert(ref1); |
| 303 | + template.insert(ref2); |
| 304 | + |
| 305 | + Map<String, Related> mapRef = new HashMap(); |
| 306 | + mapRef.put("1", ref1); |
| 307 | + mapRef.put("2", ref2); |
| 308 | + Map<String, Related> mapLazy = new HashMap(); |
| 309 | + mapLazy.put("1", ref1); |
| 310 | + mapLazy.put("2", ref2); |
| 311 | + |
| 312 | + Root root = new Root(1L, embed, null, null, null, null, mapRef, mapLazy); |
| 313 | + template.insert(root); |
| 314 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(0)); |
| 315 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(0)); |
| 316 | + |
| 317 | + // initially fetching ROOT document and also eagerly fetching 2 DBRef (eager map) |
| 318 | + Root rootR = template.findOne(query(where("id").is(root.getId())), Root.class); |
| 319 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(3)); |
| 320 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(3)); |
| 321 | + |
| 322 | + // checking that accessing eagerly fetched map does not fire any new event |
| 323 | + Assert.assertEquals(0, |
| 324 | + rootR.getMapRef().keySet().stream().filter(key -> rootR.getMapRef().get(key).getDescription() == null).count()); |
| 325 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(3)); |
| 326 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(3)); |
| 327 | + // accessing lazy map of dbref |
| 328 | + Assert.assertEquals(0, rootR.getMapLazy().keySet().stream() |
| 329 | + .filter(key -> rootR.getMapLazy().get(key).getDescription() == null).count()); |
| 330 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.size(), is(5)); |
| 331 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.size(), is(5)); |
| 332 | + |
| 333 | + // checking collectionNames fired |
| 334 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(0).getCollectionName(), is(ROOT_COLLECTION_NAME)); |
| 335 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(0).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 336 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(1).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 337 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(1).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 338 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(2).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 339 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(2).getCollectionName(), is(ROOT_COLLECTION_NAME)); |
| 340 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(3).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 341 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(3).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 342 | + assertThat(simpleMappingEventListener.onAfterLoadEvents.get(4).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 343 | + assertThat(simpleMappingEventListener.onAfterConvertEvents.get(4).getCollectionName(), is(RELATED_COLLECTION_NAME)); |
| 344 | + } |
| 345 | + |
191 | 346 | private void comparePersonAndDbo(PersonPojoStringId p, PersonPojoStringId p2, Document dbo) {
|
192 | 347 | assertEquals(p.getId(), p2.getId());
|
193 | 348 | assertEquals(p.getText(), p2.getText());
|
|
0 commit comments