Skip to content

Commit

Permalink
#13 Fix enums in predefined classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ABMC831 committed Sep 17, 2024
1 parent c7e29cd commit 18dda47
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 7 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ lazy val models = project
.settings(
commonSettings ++ Seq(
name := "KafkaCase-Models",
libraryDependencies ++= modelsDependencies
libraryDependencies ++= modelsDependencies,
scalacOptions ++= Seq("-Ymacro-annotations"),
)
)
.enablePlugins(AutomateHeaderPlugin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import za.co.absa.KafkaCase.Reader.ReaderImpl
import za.co.absa.KafkaCase.Writer.WriterImpl

import java.util.{Properties, UUID}
import io.circe.generic.auto._

object KafkaCase {
private def writer_use_case(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package za.co.absa.KafkaCase.Models

import io.circe.{Decoder, Encoder}
import io.circe.generic.JsonCodec

@JsonCodec
case class EdlaChangeTopic(
id: String,
app_id_snow: String,
Expand All @@ -31,9 +35,22 @@ case class EdlaChangeTopic(

object EdlaChangeTopic {
sealed trait Operation

object Operation {
case class CREATE() extends Operation
case class UPDATE() extends Operation
case class ARCHIVE() extends Operation

implicit val operationEncoder: Encoder[Operation] = Encoder.encodeString.contramap[Operation] {
case CREATE() => s"CREATE"
case UPDATE() => s"UPDATE"
case ARCHIVE() => s"ARCHIVE"
}

implicit val operationDecoder: Decoder[Operation] = Decoder.decodeString.emap {
case s"CREATE" => Right(CREATE())
case s"UPDATE" => Right(UPDATE())
case s"ARCHIVE" => Right(ARCHIVE())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package za.co.absa.KafkaCase.Models

import io.circe.{Decoder, Encoder}
import io.circe.generic.JsonCodec

@JsonCodec
case class SchemaRunTopic(
id: String,
job_ref: String,
Expand All @@ -30,9 +34,23 @@ case class SchemaRunTopic(
)

object SchemaRunTopic {
sealed trait Status {
sealed trait Status

object Status {
case class Finished() extends Status
case class Failed() extends Status
case class Killed() extends Status

implicit val operationEncoder: Encoder[Status] = Encoder.encodeString.contramap[Status] {
case Finished() => s"Finished"
case Failed() => s"Failed"
case Killed() => s"Killed"
}

implicit val operationDecoder: Decoder[Status] = Decoder.decodeString.emap {
case s"Finished" => Right(Finished())
case s"Failed" => Right(Failed())
case s"Killed" => Right(Killed())
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.KafkaCase.models

import io.circe.jawn.decode
import io.circe.syntax.EncoderOps
import org.scalatest.funsuite.AnyFunSuite
import za.co.absa.KafkaCase.Models.EdlaChangeTopic

class EdlaChangeTopicUnitTests extends AnyFunSuite {
private val instance = EdlaChangeTopic(
id = "DebugId",
app_id_snow = "N/A",
source_app = "ThisCode",
environment = "DEV",
timestamp_event = 12345,
data_definition = "TestingThis",
operation = EdlaChangeTopic.Operation.CREATE(),
location = "ether",
format = "FooBar",
schema_link = "http://not.here"
)

private val json =
"""{
| "id" : "DebugId",
| "app_id_snow" : "N/A",
| "source_app" : "ThisCode",
| "environment" : "DEV",
| "timestamp_event" : 12345,
| "data_definition" : "TestingThis",
| "operation" : "CREATE",
| "location" : "ether",
| "format" : "FooBar",
| "schema_link" : "http://not.here"
|}""".stripMargin

test("Serializes to JSON properly") {
assertResult(json)(instance.asJson.toString())
}

test("Deserializes from JSON properly") {
assertResult(instance)(decode[EdlaChangeTopic](json).getOrElse(throw new Exception("Failed to parse JSON")))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.KafkaCase.models

import io.circe.jawn.decode
import io.circe.syntax.EncoderOps
import org.scalatest.funsuite.AnyFunSuite
import za.co.absa.KafkaCase.Models.SchemaRunTopic

class SchemaRunTopicUnitTests extends AnyFunSuite {
private val instance = SchemaRunTopic(
id = "DebugId",
job_ref = "UnitTestJob",
app_id_snow = "N/A",
source_app = "ThisTest",
environment = "TEST",
timestamp_start = 12345,
timestamp_end = 67890,
data_definition_id = "Foo",
status = SchemaRunTopic.Status.Killed(),
message = "FooBar"
)

private val json =
"""{
| "id" : "DebugId",
| "job_ref" : "UnitTestJob",
| "app_id_snow" : "N/A",
| "source_app" : "ThisTest",
| "environment" : "TEST",
| "timestamp_start" : 12345,
| "timestamp_end" : 67890,
| "data_definition_id" : "Foo",
| "status" : "Killed",
| "message" : "FooBar"
|}""".stripMargin

test("Serializes to JSON properly") {
assertResult(json)(instance.asJson.toString())
}

test("Deserializes from JSON properly") {
assertResult(instance)(decode[SchemaRunTopic](json).getOrElse(throw new Exception("Failed to parse JSON")))
}
}
10 changes: 6 additions & 4 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ object Dependencies {
"ch.qos.logback" % "logback-classic" % "1.5.6",
)

def modelsDependencies: Seq[ModuleID] = Seq.empty[ModuleID]
def modelsDependencies: Seq[ModuleID] = Seq(
"io.circe" %% "circe-generic" % "0.14.9",
"io.circe" %% "circe-parser" % "0.14.9" % Test,
"org.scalatest" %% "scalatest" % "3.2.19" % Test
)

def readerDependencies: Seq[ModuleID] = Seq(
"io.circe" %% "circe-parser" % "0.14.9",
Expand All @@ -17,7 +21,5 @@ object Dependencies {
"org.apache.kafka" % "kafka-clients" % "3.8.0"
)

def examplesDependencies: Seq[ModuleID] = Seq(
"io.circe" %% "circe-generic" % "0.14.9",
)
def examplesDependencies: Seq[ModuleID] = Seq.empty[ModuleID]
}

0 comments on commit 18dda47

Please sign in to comment.