JAVA-4012: Moved 1 article from spring-data-mongodb to
spring-boot-persistence-mongodb
This commit is contained in:
@@ -4,3 +4,5 @@
|
||||
- [Spring Boot Integration Testing with Embedded MongoDB](http://www.baeldung.com/spring-boot-embedded-mongodb)
|
||||
- [Upload and Retrieve Files Using MongoDB and Spring Boot](https://www.baeldung.com/spring-boot-mongodb-upload-file)
|
||||
- [GridFS in Spring Data MongoDB](http://www.baeldung.com/spring-data-mongodb-gridfs)
|
||||
- [ZonedDateTime with Spring Data MongoDB](https://www.baeldung.com/spring-data-mongodb-zoneddatetime)
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.zoneddatetime.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
|
||||
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import com.baeldung.zoneddatetime.converter.ZonedDateTimeReadConverter;
|
||||
import com.baeldung.zoneddatetime.converter.ZonedDateTimeWriteConverter;
|
||||
import com.mongodb.ConnectionString;
|
||||
import com.mongodb.MongoClientSettings;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
|
||||
@Configuration
|
||||
@EnableMongoRepositories(basePackages = { "com.baeldung" })
|
||||
public class MongoConfig extends AbstractMongoClientConfiguration {
|
||||
|
||||
private final List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
|
||||
|
||||
@Override
|
||||
protected String getDatabaseName() {
|
||||
return "test";
|
||||
}
|
||||
|
||||
@Override
|
||||
public MongoClient mongoClient() {
|
||||
final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test");
|
||||
final MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
|
||||
.applyConnectionString(connectionString)
|
||||
.build();
|
||||
return MongoClients.create(mongoClientSettings);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> getMappingBasePackages() {
|
||||
return Collections.singleton("com.baeldung");
|
||||
}
|
||||
|
||||
@Override
|
||||
public MongoCustomConversions customConversions() {
|
||||
converters.add(new ZonedDateTimeReadConverter());
|
||||
converters.add(new ZonedDateTimeWriteConverter());
|
||||
return new MongoCustomConversions(converters);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.zoneddatetime.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 com.baeldung.zoneddatetime.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,51 @@
|
||||
package com.baeldung.zoneddatetime.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,7 @@
|
||||
package com.baeldung.zoneddatetime.repository;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
import com.baeldung.zoneddatetime.model.Action;
|
||||
|
||||
public interface ActionRepository extends MongoRepository<Action, String> { }
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
|
||||
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 com.baeldung.zoneddatetime.config.MongoConfig;
|
||||
import com.baeldung.zoneddatetime.model.Action;
|
||||
import com.baeldung.zoneddatetime.repository.ActionRepository;
|
||||
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
/**
|
||||
*
|
||||
* This test requires:
|
||||
* * mongodb instance running on the environment
|
||||
*
|
||||
*/
|
||||
@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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user