diff --git a/persistence-modules/spring-data-mongodb/pom.xml b/persistence-modules/spring-data-mongodb/pom.xml index 466acf5a43..76ec5a96a6 100644 --- a/persistence-modules/spring-data-mongodb/pom.xml +++ b/persistence-modules/spring-data-mongodb/pom.xml @@ -109,7 +109,7 @@ - 2.1.0.RELEASE + 2.1.2.RELEASE 4.1.4 1.1.3 5.1.0.RELEASE diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java index f1048fa145..9fa90acf86 100644 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java @@ -3,6 +3,8 @@ package com.baeldung.config; import java.util.ArrayList; import java.util.List; +import converter.ZonedDateTimeReadConverter; +import converter.ZonedDateTimeWriteConverter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; @@ -52,6 +54,8 @@ public class MongoConfig extends AbstractMongoConfiguration { @Override public MongoCustomConversions customConversions() { converters.add(new UserWriterConverter()); + converters.add(new ZonedDateTimeReadConverter()); + converters.add(new ZonedDateTimeWriteConverter()); return new MongoCustomConversions(converters); } @@ -64,5 +68,5 @@ public class MongoConfig extends AbstractMongoConfiguration { MongoTransactionManager transactionManager(MongoDbFactory dbFactory) { return new MongoTransactionManager(dbFactory); } - + } diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Action.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Action.java new file mode 100644 index 0000000000..aa480dbdf7 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Action.java @@ -0,0 +1,51 @@ +package com.baeldung.model; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.time.ZonedDateTime; + +@Document +public class Action { + + @Id + private String id; + + private String description; + private ZonedDateTime time; + + public Action(String id, String description, ZonedDateTime time) { + this.id = id; + this.description = description; + this.time = time; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public ZonedDateTime getTime() { + return time; + } + + public void setTime(ZonedDateTime time) { + this.time = time; + } + + @Override + public String toString() { + return "Action{id='" + id + "', description='" + description + "', time=" + time + '}'; + } +} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/ActionRepository.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/ActionRepository.java new file mode 100644 index 0000000000..bdca490fe6 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/ActionRepository.java @@ -0,0 +1,6 @@ +package com.baeldung.repository; + +import com.baeldung.model.Action; +import org.springframework.data.mongodb.repository.MongoRepository; + +public interface ActionRepository extends MongoRepository { } \ No newline at end of file diff --git a/persistence-modules/spring-data-mongodb/src/main/java/converter/ZonedDateTimeReadConverter.java b/persistence-modules/spring-data-mongodb/src/main/java/converter/ZonedDateTimeReadConverter.java new file mode 100644 index 0000000000..a2d847957b --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/converter/ZonedDateTimeReadConverter.java @@ -0,0 +1,14 @@ +package converter; + +import org.springframework.core.convert.converter.Converter; + +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.Date; + +public class ZonedDateTimeReadConverter implements Converter { + @Override + public ZonedDateTime convert(Date date) { + return date.toInstant().atZone(ZoneOffset.UTC); + } +} \ No newline at end of file diff --git a/persistence-modules/spring-data-mongodb/src/main/java/converter/ZonedDateTimeWriteConverter.java b/persistence-modules/spring-data-mongodb/src/main/java/converter/ZonedDateTimeWriteConverter.java new file mode 100644 index 0000000000..e13ac2d130 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/main/java/converter/ZonedDateTimeWriteConverter.java @@ -0,0 +1,13 @@ +package converter; + +import org.springframework.core.convert.converter.Converter; + +import java.time.ZonedDateTime; +import java.util.Date; + +public class ZonedDateTimeWriteConverter implements Converter { + @Override + public Date convert(ZonedDateTime zonedDateTime) { + return Date.from(zonedDateTime.toInstant()); + } +} \ No newline at end of file diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/ActionRepositoryLiveTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/ActionRepositoryLiveTest.java new file mode 100644 index 0000000000..096015ca0a --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/ActionRepositoryLiveTest.java @@ -0,0 +1,50 @@ +package com.baeldung.repository; + +import com.baeldung.config.MongoConfig; +import com.baeldung.model.Action; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.time.ZoneOffset; +import java.time.ZonedDateTime; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = MongoConfig.class) +public class ActionRepositoryLiveTest { + + @Autowired + private MongoOperations mongoOps; + + @Autowired + private ActionRepository actionRepository; + + @Before + public void setup() { + if (!mongoOps.collectionExists(Action.class)) { + mongoOps.createCollection(Action.class); + } + } + + @Test + public void givenSavedAction_TimeIsRetrievedCorrectly() { + String id = "testId"; + ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); + + actionRepository.save(new Action(id, "click-action", now)); + Action savedAction = actionRepository.findById(id).get(); + + Assert.assertEquals(now.withNano(0), savedAction.getTime().withNano(0)); + } + + @After + public void tearDown() { + mongoOps.dropCollection(Action.class); + } +} \ No newline at end of file