Skip to content

Commit 2b581cb

Browse files
laurentiudmaibin
authored andcommitted
BAEL-1962 Convert ZonedDateTime for MongoDB interactions (eugenp#5640)
1 parent e1056e0 commit 2b581cb

File tree

7 files changed

+140
-2
lines changed

7 files changed

+140
-2
lines changed

persistence-modules/spring-data-mongodb/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
</build>
110110

111111
<properties>
112-
<org.springframework.data.version>2.1.0.RELEASE</org.springframework.data.version>
112+
<org.springframework.data.version>2.1.2.RELEASE</org.springframework.data.version>
113113
<querydsl.version>4.1.4</querydsl.version>
114114
<mysema.maven.version>1.1.3</mysema.maven.version>
115115
<spring.version>5.1.0.RELEASE</spring.version>

persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
import converter.ZonedDateTimeReadConverter;
7+
import converter.ZonedDateTimeWriteConverter;
68
import org.springframework.context.annotation.Bean;
79
import org.springframework.context.annotation.Configuration;
810
import org.springframework.core.convert.converter.Converter;
@@ -52,6 +54,8 @@ public CascadeSaveMongoEventListener cascadingMongoEventListener() {
5254
@Override
5355
public MongoCustomConversions customConversions() {
5456
converters.add(new UserWriterConverter());
57+
converters.add(new ZonedDateTimeReadConverter());
58+
converters.add(new ZonedDateTimeWriteConverter());
5559
return new MongoCustomConversions(converters);
5660
}
5761

@@ -64,5 +68,5 @@ public GridFsTemplate gridFsTemplate() throws Exception {
6468
MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
6569
return new MongoTransactionManager(dbFactory);
6670
}
67-
71+
6872
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.model;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.mongodb.core.mapping.Document;
5+
6+
import java.time.ZonedDateTime;
7+
8+
@Document
9+
public class Action {
10+
11+
@Id
12+
private String id;
13+
14+
private String description;
15+
private ZonedDateTime time;
16+
17+
public Action(String id, String description, ZonedDateTime time) {
18+
this.id = id;
19+
this.description = description;
20+
this.time = time;
21+
}
22+
23+
public String getId() {
24+
return id;
25+
}
26+
27+
public void setId(String id) {
28+
this.id = id;
29+
}
30+
31+
public String getDescription() {
32+
return description;
33+
}
34+
35+
public void setDescription(String description) {
36+
this.description = description;
37+
}
38+
39+
public ZonedDateTime getTime() {
40+
return time;
41+
}
42+
43+
public void setTime(ZonedDateTime time) {
44+
this.time = time;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return "Action{id='" + id + "', description='" + description + "', time=" + time + '}';
50+
}
51+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.repository;
2+
3+
import com.baeldung.model.Action;
4+
import org.springframework.data.mongodb.repository.MongoRepository;
5+
6+
public interface ActionRepository extends MongoRepository<Action, String> { }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package converter;
2+
3+
import org.springframework.core.convert.converter.Converter;
4+
5+
import java.time.ZoneOffset;
6+
import java.time.ZonedDateTime;
7+
import java.util.Date;
8+
9+
public class ZonedDateTimeReadConverter implements Converter<Date, ZonedDateTime> {
10+
@Override
11+
public ZonedDateTime convert(Date date) {
12+
return date.toInstant().atZone(ZoneOffset.UTC);
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package converter;
2+
3+
import org.springframework.core.convert.converter.Converter;
4+
5+
import java.time.ZonedDateTime;
6+
import java.util.Date;
7+
8+
public class ZonedDateTimeWriteConverter implements Converter<ZonedDateTime, Date> {
9+
@Override
10+
public Date convert(ZonedDateTime zonedDateTime) {
11+
return Date.from(zonedDateTime.toInstant());
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.repository;
2+
3+
import com.baeldung.config.MongoConfig;
4+
import com.baeldung.model.Action;
5+
import org.junit.After;
6+
import org.junit.Assert;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.data.mongodb.core.MongoOperations;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
15+
import java.time.ZoneOffset;
16+
import java.time.ZonedDateTime;
17+
18+
@RunWith(SpringJUnit4ClassRunner.class)
19+
@ContextConfiguration(classes = MongoConfig.class)
20+
public class ActionRepositoryLiveTest {
21+
22+
@Autowired
23+
private MongoOperations mongoOps;
24+
25+
@Autowired
26+
private ActionRepository actionRepository;
27+
28+
@Before
29+
public void setup() {
30+
if (!mongoOps.collectionExists(Action.class)) {
31+
mongoOps.createCollection(Action.class);
32+
}
33+
}
34+
35+
@Test
36+
public void givenSavedAction_TimeIsRetrievedCorrectly() {
37+
String id = "testId";
38+
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
39+
40+
actionRepository.save(new Action(id, "click-action", now));
41+
Action savedAction = actionRepository.findById(id).get();
42+
43+
Assert.assertEquals(now.withNano(0), savedAction.getTime().withNano(0));
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
mongoOps.dropCollection(Action.class);
49+
}
50+
}

0 commit comments

Comments
 (0)