Skip to content

Commit d946954

Browse files
repository support
1 parent a415cb9 commit d946954

File tree

5 files changed

+185
-3
lines changed

5 files changed

+185
-3
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.mongodb.core.query.Criteria.*;
20+
import static org.springframework.data.mongodb.core.query.Query.*;
21+
22+
import lombok.EqualsAndHashCode;
23+
24+
import java.util.Arrays;
25+
import java.util.List;
26+
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.springframework.data.annotation.Embedded;
30+
import org.springframework.data.mongodb.core.mapping.Field;
31+
import org.springframework.data.mongodb.core.query.Query;
32+
import org.springframework.data.mongodb.test.util.MongoTemplateExtension;
33+
import org.springframework.data.mongodb.test.util.Template;
34+
35+
/**
36+
* @author Christoph Strobl
37+
*/
38+
@ExtendWith(MongoTemplateExtension.class)
39+
class MongoTemplateEmbeddedTests {
40+
41+
private static @Template MongoTemplate template;
42+
43+
@Test // DATAMONGO-1902
44+
void readWrite() {
45+
46+
WithEmbedded source = new WithEmbedded();
47+
source.id = "id-1";
48+
source.embeddableValue = new EmbeddableType();
49+
source.embeddableValue.stringValue = "string-val";
50+
source.embeddableValue.listValue = Arrays.asList("list-val-1", "list-val-2");
51+
source.embeddableValue.atFieldAnnotatedValue = "@Field";
52+
53+
template.save(source);
54+
55+
assertThat(template.findOne(query(where("id").is(source.id)), WithEmbedded.class)).isEqualTo(source);
56+
}
57+
58+
@Test // DATAMONGO-1902
59+
void filterOnEmbeddedValue() {
60+
61+
WithEmbedded source = new WithEmbedded();
62+
source.id = "id-1";
63+
source.embeddableValue = new EmbeddableType();
64+
source.embeddableValue.stringValue = "string-val";
65+
source.embeddableValue.listValue = Arrays.asList("list-val-1", "list-val-2");
66+
source.embeddableValue.atFieldAnnotatedValue = "@Field";
67+
68+
template.save(source);
69+
70+
assertThat(template.findOne(
71+
Query.query(where("embeddableValue.stringValue").is(source.embeddableValue.stringValue)), WithEmbedded.class))
72+
.isEqualTo(source);
73+
}
74+
75+
@Test // DATAMONGO-1902
76+
void readWritePrefixed() {
77+
78+
WithPrefixedEmbedded source = new WithPrefixedEmbedded();
79+
source.id = "id-1";
80+
source.embeddableValue = new EmbeddableType();
81+
source.embeddableValue.stringValue = "string-val";
82+
source.embeddableValue.listValue = Arrays.asList("list-val-1", "list-val-2");
83+
source.embeddableValue.atFieldAnnotatedValue = "@Field";
84+
85+
template.save(source);
86+
87+
assertThat(template.findOne(query(where("id").is(source.id)), WithPrefixedEmbedded.class)).isEqualTo(source);
88+
}
89+
90+
@Test // DATAMONGO-1902
91+
void filterOnPrefixedEmbeddedValue() {
92+
93+
WithPrefixedEmbedded source = new WithPrefixedEmbedded();
94+
source.id = "id-1";
95+
source.embeddableValue = new EmbeddableType();
96+
source.embeddableValue.stringValue = "string-val";
97+
source.embeddableValue.listValue = Arrays.asList("list-val-1", "list-val-2");
98+
source.embeddableValue.atFieldAnnotatedValue = "@Field";
99+
100+
template.save(source);
101+
102+
assertThat(
103+
template.findOne(Query.query(where("embeddableValue.stringValue").is(source.embeddableValue.stringValue)),
104+
WithPrefixedEmbedded.class)).isEqualTo(source);
105+
}
106+
107+
@EqualsAndHashCode
108+
static class WithEmbedded {
109+
110+
String id;
111+
112+
@Embedded.Nullable EmbeddableType embeddableValue;
113+
}
114+
115+
@EqualsAndHashCode
116+
static class WithPrefixedEmbedded {
117+
118+
String id;
119+
120+
@Embedded.Nullable("prefix-") EmbeddableType embeddableValue;
121+
}
122+
123+
@EqualsAndHashCode
124+
static class EmbeddableType {
125+
126+
String stringValue;
127+
List<String> listValue;
128+
129+
@Field("with-at-field-annotation") //
130+
String atFieldAnnotatedValue;
131+
}
132+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,8 +2195,6 @@ void writeFlattensEmbeddedType() {
21952195
org.bson.Document target = new org.bson.Document();
21962196
converter.write(source, target);
21972197

2198-
System.out.println("target.toJson(): " + target.toJson());
2199-
22002198
assertThat(target).containsEntry("_id", "id-1") //
22012199
.containsEntry("stringValue", "string-val") //
22022200
.containsEntry("listValue", Arrays.asList("list-val-1", "list-val-2")) //

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,41 @@ void findWithMoreThan10Arguments() {
13611361
void spelExpressionArgumentsGetReevaluatedOnEveryInvocation() {
13621362

13631363
assertThat(repository.findWithSpelByFirstnameForSpELExpressionWithParameterIndexOnly("Dave")).containsExactly(dave);
1364-
assertThat(repository.findWithSpelByFirstnameForSpELExpressionWithParameterIndexOnly("Carter")).containsExactly(carter);
1364+
assertThat(repository.findWithSpelByFirstnameForSpELExpressionWithParameterIndexOnly("Carter"))
1365+
.containsExactly(carter);
1366+
}
1367+
1368+
@Test // DATAMONGO-1902
1369+
void findByValueInsideEmbedded() {
1370+
1371+
Person bart = new Person("bart", "simpson");
1372+
User user = new User();
1373+
user.setUsername("bartman");
1374+
user.setId("84r1m4n");
1375+
bart.setEmbeddedUser(user);
1376+
1377+
operations.save(bart);
1378+
1379+
List<Person> result = repository.findByEmbeddedUserUsername(user.getUsername());
1380+
1381+
assertThat(result).hasSize(1);
1382+
assertThat(result.get(0).getId().equals(bart.getId()));
1383+
}
1384+
1385+
@Test // DATAMONGO-1902
1386+
void findByEmbedded() {
1387+
1388+
Person bart = new Person("bart", "simpson");
1389+
User user = new User();
1390+
user.setUsername("bartman");
1391+
user.setId("84r1m4n");
1392+
bart.setEmbeddedUser(user);
1393+
1394+
operations.save(bart);
1395+
1396+
List<Person> result = repository.findByEmbeddedUser(user);
1397+
1398+
assertThat(result).hasSize(1);
1399+
assertThat(result.get(0).getId().equals(bart.getId()));
13651400
}
13661401
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/Person.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Set;
2222
import java.util.UUID;
2323

24+
import org.springframework.data.annotation.Embedded;
2425
import org.springframework.data.geo.Point;
2526
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType;
2627
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
@@ -70,6 +71,9 @@ public enum Sex {
7071

7172
Credentials credentials;
7273

74+
@Embedded.Nullable(prefix = "u") //
75+
User embeddedUser;
76+
7377
public Person() {
7478

7579
this(null, null);
@@ -296,6 +300,14 @@ public List<String> getSkills() {
296300
return skills;
297301
}
298302

303+
public User getEmbeddedUser() {
304+
return embeddedUser;
305+
}
306+
307+
public void setEmbeddedUser(User embeddedUser) {
308+
this.embeddedUser = embeddedUser;
309+
}
310+
299311
/*
300312
* (non-Javadoc)
301313
*

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,9 @@ Page<Person> findByCustomQueryLastnameAndAddressStreetInList(String lastname, Li
401401
Person findPersonByManyArguments(String firstname, String lastname, String email, Integer age, Sex sex,
402402
Date createdAt, List<String> skills, String street, String zipCode, //
403403
String city, UUID uniqueId, String username, String password);
404+
405+
List<Person> findByEmbeddedUserUsername(String username);
406+
List<Person> findByEmbeddedUser(User user);
407+
408+
404409
}

0 commit comments

Comments
 (0)