rename integration to live test (#9232)

This commit is contained in:
Benjamin Caure 2020-05-04 22:00:03 +02:00 committed by GitHub
parent 9e90cf5c80
commit aee002eeca

View File

@ -1,142 +1,142 @@
package com.baeldung.bsontojson; package com.baeldung.bsontojson;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bson.Document; import org.bson.Document;
import org.bson.json.Converter; import org.bson.json.Converter;
import org.bson.json.JsonMode; import org.bson.json.JsonMode;
import org.bson.json.JsonWriterSettings; import org.bson.json.JsonWriterSettings;
import org.bson.json.StrictJsonWriter; import org.bson.json.StrictJsonWriter;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.baeldung.morphia.domain.Book; import com.baeldung.morphia.domain.Book;
import com.baeldung.morphia.domain.Publisher; import com.baeldung.morphia.domain.Publisher;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import dev.morphia.Datastore; import dev.morphia.Datastore;
import dev.morphia.Morphia; import dev.morphia.Morphia;
public class BsonToJsonIntegrationTest { public class BsonToJsonLiveTest {
private static final String DB_NAME = "library"; private static final String DB_NAME = "library";
private static Datastore datastore; private static Datastore datastore;
@BeforeClass @BeforeClass
public static void setUp() { public static void setUp() {
Morphia morphia = new Morphia(); Morphia morphia = new Morphia();
morphia.mapPackage("com.baeldung.morphia"); morphia.mapPackage("com.baeldung.morphia");
datastore = morphia.createDatastore(new MongoClient(), DB_NAME); datastore = morphia.createDatastore(new MongoClient(), DB_NAME);
datastore.ensureIndexes(); datastore.ensureIndexes();
datastore.save(new Book() datastore.save(new Book()
.setIsbn("isbn") .setIsbn("isbn")
.setTitle("title") .setTitle("title")
.setAuthor("author") .setAuthor("author")
.setCost(3.95) .setCost(3.95)
.setPublisher(new Publisher(new ObjectId("fffffffffffffffffffffffa"),"publisher")) .setPublisher(new Publisher(new ObjectId("fffffffffffffffffffffffa"),"publisher"))
.setPublishDate(LocalDateTime.parse("2020-01-01T18:13:32Z", DateTimeFormatter.ISO_DATE_TIME)) .setPublishDate(LocalDateTime.parse("2020-01-01T18:13:32Z", DateTimeFormatter.ISO_DATE_TIME))
.addCompanionBooks(new Book().setIsbn("isbn2"))); .addCompanionBooks(new Book().setIsbn("isbn2")));
} }
@AfterClass @AfterClass
public static void tearDown() { public static void tearDown() {
datastore.delete(datastore.createQuery(Book.class)); datastore.delete(datastore.createQuery(Book.class));
} }
@Test @Test
public void givenBsonDocument_whenUsingStandardJsonTransformation_thenJsonDateIsObjectEpochTime() { public void givenBsonDocument_whenUsingStandardJsonTransformation_thenJsonDateIsObjectEpochTime() {
String json = null; String json = null;
try (MongoClient mongoClient = new MongoClient()) { try (MongoClient mongoClient = new MongoClient()) {
MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME); MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME);
Document bson = mongoDatabase.getCollection("Books").find().first(); Document bson = mongoDatabase.getCollection("Books").find().first();
json = bson.toJson(); json = bson.toJson();
} }
String expectedJson = "{\"_id\": \"isbn\", " + String expectedJson = "{\"_id\": \"isbn\", " +
"\"className\": \"com.baeldung.morphia.domain.Book\", " + "\"className\": \"com.baeldung.morphia.domain.Book\", " +
"\"title\": \"title\", " + "\"title\": \"title\", " +
"\"author\": \"author\", " + "\"author\": \"author\", " +
"\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " +
"\"name\": \"publisher\"}, " + "\"name\": \"publisher\"}, " +
"\"price\": 3.95, " + "\"price\": 3.95, " +
"\"publishDate\": {\"$date\": 1577898812000}}"; "\"publishDate\": {\"$date\": 1577898812000}}";
assertNotNull(json); assertNotNull(json);
assertEquals(expectedJson, json); assertEquals(expectedJson, json);
} }
@Test @Test
public void givenBsonDocument_whenUsingRelaxedJsonTransformation_thenJsonDateIsObjectIsoDate() { public void givenBsonDocument_whenUsingRelaxedJsonTransformation_thenJsonDateIsObjectIsoDate() {
String json = null; String json = null;
try (MongoClient mongoClient = new MongoClient()) { try (MongoClient mongoClient = new MongoClient()) {
MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME); MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME);
Document bson = mongoDatabase.getCollection("Books").find().first(); Document bson = mongoDatabase.getCollection("Books").find().first();
json = bson.toJson(JsonWriterSettings json = bson.toJson(JsonWriterSettings
.builder() .builder()
.outputMode(JsonMode.RELAXED) .outputMode(JsonMode.RELAXED)
.build()); .build());
} }
String expectedJson = "{\"_id\": \"isbn\", " + String expectedJson = "{\"_id\": \"isbn\", " +
"\"className\": \"com.baeldung.morphia.domain.Book\", " + "\"className\": \"com.baeldung.morphia.domain.Book\", " +
"\"title\": \"title\", " + "\"title\": \"title\", " +
"\"author\": \"author\", " + "\"author\": \"author\", " +
"\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " +
"\"name\": \"publisher\"}, " + "\"name\": \"publisher\"}, " +
"\"price\": 3.95, " + "\"price\": 3.95, " +
"\"publishDate\": {\"$date\": \"2020-01-01T17:13:32Z\"}}"; "\"publishDate\": {\"$date\": \"2020-01-01T17:13:32Z\"}}";
assertNotNull(json); assertNotNull(json);
assertEquals(expectedJson, json); assertEquals(expectedJson, json);
} }
@Test @Test
public void givenBsonDocument_whenUsingCustomJsonTransformation_thenJsonDateIsStringField() { public void givenBsonDocument_whenUsingCustomJsonTransformation_thenJsonDateIsStringField() {
String json = null; String json = null;
try (MongoClient mongoClient = new MongoClient()) { try (MongoClient mongoClient = new MongoClient()) {
MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME); MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME);
Document bson = mongoDatabase.getCollection("Books").find().first(); Document bson = mongoDatabase.getCollection("Books").find().first();
json = bson.toJson(JsonWriterSettings json = bson.toJson(JsonWriterSettings
.builder() .builder()
.dateTimeConverter(new JsonDateTimeConverter()) .dateTimeConverter(new JsonDateTimeConverter())
.build()); .build());
} }
String expectedJson = "{\"_id\": \"isbn\", " + String expectedJson = "{\"_id\": \"isbn\", " +
"\"className\": \"com.baeldung.morphia.domain.Book\", " + "\"className\": \"com.baeldung.morphia.domain.Book\", " +
"\"title\": \"title\", " + "\"title\": \"title\", " +
"\"author\": \"author\", " + "\"author\": \"author\", " +
"\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " +
"\"name\": \"publisher\"}, " + "\"name\": \"publisher\"}, " +
"\"price\": 3.95, " + "\"price\": 3.95, " +
"\"publishDate\": \"2020-01-01T17:13:32Z\"}"; "\"publishDate\": \"2020-01-01T17:13:32Z\"}";
assertEquals(expectedJson, json); assertEquals(expectedJson, json);
} }
} }