Browse Source

DATAMONGO-1834 - Polishing.

Remove DateFactory and split up tests.
Introduce dedicated Timezone abstraction and update existing factories to apply the timezone if appropriate. Update builders and align code style.

Original Pull Request: #539
pull/547/merge
Christoph Strobl 8 years ago
parent
commit
bc257aa260
  1. 92
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AbstractAggregationExpression.java
  2. 62
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/DateFactory.java
  3. 3485
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/DateOperators.java
  4. 899
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/DateOperatorsUnitTests.java
  5. 293
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java
  6. 2
      src/main/asciidoc/reference/mongodb.adoc

92
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AbstractAggregationExpression.java

@ -27,6 +27,7 @@ import org.springframework.util.ObjectUtils; @@ -27,6 +27,7 @@ import org.springframework.util.ObjectUtils;
/**
* @author Christoph Strobl
* @author Matt Morrissette
* @since 1.10
*/
abstract class AbstractAggregationExpression implements AggregationExpression {
@ -45,6 +46,7 @@ abstract class AbstractAggregationExpression implements AggregationExpression { @@ -45,6 +46,7 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
return toDocument(this.value, context);
}
@SuppressWarnings("unchecked")
public Document toDocument(Object value, AggregationOperationContext context) {
return new Document(getMongoMethod(), unpack(value, context));
}
@ -63,11 +65,13 @@ abstract class AbstractAggregationExpression implements AggregationExpression { @@ -63,11 +65,13 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
if (value instanceof AggregationExpression) {
return ((AggregationExpression) value).toDocument(context);
} else if (value instanceof DateFactory) {
return ((DateFactory) value).currentDate();
} else if (value instanceof Field) {
}
if (value instanceof Field) {
return context.getReference((Field) value).toString();
} else if (value instanceof List) {
}
if (value instanceof List) {
List<Object> sourceList = (List<Object>) value;
List<Object> mappedList = new ArrayList<>(sourceList.size());
@ -75,16 +79,21 @@ abstract class AbstractAggregationExpression implements AggregationExpression { @@ -75,16 +79,21 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
sourceList.stream().map((item) -> unpack(item, context)).forEach(mappedList::add);
return mappedList;
} else if (value instanceof java.util.Map) {
Document dbo = new Document();
((Map<String, Object>) value).forEach((k, v) -> dbo.put(k, unpack(v, context)));
return dbo;
}
if (value instanceof Map) {
Document targetDocument = new Document();
Map<String, Object> sourceMap = (Map<String, Object>) value;
sourceMap.forEach((k, v) -> targetDocument.append(k, unpack(v, context)));
return targetDocument;
}
return value;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
protected List<Object> append(Object value) {
if (this.value instanceof List) {
@ -110,13 +119,12 @@ abstract class AbstractAggregationExpression implements AggregationExpression { @@ -110,13 +119,12 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
if (!(this.value instanceof java.util.Map)) {
throw new IllegalArgumentException("o_O");
}
java.util.Map<String, Object> clone = new LinkedHashMap<String, Object>((java.util.Map<String, Object>) this.value);
java.util.Map<String, Object> clone = new LinkedHashMap<>((java.util.Map) this.value);
clone.put(key, value);
return clone;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
protected List<Object> values() {
if (value instanceof List) {
@ -125,7 +133,67 @@ abstract class AbstractAggregationExpression implements AggregationExpression { @@ -125,7 +133,67 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
if (value instanceof java.util.Map) {
return new ArrayList<Object>(((java.util.Map) value).values());
}
return new ArrayList<Object>(Collections.singletonList(value));
return new ArrayList<>(Collections.singletonList(value));
}
/**
* Get the value at a given index.
*
* @param index
* @param <T>
* @return
* @since 2.1
*/
protected <T> T get(int index) {
return (T) values().get(index);
}
/**
* Get the value for a given key.
*
* @param key
* @param <T>
* @return
* @since 2.1
*/
protected <T> T get(Object key) {
if (!(this.value instanceof java.util.Map)) {
throw new IllegalArgumentException("o_O");
}
return (T) ((java.util.Map<String, Object>) this.value).get(key);
}
/**
* Get the argument map.
*
* @since 2.1
* @return
*/
protected java.util.Map<String, Object> argumentMap() {
if (!(this.value instanceof java.util.Map)) {
throw new IllegalArgumentException("o_O");
}
return Collections.unmodifiableMap((java.util.Map) value);
}
/**
* Check if the given key is available.
*
* @param key
* @return
* @since 2.1
*/
protected boolean contains(Object key) {
if (!(this.value instanceof java.util.Map)) {
return false;
}
return ((java.util.Map<String, Object>) this.value).containsKey(key);
}
protected abstract String getMongoMethod();

62
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/DateFactory.java

@ -1,62 +0,0 @@ @@ -1,62 +0,0 @@
/*
* Copyright 2018. the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.aggregation;
import java.util.Date;
/**
* Used for {@link DateOperators} related functions to access the current date
*
* @since 2.1
* @author Matt Morrissette
*/
@FunctionalInterface
public interface DateFactory {
/**
* @author Matt Morrissette
* @param currentDate
* @return A date factory that always uses the given date as the current date. Primary used in testing and mock
* scenarios.
*/
public static DateFactory fixedDate(final Object currentDate) {
return () -> currentDate;
}
/**
* DateFactory that uses the date as it is on the local server
*/
public static final DateFactory LOCAL_DATE_FACTORY = Date::new;
/**
* Should return an object that is serializable by the BSON encoder and would resolve to a BSON Date when evaluated.
* <p>
* This includes
* <ul>
* <li>{@link java.util.Date}</li>
* <li>{@link java.util.Calendar}</li>
* <li>{@link java.time.Instant}</li>
* <li>{@link java.time.ZonedDateTime}</li>
* <li>{@link java.lang.Long}</li>
* <li>org.joda.time.AbstractInstant</li>
* </ul>
*
* @author Matt Morrissette
* @return
*/
public Object currentDate();
}

3485
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/DateOperators.java

File diff suppressed because it is too large Load Diff

899
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/DateOperatorsUnitTests.java

@ -1,899 +0,0 @@ @@ -1,899 +0,0 @@
/*
* Copyright 2016-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.aggregation;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.mongodb.core.aggregation.DateOperators.*;
import static org.springframework.data.mongodb.core.aggregation.LiteralOperators.Literal.*;
import java.util.Date;
import org.bson.Document;
import org.junit.Test;
import com.google.common.collect.Lists;
/**
* Unit tests for {@link DateOperators}. DATAMONGO-1834 - Add support for aggregation operators $dateFromString,
* $dateFromParts and $dateToParts This test case now covers all existing methods in the DateOperators class as well as
* those added as part of DATAMONGO-1834
*
* @author Matt Morrissette
*/
public class DateOperatorsUnitTests {
private static final String FIELD = "field";
private static final String VAR = "$";
private static final String VAR_FIELD = VAR + FIELD;
private static final String TIMEZONE = "America/Los_Angeles";
private static final String TIMEZONE2 = "America/New_York";
private static final String FORMAT = "%Y-%m-%d";
private static final Document LITERAL = new Document("$literal", VAR_FIELD);
private static final String TO_STRING_OP = "$dateToString";
private static Object CURRENT_DATE;
@Test(expected = IllegalArgumentException.class)
public void rejectsEmptyFieldName() {
dateOf("");
}
@Test
public void shouldRenderFieldCorrectly() {
final DateOperatorFactory f = dateOf(FIELD);
assertDateFieldOp(f.dayOfMonth(), "dayOfMonth");
assertDateFieldOp(f.dayOfWeek(), "dayOfWeek");
assertDateFieldOp(f.dayOfYear(), "dayOfYear");
assertDateFieldOp(f.hour(), "hour");
assertDateFieldOp(f.isoDayOfWeek(), "isoDayOfWeek");
assertDateFieldOp(f.isoWeek(), "isoWeek");
assertDateFieldOp(f.isoWeekYear(), "isoWeekYear");
assertDateFieldOp(f.millisecond(), "millisecond");
assertDateFieldOp(f.minute(), "minute");
assertDateFieldOp(f.month(), "month");
assertDateFieldOp(f.second(), "second");
assertDateFieldOp(f.week(), "week");
assertDateFieldOp(f.year(), "year");
assertQuarterFieldOp(f.quarter());
assertDateFromStringField(f.fromString());
assertDateToPartsField(f.toParts(), null);
assertDateToPartsField(f.toIsoWeekParts(), true);
assertDateToPartsField(f.toParts(true), true);
assertDateToPartsField(f.toParts(false), false);
assertDateFieldStringNoTimezoneOp(f.toString(FORMAT));
assertDateFieldTimezoneOp(f.dayOfMonth(TIMEZONE), "dayOfMonth");
assertDateFieldTimezoneOp(f.dayOfWeek(TIMEZONE), "dayOfWeek");
assertDateFieldTimezoneOp(f.dayOfYear(TIMEZONE), "dayOfYear");
assertDateFieldTimezoneOp(f.hour(TIMEZONE), "hour");
assertDateFieldTimezoneOp(f.isoDayOfWeek(TIMEZONE), "isoDayOfWeek");
assertDateFieldTimezoneOp(f.isoWeek(TIMEZONE), "isoWeek");
assertDateFieldTimezoneOp(f.isoWeekYear(TIMEZONE), "isoWeekYear");
assertDateFieldTimezoneOp(f.millisecond(TIMEZONE), "millisecond");
assertDateFieldTimezoneOp(f.minute(TIMEZONE), "minute");
assertDateFieldTimezoneOp(f.month(TIMEZONE), "month");
assertDateFieldTimezoneOp(f.second(TIMEZONE), "second");
assertDateFieldTimezoneOp(f.week(TIMEZONE), "week");
assertDateFieldTimezoneOp(f.year(TIMEZONE), "year");
assertDateToPartsFieldTimezone(f.toParts(TIMEZONE), null);
assertDateToPartsFieldTimezone(f.toIsoWeekParts(TIMEZONE), true);
assertDateToPartsFieldTimezone(f.toParts(TIMEZONE, true), true);
assertDateToPartsFieldTimezone(f.toParts(TIMEZONE, false), false);
assertQuarterFieldTimezoneOp(f.quarter(TIMEZONE));
assertDateFromStringFieldTimezone(f.fromString(TIMEZONE));
assertDateFieldStringTimezoneOp(f.toString(FORMAT, TIMEZONE));
}
@Test
public void shouldRenderFieldTimezoneCorrectly() {
final DateOperatorFactory f = dateOfWithTimezone(FIELD, TIMEZONE);
assertDateFieldTimezoneOp(f.dayOfMonth(), "dayOfMonth");
assertDateFieldTimezoneOp(f.dayOfWeek(), "dayOfWeek");
assertDateFieldTimezoneOp(f.dayOfYear(), "dayOfYear");
assertDateFieldTimezoneOp(f.hour(), "hour");
assertDateFieldTimezoneOp(f.isoDayOfWeek(), "isoDayOfWeek");
assertDateFieldTimezoneOp(f.isoWeek(), "isoWeek");
assertDateFieldTimezoneOp(f.isoWeekYear(), "isoWeekYear");
assertDateFieldTimezoneOp(f.millisecond(), "millisecond");
assertDateFieldTimezoneOp(f.minute(), "minute");
assertDateFieldTimezoneOp(f.month(), "month");
assertDateFieldTimezoneOp(f.second(), "second");
assertDateFieldTimezoneOp(f.week(), "week");
assertDateFieldTimezoneOp(f.year(), "year");
assertQuarterFieldTimezoneOp(f.quarter());
assertDateFromStringFieldTimezone(f.fromString());
assertDateToPartsFieldTimezone(f.toParts(), null);
assertDateToPartsFieldTimezone(f.toIsoWeekParts(), true);
assertDateToPartsFieldTimezone(f.toParts(true), true);
assertDateToPartsFieldTimezone(f.toParts(false), false);
assertDateFieldStringTimezoneOp(f.toString(FORMAT));
assertDateFieldTimezone2Op(f.dayOfMonth(TIMEZONE2), "dayOfMonth");
assertDateFieldTimezone2Op(f.dayOfWeek(TIMEZONE2), "dayOfWeek");
assertDateFieldTimezone2Op(f.dayOfYear(TIMEZONE2), "dayOfYear");
assertDateFieldTimezone2Op(f.hour(TIMEZONE2), "hour");
assertDateFieldTimezone2Op(f.isoDayOfWeek(TIMEZONE2), "isoDayOfWeek");
assertDateFieldTimezone2Op(f.isoWeek(TIMEZONE2), "isoWeek");
assertDateFieldTimezone2Op(f.isoWeekYear(TIMEZONE2), "isoWeekYear");
assertDateFieldTimezone2Op(f.millisecond(TIMEZONE2), "millisecond");
assertDateFieldTimezone2Op(f.minute(TIMEZONE2), "minute");
assertDateFieldTimezone2Op(f.month(TIMEZONE2), "month");
assertDateFieldTimezone2Op(f.second(TIMEZONE2), "second");
assertDateFieldTimezone2Op(f.week(TIMEZONE2), "week");
assertDateFieldTimezone2Op(f.year(TIMEZONE2), "year");
assertQuarterFieldTimezone2Op(f.quarter(TIMEZONE2));
assertDateFromStringFieldTimezone2(f.fromString(TIMEZONE2));
assertDateToPartsFieldTimezone2(f.toParts(TIMEZONE2), null);
assertDateToPartsFieldTimezone2(f.toIsoWeekParts(TIMEZONE2), true);
assertDateToPartsFieldTimezone2(f.toParts(TIMEZONE2, true), true);
assertDateToPartsFieldTimezone2(f.toParts(TIMEZONE2, false), false);
assertDateFieldStringTimezone2Op(f.toString(FORMAT, TIMEZONE2));
assertDateFieldOp(f.dayOfMonth(null), "dayOfMonth");
assertDateFieldOp(f.dayOfWeek(null), "dayOfWeek");
assertDateFieldOp(f.dayOfYear(null), "dayOfYear");
assertDateFieldOp(f.hour(null), "hour");
assertDateFieldOp(f.isoDayOfWeek(null), "isoDayOfWeek");
assertDateFieldOp(f.isoWeek(null), "isoWeek");
assertDateFieldOp(f.isoWeekYear(null), "isoWeekYear");
assertDateFieldOp(f.millisecond(null), "millisecond");
assertDateFieldOp(f.minute(null), "minute");
assertDateFieldOp(f.month(null), "month");
assertDateFieldOp(f.second(null), "second");
assertDateFieldOp(f.week(null), "week");
assertDateFieldOp(f.year(null), "year");
assertQuarterFieldOp(f.quarter(null));
assertDateFromStringField(f.fromString(null));
assertDateToPartsField(f.toParts((String) null), null);
assertDateToPartsField(f.toIsoWeekParts(null), true);
assertDateToPartsField(f.toParts(null, true), true);
assertDateToPartsField(f.toParts(null, false), false);
assertDateFieldStringNoTimezoneOp(f.toString(FORMAT, null));
}
@Test
public void shouldRenderExprCorrectly() {
final DateOperatorFactory f = dateOf(asLiteral(VAR_FIELD));
assertDateExprOp(f.dayOfMonth(), "dayOfMonth");
assertDateExprOp(f.dayOfWeek(), "dayOfWeek");
assertDateExprOp(f.dayOfYear(), "dayOfYear");
assertDateExprOp(f.hour(), "hour");
assertDateExprOp(f.isoDayOfWeek(), "isoDayOfWeek");
assertDateExprOp(f.isoWeek(), "isoWeek");
assertDateExprOp(f.isoWeekYear(), "isoWeekYear");
assertDateExprOp(f.millisecond(), "millisecond");
assertDateExprOp(f.minute(), "minute");
assertDateExprOp(f.month(), "month");
assertDateExprOp(f.second(), "second");
assertDateExprOp(f.week(), "week");
assertDateExprOp(f.year(), "year");
assertQuarterExprOp(f.quarter());
assertDateFromStringExpr(f.fromString());
assertDateToPartsExpr(f.toParts(), null);
assertDateToPartsExpr(f.toIsoWeekParts(), true);
assertDateToPartsExpr(f.toParts(true), true);
assertDateToPartsExpr(f.toParts(false), false);
assertDateExprStringNoTimezoneOp(f.toString(FORMAT));
assertDateExprTimezoneOp(f.dayOfMonth(TIMEZONE), "dayOfMonth");
assertDateExprTimezoneOp(f.dayOfWeek(TIMEZONE), "dayOfWeek");
assertDateExprTimezoneOp(f.dayOfYear(TIMEZONE), "dayOfYear");
assertDateExprTimezoneOp(f.hour(TIMEZONE), "hour");
assertDateExprTimezoneOp(f.isoDayOfWeek(TIMEZONE), "isoDayOfWeek");
assertDateExprTimezoneOp(f.isoWeek(TIMEZONE), "isoWeek");
assertDateExprTimezoneOp(f.isoWeekYear(TIMEZONE), "isoWeekYear");
assertDateExprTimezoneOp(f.millisecond(TIMEZONE), "millisecond");
assertDateExprTimezoneOp(f.minute(TIMEZONE), "minute");
assertDateExprTimezoneOp(f.month(TIMEZONE), "month");
assertDateExprTimezoneOp(f.second(TIMEZONE), "second");
assertDateExprTimezoneOp(f.week(TIMEZONE), "week");
assertDateExprTimezoneOp(f.year(TIMEZONE), "year");
assertQuarterExprTimezoneOp(f.quarter(TIMEZONE));
assertDateFromStringExprTimezone(f.fromString(TIMEZONE));
assertDateToPartsExprTimezone(f.toParts(TIMEZONE), null);
assertDateToPartsExprTimezone(f.toIsoWeekParts(TIMEZONE), true);
assertDateToPartsExprTimezone(f.toParts(TIMEZONE, true), true);
assertDateToPartsExprTimezone(f.toParts(TIMEZONE, false), false);
assertDateExprStringTimezoneOp(f.toString(FORMAT, TIMEZONE));
}
@Test
public void shouldRenderExprTimezoneCorrectly() {
final DateOperatorFactory f = dateOfWithTimezone(asLiteral(VAR_FIELD), TIMEZONE);
assertDateExprTimezoneOp(f.dayOfMonth(), "dayOfMonth");
assertDateExprTimezoneOp(f.dayOfWeek(), "dayOfWeek");
assertDateExprTimezoneOp(f.dayOfYear(), "dayOfYear");
assertDateExprTimezoneOp(f.hour(), "hour");
assertDateExprTimezoneOp(f.isoDayOfWeek(), "isoDayOfWeek");
assertDateExprTimezoneOp(f.isoWeek(), "isoWeek");
assertDateExprTimezoneOp(f.isoWeekYear(), "isoWeekYear");
assertDateExprTimezoneOp(f.millisecond(), "millisecond");
assertDateExprTimezoneOp(f.minute(), "minute");
assertDateExprTimezoneOp(f.month(), "month");
assertDateExprTimezoneOp(f.second(), "second");
assertDateExprTimezoneOp(f.week(), "week");
assertDateExprTimezoneOp(f.year(), "year");
assertQuarterExprTimezoneOp(f.quarter());
assertDateFromStringExprTimezone(f.fromString());
assertDateToPartsExprTimezone(f.toParts(), null);
assertDateToPartsExprTimezone(f.toIsoWeekParts(), true);
assertDateToPartsExprTimezone(f.toParts(true), true);
assertDateToPartsExprTimezone(f.toParts(false), false);
assertDateExprStringTimezoneOp(f.toString(FORMAT));
assertDateExprTimezone2Op(f.dayOfMonth(TIMEZONE2), "dayOfMonth");
assertDateExprTimezone2Op(f.dayOfWeek(TIMEZONE2), "dayOfWeek");
assertDateExprTimezone2Op(f.dayOfYear(TIMEZONE2), "dayOfYear");
assertDateExprTimezone2Op(f.hour(TIMEZONE2), "hour");
assertDateExprTimezone2Op(f.isoDayOfWeek(TIMEZONE2), "isoDayOfWeek");
assertDateExprTimezone2Op(f.isoWeek(TIMEZONE2), "isoWeek");
assertDateExprTimezone2Op(f.isoWeekYear(TIMEZONE2), "isoWeekYear");
assertDateExprTimezone2Op(f.millisecond(TIMEZONE2), "millisecond");
assertDateExprTimezone2Op(f.minute(TIMEZONE2), "minute");
assertDateExprTimezone2Op(f.month(TIMEZONE2), "month");
assertDateExprTimezone2Op(f.second(TIMEZONE2), "second");
assertDateExprTimezone2Op(f.week(TIMEZONE2), "week");
assertDateExprTimezone2Op(f.year(TIMEZONE2), "year");
assertDateToPartsExprTimezone2(f.toParts(TIMEZONE2), null);
assertDateToPartsExprTimezone2(f.toIsoWeekParts(TIMEZONE2), true);
assertDateToPartsExprTimezone2(f.toParts(TIMEZONE2, true), true);
assertDateToPartsExprTimezone2(f.toParts(TIMEZONE2, false), false);
assertDateFromStringExprTimezone2(f.fromString(TIMEZONE2));
assertQuarterExprStringTimezone2Op(f.quarter(TIMEZONE2));
assertDateExprTimezone2Op(f.toString(FORMAT, TIMEZONE2));
assertDateExprOp(f.dayOfMonth(null), "dayOfMonth");
assertDateExprOp(f.dayOfWeek(null), "dayOfWeek");
assertDateExprOp(f.dayOfYear(null), "dayOfYear");
assertDateExprOp(f.hour(null), "hour");
assertDateExprOp(f.isoDayOfWeek(null), "isoDayOfWeek");
assertDateExprOp(f.isoWeek(null), "isoWeek");
assertDateExprOp(f.isoWeekYear(null), "isoWeekYear");
assertDateExprOp(f.millisecond(null), "millisecond");
assertDateExprOp(f.minute(null), "minute");
assertDateExprOp(f.month(null), "month");
assertDateExprOp(f.second(null), "second");
assertDateExprOp(f.week(null), "week");
assertDateExprOp(f.year(null), "year");
assertQuarterExprOp(f.quarter(null));
assertDateFromStringExpr(f.fromString(null));
assertDateToPartsExpr(f.toParts((String) null), null);
assertDateToPartsExpr(f.toIsoWeekParts(null), true);
assertDateToPartsExpr(f.toParts(null, true), true);
assertDateToPartsExpr(f.toParts(null, false), false);
assertDateExprStringNoTimezoneOp(f.toString(FORMAT, null));
}
@Test
public void shouldRenderCurrentDateCorrectly() {
CURRENT_DATE = new Date();
pShouldRenderCurrentDateCorrectly(DateFactory.fixedDate(CURRENT_DATE));
}
private void pShouldRenderCurrentDateCorrectly(DateFactory dateFactory) {
pShouldRenderCurrentDateCorrectly(dateOf(dateFactory).withTimezone(TIMEZONE));
}
private void pShouldRenderCurrentDateCorrectly(DateOperatorFactory f) {
assertCurrentDateTimezoneOp(f.dayOfMonth(), "dayOfMonth");
assertCurrentDateTimezoneOp(f.dayOfWeek(), "dayOfWeek");
assertCurrentDateTimezoneOp(f.dayOfYear(), "dayOfYear");
assertCurrentDateTimezoneOp(f.hour(), "hour");
assertCurrentDateTimezoneOp(f.isoDayOfWeek(), "isoDayOfWeek");
assertCurrentDateTimezoneOp(f.isoWeek(), "isoWeek");
assertCurrentDateTimezoneOp(f.isoWeekYear(), "isoWeekYear");
assertCurrentDateTimezoneOp(f.millisecond(), "millisecond");
assertCurrentDateTimezoneOp(f.minute(), "minute");
assertCurrentDateTimezoneOp(f.month(), "month");
assertCurrentDateTimezoneOp(f.second(), "second");
assertCurrentDateTimezoneOp(f.week(), "week");
assertCurrentDateTimezoneOp(f.year(), "year");
assertCurrentDateToPartsTimezone(f.toParts(), null);
assertCurrentDateToPartsTimezone(f.toIsoWeekParts(), true);
assertCurrentDateToPartsTimezone(f.toParts(true), true);
assertCurrentDateToPartsTimezone(f.toParts(false), false);
assertQuarterCurrentDateTimezoneOp(f.quarter());
assertCurrentDateFromStringTimezone(f.fromString());
assertCurrentDateStringTimezoneOp(f.toString(FORMAT));
assertCurrentDateTimezone2Op(f.dayOfMonth(TIMEZONE2), "dayOfMonth");
assertCurrentDateTimezone2Op(f.dayOfWeek(TIMEZONE2), "dayOfWeek");
assertCurrentDateTimezone2Op(f.dayOfYear(TIMEZONE2), "dayOfYear");
assertCurrentDateTimezone2Op(f.hour(TIMEZONE2), "hour");
assertCurrentDateTimezone2Op(f.isoDayOfWeek(TIMEZONE2), "isoDayOfWeek");
assertCurrentDateTimezone2Op(f.isoWeek(TIMEZONE2), "isoWeek");
assertCurrentDateTimezone2Op(f.isoWeekYear(TIMEZONE2), "isoWeekYear");
assertCurrentDateTimezone2Op(f.millisecond(TIMEZONE2), "millisecond");
assertCurrentDateTimezone2Op(f.minute(TIMEZONE2), "minute");
assertCurrentDateTimezone2Op(f.month(TIMEZONE2), "month");
assertCurrentDateTimezone2Op(f.second(TIMEZONE2), "second");
assertCurrentDateTimezone2Op(f.week(TIMEZONE2), "week");
assertCurrentDateTimezone2Op(f.year(TIMEZONE2), "year");
assertCurrentDateToPartsTimezone2(f.toParts(TIMEZONE2), null);
assertCurrentDateToPartsTimezone2(f.toIsoWeekParts(TIMEZONE2), true);
assertCurrentDateToPartsTimezone2(f.toParts(TIMEZONE2, true), true);
assertCurrentDateToPartsTimezone2(f.toParts(TIMEZONE2, false), false);
assertQuarterCurrentDateTimezone2Op(f.quarter(TIMEZONE2));
assertCurrentDateFromStringTimezone2(f.fromString(TIMEZONE2));
assertCurrentDateStringTimezone2Op(f.toString(FORMAT, TIMEZONE2));
assertCurrentDateOp(f.dayOfMonth(null), "dayOfMonth");
assertCurrentDateOp(f.dayOfWeek(null), "dayOfWeek");
assertCurrentDateOp(f.dayOfYear(null), "dayOfYear");
assertCurrentDateOp(f.hour(null), "hour");
assertCurrentDateOp(f.isoDayOfWeek(null), "isoDayOfWeek");
assertCurrentDateOp(f.isoWeek(null), "isoWeek");
assertCurrentDateOp(f.isoWeekYear(null), "isoWeekYear");
assertCurrentDateOp(f.millisecond(null), "millisecond");
assertCurrentDateOp(f.minute(null), "minute");
assertCurrentDateOp(f.month(null), "month");
assertCurrentDateOp(f.second(null), "second");
assertCurrentDateOp(f.week(null), "week");
assertCurrentDateOp(f.year(null), "year");
assertQuarterCurrentDateOp(f.quarter(null));
assertCurrentDateFromString(f.fromString(null));
assertCurrentDateStringNoTimezoneOp(f.toString(FORMAT, null));
assertCurrentDateToParts(f.toParts((String) null), null);
assertCurrentDateToParts(f.toIsoWeekParts(null), true);
assertCurrentDateToParts(f.toParts(null, true), true);
assertCurrentDateToParts(f.toParts(null, false), false);
}
@Test
public void shouldRenderCalendarDateFromPartsCorrectly() {
final Document doc = new Document();
final Document parts = new Document();
doc.put("$dateFromParts", parts);
int year = 2017;
DateFromParts.CalendarDatePartsBuilder dateFromParts = dateFromParts();
parts.put("year", year);
assertThat(dateFromParts.year(year).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("year", "$year");
assertThat(dateFromParts.yearOf("year").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("year", LITERAL);
assertThat(dateFromParts.yearOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("year", year);
dateFromParts.year(year);
int month = 10;
parts.put("month", month);
assertThat(dateFromParts.month(month).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("month", "$month");
assertThat(dateFromParts.monthOf("month").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("month", LITERAL);
assertThat(dateFromParts.monthOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.remove("month");
dateFromParts.month(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
int day = 8;
parts.put("day", day);
assertThat(dateFromParts.day(day).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("day", "$day");
assertThat(dateFromParts.dayOf("day").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("day", LITERAL);
assertThat(dateFromParts.dayOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.remove("day");
dateFromParts.day(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
int hour = 9;
parts.put("hour", hour);
assertThat(dateFromParts.hour(hour).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("hour", "$hour");
assertThat(dateFromParts.hourOf("hour").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("hour", LITERAL);
assertThat(dateFromParts.hourOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.remove("hour");
dateFromParts.hour(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
int minute = 15;
parts.put("minute", minute);
assertThat(dateFromParts.minute(minute).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("minute", "$minute");
assertThat(dateFromParts.minuteOf("minute").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("minute", LITERAL);
assertThat(dateFromParts.minuteOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.remove("minute");
dateFromParts.minute(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
int second = 35;
parts.put("second", second);
assertThat(dateFromParts.second(second).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("second", "$second");
assertThat(dateFromParts.secondOf("second").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("second", LITERAL);
assertThat(dateFromParts.secondOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.remove("second");
dateFromParts.second(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
int millisecond = 35;
parts.put("millisecond", millisecond);
assertThat(dateFromParts.millisecond(millisecond).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("millisecond", "$millisecond");
assertThat(dateFromParts.millisecondOf("millisecond").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("millisecond", LITERAL);
assertThat(dateFromParts.millisecondOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT),
is(doc));
parts.remove("millisecond");
dateFromParts.millisecond(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
String timezone = "America/New_York";
parts.put("timezone", timezone);
assertThat(dateFromParts.timezone(timezone).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("timezone", "$timezone");
assertThat(dateFromParts.timezoneOf("timezone").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("timezone", LITERAL);
assertThat(dateFromParts.timezoneOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT),
is(doc));
parts.remove("timezone");
dateFromParts.timezone(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
}
@Test
public void shouldRenderIsoWeekDateFromPartsCorrectly() {
final Document doc = new Document();
final Document parts = new Document();
doc.put("$dateFromParts", parts);
int isoWeekYear = 2017;
DateFromParts.IsoWeekDatePartsBuilder dateFromParts = dateFromIsoWeekParts();
parts.put("isoWeekYear", isoWeekYear);
assertThat(dateFromParts.isoWeekYear(isoWeekYear).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("isoWeekYear", "$isoWeekYear");
assertThat(dateFromParts.isoWeekYearOf("isoWeekYear").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("isoWeekYear", LITERAL);
assertThat(dateFromParts.isoWeekYearOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT),
is(doc));
parts.put("isoWeekYear", isoWeekYear);
dateFromParts.isoWeekYear(isoWeekYear);
int isoWeek = 25;
parts.put("isoWeek", isoWeek);
assertThat(dateFromParts.isoWeek(isoWeek).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("isoWeek", "$isoWeek");
assertThat(dateFromParts.isoWeekOf("isoWeek").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("isoWeek", LITERAL);
assertThat(dateFromParts.isoWeekOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.remove("isoWeek");
dateFromParts.isoWeek(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
int isoDayOfWeek = 4;
parts.put("isoDayOfWeek", isoDayOfWeek);
assertThat(dateFromParts.isoDayOfWeek(isoDayOfWeek).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("isoDayOfWeek", "$isoDayOfWeek");
assertThat(dateFromParts.isoDayOfWeekOf("isoDayOfWeek").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("isoDayOfWeek", LITERAL);
assertThat(dateFromParts.isoDayOfWeekOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT),
is(doc));
parts.remove("isoDayOfWeek");
dateFromParts.isoDayOfWeek(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
int hour = 9;
parts.put("hour", hour);
assertThat(dateFromParts.hour(hour).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("hour", "$hour");
assertThat(dateFromParts.hourOf("hour").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("hour", LITERAL);
assertThat(dateFromParts.hourOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.remove("hour");
dateFromParts.hour(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
int minute = 15;
parts.put("minute", minute);
assertThat(dateFromParts.minute(minute).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("minute", "$minute");
assertThat(dateFromParts.minuteOf("minute").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("minute", LITERAL);
assertThat(dateFromParts.minuteOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.remove("minute");
dateFromParts.minute(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
int second = 35;
parts.put("second", second);
assertThat(dateFromParts.second(second).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("second", "$second");
assertThat(dateFromParts.secondOf("second").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("second", LITERAL);
assertThat(dateFromParts.secondOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.remove("second");
dateFromParts.second(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
int millisecond = 35;
parts.put("millisecond", millisecond);
assertThat(dateFromParts.millisecond(millisecond).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("millisecond", "$millisecond");
assertThat(dateFromParts.millisecondOf("millisecond").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("millisecond", LITERAL);
assertThat(dateFromParts.millisecondOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT),
is(doc));
parts.remove("millisecond");
dateFromParts.millisecond(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
String timezone = "America/New_York";
parts.put("timezone", timezone);
assertThat(dateFromParts.timezone(timezone).toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("timezone", "$timezone");
assertThat(dateFromParts.timezoneOf("timezone").toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
parts.put("timezone", LITERAL);
assertThat(dateFromParts.timezoneOf(asLiteral(VAR_FIELD)).toDate().toDocument(Aggregation.DEFAULT_CONTEXT),
is(doc));
parts.remove("timezone");
dateFromParts.timezone(null);
assertThat(dateFromParts.toDate().toDocument(Aggregation.DEFAULT_CONTEXT), is(doc));
}
@Test(expected = IllegalArgumentException.class)
public void testDateFromPartsNoCalendarYearException() {
dateFromParts().toDate();
}
@Test(expected = IllegalArgumentException.class)
public void testDateFromPartsNoIsoWeekYearException() {
dateFromIsoWeekParts().toDate();
}
private void assertDateFieldOp(AggregationExpression operation, final String opName) {
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(VAR + opName, VAR_FIELD)));
}
private void assertDateFieldTimezoneOp(AggregationExpression operation, final String opName) {
final Document val = new Document();
val.put("date", VAR_FIELD);
val.put("timezone", TIMEZONE);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(VAR + opName, val)));
}
private void assertDateFieldTimezone2Op(AggregationExpression operation, final String opName) {
final Document val = new Document();
val.put("date", VAR_FIELD);
val.put("timezone", TIMEZONE2);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(VAR + opName, val)));
}
private void assertDateExprOp(AggregationExpression operation, final String opName) {
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(VAR + opName, LITERAL)));
}
private void assertDateExprTimezoneOp(AggregationExpression operation, final String opName) {
final Document val = new Document();
val.put("date", LITERAL);
val.put("timezone", TIMEZONE);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(VAR + opName, val)));
}
private void assertDateExprTimezone2Op(AggregationExpression operation, final String opName) {
final Document val = new Document();
val.put("date", LITERAL);
val.put("timezone", TIMEZONE2);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(VAR + opName, val)));
}
private void assertDateFieldStringNoTimezoneOp(AggregationExpression operation) {
final Document val = new Document();
val.put("date", VAR_FIELD);
val.put("format", FORMAT);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(TO_STRING_OP, val)));
}
private void assertDateFieldStringTimezoneOp(AggregationExpression operation) {
final Document val = new Document();
val.put("date", VAR_FIELD);
val.put("format", FORMAT);
val.put("timezone", TIMEZONE);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(TO_STRING_OP, val)));
}
private void assertDateFieldStringTimezone2Op(AggregationExpression operation) {
final Document val = new Document();
val.put("date", VAR_FIELD);
val.put("format", FORMAT);
val.put("timezone", TIMEZONE2);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(TO_STRING_OP, val)));
}
private void assertDateExprStringNoTimezoneOp(AggregationExpression operation) {
final Document val = new Document();
val.put("date", LITERAL);
val.put("format", FORMAT);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(TO_STRING_OP, val)));
}
private void assertDateExprStringTimezoneOp(AggregationExpression operation) {
final Document val = new Document();
val.put("date", LITERAL);
val.put("format", FORMAT);
val.put("timezone", TIMEZONE);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(TO_STRING_OP, val)));
}
private void assertDateExprTimezone2Op(AggregationExpression operation) {
final Document val = new Document();
val.put("date", LITERAL);
val.put("format", FORMAT);
val.put("timezone", TIMEZONE2);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(TO_STRING_OP, val)));
}
private void assertDateFromStringField(AggregationExpression operation) {
final Document val = new Document();
val.put("dateString", VAR_FIELD);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document("$dateFromString", val)));
}
private void assertDateFromStringFieldTimezone(AggregationExpression operation) {
final Document val = new Document();
val.put("dateString", VAR_FIELD);
val.put("timezone", TIMEZONE);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document("$dateFromString", val)));
}
private void assertDateFromStringFieldTimezone2(AggregationExpression operation) {
final Document val = new Document();
val.put("dateString", VAR_FIELD);
val.put("timezone", TIMEZONE2);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document("$dateFromString", val)));
}
private void assertDateFromStringExpr(AggregationExpression operation) {
final Document val = new Document();
val.put("dateString", LITERAL);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document("$dateFromString", val)));
}
private void assertDateFromStringExprTimezone(AggregationExpression operation) {
final Document val = new Document();
val.put("dateString", LITERAL);
val.put("timezone", TIMEZONE);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document("$dateFromString", val)));
}
private void assertDateFromStringExprTimezone2(AggregationExpression operation) {
final Document val = new Document();
val.put("dateString", LITERAL);
val.put("timezone", TIMEZONE2);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document("$dateFromString", val)));
}
private void assertCurrentDateFromString(AggregationExpression operation) {
final Document val = new Document();
val.put("dateString", CURRENT_DATE);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document("$dateFromString", val)));
}
private void assertCurrentDateFromStringTimezone(AggregationExpression operation) {
final Document val = new Document();
val.put("dateString", CURRENT_DATE);
val.put("timezone", TIMEZONE);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document("$dateFromString", val)));
}
private void assertCurrentDateFromStringTimezone2(AggregationExpression operation) {
final Document val = new Document();
val.put("dateString", CURRENT_DATE);
val.put("timezone", TIMEZONE2);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document("$dateFromString", val)));
}
private void assertDateToParts(AggregationExpression operation, Boolean iso8601, Object dateValue,
Object timezoneValue) {
final Document val = new Document();
val.put("date", dateValue);
if (iso8601 != null) {
val.put("iso8601", iso8601);
}
if (timezoneValue != null) {
val.put("timezone", timezoneValue);
}
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document("$dateToParts", val)));
}
private void assertDateToPartsField(AggregationExpression operation, Boolean iso8601) {
assertDateToParts(operation, iso8601, VAR_FIELD, null);
}
private void assertDateToPartsFieldTimezone(AggregationExpression operation, Boolean iso8601) {
assertDateToParts(operation, iso8601, VAR_FIELD, TIMEZONE);
}
private void assertDateToPartsFieldTimezone2(AggregationExpression operation, Boolean iso8601) {
assertDateToParts(operation, iso8601, VAR_FIELD, TIMEZONE2);
}
private void assertDateToPartsExpr(AggregationExpression operation, Boolean iso8601) {
assertDateToParts(operation, iso8601, LITERAL, null);
}
private void assertDateToPartsExprTimezone(AggregationExpression operation, Boolean iso8601) {
assertDateToParts(operation, iso8601, LITERAL, TIMEZONE);
}
private void assertDateToPartsExprTimezone2(AggregationExpression operation, Boolean iso8601) {
assertDateToParts(operation, iso8601, LITERAL, TIMEZONE2);
}
private void assertCurrentDateToParts(AggregationExpression operation, Boolean iso8601) {
assertDateToParts(operation, iso8601, CURRENT_DATE, null);
}
private void assertCurrentDateToPartsTimezone(AggregationExpression operation, Boolean iso8601) {
assertDateToParts(operation, iso8601, CURRENT_DATE, TIMEZONE);
}
private void assertCurrentDateToPartsTimezone2(AggregationExpression operation, Boolean iso8601) {
assertDateToParts(operation, iso8601, CURRENT_DATE, TIMEZONE2);
}
private void assertQuarter(AggregationExpression operation, final Document monthDoc) {
final Document document = new Document("$cond",
new Document()
.append("if",
new Document("$lte",
Lists.newArrayList(monthDoc,
3)))
.append("then", 1).append("else",
new Document("$cond",
new Document().append("if", new Document("$lte", Lists.newArrayList(monthDoc, 6))).append("then", 2)
.append("else",
new Document("$cond",
new Document().append("if", new Document("$lte", Lists.newArrayList(monthDoc, 9)))
.append("then", 3).append("else", 4))))));
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(document));
}
private void assertQuarterFieldOp(AggregationExpression operation) {
assertQuarter(operation, new Document("$month", VAR_FIELD));
}
private void assertQuarterFieldTimezoneOp(AggregationExpression operation) {
final Document val = new Document();
val.put("date", VAR_FIELD);
val.put("timezone", TIMEZONE);
assertQuarter(operation, new Document("$month", val));
}
private void assertQuarterFieldTimezone2Op(AggregationExpression operation) {
final Document val = new Document();
val.put("date", VAR_FIELD);
val.put("timezone", TIMEZONE2);
assertQuarter(operation, new Document("$month", val));
}
private void assertQuarterExprOp(AggregationExpression operation) {
assertQuarter(operation, new Document("$month", LITERAL));
}
private void assertQuarterExprTimezoneOp(AggregationExpression operation) {
final Document val = new Document();
val.put("date", LITERAL);
val.put("timezone", TIMEZONE);
assertQuarter(operation, new Document("$month", val));
}
private void assertQuarterExprStringTimezone2Op(AggregationExpression operation) {
final Document val = new Document();
val.put("date", LITERAL);
val.put("timezone", TIMEZONE2);
assertQuarter(operation, new Document("$month", val));
}
private void assertQuarterCurrentDateOp(AggregationExpression operation) {
assertQuarter(operation, new Document("$month", CURRENT_DATE));
}
private void assertQuarterCurrentDateTimezoneOp(AggregationExpression operation) {
final Document val = new Document();
val.put("date", CURRENT_DATE);
val.put("timezone", TIMEZONE);
assertQuarter(operation, new Document("$month", val));
}
private void assertQuarterCurrentDateTimezone2Op(AggregationExpression operation) {
final Document val = new Document();
val.put("date", CURRENT_DATE);
val.put("timezone", TIMEZONE2);
assertQuarter(operation, new Document("$month", val));
}
private void assertCurrentDateOp(AggregationExpression operation, final String opName) {
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(VAR + opName, CURRENT_DATE)));
}
private void assertCurrentDateTimezoneOp(AggregationExpression operation, final String opName) {
final Document val = new Document();
val.put("date", CURRENT_DATE);
val.put("timezone", TIMEZONE);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(VAR + opName, val)));
}
private void assertCurrentDateTimezone2Op(AggregationExpression operation, final String opName) {
final Document val = new Document();
val.put("date", CURRENT_DATE);
val.put("timezone", TIMEZONE2);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(VAR + opName, val)));
}
private void assertCurrentDateStringNoTimezoneOp(AggregationExpression operation) {
final Document val = new Document();
val.put("date", CURRENT_DATE);
val.put("format", FORMAT);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(TO_STRING_OP, val)));
}
private void assertCurrentDateStringTimezoneOp(AggregationExpression operation) {
final Document val = new Document();
val.put("date", CURRENT_DATE);
val.put("format", FORMAT);
val.put("timezone", TIMEZONE);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(TO_STRING_OP, val)));
}
private void assertCurrentDateStringTimezone2Op(AggregationExpression operation) {
final Document val = new Document();
val.put("date", CURRENT_DATE);
val.put("format", FORMAT);
val.put("timezone", TIMEZONE2);
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT), is(new Document(TO_STRING_OP, val)));
}
}

293
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java

@ -35,6 +35,7 @@ import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.P @@ -35,6 +35,7 @@ import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.P
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.Variable;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Slice;
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Switch.CaseOperator;
import org.springframework.data.mongodb.core.aggregation.DateOperators.Timezone;
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation.ProjectionOperationBuilder;
import org.springframework.data.mongodb.core.aggregation.StringOperators.Concat;
import org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable;
@ -1083,6 +1084,17 @@ public class ProjectionOperationUnitTests { @@ -1083,6 +1084,17 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project: { dayOfYear: { $dayOfYear: \"$date\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDayOfYearAggregationExpressionWithTimezone() {
Document agg = project()
.and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).dayOfYear()).as("dayOfYear")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project: { dayOfYear: { $dayOfYear: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } }"));
}
@Test // DATAMONGO-1536
public void shouldRenderDayOfMonthAggregationExpression() {
@ -1092,6 +1104,17 @@ public class ProjectionOperationUnitTests { @@ -1092,6 +1104,17 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project: { day: { $dayOfMonth: \"$date\" }} }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDayOfMonthAggregationExpressionWithTimezone() {
Document agg = project()
.and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).dayOfMonth()).as("day")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project: { day: { $dayOfMonth: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } } }"));
}
@Test // DATAMONGO-1536
public void shouldRenderDayOfWeekAggregationExpression() {
@ -1101,6 +1124,17 @@ public class ProjectionOperationUnitTests { @@ -1101,6 +1124,17 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project: { dayOfWeek: { $dayOfWeek: \"$date\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDayOfWeekAggregationExpressionWithTimezone() {
Document agg = project()
.and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).dayOfWeek()).as("dayOfWeek")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project: { dayOfWeek: { $dayOfWeek: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } } }"));
}
@Test // DATAMONGO-1536
public void shouldRenderYearAggregationExpression() {
@ -1110,6 +1144,16 @@ public class ProjectionOperationUnitTests { @@ -1110,6 +1144,16 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project: { year: { $year: \"$date\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderYearAggregationExpressionWithTimezone() {
Document agg = project().and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).year())
.as("year").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document
.parse("{ $project: { year: { $year: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } } }"));
}
@Test // DATAMONGO-1536
public void shouldRenderMonthAggregationExpression() {
@ -1119,6 +1163,16 @@ public class ProjectionOperationUnitTests { @@ -1119,6 +1163,16 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project: { month: { $month: \"$date\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderMonthAggregationExpressionWithTimezone() {
Document agg = project().and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).month())
.as("month").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document
.parse("{ $project: { month: { $month: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } } }"));
}
@Test // DATAMONGO-1536
public void shouldRenderWeekAggregationExpression() {
@ -1128,6 +1182,16 @@ public class ProjectionOperationUnitTests { @@ -1128,6 +1182,16 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project: { week: { $week: \"$date\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderWeekAggregationExpressionWithTimezone() {
Document agg = project().and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).week())
.as("week").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document
.parse("{ $project: { week: { $week: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } } }"));
}
@Test // DATAMONGO-1536
public void shouldRenderHourAggregationExpression() {
@ -1137,6 +1201,16 @@ public class ProjectionOperationUnitTests { @@ -1137,6 +1201,16 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project: { hour: { $hour: \"$date\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderHourAggregationExpressionWithTimezone() {
Document agg = project().and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).hour())
.as("hour").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document
.parse("{ $project: { hour: { $hour: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } } }"));
}
@Test // DATAMONGO-1536
public void shouldRenderMinuteAggregationExpression() {
@ -1146,6 +1220,17 @@ public class ProjectionOperationUnitTests { @@ -1146,6 +1220,17 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project: { minute: { $minute: \"$date\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderMinuteAggregationExpressionWithTimezone() {
Document agg = project()
.and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).minute()).as("minute")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project: { minute: { $minute: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } } }"));
}
@Test // DATAMONGO-1536
public void shouldRenderSecondAggregationExpression() {
@ -1155,6 +1240,17 @@ public class ProjectionOperationUnitTests { @@ -1155,6 +1240,17 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project: { second: { $second: \"$date\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderSecondAggregationExpressionWithTimezone() {
Document agg = project()
.and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).second()).as("second")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project: { second: { $second: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } } }"));
}
@Test // DATAMONGO-1536
public void shouldRenderMillisecondAggregationExpression() {
@ -1164,6 +1260,17 @@ public class ProjectionOperationUnitTests { @@ -1164,6 +1260,17 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project: { msec: { $millisecond: \"$date\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderMillisecondAggregationExpressionWithTimezone() {
Document agg = project()
.and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).millisecond()).as("msec")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project: { msec: { $millisecond: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } } }"));
}
@Test // DATAMONGO-1536
public void shouldRenderDateToString() {
@ -1184,6 +1291,17 @@ public class ProjectionOperationUnitTests { @@ -1184,6 +1291,17 @@ public class ProjectionOperationUnitTests {
Document.parse("{ $project: { time: { $dateToString: { format: \"%H:%M:%S:%L\", date: \"$date\" } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDateToStringAggregationExpressionWithTimezone() {
Document agg = project()
.and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).toString("%H:%M:%S:%L"))
.as("time").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project: { time: { $dateToString: { format: \"%H:%M:%S:%L\", date: \"$date\", \"timezone\" : \"America/Chicago\" } } } } } }"));
}
@Test // DATAMONGO-1536
public void shouldRenderSumAggregationExpression() {
@ -1440,11 +1558,11 @@ public class ProjectionOperationUnitTests { @@ -1440,11 +1558,11 @@ public class ProjectionOperationUnitTests {
.as("finalTotal").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse("{ $project:{ \"finalTotal\" : { \"$let\": {" + //
"\"vars\": {" + //
"\"total\": { \"$add\": [ \"$price\", \"$tax\" ] }," + //
"\"discounted\": { \"$cond\": { \"if\": \"$applyDiscount\", \"then\": 0.9, \"else\": 1.0 } }" + //
"}," + //
"\"in\": { \"$multiply\": [ \"$$total\", \"$$discounted\" ] }" + //
"\"vars\": {" + //
"\"total\": { \"$add\": [ \"$price\", \"$tax\" ] }," + //
"\"discounted\": { \"$cond\": { \"if\": \"$applyDiscount\", \"then\": 0.9, \"else\": 1.0 } }" + //
"}," + //
"\"in\": { \"$multiply\": [ \"$$total\", \"$$discounted\" ] }" + //
"}}}}"));
}
@ -1463,11 +1581,11 @@ public class ProjectionOperationUnitTests { @@ -1463,11 +1581,11 @@ public class ProjectionOperationUnitTests {
.as("finalTotal").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse("{ $project:{ \"finalTotal\" : { \"$let\": {" + //
"\"vars\": {" + //
"\"total\": { \"$add\": [ \"$price\", \"$tax\" ] }," + //
"\"discounted\": { \"$cond\": { \"if\": \"$applyDiscount\", \"then\": 0.9, \"else\": 1.0 } }" + //
"}," + //
"\"in\": { \"$multiply\": [ \"$$total\", \"$$discounted\" ] }" + //
"\"vars\": {" + //
"\"total\": { \"$add\": [ \"$price\", \"$tax\" ] }," + //
"\"discounted\": { \"$cond\": { \"if\": \"$applyDiscount\", \"then\": 0.9, \"else\": 1.0 } }" + //
"}," + //
"\"in\": { \"$multiply\": [ \"$$total\", \"$$discounted\" ] }" + //
"}}}}"));
}
@ -1660,6 +1778,17 @@ public class ProjectionOperationUnitTests { @@ -1660,6 +1778,17 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project : { dayOfWeek: { $isoDayOfWeek: \"$birthday\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderIsoDayOfWeekWithTimezoneCorrectly() {
Document agg = project()
.and(DateOperators.dateOf("birthday").withTimezone(Timezone.valueOf("America/Chicago")).isoDayOfWeek())
.as("dayOfWeek").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project : { dayOfWeek: { $isoDayOfWeek: { \"date\" : \"$birthday\", \"timezone\" : \"America/Chicago\" } } } } }"));
}
@Test // DATAMONGO-1548
public void shouldRenderIsoWeekCorrectly() {
@ -1669,6 +1798,17 @@ public class ProjectionOperationUnitTests { @@ -1669,6 +1798,17 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project : { weekNumber: { $isoWeek: \"$date\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderIsoWeekWithTimezoneCorrectly() {
Document agg = project()
.and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).isoWeek()).as("weekNumber")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project : { weekNumber: { $isoWeek: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } } }"));
}
@Test // DATAMONGO-1548
public void shouldRenderIsoWeekYearCorrectly() {
@ -1678,6 +1818,17 @@ public class ProjectionOperationUnitTests { @@ -1678,6 +1818,17 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project : { yearNumber: { $isoWeekYear: \"$date\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderIsoWeekYearWithTimezoneCorrectly() {
Document agg = project()
.and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).isoWeekYear())
.as("yearNumber").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project : { yearNumber: { $isoWeekYear: { \"date\" : \"$date\", \"timezone\" : \"America/Chicago\" } } } } }"));
}
@Test // DATAMONGO-1548
public void shouldRenderSwitchCorrectly() {
@ -1728,6 +1879,128 @@ public class ProjectionOperationUnitTests { @@ -1728,6 +1879,128 @@ public class ProjectionOperationUnitTests {
assertThat(agg).isEqualTo(Document.parse("{ $project : { a: { $type: \"$a\" } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDateFromPartsWithJustTheYear() {
Document agg = project().and(DateOperators.dateFromParts().year(2018)).as("newDate")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse("{ $project : { newDate: { $dateFromParts: { year : 2018 } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDateFromParts() {
Document agg = project()
.and(DateOperators.dateFromParts().year(2018).month(3).day(23).hour(14).minute(25).second(10).milliseconds(2))
.as("newDate").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project : { newDate: { $dateFromParts: { year : 2018, month : 3, day : 23, hour : 14, minute : 25, second : 10, milliseconds : 2 } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDateFromPartsWithTimezone() {
Document agg = project()
.and(DateOperators.dateFromParts().withTimezone(Timezone.valueOf("America/Chicago")).year(2018)).as("newDate")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document
.parse("{ $project : { newDate: { $dateFromParts: { year : 2018, timezone : \"America/Chicago\" } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderIsoDateFromPartsWithJustTheYear() {
Document agg = project().and(DateOperators.dateFromParts().isoWeekYear(2018)).as("newDate")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse("{ $project : { newDate: { $dateFromParts: { isoWeekYear : 2018 } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderIsoDateFromParts() {
Document agg = project().and(DateOperators.dateFromParts().isoWeekYear(2018).isoWeek(12).isoDayOfWeek(5).hour(14)
.minute(30).second(42).milliseconds(2)).as("newDate").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project : { newDate: { $dateFromParts: { isoWeekYear : 2018, isoWeek : 12, isoDayOfWeek : 5, hour : 14, minute : 30, second : 42, milliseconds : 2 } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderIsoDateFromPartsWithTimezone() {
Document agg = project()
.and(DateOperators.dateFromParts().withTimezone(Timezone.valueOf("America/Chicago")).isoWeekYear(2018))
.as("newDate").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project : { newDate: { $dateFromParts: { isoWeekYear : 2018, timezone : \"America/Chicago\" } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDateToParts() {
Document agg = project().and(DateOperators.dateOf("date").toParts()).as("newDate")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse("{ $project : { newDate: { $dateToParts: { date : \"$date\" } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDateToIsoParts() {
Document agg = project().and(DateOperators.dateOf("date").toParts().iso8601()).as("newDate")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(
Document.parse("{ $project : { newDate: { $dateToParts: { date : \"$date\", iso8601 : true } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDateToPartsWithTimezone() {
Document agg = project()
.and(DateOperators.dateOf("date").withTimezone(Timezone.valueOf("America/Chicago")).toParts()).as("newDate")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document
.parse("{ $project : { newDate: { $dateToParts: { date : \"$date\", timezone : \"America/Chicago\" } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDateFromString() {
Document agg = project().and(DateOperators.dateFromString("2017-02-08T12:10:40.787")).as("newDate")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document
.parse("{ $project : { newDate: { $dateFromString: { dateString : \"2017-02-08T12:10:40.787\" } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDateFromStringWithFieldReference() {
Document agg = project().and(DateOperators.dateOf("date").fromString()).as("newDate")
.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg)
.isEqualTo(Document.parse("{ $project : { newDate: { $dateFromString: { dateString : \"$date\" } } } }"));
}
@Test // DATAMONGO-1834
public void shouldRenderDateFromStringWithTimezone() {
Document agg = project()
.and(DateOperators.dateFromString("2017-02-08T12:10:40.787").withTimezone(Timezone.valueOf("America/Chicago")))
.as("newDate").toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(agg).isEqualTo(Document.parse(
"{ $project : { newDate: { $dateFromString: { dateString : \"2017-02-08T12:10:40.787\", timezone : \"America/Chicago\" } } } }"));
}
private static Document exctractOperation(String field, Document fromProjectClause) {
return (Document) fromProjectClause.get(field);
}

2
src/main/asciidoc/reference/mongodb.adoc

@ -2051,7 +2051,7 @@ At the time of this writing we provide support for the following Aggregation Ope @@ -2051,7 +2051,7 @@ At the time of this writing we provide support for the following Aggregation Ope
| literal
| Date Aggregation Operators
| dayOfYear, dayOfMonth, dayOfWeek, year, month, week, hour, minute, second, millisecond, dateToString, isoDayOfWeek, isoWeek, isoWeekYear
| dayOfYear, dayOfMonth, dayOfWeek, year, month, week, hour, minute, second, millisecond, dateToString, dateFromString, dateFromParts, dateToParts, isoDayOfWeek, isoWeek, isoWeekYear
| Variable Operators
| map

Loading…
Cancel
Save