Skip to content

Commit

Permalink
Fix provider tests content type matching (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
austek authored Mar 22, 2023
1 parent 5ba11f0 commit ae0f72b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 44 deletions.
10 changes: 4 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ lazy val plugin = project
.in(file("modules/plugin"))
.enablePlugins(
AkkaGrpcPlugin,
DockerPlugin,
GitHubPagesPlugin,
GitVersioning,
JavaAppPackaging
Expand All @@ -20,15 +19,14 @@ lazy val plugin = project
maintainer := "[email protected]",
basicSettings,
publishSettings,
executableScriptName := "pact-avro-plugin",
gitHubPagesOrgName := "austek",
gitHubPagesRepoName := "pact-avro-plugin",
gitHubPagesSiteDir := (`pact-avro-plugin` / baseDirectory).value / "build" / "site",
gitHubPagesAcceptedTextExtensions := Set(".css", ".html", ".js", ".svg", ".txt", ".woff", ".woff2", ".xml"),
libraryDependencies ++=
Dependencies.compile(apacheAvro, auPacMatchers, logback, pactCore, scalaLogging).map(withExclusions) ++
Dependencies.compile(apacheAvro, auPactMatchers, logback, pactCore, scalaLogging).map(withExclusions) ++
Dependencies.test(scalaTest).map(withExclusions),
dependencyOverrides += Dependencies.grpcStub
dependencyOverrides ++= Seq(grpcStub)
)
lazy val pluginRef = LocalProject("plugin")

Expand All @@ -40,7 +38,7 @@ lazy val provider = project
Test / envVars := Map("PACT_PLUGIN_DIR" -> (pluginRef / Universal / stagingDirectory).value.absolutePath),
libraryDependencies ++=
Dependencies.compile(avroCompiler, logback, pulsar4sCore, pulsar4sAvro, pureConfig, scalacheck).map(withExclusions) ++
Dependencies.test(assertJCore, jUnitInterface, pactProviderJunit).map(withExclusions),
Dependencies.test(assertJCore, jUnitInterface, pactProviderJunit, pactCore).map(withExclusions),
publish / skip := false
)

Expand All @@ -53,7 +51,7 @@ lazy val consumer = project
Test / envVars := Map("PACT_PLUGIN_DIR" -> (pluginRef / Universal / stagingDirectory).value.absolutePath),
libraryDependencies ++=
Dependencies.compile(avroCompiler, logback, pulsar4sCore, pulsar4sAvro, pureConfig, scalaLogging).map(withExclusions) ++
Dependencies.test(assertJCore, jUnitInterface, pactConsumerJunit).map(withExclusions),
Dependencies.test(assertJCore, jUnitInterface, pactConsumerJunit, pactCore).map(withExclusions),
dependencyOverrides += Dependencies.pactCore,
publish / skip := false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ import org.apache.avro.Schema.Type.{RECORD, UNION}
import org.apache.avro.generic.GenericRecord

import scala.jdk.CollectionConverters._
import scala.util.matching.Regex

