Skip to content

Commit 9a5ddec

Browse files
Example mapping of embedded types
1 parent 585b922 commit 9a5ddec

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoExampleMapperUnitTests.java

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@
2828

2929
import org.bson.conversions.Bson;
3030
import org.junit.jupiter.api.BeforeEach;
31-
import org.junit.jupiter.api.Disabled;
3231
import org.junit.jupiter.api.Test;
3332
import org.junit.jupiter.api.extension.ExtendWith;
3433
import org.mockito.Mock;
3534
import org.mockito.junit.jupiter.MockitoExtension;
36-
35+
import org.springframework.data.annotation.Embedded;
3736
import org.springframework.data.annotation.Id;
3837
import org.springframework.data.domain.Example;
3938
import org.springframework.data.domain.ExampleMatcher;
4039
import org.springframework.data.domain.ExampleMatcher.*;
4140
import org.springframework.data.geo.Point;
4241
import org.springframework.data.mongodb.MongoDatabaseFactory;
42+
import org.springframework.data.mongodb.core.convert.MappingMongoConverterUnitTests.EmbeddableType;
4343
import org.springframework.data.mongodb.core.convert.QueryMapperUnitTests.ClassWithGeoTypes;
4444
import org.springframework.data.mongodb.core.convert.QueryMapperUnitTests.WithDBRef;
45+
import org.springframework.data.mongodb.core.index.Indexed;
4546
import org.springframework.data.mongodb.core.mapping.DBRef;
4647
import org.springframework.data.mongodb.core.mapping.Document;
4748
import org.springframework.data.mongodb.core.mapping.Field;
@@ -455,10 +456,58 @@ public void untypedExampleShouldNotInferTypeRestriction() {
455456
assertThat(document).doesNotContainKey("_class");
456457
}
457458

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");
462511
}
463512

464513
static class FlatDocument {
@@ -488,4 +537,29 @@ static class ReferenceDocument {
488537
@Id String id;
489538
String value;
490539
}
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+
}
491565
}

0 commit comments

Comments
 (0)