|
| 1 | +/* |
| 2 | + * Copyright 2018 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 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package za.co.absa.atum.core |
| 17 | + |
| 18 | +import org.scalatest.flatspec.AnyFlatSpec |
| 19 | +import org.scalatest.matchers.should.Matchers.convertToAnyShouldWrapper |
| 20 | +import za.co.absa.atum.model.{ControlMeasure, ControlMeasureMetadata} |
| 21 | +import za.co.absa.atum.persistence.ControlMeasuresLoader |
| 22 | + |
| 23 | +class AccumulatorSpec extends AnyFlatSpec { |
| 24 | + |
| 25 | + private val emptyControlMeasureMetadata = ControlMeasureMetadata( |
| 26 | + sourceApplication = "", |
| 27 | + country = "", |
| 28 | + historyType = "", |
| 29 | + dataFilename = "", |
| 30 | + sourceType = "", |
| 31 | + version = 0, |
| 32 | + informationDate = "", |
| 33 | + additionalInfo = Map.empty |
| 34 | + ) |
| 35 | + private val emptyControlMeasure: ControlMeasure = ControlMeasure( |
| 36 | + emptyControlMeasureMetadata, |
| 37 | + None, |
| 38 | + List.empty |
| 39 | + ) |
| 40 | + private def initAccumulator(controlMeasure: ControlMeasure = emptyControlMeasure): Accumulator = { |
| 41 | + val result = new Accumulator |
| 42 | + val loader: ControlMeasuresLoader = new ControlMeasuresLoader{ |
| 43 | + override def load(): ControlMeasure = controlMeasure |
| 44 | + override def getInfo: String = "" |
| 45 | + } |
| 46 | + result.loadControlMeasurements(loader) |
| 47 | + result |
| 48 | + } |
| 49 | + |
| 50 | + private def initAccumulator(controlMeasureMetadata: ControlMeasureMetadata): Accumulator = { |
| 51 | + initAccumulator(emptyControlMeasure.copy(metadata = controlMeasureMetadata)) |
| 52 | + } |
| 53 | + |
| 54 | + "setAdditionalInfo" should "add additional key" in { |
| 55 | + val expected = emptyControlMeasureMetadata.copy(additionalInfo = Map("Luke"->"Skywalker", "Han"->"Solo")) |
| 56 | + |
| 57 | + val accumulator = initAccumulator() |
| 58 | + accumulator.setAdditionalInfo(("Luke","Skywalker"), replaceIfExists = false) |
| 59 | + accumulator.setAdditionalInfo(("Han","Solo"), replaceIfExists = true) |
| 60 | + val actual = accumulator.getControlMeasure.metadata |
| 61 | + actual shouldBe expected |
| 62 | + } |
| 63 | + |
| 64 | + it should "overwrite a key with overwrite on" in { |
| 65 | + val initControlMeasureMetadata = emptyControlMeasureMetadata.copy(additionalInfo = Map("Leia"->"Organa", "Han"->"Solo")) |
| 66 | + val expected = emptyControlMeasureMetadata.copy(additionalInfo = Map("Leia"->"Organa Solo", "Han"->"Solo")) |
| 67 | + val accumulator = initAccumulator(initControlMeasureMetadata) |
| 68 | + accumulator.setAdditionalInfo(("Leia","Organa Solo"), replaceIfExists = true) |
| 69 | + val actual = accumulator.getControlMeasure.metadata |
| 70 | + actual shouldBe expected |
| 71 | + } |
| 72 | + |
| 73 | + it should "keep the old value if overwrite is off" in { |
| 74 | + val initControlMeasureMetadata = emptyControlMeasureMetadata.copy(additionalInfo = Map("Luke"->"Skywalker", "Han"->"Solo")) |
| 75 | + val accumulator = initAccumulator(initControlMeasureMetadata) |
| 76 | + accumulator.setAdditionalInfo(("Luke","Vader"), replaceIfExists = false) |
| 77 | + val actual = accumulator.getControlMeasure.metadata |
| 78 | + actual shouldBe initControlMeasureMetadata |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | +} |
0 commit comments