Browse Source

DATAMONGO-2294 - Polishing.

Reorganize imports after Delomboking. Use for-loop instead of Stream.forEach(…). Add Javadoc to methods. Add since tags.

Simplify tests.

Original pull request: #761.
pull/887/head
Mark Paluch 5 years ago
parent
commit
c0581c4943
No known key found for this signature in database
GPG Key ID: 51A00FA751B91849
  1. 112
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java
  2. 33
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java
  3. 28
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java

112
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java

@ -15,19 +15,19 @@
*/ */
package org.springframework.data.mongodb.core.query; package org.springframework.data.mongodb.core.query;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import lombok.EqualsAndHashCode;
import org.bson.Document; import org.bson.Document;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
/** /**
* Field projection.
*
* @author Thomas Risberg * @author Thomas Risberg
* @author Oliver Gierke * @author Oliver Gierke
* @author Patryk Wasik * @author Patryk Wasik
@ -37,50 +37,112 @@ import org.springframework.util.ObjectUtils;
*/ */
public class Field { public class Field {
private final Map<String, Integer> criteria = new HashMap<String, Integer>(); private final Map<String, Integer> criteria = new HashMap<>();
private final Map<String, Object> slices = new HashMap<String, Object>(); private final Map<String, Object> slices = new HashMap<>();
private final Map<String, Criteria> elemMatchs = new HashMap<String, Criteria>(); private final Map<String, Criteria> elemMatchs = new HashMap<>();
private @Nullable String positionKey; private @Nullable String positionKey;
private int positionValue; private int positionValue;
public Field include(String key) { /**
criteria.put(key, Integer.valueOf(1)); * Include a single {@code field} to be returned by the query operation.
*
* @param field the document field name to be included.
* @return {@code this} field projection instance.
*/
public Field include(String field) {
Assert.notNull(field, "Key must not be null!");
criteria.put(field, 1);
return this; return this;
} }
public Field includes(String... keys) { /**
Assert.notNull(keys, "Keys must not be null!"); * Include one or more {@code fields} to be returned by the query operation.
Assert.notEmpty(keys, "Keys must not be empty!"); *
* @param fields the document field names to be included.
* @return {@code this} field projection instance.
* @since 3.1
*/
public Field include(String... fields) {
Assert.notNull(fields, "Keys must not be null!");
for (String key : fields) {
criteria.put(key, 1);
}
Arrays.asList(keys).stream().forEach(this::include);
return this; return this;
} }
public Field exclude(String key) { /**
criteria.put(key, Integer.valueOf(0)); * Exclude a single {@code field} from being returned by the query operation.
*
* @param field the document field name to be included.
* @return {@code this} field projection instance.
*/
public Field exclude(String field) {
Assert.notNull(field, "Key must not be null!");
criteria.put(field, 0);
return this; return this;
} }
public Field excludes(String... keys) { /**
Assert.notNull(keys, "Keys must not be null!"); * Exclude one or more {@code fields} from being returned by the query operation.
Assert.notEmpty(keys, "Keys must not be empty!"); *
* @param fields the document field names to be included.
* @return {@code this} field projection instance.
* @since 3.1
*/
public Field exclude(String... fields) {
Assert.notNull(fields, "Keys must not be null!");
for (String key : fields) {
criteria.put(key, 0);
}
Arrays.asList(keys).stream().forEach(this::exclude);
return this; return this;
} }
public Field slice(String key, int size) { /**
slices.put(key, Integer.valueOf(size)); * Project a {@code $slice} of the array {@code field} using the first {@code size} elements.
*
* @param field the document field name to project, must be an array field.
* @param size the number of elements to include.
* @return {@code this} field projection instance.
*/
public Field slice(String field, int size) {
Assert.notNull(field, "Key must not be null!");
slices.put(field, size);
return this; return this;
} }
public Field slice(String key, int offset, int size) { /**
slices.put(key, new Integer[] { Integer.valueOf(offset), Integer.valueOf(size) }); * Project a {@code $slice} of the array {@code field} using the first {@code size} elements starting at
* {@code offset}.
*
* @param field the document field name to project, must be an array field.
* @param offset the offset to start at.
* @param size the number of elements to include.
* @return {@code this} field projection instance.
*/
public Field slice(String field, int offset, int size) {
slices.put(field, new Integer[] { offset, size });
return this; return this;
} }
public Field elemMatch(String key, Criteria elemMatchCriteria) { public Field elemMatch(String field, Criteria elemMatchCriteria) {
elemMatchs.put(key, elemMatchCriteria);
elemMatchs.put(field, elemMatchCriteria);
return this; return this;
} }
@ -90,7 +152,7 @@ public class Field {
* *
* @param field query array field, must not be {@literal null} or empty. * @param field query array field, must not be {@literal null} or empty.
* @param value * @param value
* @return * @return {@code this} field projection instance.
*/ */
public Field position(String field, int value) { public Field position(String field, int value) {

33
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

@ -15,18 +15,18 @@
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.mongodb.core.query.Criteria.*; import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.*; import static org.springframework.data.mongodb.core.query.Query.*;
import static org.springframework.data.mongodb.core.query.Update.*; import static org.springframework.data.mongodb.core.query.Update.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.Value;
import lombok.With;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
@ -38,13 +38,6 @@ import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.Value;
import lombok.experimental.Wither;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
@ -98,7 +91,6 @@ import org.springframework.util.StringUtils;
import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import com.mongodb.DBRef; import com.mongodb.DBRef;
import com.mongodb.MongoClient;
import com.mongodb.MongoException; import com.mongodb.MongoException;
import com.mongodb.ReadPreference; import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern; import com.mongodb.WriteConcern;
@ -1847,7 +1839,7 @@ public class MongoTemplateTests {
assertThat(result.property3).isEqualTo(obj.property3); assertThat(result.property3).isEqualTo(obj.property3);
} }
@Test // DATAMONGO-702 @Test // DATAMONGO-702, DATAMONGO-2294
public void queryShouldSupportRealAndAliasedPropertyNamesForFieldExclusions() { public void queryShouldSupportRealAndAliasedPropertyNamesForFieldExclusions() {
ObjectWith3AliasedFields obj = new ObjectWith3AliasedFields(); ObjectWith3AliasedFields obj = new ObjectWith3AliasedFields();
@ -1860,8 +1852,7 @@ public class MongoTemplateTests {
Query query = new Query(Criteria.where("id").is(obj.id)); Query query = new Query(Criteria.where("id").is(obj.id));
query.fields() // query.fields() //
.exclude("property2") // real property name .exclude("property2", "prop3"); // real property name, aliased property name
.exclude("prop3"); // aliased property name
ObjectWith3AliasedFields result = template.findOne(query, ObjectWith3AliasedFields.class); ObjectWith3AliasedFields result = template.findOne(query, ObjectWith3AliasedFields.class);
@ -3688,7 +3679,7 @@ public class MongoTemplateTests {
queryByChainedInclude.fields().include("id").include("name"); queryByChainedInclude.fields().include("id").include("name");
Query queryByCollectionInclude = query(where("name").is("Walter")); Query queryByCollectionInclude = query(where("name").is("Walter"));
queryByCollectionInclude.fields().includes("id", "name"); queryByCollectionInclude.fields().include("id", "name");
MyPerson first = template.findAndReplace(queryByChainedInclude, new MyPerson("Walter")); MyPerson first = template.findAndReplace(queryByChainedInclude, new MyPerson("Walter"));
MyPerson second = template.findAndReplace(queryByCollectionInclude, new MyPerson("Walter")); MyPerson second = template.findAndReplace(queryByCollectionInclude, new MyPerson("Walter"));
@ -4209,7 +4200,7 @@ public class MongoTemplateTests {
// DATAMONGO-1992 // DATAMONGO-1992
@AllArgsConstructor @AllArgsConstructor
@Wither @With
static class ImmutableVersioned { static class ImmutableVersioned {
final @Id String id; final @Id String id;
@ -4222,7 +4213,7 @@ public class MongoTemplateTests {
} }
@Value @Value
@Wither @With
static class ImmutableAudited { static class ImmutableAudited {
@Id String id; @Id String id;
@LastModifiedDate Instant modified; @LastModifiedDate Instant modified;

28
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java

@ -15,40 +15,40 @@
*/ */
package org.springframework.data.mongodb.core.query; package org.springframework.data.mongodb.core.query;
import static org.assertj.core.api.Assertions.*; import static org.springframework.data.mongodb.test.util.Assertions.*;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
* Unit tests for {@link DocumentField}. * Unit tests for {@link Field}.
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Owen Q * @author Owen Q
* @author Mark Paluch
*/ */
public class FieldUnitTests { class FieldUnitTests {
@Test @Test
public void sameObjectSetupCreatesEqualField() { void sameObjectSetupCreatesEqualField() {
Field left = new Field().elemMatch("key", Criteria.where("foo").is("bar")); Field left = new Field().elemMatch("key", Criteria.where("foo").is("bar"));
Field right = new Field().elemMatch("key", Criteria.where("foo").is("bar")); Field right = new Field().elemMatch("key", Criteria.where("foo").is("bar"));
assertThat(left).isEqualTo(right); assertThat(left).isEqualTo(right);
assertThat(right).isEqualTo(left); assertThat(right).isEqualTo(left);
assertThat(left.getFieldsObject()).isEqualTo("{key: { $elemMatch: {foo:\"bar\"}}}");
} }
@Test // DATAMONGO-2294 @Test // DATAMONGO-2294
public void sameObjectSetupCreatesEqualFieldByCollections() { void rendersInclusionCorrectly() {
Field left = new Field().includes("foo", "bar"); Field fields = new Field().include("foo", "bar").include("baz");
Field right = new Field().include("foo").include("bar");
assertThat(left, is(right)); assertThat(fields.getFieldsObject()).isEqualTo("{foo:1, bar:1, baz:1}");
assertThat(right, is(left));
} }
@Test @Test
public void differentObjectSetupCreatesEqualField() { void differentObjectSetupCreatesEqualField() {
Field left = new Field().elemMatch("key", Criteria.where("foo").is("bar")); Field left = new Field().elemMatch("key", Criteria.where("foo").is("bar"));
Field right = new Field().elemMatch("key", Criteria.where("foo").is("foo")); Field right = new Field().elemMatch("key", Criteria.where("foo").is("foo"));
@ -58,12 +58,10 @@ public class FieldUnitTests {
} }
@Test // DATAMONGO-2294 @Test // DATAMONGO-2294
public void differentObjectSetupCreatesEqualFieldByCollections() { void rendersExclusionCorrectly() {
Field left = new Field().includes("foo", "bar"); Field fields = new Field().exclude("foo", "bar").exclude("baz");
Field right = new Field().include("foo").include("zoo");
assertThat(left, is(not(right))); assertThat(fields.getFieldsObject()).isEqualTo("{foo:0, bar:0, baz:0}");
assertThat(right, is(not(left)));
} }
} }

Loading…
Cancel
Save