Skip to content

Commit

Permalink
test: Added Joda Duration Converter to plugin sample
Browse files Browse the repository at this point in the history
  • Loading branch information
QuadStingray committed Nov 2, 2023
1 parent c0833d7 commit 27f8045
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion scalastyle-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
</check>
<check level="warning" class="org.scalastyle.scalariform.CyclomaticComplexityChecker" enabled="true">
<parameters>
<parameter name="maximum"><![CDATA[30]]></parameter>
<parameter name="maximum"><![CDATA[35]]></parameter>
</parameters>
</check>
<check level="warning" class="org.scalastyle.scalariform.UppercaseLChecker" enabled="true"></check>
Expand Down
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 27f8045

Please sign in to comment.