|
28 | 28 |
|
29 | 29 | import org.bson.conversions.Bson;
|
30 | 30 | import org.junit.jupiter.api.BeforeEach;
|
31 |
| -import org.junit.jupiter.api.Disabled; |
32 | 31 | import org.junit.jupiter.api.Test;
|
33 | 32 | import org.junit.jupiter.api.extension.ExtendWith;
|
34 | 33 | import org.mockito.Mock;
|
35 | 34 | import org.mockito.junit.jupiter.MockitoExtension;
|
36 |
| - |
| 35 | +import org.springframework.data.annotation.Embedded; |
37 | 36 | import org.springframework.data.annotation.Id;
|
38 | 37 | import org.springframework.data.domain.Example;
|
39 | 38 | import org.springframework.data.domain.ExampleMatcher;
|
40 | 39 | import org.springframework.data.domain.ExampleMatcher.*;
|
41 | 40 | import org.springframework.data.geo.Point;
|
42 | 41 | import org.springframework.data.mongodb.MongoDatabaseFactory;
|
| 42 | +import org.springframework.data.mongodb.core.convert.MappingMongoConverterUnitTests.EmbeddableType; |
43 | 43 | import org.springframework.data.mongodb.core.convert.QueryMapperUnitTests.ClassWithGeoTypes;
|
44 | 44 | import org.springframework.data.mongodb.core.convert.QueryMapperUnitTests.WithDBRef;
|
| 45 | +import org.springframework.data.mongodb.core.index.Indexed; |
45 | 46 | import org.springframework.data.mongodb.core.mapping.DBRef;
|
46 | 47 | import org.springframework.data.mongodb.core.mapping.Document;
|
47 | 48 | import org.springframework.data.mongodb.core.mapping.Field;
|
@@ -455,10 +456,58 @@ public void untypedExampleShouldNotInferTypeRestriction() {
|
455 | 456 | assertThat(document).doesNotContainKey("_class");
|
456 | 457 | }
|
457 | 458 |
|
458 |
| - @Test |
459 |
| - @Disabled("TODO: embedded types") |
460 |
| - void xxx() { |
461 |
| - // TODO: embedded |
| 459 | + @Test // DATAMONGO-1902 |
| 460 | + void mapsEmbeddedType() { |
| 461 | + |
| 462 | + WithEmbedded probe = new WithEmbedded(); |
| 463 | + probe.embeddableType = new EmbeddableType(); |
| 464 | + probe.embeddableType.atFieldAnnotatedValue = "@Field"; |
| 465 | + probe.embeddableType.stringValue = "string-value"; |
| 466 | + |
| 467 | + org.bson.Document document = mapper.getMappedExample(Example.of(probe, UntypedExampleMatcher.matching())); |
| 468 | + assertThat(document).containsEntry("stringValue", "string-value").containsEntry("with-at-field-annotation", |
| 469 | + "@Field"); |
| 470 | + } |
| 471 | + |
| 472 | + @Test // DATAMONGO-1902 |
| 473 | + void mapsPrefixedEmbeddedType() { |
| 474 | + |
| 475 | + WithEmbedded probe = new WithEmbedded(); |
| 476 | + probe.prefixedEmbeddableValue = new EmbeddableType(); |
| 477 | + probe.prefixedEmbeddableValue.atFieldAnnotatedValue = "@Field"; |
| 478 | + probe.prefixedEmbeddableValue.stringValue = "string-value"; |
| 479 | + |
| 480 | + org.bson.Document document = mapper.getMappedExample(Example.of(probe, UntypedExampleMatcher.matching())); |
| 481 | + assertThat(document).containsEntry("prefix-stringValue", "string-value").containsEntry("prefix-with-at-field-annotation", |
| 482 | + "@Field"); |
| 483 | + } |
| 484 | + |
| 485 | + @Test // DATAMONGO-1902 |
| 486 | + void mapsNestedEmbeddedType() { |
| 487 | + |
| 488 | + WrapperAroundWithEmbedded probe = new WrapperAroundWithEmbedded(); |
| 489 | + probe.withEmbedded = new WithEmbedded(); |
| 490 | + probe.withEmbedded.embeddableType = new EmbeddableType(); |
| 491 | + probe.withEmbedded.embeddableType.atFieldAnnotatedValue = "@Field"; |
| 492 | + probe.withEmbedded.embeddableType.stringValue = "string-value"; |
| 493 | + |
| 494 | + org.bson.Document document = mapper.getMappedExample(Example.of(probe, UntypedExampleMatcher.matching())); |
| 495 | + assertThat(document).containsEntry("withEmbedded.stringValue", "string-value") |
| 496 | + .containsEntry("withEmbedded.with-at-field-annotation", "@Field"); |
| 497 | + } |
| 498 | + |
| 499 | + @Test // DATAMONGO-1902 |
| 500 | + void mapsNestedPrefixedEmbeddedType() { |
| 501 | + |
| 502 | + WrapperAroundWithEmbedded probe = new WrapperAroundWithEmbedded(); |
| 503 | + probe.withEmbedded = new WithEmbedded(); |
| 504 | + probe.withEmbedded.prefixedEmbeddableValue = new EmbeddableType(); |
| 505 | + probe.withEmbedded.prefixedEmbeddableValue.atFieldAnnotatedValue = "@Field"; |
| 506 | + probe.withEmbedded.prefixedEmbeddableValue.stringValue = "string-value"; |
| 507 | + |
| 508 | + org.bson.Document document = mapper.getMappedExample(Example.of(probe, UntypedExampleMatcher.matching())); |
| 509 | + assertThat(document).containsEntry("withEmbedded.prefix-stringValue", "string-value") |
| 510 | + .containsEntry("withEmbedded.prefix-with-at-field-annotation", "@Field"); |
462 | 511 | }
|
463 | 512 |
|
464 | 513 | static class FlatDocument {
|
@@ -488,4 +537,29 @@ static class ReferenceDocument {
|
488 | 537 | @Id String id;
|
489 | 538 | String value;
|
490 | 539 | }
|
| 540 | + |
| 541 | + @Document |
| 542 | + static class WrapperAroundWithEmbedded { |
| 543 | + |
| 544 | + String id; |
| 545 | + WithEmbedded withEmbedded; |
| 546 | + } |
| 547 | + |
| 548 | + @Document |
| 549 | + static class WithEmbedded { |
| 550 | + |
| 551 | + String id; |
| 552 | + |
| 553 | + @Embedded.Nullable EmbeddableType embeddableType; |
| 554 | + @Embedded.Nullable("prefix-") EmbeddableType prefixedEmbeddableValue; |
| 555 | + } |
| 556 | + |
| 557 | + static class EmbeddableType { |
| 558 | + |
| 559 | + @Indexed String stringValue; |
| 560 | + |
| 561 | + @Indexed // |
| 562 | + @Field("with-at-field-annotation") // |
| 563 | + String atFieldAnnotatedValue; |
| 564 | + } |
491 | 565 | }
|
0 commit comments