|
| 1 | +/* |
| 2 | + * Copyright 2021 ABSA Group Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package za.co.absa.atum.model.dto |
| 18 | + |
| 19 | +import io.circe.parser._ |
| 20 | +import io.circe.syntax._ |
| 21 | +import org.scalatest.funsuite.AnyFunSuiteLike |
| 22 | + |
| 23 | +class PartitioningSubmitV2DTOUnitTests extends AnyFunSuiteLike { |
| 24 | + |
| 25 | + private val partitioning: PartitioningDTO = Seq(PartitionDTO("key", "value")) |
| 26 | + |
| 27 | + test("Single-parent compatibility factory wraps the id in a Seq") { |
| 28 | + val withParent = PartitioningSubmitV2DTO(partitioning, Some(1L), "author") |
| 29 | + assert(withParent == PartitioningSubmitV2DTO(partitioning, Seq(1L), "author")) |
| 30 | + |
| 31 | + val withoutParent = PartitioningSubmitV2DTO(partitioning, None, "author") |
| 32 | + assert(withoutParent == PartitioningSubmitV2DTO(partitioning, Seq.empty, "author")) |
| 33 | + } |
| 34 | + |
| 35 | + test("Default copyMeasureDefinitions is false when not specified") { |
| 36 | + val dto = PartitioningSubmitV2DTO(partitioning, Seq(1L), "author") |
| 37 | + assert(!dto.copyMeasureDefinitions) |
| 38 | + } |
| 39 | + |
| 40 | + test("Decoder merges legacy parentPartitioningId with parentPartitioningIds and de-duplicates") { |
| 41 | + val json = |
| 42 | + """{ |
| 43 | + | "partitioning": [{"key": "key", "value": "value"}], |
| 44 | + | "parentPartitioningId": 1, |
| 45 | + | "parentPartitioningIds": [1, 2], |
| 46 | + | "author": "author", |
| 47 | + | "copyMeasureDefinitions": true |
| 48 | + |}""".stripMargin |
| 49 | + |
| 50 | + val decoded = decode[PartitioningSubmitV2DTO](json) |
| 51 | + assert(decoded == Right(PartitioningSubmitV2DTO(partitioning, Seq(1L, 2L), "author", copyMeasureDefinitions = true))) |
| 52 | + } |
| 53 | + |
| 54 | + test("Decoder defaults copyMeasureDefinitions to false and parents to empty when absent") { |
| 55 | + val json = |
| 56 | + """{ |
| 57 | + | "partitioning": [{"key": "key", "value": "value"}], |
| 58 | + | "author": "author" |
| 59 | + |}""".stripMargin |
| 60 | + |
| 61 | + val decoded = decode[PartitioningSubmitV2DTO](json) |
| 62 | + assert(decoded == Right(PartitioningSubmitV2DTO(partitioning, Seq.empty, "author", copyMeasureDefinitions = false))) |
| 63 | + } |
| 64 | + |
| 65 | + test("Encoder round-trips a decoded DTO") { |
| 66 | + val dto = PartitioningSubmitV2DTO(partitioning, Seq(1L, 2L), "author", copyMeasureDefinitions = true) |
| 67 | + val encoded = dto.asJson |
| 68 | + assert(decode[PartitioningSubmitV2DTO](encoded.noSpaces) == Right(dto)) |
| 69 | + assert(encoded.hcursor.downField("parentPartitioningIds").as[Seq[Long]] == Right(Seq(1L, 2L))) |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments