BAEL-1962 Convert ZonedDateTime for MongoDB interactions (#5640)
This commit is contained in:
		
							parent
							
								
									e1056e04de
								
							
						
					
					
						commit
						2b581cb3f6
					
				| @ -109,7 +109,7 @@ | ||||
|     </build> | ||||
| 
 | ||||
|     <properties> | ||||
|         <org.springframework.data.version>2.1.0.RELEASE</org.springframework.data.version> | ||||
|         <org.springframework.data.version>2.1.2.RELEASE</org.springframework.data.version> | ||||
|         <querydsl.version>4.1.4</querydsl.version> | ||||
|         <mysema.maven.version>1.1.3</mysema.maven.version> | ||||
|         <spring.version>5.1.0.RELEASE</spring.version> | ||||
|  | ||||
| @ -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); | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -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 + '}'; | ||||
|     } | ||||
| } | ||||
| @ -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<Action, String> { } | ||||
| @ -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<Date, ZonedDateTime> { | ||||
|     @Override | ||||
|     public ZonedDateTime convert(Date date) { | ||||
|         return date.toInstant().atZone(ZoneOffset.UTC); | ||||
|     } | ||||
| } | ||||
| @ -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<ZonedDateTime, Date> { | ||||
|     @Override | ||||
|     public Date convert(ZonedDateTime zonedDateTime) { | ||||
|         return Date.from(zonedDateTime.toInstant()); | ||||
|     } | ||||
| } | ||||
| @ -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); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user