Browse Source

DATAMONGO-2440 - Polishing.

Iterate over List instead of using the Stream API.

Reduce visibility of JUnit 5 test methods. Consolidate assertions.

Original pull request: #817.
pull/819/head
Mark Paluch 6 years ago
parent
commit
e2200eccf7
No known key found for this signature in database
GPG Key ID: 51A00FA751B91849
  1. 16
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  2. 205
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

16
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

@ -19,12 +19,12 @@ import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.bson.BsonValue; import org.bson.BsonValue;
import org.bson.Document; import org.bson.Document;
import org.bson.conversions.Bson; import org.bson.conversions.Bson;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Example; import org.springframework.data.domain.Example;
@ -642,7 +642,7 @@ public class QueryMapper {
return false; return false;
} }
return isKeyword(keys.iterator().next().toString()); return isKeyword(keys.iterator().next());
} }
/** /**
@ -690,10 +690,16 @@ public class QueryMapper {
if (value instanceof Collection) { if (value instanceof Collection) {
return ((Collection<Object>) value).stream() Collection<Object> source = (Collection<Object>) value;
.map(it -> conversionService.convert(it, documentField.getProperty().getFieldType())) Collection<Object> converted = new ArrayList<>(source.size());
.collect(Collectors.toList());
for (Object o : source) {
converted.add(conversionService.convert(o, documentField.getProperty().getFieldType()));
}
return converted;
} }
return conversionService.convert(value, documentField.getProperty().getFieldType()); return conversionService.convert(value, documentField.getProperty().getFieldType());
} }

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

@ -79,7 +79,7 @@ public class QueryMapperUnitTests {
@Mock MongoDbFactory factory; @Mock MongoDbFactory factory;
@BeforeEach @BeforeEach
public void beforeEach() { void beforeEach() {
this.context = new MongoMappingContext(); this.context = new MongoMappingContext();
@ -90,18 +90,18 @@ public class QueryMapperUnitTests {
} }
@Test @Test
public void translatesIdPropertyIntoIdKey() { void translatesIdPropertyIntoIdKey() {
org.bson.Document query = new org.bson.Document("foo", "value"); org.bson.Document query = new org.bson.Document("foo", "value");
MongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(Sample.class); MongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(Sample.class);
org.bson.Document result = mapper.getMappedObject(query, entity); org.bson.Document result = mapper.getMappedObject(query, entity);
assertThat(result.get("_id")).isNotNull(); assertThat(result).containsKey("_id");
assertThat(result.get("foo")).isNull(); assertThat(result).doesNotContainKey("foo");
} }
@Test @Test
public void convertsStringIntoObjectId() { void convertsStringIntoObjectId() {
org.bson.Document query = new org.bson.Document("_id", new ObjectId().toString()); org.bson.Document query = new org.bson.Document("_id", new ObjectId().toString());
org.bson.Document result = mapper.getMappedObject(query, context.getPersistentEntity(IdWrapper.class)); org.bson.Document result = mapper.getMappedObject(query, context.getPersistentEntity(IdWrapper.class));
@ -109,24 +109,24 @@ public class QueryMapperUnitTests {
} }
@Test @Test
public void handlesBigIntegerIdsCorrectly() { void handlesBigIntegerIdsCorrectly() {
org.bson.Document document = new org.bson.Document("id", new BigInteger("1")); org.bson.Document document = new org.bson.Document("id", new BigInteger("1"));
org.bson.Document result = mapper.getMappedObject(document, context.getPersistentEntity(IdWrapper.class)); org.bson.Document result = mapper.getMappedObject(document, context.getPersistentEntity(IdWrapper.class));
assertThat(result.get("_id")).isEqualTo("1"); assertThat(result).containsEntry("_id", "1");
} }
@Test @Test
public void handlesObjectIdCapableBigIntegerIdsCorrectly() { void handlesObjectIdCapableBigIntegerIdsCorrectly() {
ObjectId id = new ObjectId(); ObjectId id = new ObjectId();
org.bson.Document document = new org.bson.Document("id", new BigInteger(id.toString(), 16)); org.bson.Document document = new org.bson.Document("id", new BigInteger(id.toString(), 16));
org.bson.Document result = mapper.getMappedObject(document, context.getPersistentEntity(IdWrapper.class)); org.bson.Document result = mapper.getMappedObject(document, context.getPersistentEntity(IdWrapper.class));
assertThat(result.get("_id")).isEqualTo(id); assertThat(result).containsEntry("_id", id);
} }
@Test // DATAMONGO-278 @Test // DATAMONGO-278
public void translates$NeCorrectly() { void translates$NeCorrectly() {
Criteria criteria = where("foo").ne(new ObjectId().toString()); Criteria criteria = where("foo").ne(new ObjectId().toString());
@ -139,7 +139,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-326 @Test // DATAMONGO-326
public void handlesEnumsCorrectly() { void handlesEnumsCorrectly() {
Query query = query(where("foo").is(Enum.INSTANCE)); Query query = query(where("foo").is(Enum.INSTANCE));
org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), Optional.empty()); org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), Optional.empty());
@ -148,7 +148,7 @@ public class QueryMapperUnitTests {
} }
@Test @Test
public void handlesEnumsInNotEqualCorrectly() { void handlesEnumsInNotEqualCorrectly() {
Query query = query(where("foo").ne(Enum.INSTANCE)); Query query = query(where("foo").ne(Enum.INSTANCE));
org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), Optional.empty()); org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), Optional.empty());
@ -156,12 +156,11 @@ public class QueryMapperUnitTests {
assertThat(object).isInstanceOf(org.bson.Document.class); assertThat(object).isInstanceOf(org.bson.Document.class);
Object ne = ((org.bson.Document) object).get("$ne"); Object ne = ((org.bson.Document) object).get("$ne");
assertThat(ne).isInstanceOf(String.class); assertThat(ne).isInstanceOf(String.class).hasToString(Enum.INSTANCE.name());
assertThat(ne.toString()).isEqualTo(Enum.INSTANCE.name());
} }
@Test @Test
public void handlesEnumsIn$InCorrectly() { void handlesEnumsIn$InCorrectly() {
Query query = query(where("foo").in(Enum.INSTANCE)); Query query = query(where("foo").in(Enum.INSTANCE));
org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), Optional.empty()); org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), Optional.empty());
@ -174,30 +173,29 @@ public class QueryMapperUnitTests {
List list = (List) in; List list = (List) in;
assertThat(list).hasSize(1); assertThat(list).hasSize(1);
assertThat(list.get(0)).isInstanceOf(String.class); assertThat(list.get(0)).isInstanceOf(String.class).hasToString(Enum.INSTANCE.name());
assertThat(list.get(0).toString()).isEqualTo(Enum.INSTANCE.name());
} }
@Test // DATAMONGO-373 @Test // DATAMONGO-373
public void handlesNativelyBuiltQueryCorrectly() { void handlesNativelyBuiltQueryCorrectly() {
DBObject query = new QueryBuilder().or(new BasicDBObject("foo", "bar")).get(); DBObject query = new QueryBuilder().or(new BasicDBObject("foo", "bar")).get();
mapper.getMappedObject(new org.bson.Document(query.toMap()), Optional.empty()); mapper.getMappedObject(new org.bson.Document(query.toMap()), Optional.empty());
} }
@Test // DATAMONGO-369 @Test // DATAMONGO-369
public void handlesAllPropertiesIfDocument() { void handlesAllPropertiesIfDocument() {
org.bson.Document query = new org.bson.Document(); org.bson.Document query = new org.bson.Document();
query.put("foo", new org.bson.Document("$in", Arrays.asList(1, 2))); query.put("foo", new org.bson.Document("$in", Arrays.asList(1, 2)));
query.put("bar", new Person()); query.put("bar", new Person());
org.bson.Document result = mapper.getMappedObject(query, Optional.empty()); org.bson.Document result = mapper.getMappedObject(query, Optional.empty());
assertThat(result.get("bar")).isNotNull(); assertThat(result).containsKey("bar");
} }
@Test // DATAMONGO-429 @Test // DATAMONGO-429
public void transformsArraysCorrectly() { void transformsArraysCorrectly() {
Query query = new BasicQuery("{ 'tags' : { '$all' : [ 'green', 'orange']}}"); Query query = new BasicQuery("{ 'tags' : { '$all' : [ 'green', 'orange']}}");
@ -206,7 +204,7 @@ public class QueryMapperUnitTests {
} }
@Test @Test
public void doesHandleNestedFieldsWithDefaultIdNames() { void doesHandleNestedFieldsWithDefaultIdNames() {
org.bson.Document document = new org.bson.Document("id", new ObjectId().toString()); org.bson.Document document = new org.bson.Document("id", new ObjectId().toString());
document.put("nested", new org.bson.Document("id", new ObjectId().toString())); document.put("nested", new org.bson.Document("id", new ObjectId().toString()));
@ -219,7 +217,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-493 @Test // DATAMONGO-493
public void doesNotTranslateNonIdPropertiesFor$NeCriteria() { void doesNotTranslateNonIdPropertiesFor$NeCriteria() {
ObjectId accidentallyAnObjectId = new ObjectId(); ObjectId accidentallyAnObjectId = new ObjectId();
@ -231,12 +229,12 @@ public class QueryMapperUnitTests {
assertThat(document.get("publishers")).isInstanceOf(org.bson.Document.class); assertThat(document.get("publishers")).isInstanceOf(org.bson.Document.class);
org.bson.Document publishers = (org.bson.Document) document.get("publishers"); org.bson.Document publishers = (org.bson.Document) document.get("publishers");
assertThat(publishers.containsKey("$ne")).isTrue(); assertThat(publishers).containsKey("$ne");
assertThat(publishers.get("$ne")).isInstanceOf(String.class); assertThat(publishers.get("$ne")).isInstanceOf(String.class);
} }
@Test // DATAMONGO-494 @Test // DATAMONGO-494
public void usesEntityMetadataInOr() { void usesEntityMetadataInOr() {
Query query = query(new Criteria().orOperator(where("foo").is("bar"))); Query query = query(new Criteria().orOperator(where("foo").is("bar")));
org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), org.bson.Document result = mapper.getMappedObject(query.getQueryObject(),
@ -248,44 +246,43 @@ public class QueryMapperUnitTests {
assertThat(ors).hasSize(1); assertThat(ors).hasSize(1);
org.bson.Document criterias = getAsDocument(ors, 0); org.bson.Document criterias = getAsDocument(ors, 0);
assertThat(criterias.keySet()).hasSize(1).doesNotContain("foo"); assertThat(criterias.keySet()).hasSize(1).doesNotContain("foo");
assertThat(criterias.get("_id")).isNotNull(); assertThat(criterias).containsKey("_id");
} }
@Test @Test
public void translatesPropertyReferenceCorrectly() { void translatesPropertyReferenceCorrectly() {
Query query = query(where("field").is(new CustomizedField())); Query query = query(where("field").is(new CustomizedField()));
org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), org.bson.Document result = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(CustomizedField.class)); context.getPersistentEntity(CustomizedField.class));
assertThat(result.containsKey("foo")).isTrue(); assertThat(result).containsKey("foo").hasSize(1);
assertThat(result.keySet()).hasSize(1);
} }
@Test @Test
public void translatesNestedPropertyReferenceCorrectly() { void translatesNestedPropertyReferenceCorrectly() {
Query query = query(where("field.field").is(new CustomizedField())); Query query = query(where("field.field").is(new CustomizedField()));
org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), org.bson.Document result = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(CustomizedField.class)); context.getPersistentEntity(CustomizedField.class));
assertThat(result.containsKey("foo.foo")).isTrue(); assertThat(result).containsKey("foo.foo");
assertThat(result.keySet()).hasSize(1); assertThat(result.keySet()).hasSize(1);
} }
@Test @Test
public void returnsOriginalKeyIfNoPropertyReference() { void returnsOriginalKeyIfNoPropertyReference() {
Query query = query(where("bar").is(new CustomizedField())); Query query = query(where("bar").is(new CustomizedField()));
org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), org.bson.Document result = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(CustomizedField.class)); context.getPersistentEntity(CustomizedField.class));
assertThat(result.containsKey("bar")).isTrue(); assertThat(result).containsKey("bar");
assertThat(result.keySet()).hasSize(1); assertThat(result.keySet()).hasSize(1);
} }
@Test @Test
public void convertsAssociationCorrectly() { void convertsAssociationCorrectly() {
Reference reference = new Reference(); Reference reference = new Reference();
reference.id = 5L; reference.id = 5L;
@ -300,7 +297,7 @@ public class QueryMapperUnitTests {
} }
@Test @Test
public void convertsNestedAssociationCorrectly() { void convertsNestedAssociationCorrectly() {
Reference reference = new Reference(); Reference reference = new Reference();
reference.id = 5L; reference.id = 5L;
@ -315,7 +312,7 @@ public class QueryMapperUnitTests {
} }
@Test @Test
public void convertsInKeywordCorrectly() { void convertsInKeywordCorrectly() {
Reference first = new Reference(); Reference first = new Reference();
first.id = 5L; first.id = 5L;
@ -336,7 +333,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-570 @Test // DATAMONGO-570
public void correctlyConvertsNullReference() { void correctlyConvertsNullReference() {
Query query = query(where("reference").is(null)); Query query = query(where("reference").is(null));
org.bson.Document object = mapper.getMappedObject(query.getQueryObject(), org.bson.Document object = mapper.getMappedObject(query.getQueryObject(),
@ -346,20 +343,20 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-629 @Test // DATAMONGO-629
public void doesNotMapIdIfNoEntityMetadataAvailable() { void doesNotMapIdIfNoEntityMetadataAvailable() {
String id = new ObjectId().toString(); String id = new ObjectId().toString();
Query query = query(where("id").is(id)); Query query = query(where("id").is(id));
org.bson.Document object = mapper.getMappedObject(query.getQueryObject(), Optional.empty()); org.bson.Document object = mapper.getMappedObject(query.getQueryObject(), Optional.empty());
assertThat(object.containsKey("id")).isTrue(); assertThat(object).containsKey("id");
assertThat(object.get("id")).isEqualTo(id); assertThat(object).containsEntry("id", id);
assertThat(object.containsKey("_id")).isFalse(); assertThat(object).doesNotContainKey("_id");
} }
@Test // DATAMONGO-677 @Test // DATAMONGO-677
public void handleMapWithDBRefCorrectly() { void handleMapWithDBRefCorrectly() {
org.bson.Document mapDocument = new org.bson.Document(); org.bson.Document mapDocument = new org.bson.Document();
mapDocument.put("test", new com.mongodb.DBRef("test", "test")); mapDocument.put("test", new com.mongodb.DBRef("test", "test"));
@ -368,24 +365,24 @@ public class QueryMapperUnitTests {
org.bson.Document mapped = mapper.getMappedObject(document, context.getPersistentEntity(WithMapDBRef.class)); org.bson.Document mapped = mapper.getMappedObject(document, context.getPersistentEntity(WithMapDBRef.class));
assertThat(mapped.containsKey("mapWithDBRef")).isTrue(); assertThat(mapped).containsKey("mapWithDBRef");
assertThat(mapped.get("mapWithDBRef")).isInstanceOf(org.bson.Document.class); assertThat(mapped.get("mapWithDBRef")).isInstanceOf(org.bson.Document.class);
assertThat(((org.bson.Document) mapped.get("mapWithDBRef")).containsKey("test")).isTrue(); assertThat(((org.bson.Document) mapped.get("mapWithDBRef"))).containsKey("test");
assertThat(((org.bson.Document) mapped.get("mapWithDBRef")).get("test")).isInstanceOf(com.mongodb.DBRef.class); assertThat(((org.bson.Document) mapped.get("mapWithDBRef")).get("test")).isInstanceOf(com.mongodb.DBRef.class);
} }
@Test @Test
public void convertsUnderscoreIdValueWithoutMetadata() { void convertsUnderscoreIdValueWithoutMetadata() {
org.bson.Document document = new org.bson.Document().append("_id", new ObjectId().toString()); org.bson.Document document = new org.bson.Document().append("_id", new ObjectId().toString());
org.bson.Document mapped = mapper.getMappedObject(document, Optional.empty()); org.bson.Document mapped = mapper.getMappedObject(document, Optional.empty());
assertThat(mapped.containsKey("_id")).isTrue(); assertThat(mapped).containsKey("_id");
assertThat(mapped.get("_id")).isInstanceOf(ObjectId.class); assertThat(mapped.get("_id")).isInstanceOf(ObjectId.class);
} }
@Test // DATAMONGO-705 @Test // DATAMONGO-705
public void convertsDBRefWithExistsQuery() { void convertsDBRefWithExistsQuery() {
Query query = query(where("reference").exists(false)); Query query = query(where("reference").exists(false));
@ -393,12 +390,12 @@ public class QueryMapperUnitTests {
org.bson.Document mappedObject = mapper.getMappedObject(query.getQueryObject(), entity); org.bson.Document mappedObject = mapper.getMappedObject(query.getQueryObject(), entity);
org.bson.Document reference = getAsDocument(mappedObject, "reference"); org.bson.Document reference = getAsDocument(mappedObject, "reference");
assertThat(reference.containsKey("$exists")).isTrue(); assertThat(reference).containsKey("$exists");
assertThat(reference.get("$exists")).isEqualTo(false); assertThat(reference).containsEntry("$exists", false);
} }
@Test // DATAMONGO-706 @Test // DATAMONGO-706
public void convertsNestedDBRefsCorrectly() { void convertsNestedDBRefsCorrectly() {
Reference reference = new Reference(); Reference reference = new Reference();
reference.id = 5L; reference.id = 5L;
@ -408,7 +405,7 @@ public class QueryMapperUnitTests {
BasicMongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(WithDBRef.class); BasicMongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(WithDBRef.class);
org.bson.Document mappedObject = mapper.getMappedObject(query.getQueryObject(), entity); org.bson.Document mappedObject = mapper.getMappedObject(query.getQueryObject(), entity);
assertThat(mappedObject.get("someString")).isEqualTo("foo"); assertThat(mappedObject).containsEntry("someString", "foo");
List<Object> andClause = getAsDBList(mappedObject, "$and"); List<Object> andClause = getAsDBList(mappedObject, "$and");
assertThat(andClause).hasSize(1); assertThat(andClause).hasSize(1);
@ -419,29 +416,29 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-752 @Test // DATAMONGO-752
public void mapsSimpleValuesStartingWith$Correctly() { void mapsSimpleValuesStartingWith$Correctly() {
Query query = query(where("myvalue").is("$334")); Query query = query(where("myvalue").is("$334"));
org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), Optional.empty()); org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), Optional.empty());
assertThat(result.keySet()).hasSize(1); assertThat(result.keySet()).hasSize(1);
assertThat(result.get("myvalue")).isEqualTo("$334"); assertThat(result).containsEntry("myvalue", "$334");
} }
@Test // DATAMONGO-752 @Test // DATAMONGO-752
public void mapsKeywordAsSimpleValuesCorrectly() { void mapsKeywordAsSimpleValuesCorrectly() {
Query query = query(where("myvalue").is("$center")); Query query = query(where("myvalue").is("$center"));
org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), Optional.empty()); org.bson.Document result = mapper.getMappedObject(query.getQueryObject(), Optional.empty());
assertThat(result.keySet()).hasSize(1); assertThat(result.keySet()).hasSize(1);
assertThat(result.get("myvalue")).isEqualTo("$center"); assertThat(result).containsEntry("myvalue", "$center");
} }
@Test // DATAMONGO-805 @Test // DATAMONGO-805
public void shouldExcludeDBRefAssociation() { void shouldExcludeDBRefAssociation() {
Query query = query(where("someString").is("foo")); Query query = query(where("someString").is("foo"));
query.fields().exclude("reference"); query.fields().exclude("reference");
@ -450,12 +447,12 @@ public class QueryMapperUnitTests {
org.bson.Document queryResult = mapper.getMappedObject(query.getQueryObject(), entity); org.bson.Document queryResult = mapper.getMappedObject(query.getQueryObject(), entity);
org.bson.Document fieldsResult = mapper.getMappedObject(query.getFieldsObject(), entity); org.bson.Document fieldsResult = mapper.getMappedObject(query.getFieldsObject(), entity);
assertThat(queryResult.get("someString")).isEqualTo("foo"); assertThat(queryResult).containsEntry("someString", "foo");
assertThat(fieldsResult.get("reference")).isEqualTo(0); assertThat(fieldsResult).containsEntry("reference", 0);
} }
@Test // DATAMONGO-686 @Test // DATAMONGO-686
public void queryMapperShouldNotChangeStateInGivenQueryObjectWhenIdConstrainedByInList() { void queryMapperShouldNotChangeStateInGivenQueryObjectWhenIdConstrainedByInList() {
BasicMongoPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(Sample.class); BasicMongoPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(Sample.class);
String idPropertyName = persistentEntity.getIdProperty().getName(); String idPropertyName = persistentEntity.getIdProperty().getName();
@ -469,7 +466,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-821 @Test // DATAMONGO-821
public void queryMapperShouldNotTryToMapDBRefListPropertyIfNestedInsideDocumentWithinDocument() { void queryMapperShouldNotTryToMapDBRefListPropertyIfNestedInsideDocumentWithinDocument() {
org.bson.Document queryObject = query( org.bson.Document queryObject = query(
where("referenceList").is(new org.bson.Document("$nested", new org.bson.Document("$keys", 0L)))) where("referenceList").is(new org.bson.Document("$nested", new org.bson.Document("$keys", 0L))))
@ -484,7 +481,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-821 @Test // DATAMONGO-821
public void queryMapperShouldNotTryToMapDBRefPropertyIfNestedInsideDocumentWithinDocument() { void queryMapperShouldNotTryToMapDBRefPropertyIfNestedInsideDocumentWithinDocument() {
org.bson.Document queryObject = query( org.bson.Document queryObject = query(
where("reference").is(new org.bson.Document("$nested", new org.bson.Document("$keys", 0L)))).getQueryObject(); where("reference").is(new org.bson.Document("$nested", new org.bson.Document("$keys", 0L)))).getQueryObject();
@ -497,7 +494,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-821 @Test // DATAMONGO-821
public void queryMapperShouldMapDBRefPropertyIfNestedInDocument() { void queryMapperShouldMapDBRefPropertyIfNestedInDocument() {
Reference sample = new Reference(); Reference sample = new Reference();
sample.id = 321L; sample.id = 321L;
@ -513,7 +510,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-773 @Test // DATAMONGO-773
public void queryMapperShouldBeAbleToProcessQueriesThatIncludeDbRefFields() { void queryMapperShouldBeAbleToProcessQueriesThatIncludeDbRefFields() {
BasicMongoPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(WithDBRef.class); BasicMongoPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(WithDBRef.class);
@ -525,7 +522,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-893 @Test // DATAMONGO-893
public void classInformationShouldNotBePresentInDocumentUsedInFinderMethods() { void classInformationShouldNotBePresentInDocumentUsedInFinderMethods() {
EmbeddedClass embedded = new EmbeddedClass(); EmbeddedClass embedded = new EmbeddedClass();
embedded.id = "1"; embedded.id = "1";
@ -539,7 +536,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1406 @Test // DATAMONGO-1406
public void shouldMapQueryForNestedCustomizedPropertiesUsingConfiguredFieldNames() { void shouldMapQueryForNestedCustomizedPropertiesUsingConfiguredFieldNames() {
EmbeddedClass embeddedClass = new EmbeddedClass(); EmbeddedClass embeddedClass = new EmbeddedClass();
embeddedClass.customizedField = "hello"; embeddedClass.customizedField = "hello";
@ -558,7 +555,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-647 @Test // DATAMONGO-647
public void customizedFieldNameShouldBeMappedCorrectlyWhenApplyingSort() { void customizedFieldNameShouldBeMappedCorrectlyWhenApplyingSort() {
Query query = query(where("field").is("bar")).with(Sort.by(Direction.DESC, "field")); Query query = query(where("field").is("bar")).with(Sort.by(Direction.DESC, "field"));
org.bson.Document document = mapper.getMappedObject(query.getSortObject(), org.bson.Document document = mapper.getMappedObject(query.getSortObject(),
@ -567,7 +564,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-973 @Test // DATAMONGO-973
public void getMappedFieldsAppendsTextScoreFieldProperlyCorrectlyWhenNotPresent() { void getMappedFieldsAppendsTextScoreFieldProperlyCorrectlyWhenNotPresent() {
Query query = new Query(); Query query = new Query();
@ -579,7 +576,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-973 @Test // DATAMONGO-973
public void getMappedFieldsReplacesTextScoreFieldProperlyCorrectlyWhenPresent() { void getMappedFieldsReplacesTextScoreFieldProperlyCorrectlyWhenPresent() {
Query query = new Query(); Query query = new Query();
query.fields().include("textScore"); query.fields().include("textScore");
@ -592,7 +589,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-973 @Test // DATAMONGO-973
public void getMappedSortAppendsTextScoreProperlyWhenSortedByScore() { void getMappedSortAppendsTextScoreProperlyWhenSortedByScore() {
Query query = new Query().with(Sort.by("textScore")); Query query = new Query().with(Sort.by("textScore"));
@ -604,7 +601,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-973 @Test // DATAMONGO-973
public void getMappedSortIgnoresTextScoreWhenNotSortedByScore() { void getMappedSortIgnoresTextScoreWhenNotSortedByScore() {
Query query = new Query().with(Sort.by("id")); Query query = new Query().with(Sort.by("id"));
@ -615,20 +612,20 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1070, DATAMONGO-1798 @Test // DATAMONGO-1070, DATAMONGO-1798
public void mapsIdReferenceToDBRefCorrectly() { void mapsIdReferenceToDBRefCorrectly() {
ObjectId id = new ObjectId(); ObjectId id = new ObjectId();
org.bson.Document query = new org.bson.Document("reference.id", new com.mongodb.DBRef("reference", id)); org.bson.Document query = new org.bson.Document("reference.id", new com.mongodb.DBRef("reference", id));
org.bson.Document result = mapper.getMappedObject(query, context.getPersistentEntity(WithDBRef.class)); org.bson.Document result = mapper.getMappedObject(query, context.getPersistentEntity(WithDBRef.class));
assertThat(result.containsKey("reference")).isTrue(); assertThat(result).containsKey("reference");
com.mongodb.DBRef reference = getTypedValue(result, "reference", com.mongodb.DBRef.class); com.mongodb.DBRef reference = getTypedValue(result, "reference", com.mongodb.DBRef.class);
assertThat(reference.getId()).isInstanceOf(ObjectId.class); assertThat(reference.getId()).isInstanceOf(ObjectId.class);
} }
@Test // DATAMONGO-1050 @Test // DATAMONGO-1050
public void shouldUseExplicitlySetFieldnameForIdPropertyCandidates() { void shouldUseExplicitlySetFieldnameForIdPropertyCandidates() {
Query query = query(where("nested.id").is("bar")); Query query = query(where("nested.id").is("bar"));
@ -639,7 +636,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1050 @Test // DATAMONGO-1050
public void shouldUseExplicitlySetFieldnameForIdPropertyCandidatesUsedInSortClause() { void shouldUseExplicitlySetFieldnameForIdPropertyCandidatesUsedInSortClause() {
Query query = new Query().with(Sort.by("nested.id")); Query query = new Query().with(Sort.by("nested.id"));
@ -650,7 +647,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1135 @Test // DATAMONGO-1135
public void nearShouldUseGeoJsonRepresentationOnUnmappedProperty() { void nearShouldUseGeoJsonRepresentationOnUnmappedProperty() {
Query query = query(where("foo").near(new GeoJsonPoint(100, 50))); Query query = query(where("foo").near(new GeoJsonPoint(100, 50)));
@ -663,7 +660,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1135 @Test // DATAMONGO-1135
public void nearShouldUseGeoJsonRepresentationWhenMappingToGoJsonType() { void nearShouldUseGeoJsonRepresentationWhenMappingToGoJsonType() {
Query query = query(where("geoJsonPoint").near(new GeoJsonPoint(100, 50))); Query query = query(where("geoJsonPoint").near(new GeoJsonPoint(100, 50)));
@ -674,7 +671,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1135 @Test // DATAMONGO-1135
public void nearSphereShouldUseGeoJsonRepresentationWhenMappingToGoJsonType() { void nearSphereShouldUseGeoJsonRepresentationWhenMappingToGoJsonType() {
Query query = query(where("geoJsonPoint").nearSphere(new GeoJsonPoint(100, 50))); Query query = query(where("geoJsonPoint").nearSphere(new GeoJsonPoint(100, 50)));
@ -685,7 +682,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1135 @Test // DATAMONGO-1135
public void shouldMapNameCorrectlyForGeoJsonType() { void shouldMapNameCorrectlyForGeoJsonType() {
Query query = query(where("namedGeoJsonPoint").nearSphere(new GeoJsonPoint(100, 50))); Query query = query(where("namedGeoJsonPoint").nearSphere(new GeoJsonPoint(100, 50)));
@ -696,7 +693,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1135 @Test // DATAMONGO-1135
public void withinShouldUseGeoJsonPolygonWhenMappingPolygonOn2DSphereIndex() { void withinShouldUseGeoJsonPolygonWhenMappingPolygonOn2DSphereIndex() {
Query query = query(where("geoJsonPoint") Query query = query(where("geoJsonPoint")
.within(new GeoJsonPolygon(new Point(0, 0), new Point(100, 100), new Point(100, 0), new Point(0, 0)))); .within(new GeoJsonPolygon(new Point(0, 0), new Point(100, 100), new Point(100, 0), new Point(0, 0))));
@ -708,7 +705,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1134 @Test // DATAMONGO-1134
public void intersectsShouldUseGeoJsonRepresentationCorrectly() { void intersectsShouldUseGeoJsonRepresentationCorrectly() {
Query query = query(where("geoJsonPoint") Query query = query(where("geoJsonPoint")
.intersects(new GeoJsonPolygon(new Point(0, 0), new Point(100, 100), new Point(100, 0), new Point(0, 0)))); .intersects(new GeoJsonPolygon(new Point(0, 0), new Point(100, 100), new Point(100, 0), new Point(0, 0))));
@ -721,29 +718,29 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1269 @Test // DATAMONGO-1269
public void mappingShouldRetainNumericMapKey() { void mappingShouldRetainNumericMapKey() {
Query query = query(where("map.1.stringProperty").is("ba'alzamon")); Query query = query(where("map.1.stringProperty").is("ba'alzamon"));
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(EntityWithComplexValueTypeMap.class)); context.getPersistentEntity(EntityWithComplexValueTypeMap.class));
assertThat(document.containsKey("map.1.stringProperty")).isTrue(); assertThat(document).containsKey("map.1.stringProperty");
} }
@Test // DATAMONGO-1269 @Test // DATAMONGO-1269
public void mappingShouldRetainNumericPositionInList() { void mappingShouldRetainNumericPositionInList() {
Query query = query(where("list.1.stringProperty").is("ba'alzamon")); Query query = query(where("list.1.stringProperty").is("ba'alzamon"));
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(EntityWithComplexValueTypeList.class)); context.getPersistentEntity(EntityWithComplexValueTypeList.class));
assertThat(document.containsKey("list.1.stringProperty")).isTrue(); assertThat(document).containsKey("list.1.stringProperty");
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
public void exampleShouldBeMappedCorrectly() { void exampleShouldBeMappedCorrectly() {
Foo probe = new Foo(); Foo probe = new Foo();
probe.embedded = new EmbeddedClass(); probe.embedded = new EmbeddedClass();
@ -757,7 +754,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1245 @Test // DATAMONGO-1245
public void exampleShouldBeMappedCorrectlyWhenContainingLegacyPoint() { void exampleShouldBeMappedCorrectlyWhenContainingLegacyPoint() {
ClassWithGeoTypes probe = new ClassWithGeoTypes(); ClassWithGeoTypes probe = new ClassWithGeoTypes();
probe.legacyPoint = new Point(10D, 20D); probe.legacyPoint = new Point(10D, 20D);
@ -767,12 +764,12 @@ public class QueryMapperUnitTests {
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(WithDBRef.class)); context.getPersistentEntity(WithDBRef.class));
assertThat(document.get("legacyPoint.x")).isEqualTo(10D); assertThat(document).containsEntry("legacyPoint.x", 10D);
assertThat(document.get("legacyPoint.y")).isEqualTo(20D); assertThat(document).containsEntry("legacyPoint.y", 20D);
} }
@Test // DATAMONGO-1988 @Test // DATAMONGO-1988
public void mapsStringObjectIdRepresentationToObjectIdWhenReferencingIdProperty() { void mapsStringObjectIdRepresentationToObjectIdWhenReferencingIdProperty() {
Query query = query(where("sample.foo").is(new ObjectId().toHexString())); Query query = query(where("sample.foo").is(new ObjectId().toHexString()));
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
@ -782,7 +779,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1988 @Test // DATAMONGO-1988
public void matchesExactFieldNameToIdProperty() { void matchesExactFieldNameToIdProperty() {
Query query = query(where("sample.iid").is(new ObjectId().toHexString())); Query query = query(where("sample.iid").is(new ObjectId().toHexString()));
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
@ -792,7 +789,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1988 @Test // DATAMONGO-1988
public void leavesNonObjectIdStringIdRepresentationUntouchedWhenReferencingIdProperty() { void leavesNonObjectIdStringIdRepresentationUntouchedWhenReferencingIdProperty() {
Query query = query(where("sample.foo").is("id-1")); Query query = query(where("sample.foo").is("id-1"));
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
@ -802,7 +799,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-2168 @Test // DATAMONGO-2168
public void getMappedObjectShouldNotMapTypeHint() { void getMappedObjectShouldNotMapTypeHint() {
converter.setTypeMapper(new DefaultMongoTypeMapper("className")); converter.setTypeMapper(new DefaultMongoTypeMapper("className"));
@ -813,7 +810,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-2168 @Test // DATAMONGO-2168
public void getMappedObjectShouldIgnorePathsLeadingToJavaLangClassProperties/* like Class#getName() */() { void getMappedObjectShouldIgnorePathsLeadingToJavaLangClassProperties/* like Class#getName() */() {
org.bson.Document update = new org.bson.Document("className", "foo"); org.bson.Document update = new org.bson.Document("className", "foo");
org.bson.Document mappedObject = mapper.getMappedObject(update, context.getPersistentEntity(UserEntity.class)); org.bson.Document mappedObject = mapper.getMappedObject(update, context.getPersistentEntity(UserEntity.class));
@ -822,7 +819,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-2193 @Test // DATAMONGO-2193
public void shouldNotConvertHexStringToObjectIdForRenamedNestedIdField() { void shouldNotConvertHexStringToObjectIdForRenamedNestedIdField() {
String idHex = new ObjectId().toHexString(); String idHex = new ObjectId().toHexString();
Query query = new Query(where("nested.id").is(idHex)); Query query = new Query(where("nested.id").is(idHex));
@ -834,7 +831,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-2221 @Test // DATAMONGO-2221
public void shouldNotConvertHexStringToObjectIdForRenamedDeeplyNestedIdField() { void shouldNotConvertHexStringToObjectIdForRenamedDeeplyNestedIdField() {
String idHex = new ObjectId().toHexString(); String idHex = new ObjectId().toHexString();
Query query = new Query(where("nested.deeplyNested.id").is(idHex)); Query query = new Query(where("nested.deeplyNested.id").is(idHex));
@ -846,7 +843,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-2221 @Test // DATAMONGO-2221
public void shouldNotConvertHexStringToObjectIdForUnresolvablePath() { void shouldNotConvertHexStringToObjectIdForUnresolvablePath() {
String idHex = new ObjectId().toHexString(); String idHex = new ObjectId().toHexString();
Query query = new Query(where("nested.unresolvablePath.id").is(idHex)); Query query = new Query(where("nested.unresolvablePath.id").is(idHex));
@ -858,7 +855,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1849 @Test // DATAMONGO-1849
public void shouldConvertPropertyWithExplicitTargetType() { void shouldConvertPropertyWithExplicitTargetType() {
String script = "if (a > b) a else b"; String script = "if (a > b) a else b";
Query query = new Query(where("script").is(script)); Query query = new Query(where("script").is(script));
@ -870,7 +867,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-1849 @Test // DATAMONGO-1849
public void shouldConvertCollectionPropertyWithExplicitTargetType() { void shouldConvertCollectionPropertyWithExplicitTargetType() {
String script = "if (a > b) a else b"; String script = "if (a > b) a else b";
Query query = new Query(where("scripts").is(script)); Query query = new Query(where("scripts").is(script));
@ -882,7 +879,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-2339 @Test // DATAMONGO-2339
public void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly() { void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly() {
org.bson.Document target = mapper.getMappedObject(new org.bson.Document("with_underscore", "id-1"), org.bson.Document target = mapper.getMappedObject(new org.bson.Document("with_underscore", "id-1"),
context.getPersistentEntity(WithIdPropertyContainingUnderscore.class)); context.getPersistentEntity(WithIdPropertyContainingUnderscore.class));
@ -891,7 +888,7 @@ public class QueryMapperUnitTests {
} }
@Test // DATAMONGO-2394 @Test // DATAMONGO-2394
public void leavesDistanceUntouchedWhenUsingGeoJson() { void leavesDistanceUntouchedWhenUsingGeoJson() {
Query query = query(where("geoJsonPoint").near(new GeoJsonPoint(27.987901, 86.9165379)).maxDistance(1000)); Query query = query(where("geoJsonPoint").near(new GeoJsonPoint(27.987901, 86.9165379)).maxDistance(1000));
@ -928,17 +925,17 @@ public class QueryMapperUnitTests {
} }
@Document @Document
public class Foo { class Foo {
@Id private ObjectId id; @Id private ObjectId id;
EmbeddedClass embedded; EmbeddedClass embedded;
@Field("my_items") List<EmbeddedClass> listOfItems; @Field("my_items") List<EmbeddedClass> listOfItems;
} }
public class EmbeddedClass { class EmbeddedClass {
public String id; String id;
@Field("fancy_custom_name") public String customizedField; @Field("fancy_custom_name") String customizedField;
} }
class IdWrapper { class IdWrapper {

Loading…
Cancel
Save