|
| 1 | +package com.fasterxml.jackson.module.scala.deser |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonMerge |
| 4 | +import com.fasterxml.jackson.module.scala.DefaultScalaModule |
| 5 | +import org.junit.runner.RunWith |
| 6 | +import org.scalatestplus.junit.JUnitRunner |
| 7 | + |
| 8 | +import scala.collection.{Map, mutable} |
| 9 | + |
| 10 | +case class ClassWithLists(field1: List[String], @JsonMerge field2: List[String]) |
| 11 | +case class ClassWithMaps[T](field1: Map[String, T], @JsonMerge field2: Map[String, T]) |
| 12 | +case class ClassWithMutableMaps[T](field1: mutable.Map[String, T], @JsonMerge field2: mutable.Map[String, T]) |
| 13 | + |
| 14 | +case class Pair(first: String, second: String) |
| 15 | + |
| 16 | +@RunWith(classOf[JUnitRunner]) |
| 17 | +class MergeTest extends DeserializerTest { |
| 18 | + |
| 19 | + val module: DefaultScalaModule.type = DefaultScalaModule |
| 20 | + |
| 21 | + behavior of "The DefaultScalaModule when reading for updating" |
| 22 | + |
| 23 | + it should "merge both lists" in { |
| 24 | + val initial = deserialize[ClassWithLists](classJson(firstListJson)) |
| 25 | + val result = newBuilder.defaultMergeable(true).build() |
| 26 | + .readerForUpdating(initial).readValue[ClassWithLists](classJson(secondListJson)) |
| 27 | + |
| 28 | + result shouldBe ClassWithLists(mergedList, mergedList) |
| 29 | + } |
| 30 | + |
| 31 | + it should "merge only the annotated list" in { |
| 32 | + val initial = deserialize[ClassWithLists](classJson(firstListJson)) |
| 33 | + val result = newMapper |
| 34 | + .readerForUpdating(initial).readValue[ClassWithLists](classJson(secondListJson)) |
| 35 | + |
| 36 | + result shouldBe ClassWithLists(secondList, mergedList) |
| 37 | + } |
| 38 | + |
| 39 | + it should "merge both string maps" in { |
| 40 | + val initial = deserialize[ClassWithMaps[String]](classJson(firstStringMapJson)) |
| 41 | + val result = newBuilder.defaultMergeable(true).build() |
| 42 | + .readerForUpdating(initial).readValue[ClassWithMaps[String]](classJson(secondStringMapJson)) |
| 43 | + |
| 44 | + result shouldBe ClassWithMaps(mergedStringMap, mergedStringMap) |
| 45 | + } |
| 46 | + |
| 47 | + it should "merge only the annotated string map" in { |
| 48 | + val initial = deserialize[ClassWithMaps[String]](classJson(firstStringMapJson)) |
| 49 | + val result = newBuilder.defaultMergeable(true).build() |
| 50 | + .readerForUpdating(initial).readValue[ClassWithMaps[String]](classJson(secondStringMapJson)) |
| 51 | + |
| 52 | + result shouldBe ClassWithMaps(secondStringMap, mergedStringMap) |
| 53 | + } |
| 54 | + |
| 55 | + it should "merge both pair maps" in { |
| 56 | + val initial = deserialize[ClassWithMaps[Pair]](classJson(firstPairMapJson)) |
| 57 | + val result = newBuilder.defaultMergeable(true).build() |
| 58 | + .readerForUpdating(initial).forType(typeReference[ClassWithMaps[Pair]]).readValue[ClassWithMaps[Pair]](classJson(secondPairMapJson)) |
| 59 | + |
| 60 | + result shouldBe ClassWithMaps(mergedPairMap, mergedPairMap) |
| 61 | + } |
| 62 | + |
| 63 | + it should "merge only the annotated pair map" in { |
| 64 | + val initial = deserialize[ClassWithMaps[Pair]](classJson(firstPairMapJson)) |
| 65 | + val result = newMapper |
| 66 | + .readerForUpdating(initial).forType(typeReference[ClassWithMaps[Pair]]).readValue[ClassWithMaps[Pair]](classJson(secondPairMapJson)) |
| 67 | + |
| 68 | + result shouldBe ClassWithMaps(secondPairMap, mergedPairMap) |
| 69 | + } |
| 70 | + |
| 71 | + it should "merge both mutable maps" in { |
| 72 | + val initial = deserialize[ClassWithMutableMaps[String]](classJson(firstStringMapJson)) |
| 73 | + val result = newBuilder.defaultMergeable(true).build() |
| 74 | + .readerForUpdating(initial).readValue[ClassWithMutableMaps[String]](classJson(secondStringMapJson)) |
| 75 | + |
| 76 | + result shouldBe ClassWithMutableMaps(mutable.Map() ++ mergedStringMap, mutable.Map() ++ mergedStringMap) |
| 77 | + } |
| 78 | + |
| 79 | + it should "merge only the annotated mutable map" in { |
| 80 | + val initial = deserialize[ClassWithMutableMaps[String]](classJson(firstStringMapJson)) |
| 81 | + val result = newMapper |
| 82 | + .readerForUpdating(initial).readValue[ClassWithMutableMaps[String]](classJson(secondStringMapJson)) |
| 83 | + |
| 84 | + result shouldBe ClassWithMutableMaps(mutable.Map() ++ secondStringMap, mutable.Map() ++ mergedStringMap) |
| 85 | + } |
| 86 | + |
| 87 | + def classJson(nestedJson: String) = s"""{"field1":$nestedJson,"field2":$nestedJson}""" |
| 88 | + |
| 89 | + val firstListJson = """["one","two"]""" |
| 90 | + val secondListJson = """["three"]""" |
| 91 | + val secondList = List("three") |
| 92 | + val mergedList = List("one", "two", "three") |
| 93 | + |
| 94 | + val firstStringMapJson = """{"one":"1","two":"2"}""" |
| 95 | + val secondStringMapJson = """{"two":"22","three":"33"}""" |
| 96 | + val secondStringMap = Map("two" -> "22", "three" -> "33") |
| 97 | + val mergedStringMap = Map("one" -> "1", "two" -> "22", "three" -> "33") |
| 98 | + |
| 99 | + val firstPairMapJson = """{"one":{"first":"1"},"two":{"second":"2"},"three":{"first":"3","second":"4"}}""" |
| 100 | + val secondPairMapJson = """{"two":{"first":"22"},"three":{"second":"33"}}""" |
| 101 | + val secondPairMap = Map("two" -> Pair("22", null), "three" -> Pair(null, "33")) |
| 102 | + val mergedPairMap = Map("one" -> Pair("1", null), "two" -> Pair("22", "2"), "three" -> Pair("3", "33")) |
| 103 | +} |
0 commit comments