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