Skip to content

Commit

Permalink
Added missing test for java conversions (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsuchy authored and tovbinm committed Jun 18, 2019
1 parent 9fee4d4 commit 1a8cc73
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ package object types extends FeatureTypeSparkConverters {
implicit class VectorConversions(val v: Vector) extends AnyVal {
def toOPVector: OPVector = new OPVector(v)
}

// Arrays
implicit class ArrayDoubleConversions(val v: Array[Double]) extends AnyVal {
def toReal: Array[Real] = v.map(_.toReal)
Expand All @@ -209,22 +210,26 @@ package object types extends FeatureTypeSparkConverters {
def toTextMap: TextMap = new TextMap(Option(v).map(_.asScala.toMap).getOrElse(Map.empty))
}
implicit class JMapSetConversions(val v: java.util.Map[String, java.util.HashSet[String]]) extends AnyVal {
def toMultiPickListMap: MultiPickListMap =
new MultiPickListMap(Option(v).map(_.asScala.mapValues(_.asScala.toSet).toMap).getOrElse(Map.empty))
def toMultiPickListMap: MultiPickListMap = new MultiPickListMap(
Option(v).map(_.asScala.mapValues {
case null => Set.empty[String]
case s => s.asScala.toSet
}.toMap).getOrElse(Map.empty)
)
}
implicit class JMapLongConversions(val v: java.util.Map[String, java.lang.Long]) extends AnyVal {
def toIntegralMap: IntegralMap = new IntegralMap(
Option(v).map(_.asScala.mapValues(_.longValue()).toMap).getOrElse(Map.empty)
Option(v).map(_.asScala.toMap).getOrElse(Map.empty).asInstanceOf[Map[String, Long]]
)
}
implicit class JMapDoubleConversions(val v: java.util.Map[String, java.lang.Double]) extends AnyVal {
def toRealMap: RealMap = new RealMap(
Option(v).map(_.asScala.mapValues(_.doubleValue()).toMap).getOrElse(Map.empty)
Option(v).map(_.asScala.toMap).getOrElse(Map.empty).asInstanceOf[Map[String, Double]]
)
}
implicit class JMapBooleanConversions(val v: java.util.Map[String, java.lang.Boolean]) extends AnyVal {
def toBinaryMap: BinaryMap = new BinaryMap(
Option(v).map(_.asScala.mapValues(_.booleanValue()).toMap).getOrElse(Map.empty)
Option(v).map(_.asScala.toMap).getOrElse(Map.empty).asInstanceOf[Map[String, Boolean]]
)
}
implicit class MapStringConversions(val v: Map[String, String]) extends AnyVal {
Expand Down Expand Up @@ -263,6 +268,7 @@ package object types extends FeatureTypeSparkConverters {
implicit class MapGeolocationConversions(val v: Map[String, Seq[Double]]) extends AnyVal {
def toGeolocationMap: GeolocationMap = new GeolocationMap(v)
}

implicit def intMapToRealMap(m: IntegralMap#Value): RealMap#Value = m.map { case (k, v) => k -> v.toDouble }
implicit def booleanToRealMap(m: BinaryMap#Value): RealMap#Value = m.map { case (k, b) => k -> (if (b) 1.0 else 0.0) }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2017, Salesforce.com, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.salesforce.op.features.types

import java.util

import com.salesforce.op.test.TestCommon
import org.junit.runner.RunWith
import org.scalatest.FlatSpec
import org.scalatest.junit.JUnitRunner


@RunWith(classOf[JUnitRunner])
class JavaConversionTest extends FlatSpec with TestCommon {

Spec[JavaConversionTest] should "convert java Map to TextMap" in {
type T = util.HashMap[String, String]
null.asInstanceOf[T].toTextMap shouldEqual TextMap(Map())
val j = new T()
j.toTextMap shouldEqual TextMap(Map())
j.put("A", "a")
j.put("B", null)
j.toTextMap shouldEqual TextMap(Map("A" -> "a", "B" -> null))
}

it should "convert java Map to MultiPickListMap" in {
type T = util.HashMap[String, java.util.HashSet[String]]
null.asInstanceOf[T].toMultiPickListMap shouldEqual MultiPickListMap(Map())
val j = new T()
j.toMultiPickListMap shouldEqual MultiPickListMap(Map())
val h = new util.HashSet[String]()
h.add("X")
h.add("Y")
j.put("test", h)
j.put("test2", null)
j.toMultiPickListMap shouldEqual MultiPickListMap(Map("test" -> Set("X", "Y"), "test2" -> Set()))
}

it should "convert java Map to IntegralMap" in {
type T = util.HashMap[String, java.lang.Long]
null.asInstanceOf[T].toIntegralMap shouldEqual IntegralMap(Map())
val j = new T()
j.toIntegralMap shouldEqual IntegralMap(Map())
j.put("test", java.lang.Long.valueOf(17))
j.put("test2", null)
j.toIntegralMap.v("test") shouldEqual 17L
j.toIntegralMap.v("test2") shouldEqual (null: java.lang.Long)
}

it should "convert java Map to RealMap" in {
type T = util.HashMap[String, java.lang.Double]
null.asInstanceOf[T].toRealMap shouldEqual RealMap(Map())
val j = new T()
j.toRealMap shouldEqual RealMap(Map())
j.put("test", java.lang.Double.valueOf(17.5))
j.put("test2", null)
j.toRealMap.v("test") shouldEqual 17.5
j.toRealMap.v("test2") shouldEqual (null: java.lang.Double)
}

it should "convert java Map to BinaryMap" in {
type T = util.HashMap[String, java.lang.Boolean]
null.asInstanceOf[T].toBinaryMap shouldEqual RealMap(Map())
val j = new T()
j.toBinaryMap shouldEqual RealMap(Map())
j.put("test1", java.lang.Boolean.TRUE)
j.put("test0", java.lang.Boolean.FALSE)
j.put("test2", null)
j.toBinaryMap.v("test1") shouldEqual true
j.toBinaryMap.v("test0") shouldEqual false
j.toBinaryMap.v("test2") shouldEqual (null: java.lang.Boolean)
}

}

0 comments on commit 1a8cc73

Please sign in to comment.