Skip to content

Commit

Permalink
* several UTs added
Browse files Browse the repository at this point in the history
  • Loading branch information
benedeki committed Nov 22, 2024
1 parent 432716a commit 11b0a16
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 36 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Dependencies.*
import Dependencies.Versions.spark3
import VersionAxes.*

ThisBuild / scalaVersion := Setup.scala213.asString

ThisBuild / versionScheme := Some("early-semver")

Expand Down
3 changes: 1 addition & 2 deletions model/src/main/scala/za/co/absa/atum/model/types/basic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package za.co.absa.atum.model.types

import scala.collection.immutable.ListMap
import za.co.absa.atum.model.dto.{PartitionDTO, PartitioningDTO}
import za.co.absa.atum.model.utils.JsonSyntaxExtensions.JsonSerializationSyntax

import scala.collection.immutable.ListMap

object basic {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package za.co.absa.atum.model.utils

import io.circe.parser.decode
import io.circe.syntax._
import io.circe.{Decoder, Encoder, parser}
import io.circe.{Decoder, Encoder}

import java.util.Base64

Expand Down Expand Up @@ -49,7 +49,7 @@ object JsonSyntaxExtensions {
def fromBase64As[T: Decoder]: Either[io.circe.Error, T] = {
val decodedBytes = Base64.getDecoder.decode(jsonStr)
val decodedString = new String(decodedBytes, "UTF-8")
parser.decode[T](decodedString)
decode[T](decodedString)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
* limitations under the License.
*/

package za.co.absa.atum.model.utils
package za.co.absa.atum.model.dto

import org.scalatest.flatspec.AnyFlatSpecLike
import za.co.absa.atum.model.ResultValueType
import za.co.absa.atum.model.dto.MeasureResultDTO.TypedValue
import za.co.absa.atum.model.dto._
import za.co.absa.atum.model.utils.JsonSyntaxExtensions._
import za.co.absa.atum.model.testing.implicits.StringImplicits.StringLinearization
import za.co.absa.atum.model.utils.JsonSyntaxExtensions._

import java.time.{ZoneId, ZoneOffset, ZonedDateTime}
import java.util.UUID
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2024 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.atum.model.types

import org.scalatest.funsuite.AnyFunSuiteLike
import za.co.absa.atum.model.dto.PartitionDTO
import za.co.absa.atum.model.types.basic.AtumPartitions

import scala.collection.immutable.ListMap



class AtumPartitionsUnitTests extends AnyFunSuiteLike {
test("Creating AtumPartitions from one pair of key-value") {
val expected = ListMap("a" -> "b")
val result = AtumPartitions(("a", "b"))
assert(result == expected)
}

test("Creating AtumPartitions from multiple key-value pairs") {
val expected = ListMap(
"a" -> "b",
"e" -> "Hello",
"c" -> "d"
)
val result = AtumPartitions(List(
("a", "b"),
("e", "Hello"),
("c", "d")
))
assert(result == expected)
assert(result.head == ("a", "b"))
}

test("Conversion to PartitioningDTO returns expected result") {
import za.co.absa.atum.model.types.basic.AtumPartitionsOps

val atumPartitions = AtumPartitions(List(
("a", "b"),
("e", "Hello"),
("c", "d")
))

val expected = Seq(
PartitionDTO("a", "b"),
PartitionDTO("e", "Hello"),
PartitionDTO("c", "d")
)

assert(atumPartitions.toPartitioningDTO == expected)
}

test("Creating AtumPartitions from PartitioningDTO") {
import za.co.absa.atum.model.types.basic.PartitioningDTOOps

val partitionDTO = Seq(
PartitionDTO("a", "b"),
PartitionDTO("e", "Hello"),
PartitionDTO("c", "d")
)

val expected = AtumPartitions(List(
("a", "b"),
("e", "Hello"),
("c", "d")
))

assert(partitionDTO.toAtumPartitions == expected)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2024 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.atum.model.utils

import org.scalatest.funsuite.AnyFunSuiteLike
import za.co.absa.atum.model.dto.{FlowDTO, PartitionDTO}
import za.co.absa.atum.model.utils.JsonSyntaxExtensions.JsonDeserializationSyntax

class JsonDeserializationSyntaxUnitTests extends AnyFunSuiteLike {
test("Decode object from Json with defined Option field") {
val source =
"""{
| "id": 1,
| "name": "Test flow",
| "description": "Having description",
| "fromPattern": false
|}""".stripMargin
val result = source.as[FlowDTO]
val expected = FlowDTO(
id = 1,
name = "Test flow",
description = Some("Having description"),
fromPattern = false
)
assert(result == expected)
}

test("Decode object from Json with Option field undefined") {
val source =
"""{
| "id": 1,
| "name": "Test flow",
| "fromPattern": true
|}""".stripMargin
val result = source.as[FlowDTO]
val expected = FlowDTO(
id = 1,
name = "Test flow",
description = None,
fromPattern = true
)
assert(result == expected)
}

test("Fail when input is not Json") {
val source = "This is not a Json!"
intercept[io.circe.Error] {
source.as[FlowDTO]
}
}

test("Fail when given wrong class") {
val source =
"""{
| "id": 1,
| "name": "Test flow",
| "description": "Having description",
| "fromPattern": false
|}""".stripMargin
intercept[io.circe.Error] {
source.as[PartitionDTO]
}
}


test("Decode object from Base64 string") {
val source = "eyJpZCI6MSwibmFtZSI6IlRlc3QgZmxvdyIsImRlc2NyaXB0aW9uIjpudWxsLCJmcm9tUGF0dGVybiI6ZmFsc2V9"
val result = source.fromBase64As[FlowDTO]
val expected = FlowDTO(
id = 1,
name = "Test flow",
description = None,
fromPattern = false
)
assert(result == Right(expected))
}

test("Failing decode if not Base64 string") {
val source = ""
val result = source.fromBase64As[FlowDTO]
assert(result.isLeft)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.atum.model.utils

import org.scalatest.funsuite.AnyFunSuiteLike
import za.co.absa.atum.model.dto.FlowDTO
import za.co.absa.atum.model.utils.JsonSyntaxExtensions.JsonSerializationSyntax

class JsonSerializationSyntaxUnitTests extends AnyFunSuiteLike {
test("Converting to Json with option field defined") {
val expected = """{"id":1,"name":"Test flow","description":"Having description","fromPattern":false}"""
val result = FlowDTO(
id = 1,
name = "Test flow",
description = Some("Having description"),
fromPattern = false
).asJsonString
assert(result == expected)
}

test("Converting to Json with option field undefined") {
val expected = """{"id":1,"name":"Test flow","description":null,"fromPattern":true}"""
val result = FlowDTO(
id = 1,
name = "Test flow",
description = None,
fromPattern = true
).asJsonString
assert(result == expected)
}

test("Converting to Base64") {
val expected = "eyJpZCI6MSwibmFtZSI6IlRlc3QgZmxvdyIsImRlc2NyaXB0aW9uIjpudWxsLCJmcm9tUGF0dGVybiI6ZmFsc2V9"
val result = FlowDTO(
id = 1,
name = "Test flow",
description = None,
fromPattern = false
).asBase64EncodedJsonString
assert(result == expected)
}

}
1 change: 0 additions & 1 deletion project/JacocoSetup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ object JacocoSetup {
"za.co.absa.atum.server.Constants*",
"za.co.absa.atum.server.api.database.DoobieImplicits*",
"za.co.absa.atum.server.api.database.TransactorProvider*",
//TDO "za.co.absa.atum.model.dto.*",
"za.co.absa.atum.model.envelopes.Pagination",
"za.co.absa.atum.model.envelopes.ResponseEnvelope",
"za.co.absa.atum.model.envelopes.StatusResponse",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2024 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.atum.reader.basic

import io.circe.Decoder
Expand All @@ -13,31 +29,31 @@ import za.co.absa.atum.reader.server.ServerConfig
import zio.test.{Spec, TestEnvironment, ZIOSpecDefault, assertTrue}
import zio.{Scope, Task}

object Reader_ZIOUnitTests extends ZIOSpecDefault {
private implicit val serverConfig: ServerConfig = ServerConfig("http://localhost:8080")

private class ReaderForTest[F[_]](implicit serverConfig: ServerConfig, backend: SttpBackend[F, Any], ev: MonadError[F])
extends Reader {
override def getQuery[R: Decoder](endpointUri: String, params: Map[String, String]): F[RequestResult[R]] = super.getQuery(endpointUri, params)
}

override def spec: Spec[TestEnvironment with Scope, Any] = {
suite("Reader_ZIO")(
test("Using ZIO based backend") {
import za.co.absa.atum.reader.implicits.zio.ZIOMonad

val partitionDTO = PartitionDTO("someKey", "someValue")

implicit val server: SttpBackendStub[Task, WebSockets] = SttpBackendStub[Task, WebSockets](new RIOMonadAsyncError[Any])
.whenAnyRequest.thenRespond(partitionDTO.asJsonString)

val reader = new ReaderForTest
val expected: RequestResult[PartitionDTO] = Right(partitionDTO)
for {
result <- reader.getQuery[PartitionDTO]("test/", Map.empty)
} yield assertTrue(result == expected)
}
)
}

}
//object Reader_ZIOUnitTests extends ZIOSpecDefault {
// private implicit val serverConfig: ServerConfig = ServerConfig("http://localhost:8080")
//
// private class ReaderForTest[F[_]](implicit serverConfig: ServerConfig, backend: SttpBackend[F, Any], ev: MonadError[F])
// extends Reader {
// override def getQuery[R: Decoder](endpointUri: String, params: Map[String, String]): F[RequestResult[R]] = super.getQuery(endpointUri, params)
// }
//
// override def spec: Spec[TestEnvironment with Scope, Any] = {
// suite("Reader_ZIO")(
// test("Using ZIO based backend") {
// import za.co.absa.atum.reader.implicits.zio.ZIOMonad
//
// val partitionDTO = PartitionDTO("someKey", "someValue")
//
// implicit val server: SttpBackendStub[Task, WebSockets] = SttpBackendStub[Task, WebSockets](new RIOMonadAsyncError[Any])
// .whenAnyRequest.thenRespond(partitionDTO.asJsonString)
//
// val reader = new ReaderForTest
// val expected: RequestResult[PartitionDTO] = Right(partitionDTO)
// for {
// result <- reader.getQuery[PartitionDTO]("test/", Map.empty)
// } yield assertTrue(result == expected)
// }
// )
// }
//
//}

0 comments on commit 11b0a16

Please sign in to comment.