From 27f80452ec29dcfe8013b180ccd8ecfa6b300ff8 Mon Sep 17 00:00:00 2001 From: QuadStingray Date: Thu, 2 Nov 2023 22:39:36 +0100 Subject: [PATCH] test: Added Joda Duration Converter to plugin sample --- scalastyle-config.xml | 2 +- .../mongodb/bson/JodaConverterPlugin.scala | 11 +++--- .../bson/JodaConverterPluginSpec.scala | 36 +++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/test/scala/dev/mongocamp/driver/mongodb/bson/JodaConverterPluginSpec.scala diff --git a/scalastyle-config.xml b/scalastyle-config.xml index 9123d9d4..11b327cb 100644 --- a/scalastyle-config.xml +++ b/scalastyle-config.xml @@ -85,7 +85,7 @@ - + diff --git a/src/test/scala/dev/mongocamp/driver/mongodb/bson/JodaConverterPlugin.scala b/src/test/scala/dev/mongocamp/driver/mongodb/bson/JodaConverterPlugin.scala index 8740d2c3..8d435657 100644 --- a/src/test/scala/dev/mongocamp/driver/mongodb/bson/JodaConverterPlugin.scala +++ b/src/test/scala/dev/mongocamp/driver/mongodb/bson/JodaConverterPlugin.scala @@ -1,15 +1,18 @@ package dev.mongocamp.driver.mongodb.bson import org.bson.BsonValue -import org.joda.time.DateTime -import org.mongodb.scala.bson.{BsonDateTime, BsonNull} +import org.joda.time.{DateTime, Duration} +import org.mongodb.scala.bson.{BsonDateTime, BsonNull, BsonString} class JodaConverterPlugin extends AbstractConverterPlugin { - override def customClassList: List[Class[_]] = List(classOf[DateTime]) + override def customClassList: List[Class[_]] = List(classOf[DateTime], classOf[Duration]) override def toBson(value: Any): BsonValue = value match { - case dt: DateTime => BsonDateTime(dt.toDate) + case dt: DateTime => + BsonDateTime(dt.toDate) + case dt: org.joda.time.Duration => + BsonString(s"${dt.getMillis}ms") case _ => BsonNull() diff --git a/src/test/scala/dev/mongocamp/driver/mongodb/bson/JodaConverterPluginSpec.scala b/src/test/scala/dev/mongocamp/driver/mongodb/bson/JodaConverterPluginSpec.scala new file mode 100644 index 00000000..99210833 --- /dev/null +++ b/src/test/scala/dev/mongocamp/driver/mongodb/bson/JodaConverterPluginSpec.scala @@ -0,0 +1,36 @@ +package dev.mongocamp.driver.mongodb.bson + +import org.joda.time.DateTime +import org.specs2.mutable.Specification +import org.specs2.specification.BeforeAfterAll + +import scala.concurrent.duration.Duration + +class JodaConverterPluginSpec extends Specification with BeforeAfterAll { + + sequential + + "JodaConverterPlugin" should { + + "convert joda dates to bson dates" in { + val dateTime = new DateTime("2023-11-02") + val bsonDocument = BsonConverter.toBson(dateTime) + (bsonDocument.toString must be).equalTo("BsonDateTime{value=1698879600000}") + } + + "convert joda duration to bson string" in { + val duration = org.joda.time.Duration.standardDays(1) + val bsonDocument = BsonConverter.toBson(duration) + (bsonDocument.toString must be).equalTo("BsonString{value='86400000ms'}") + (Duration("86400000ms").toMillis must be).equalTo(duration.getMillis) + } + + } + + override def beforeAll(): Unit = { + BsonConverter.converterPlugin = new JodaConverterPlugin() + } + override def afterAll(): Unit = { + BsonConverter.converterPlugin = new BaseConverterPlugin() + } +}