JAVA-4012: Moved 1 article from spring-data-mongodb to
spring-boot-persistence-mongodb
This commit is contained in:
@@ -9,9 +9,8 @@
|
||||
- [Custom Cascading in Spring Data MongoDB](http://www.baeldung.com/cascading-with-dbref-and-lifecycle-events-in-spring-data-mongodb)
|
||||
- [Introduction to Spring Data MongoDB](http://www.baeldung.com/spring-data-mongodb-tutorial)
|
||||
- [Spring Data MongoDB: Projections and Aggregations](http://www.baeldung.com/spring-data-mongodb-projections-aggregations)
|
||||
- [Spring Data MongoDB Transactions](https://www.baeldung.com/spring-data-mongodb-transactions )
|
||||
- [ZonedDateTime with Spring Data MongoDB](https://www.baeldung.com/spring-data-mongodb-zoneddatetime)
|
||||
|
||||
- [Spring Data Annotations](http://www.baeldung.com/spring-data-annotations)
|
||||
- [Spring Data MongoDB Transactions](https://www.baeldung.com/spring-data-mongodb-transactions)
|
||||
|
||||
## Spring Data MongoDB Live Testing
|
||||
|
||||
|
||||
@@ -25,9 +25,6 @@ import com.mongodb.MongoClientSettings;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
|
||||
import converter.ZonedDateTimeReadConverter;
|
||||
import converter.ZonedDateTimeWriteConverter;
|
||||
|
||||
@Configuration
|
||||
@EnableMongoRepositories(basePackages = "com.baeldung.repository")
|
||||
public class MongoConfig extends AbstractMongoClientConfiguration {
|
||||
@@ -69,8 +66,6 @@ public class MongoConfig extends AbstractMongoClientConfiguration {
|
||||
@Override
|
||||
public MongoCustomConversions customConversions() {
|
||||
converters.add(new UserWriterConverter());
|
||||
converters.add(new ZonedDateTimeReadConverter());
|
||||
converters.add(new ZonedDateTimeWriteConverter());
|
||||
return new MongoCustomConversions(converters);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
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 + '}';
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.model.Action;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
public interface ActionRepository extends MongoRepository<Action, String> { }
|
||||
@@ -1,14 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
* 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