DATAMONGO-1128 - Added test cases to validate Optional mapping.

Added test cases to make sure Optional instances are handled correctly and the converters are actually applied to the nested value.
This commit is contained in:
Oliver Gierke
2014-12-31 13:59:43 +01:00
parent f3d2ae366e
commit f814b1ef47

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.mongodb.core.convert;
import static java.time.ZoneId.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
@@ -36,6 +37,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
@@ -1962,6 +1964,43 @@ public class MappingMongoConverterUnitTests {
assertThat(converter.read(TypeWithLocalDateTime.class, result).date, is(reference));
}
/**
* @see DATAMONGO-1128
*/
@Test
public void writesOptionalsCorrectly() {
TypeWithOptional type = new TypeWithOptional();
type.localDateTime = Optional.of(LocalDateTime.now());
DBObject result = new BasicDBObject();
converter.write(type, result);
assertThat(getAsDBObject(result, "string"), is((DBObject) new BasicDBObject()));
DBObject localDateTime = getAsDBObject(result, "localDateTime");
assertThat(localDateTime.get("value"), is(instanceOf(Date.class)));
}
/**
* @see DATAMONGO-1128
*/
@Test
public void readsOptionalsCorrectly() {
LocalDateTime now = LocalDateTime.now();
Date reference = Date.from(now.atZone(systemDefault()).toInstant());
BasicDBObject optionalOfLocalDateTime = new BasicDBObject("value", reference);
DBObject result = new BasicDBObject("localDateTime", optionalOfLocalDateTime);
TypeWithOptional read = converter.read(TypeWithOptional.class, result);
assertThat(read.string, is(Optional.<String> empty()));
assertThat(read.localDateTime, is(Optional.of(now)));
}
static class GenericType<T> {
T content;
}
@@ -2258,4 +2297,10 @@ public class MappingMongoConverterUnitTests {
this.date = LocalDateTime.now();
}
}
static class TypeWithOptional {
Optional<String> string = Optional.empty();
Optional<LocalDateTime> localDateTime = Optional.empty();
}
}