[JAVA-11252] Fix Mongodb live test

This commit is contained in:
Haroon Khan 2022-04-14 21:10:20 +01:00
parent af929125fc
commit 0c480dbeed

View File

@ -21,7 +21,7 @@ import dev.morphia.Datastore;
import dev.morphia.Morphia; import dev.morphia.Morphia;
public class BsonToJsonLiveTest { 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;
@ -31,17 +31,17 @@ public class BsonToJsonLiveTest {
morphia.mapPackage("com.baeldung.bsontojson"); morphia.mapPackage("com.baeldung.bsontojson");
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-01T17: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));
@ -50,32 +50,32 @@ public class BsonToJsonLiveTest {
@Test @Test
public void givenBsonDocument_whenUsingStandardJsonTransformation_thenJsonDateIsObjectEpochTime() { public void givenBsonDocument_whenUsingStandardJsonTransformation_thenJsonDateIsObjectEpochTime() {
String json = null; String json;
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.bsontojson.Book\", " + "\"className\": \"com.baeldung.bsontojson.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;
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();
@ -84,25 +84,25 @@ public class BsonToJsonLiveTest {
.outputMode(JsonMode.RELAXED) .outputMode(JsonMode.RELAXED)
.build()); .build());
} }
String expectedJson = "{\"_id\": \"isbn\", " + String expectedJson = "{\"_id\": \"isbn\", " +
"\"className\": \"com.baeldung.bsontojson.Book\", " + "\"className\": \"com.baeldung.bsontojson.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;
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();
@ -112,13 +112,13 @@ public class BsonToJsonLiveTest {
.build()); .build());
} }
String expectedJson = "{\"_id\": \"isbn\", " + String expectedJson = "{\"_id\": \"isbn\", " +
"\"className\": \"com.baeldung.bsontojson.Book\", " + "\"className\": \"com.baeldung.bsontojson.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);