Skip to content

Commit

Permalink
finos#1296 add 3 more default type converters + tests for DefaultType…
Browse files Browse the repository at this point in the history
…Converters
  • Loading branch information
junaidzm13 committed Apr 24, 2024
1 parent e1858bd commit af4772c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ object DefaultTypeConverters {
val stringToDoubleConverter: TypeConverter[String, Double] = TypeConverter(classOf[String], classOf[Double], withNullSafety[String, Double](_, _.toDouble))
val stringToLongConverter: TypeConverter[String, Long] = TypeConverter[String, Long](classOf[String], classOf[Long], withNullSafety[String, Long](_, _.toLong))
val stringToIntConverter: TypeConverter[String, Integer] = TypeConverter(classOf[String], classOf[Integer], withNullSafety[String, Integer](_, _.toInt))
val intToStringConverter: TypeConverter[Integer, String] = TypeConverter(classOf[Integer], classOf[String], withNullSafety[Integer, String](_, _.toString))
val longToStringConverter: TypeConverter[Long, String] = TypeConverter(classOf[Long], classOf[String], withNullSafety[Long, String](_, _.toString))
val doubleToStringConverter: TypeConverter[Double, String] = TypeConverter(classOf[Double], classOf[String], withNullSafety[Double, String](_, _.toString))


private def withNullSafety[T1, T2 >: Null](v: T1, fn: T1 => T2): T2 = Option(v).map(fn).orNull

def getAll: List[TypeConverter[_, _]] = List(
stringToDoubleConverter,
doubleToStringConverter,
stringToLongConverter,
stringToIntConverter
longToStringConverter,
stringToIntConverter,
intToStringConverter,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.finos.vuu.util.schema.typeConversion

import org.finos.vuu.util.schema.typeConversion.DefaultTypeConverters._
import org.scalatest.prop.TableDrivenPropertyChecks._
import org.scalatest.featurespec.AnyFeatureSpec
import org.scalatest.matchers.should.Matchers

class DefaultTypeConvertersTest extends AnyFeatureSpec with Matchers {

Feature("Handle converting valid value to the given type") {

forAll(Table(
("title", "converter", "input", "expected output"),
("String to Double", stringToDoubleConverter, "10.5", 10.5),
("Double to String", doubleToStringConverter, 10.5, "10.5"),
("String to Long", stringToLongConverter, "10000", 10_000L),
("Long to String", longToStringConverter, 10_000L, "10000"),
("Int to String", intToStringConverter, 10, "10"),
("String to Int", stringToIntConverter, "10", 10),
))((title, converter, input, expectedOutput) => {
Scenario(title) {
converter.asInstanceOf[TypeConverter[Any, Any]].convert(input) shouldEqual expectedOutput
}
})
}

Feature("Handle null inputs") {
forAll(Table(
("title", "converter"),
("String to Double", stringToDoubleConverter),
("Double to String", doubleToStringConverter),
("String to Long", stringToLongConverter),
("Long to String", longToStringConverter),
("Int to String", intToStringConverter),
("String to Int", stringToIntConverter),
))((title, converter) => {
Scenario(title) {
converter.convert(null) shouldEqual null
}
})
}

}

0 comments on commit af4772c

Please sign in to comment.