Skip to content

Commit e9f72fd

Browse files
benedekidk1844
andauthored
#139: Cannot add new additionalInfo in metadata (#140)
* fixes that cannot add new additionalInfo in metadata * GitHUb Action Spark increased to 3.2.2 Co-authored-by: Daniel Kavan <[email protected]>
1 parent 85f5213 commit e9f72fd

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

.github/workflows/build-sbt.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ jobs:
2828
fail-fast: false
2929
matrix:
3030
scala: [ 2.11.12, 2.12.15 ]
31-
spark: [ 2.4.8, 3.2.1 ]
31+
spark: [ 2.4.8, 3.2.2 ]
3232
exclude:
3333
- scala: 2.11.12
34-
spark: 3.2.1
34+
spark: 3.2.2
3535
- scala: 2.12.15
3636
spark: 2.4.8
3737
name: SBT Spark ${{matrix.spark}} on Scala ${{matrix.scala}}

atum/src/main/scala/za/co/absa/atum/core/Accumulator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Accumulator() {
8989

9090
/** Adds a key-value pair as an additional information stored in the metadata. */
9191
def setAdditionalInfo(kv: (String, String), replaceIfExists: Boolean): Unit = {
92-
controlMeasure.setAdditionalInfo(kv, replaceIfExists)
92+
controlMeasure = controlMeasure.setAdditionalInfo(kv, replaceIfExists)
9393
}
9494

9595
/** The method returns Control Info object as a Json string. */
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

model/src/main/scala/za/co/absa/atum/model/ControlMeasure.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ case class ControlMeasure
5252
*/
5353
def setAdditionalInfo(kv: (String, String), replaceIfExists: Boolean): ControlMeasure = {
5454
kv match {
55-
case (key, _) if replaceIfExists || !this.metadata.additionalInfo.contains(kv._1) =>
56-
val newInfo = this.metadata.additionalInfo + kv
57-
val newMetadata = this.metadata.copy(additionalInfo = newInfo)
58-
this.copy(metadata = newMetadata)
55+
case (key, _) if replaceIfExists || !this.metadata.additionalInfo.contains(key) =>
56+
val newInfo = metadata.additionalInfo + kv
57+
val newMetadata = metadata.copy(additionalInfo = newInfo)
58+
copy(metadata = newMetadata)
5959
case _ =>
6060
this
6161
}

0 commit comments

Comments
 (0)