object CompareContentsResponseBuilder extends StrictLogging {

private val ContentTypeRegex: Regex = raw"(\w+/\*?\+?\w+);\s*record=(\w+)".r

def build(request: CompareContentsRequest, avroSchema: Schema): Either[PluginError[_], CompareContentsResponse] = {
for {
actualBody <- getBody(request.actual, "Actual body required")
expectedBody <- getBody(request.expected, "Expected body required")
_ <- contentTypesMatch(actualBody, expectedBody)
_ <- correctContentType(actualBody, "Actual")
_ <- correctContentType(expectedBody, "Expected")
schema <- recordSchema(avroSchema, "Order")
recordName <- extractRecordName(actualBody, expectedBody)
schema <- recordSchema(avroSchema, recordName)
actual <- AvroUtils.deserialize(schema, actualBody.getContent.toByteArray)
expected <- AvroUtils.deserialize(schema, expectedBody.getContent.toByteArray)
response <- buildResponse(request, actual, expected)
Expand Down Expand Up @@ -99,23 +100,30 @@ object CompareContentsResponseBuilder extends StrictLogging {
)
}

private def contentTypesMatch(actual: Body, expected: Body): Either[PluginError[_], Unit] = {
Either.cond(
actual.contentType == expected.contentType,
(),
PluginErrorMessage("Content types don't match")
)
}
private def extractRecordName(actual: Body, expected: Body): Either[PluginError[_], String] = {
def extract(body: Body, name: String): Either[PluginError[_], String] = {
body.contentType match {
case ContentTypeRegex(contentType, recordName) =>
Either.cond(
ContentTypes.contains(contentType),
recordName,
PluginErrorMessage(
s"$name body is not one of '$ContentTypesStr' content type"
)
)
case _ => Left(PluginErrorMessage(s"$name body content type didn't match expected template of 'content/type; record=NameOfRecord'"))
}
}

private def correctContentType(body: Body, name: String): Either[PluginError[_], Unit] = {
Either.cond(
// ContentTypes.contains(body.contentType), //TODO Uncomment after bug is fixed
body.getContent != null,
(),
PluginErrorMessage(
s"$name body is not one of '$ContentTypesStr' content type"
)
)
extract(actual, "Actual").flatMap { actualRecordName =>
extract(expected, "Expected").flatMap { expectedRecordName =>
if (actualRecordName == expectedRecordName) {
Right(expectedRecordName)
} else {
Left(PluginErrorMessage(s"Record names don't match, actual: '$actualRecordName' expected: '$expectedRecordName'"))
}
}
}
}

private def getBody(body: Option[Body], msg: String): Either[PluginError[_], Body] = {
Expand Down
32 changes: 15 additions & 17 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sbt._
import sbt.librarymanagement.syntax.ExclusionRule

object Dependencies extends DependencyUtils {

Expand All @@ -10,28 +11,25 @@ object Dependencies extends DependencyUtils {
val scalaTest = "3.2.15"
}

val auPacMatchers: ModuleID = "au.com.dius.pact.core" % "matchers" % "4.5.2"
val auPactMatchers: ModuleID = "au.com.dius.pact.core" % "matchers" % Versions.pact excludeAll ExclusionRule("io.pact.plugin.driver")
val grpcStub: ModuleID = "io.grpc" % "grpc-stub" % "1.53.0"
val logback: ModuleID = "ch.qos.logback" % "logback-classic" % "1.4.5"
val pactCore: ModuleID = "io.pact.plugin.driver" % "core" % "0.3.1"
val logback: ModuleID = "ch.qos.logback" % "logback-classic" % "1.4.6"
val pactCore: ModuleID = "io.pact.plugin.driver" % "core" % "0.3.2" excludeAll ExclusionRule("au.com.dius.pact.core")
val scalaLogging: ModuleID = "com.typesafe.scala-logging" %% "scala-logging" % "3.9.5"
val pureConfig: ModuleID = "com.github.pureconfig" %% "pureconfig" % "0.17.2"

// Test dependencies
val apacheAvro: ModuleID = "org.apache.avro" % "avro" % Versions.avro
val assertJCore: ModuleID = "org.assertj" % "assertj-core" % "3.24.2"
val avroCompiler: ModuleID = "org.apache.avro" % "avro-compiler" % Versions.avro
val hamcrest: ModuleID = "org.hamcrest" % "hamcrest" % "2.2"
val jUnitInterface: ModuleID = "net.aichler" % "jupiter-interface" % "0.11.1"
val pact4sScalaTest: ModuleID = "io.github.jbwheatley" %% "pact4s-scalatest" % "0.9.0"
val pactConsumerJunit: ModuleID = "au.com.dius.pact.consumer" % "junit5" % Versions.pact
val pactProviderJunit: ModuleID = "au.com.dius.pact.provider" % "junit5" % Versions.pact
val pulsar4sAvro: ModuleID = "com.clever-cloud.pulsar4s" %% "pulsar4s-avro" % Versions.pulsar4sVersion
val pulsar4sCore: ModuleID = "com.clever-cloud.pulsar4s" %% "pulsar4s-core" % Versions.pulsar4sVersion
val scalacheck: ModuleID = "org.scalacheck" %% "scalacheck" % "1.17.0"
val scalaTest: ModuleID = "org.scalatest" %% "scalatest" % Versions.scalaTest
val testContainer: ModuleID = "com.dimafeng" %% "testcontainers-scala-scalatest" % "0.40.12"
val apacheAvro: ModuleID = "org.apache.avro" % "avro" % Versions.avro
val assertJCore: ModuleID = "org.assertj" % "assertj-core" % "3.24.2"
val avroCompiler: ModuleID = "org.apache.avro" % "avro-compiler" % Versions.avro
val jUnitInterface: ModuleID = "net.aichler" % "jupiter-interface" % "0.11.1"
val pactConsumerJunit: ModuleID = "au.com.dius.pact.consumer" % "junit5" % Versions.pact excludeAll ExclusionRule("io.pact.plugin.driver")
val pactProviderJunit: ModuleID = "au.com.dius.pact.provider" % "junit5" % Versions.pact excludeAll ExclusionRule("io.pact.plugin.driver")
val pulsar4sAvro: ModuleID = "com.clever-cloud.pulsar4s" %% "pulsar4s-avro" % Versions.pulsar4sVersion
val pulsar4sCore: ModuleID = "com.clever-cloud.pulsar4s" %% "pulsar4s-core" % Versions.pulsar4sVersion
val scalacheck: ModuleID = "org.scalacheck" %% "scalacheck" % "1.17.0"
val scalaTest: ModuleID = "org.scalatest" %% "scalatest" % Versions.scalaTest

// Dependency Conflict Resolution
val exclusions = Seq()
val exclusions: Seq[ExclusionRule] = Seq[ExclusionRule]()
}
1 change: 1 addition & 0 deletions project/PublishSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ object PublishSettings {

lazy val publishSettings: Seq[Def.Setting[_]] =
Seq(
executableScriptName := "pact-avro-plugin",
Compile / doc / sources := Seq.empty,
Compile / packageDoc / mappings := Seq.empty,
Universal / mappings ++= Seq(
Expand Down
2 changes: 1 addition & 1 deletion project/sbt-avro.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
addSbtPlugin("com.github.sbt" % "sbt-avro" % "3.4.2")
addSbtPlugin("com.github.sbt" % "sbt-avro" % "3.4.2")

libraryDependencies += "org.apache.avro" % "avro-compiler" % "1.11.1"

0 comments on commit ae0f72b

Please sign in to comment.