Skip to content

Commit

Permalink
Added logics for publishing a pact with tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
markozz committed Sep 2, 2020
1 parent 1fa7ad7 commit 16f1bb1
Showing 1 changed file with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ import au.com.dius.pact.core.model.{OptionalBody, Request, Response}
import com.typesafe.scalalogging.StrictLogging

import scala.collection.JavaConverters._
import scala.io.Source
import java.io.{File, IOException}

import au.com.dius.pact.consumer.PactVerificationResult.Ok
import au.com.dius.pact.core.pactbroker.{PactBrokerClient, RequestFailedException}

object Publish extends StrictLogging {

def apply(request: Request, oldState: ServerState, config: Config): Result = {
def jsonBody = JsonUtils.parseJsonString(request.getBody.valueAsString())
def consumer: Option[String] = getVarFromJson("consumer", jsonBody)
def consumerVersion: Option[String] = getVarFromJson("consumerVersion", jsonBody)
def provider: Option[String] = getVarFromJson("provider", jsonBody)
def broker: Option[String] = getBrokerUrlFromConfig(config)
def authToken: Option[String] = getVarFromConfig(config.authToken)
val jsonBody = JsonUtils.parseJsonString(request.getBody.valueAsString())
val consumer: Option[String] = getVarFromJson("consumer", jsonBody)
val consumerVersion: Option[String] = getVarFromJson("consumerVersion", jsonBody)
val provider: Option[String] = getVarFromJson("provider", jsonBody)
val tags: Option[::[String]] = getListFromJson("tags", jsonBody)
val broker: Option[String] = getBrokerUrlFromConfig(config)
val authToken: Option[String] = getVarFromConfig(config.authToken)

var response = new Response(500, ResponseUtils.CrossSiteHeaders.asJava)
if (broker.isDefined) {
if (consumer.isDefined && consumerVersion.isDefined && provider.isDefined) {
response = publishPact(consumer.get, consumerVersion.get, provider.get, broker.get, authToken)
response = publishPact(consumer.get, consumerVersion.get, provider.get, broker.get, authToken, tags)
} else {
def errorJson: String = "{\"error\": \"body should contain consumer, consumerVersion and provider.\"}"
def body: OptionalBody = OptionalBody.body(errorJson.getBytes())
Expand All @@ -36,24 +37,22 @@ object Publish extends StrictLogging {
Result(response, oldState)
}

private def publishPact(consumer: String, consumerVersion: String, provider: String, broker: String, authToken: Option[String]) = {
val fileName: String = s"${consumer}-${provider}.json"
private def publishPact(consumer: String, consumerVersion: String, provider: String, broker: String, authToken: Option[String], tags: Option[::[String]]) = {
val fileName: String = s"$consumer-$provider.json"
val pact = new File(s"${System.getProperty("pact.rootDir", "target/pacts")}/$fileName")

logger.debug("Publishing pact with following details: ")
logger.debug("Consumer: " + consumer)
logger.debug("ConsumerVersion: " + consumerVersion)
logger.debug("Provider: " + provider)
logger.debug("Pact Broker: " + broker)
logger.debug("Tags: " + tags.getOrElse(None))

try {
var options: Map[String, _]= Map()
if(authToken.isDefined) {
options = Map("authentication" -> List("bearer",authToken.get).asJava)
}
val options = getOptions(authToken)
val brokerClient: PactBrokerClient = new PactBrokerClient(broker, options.asJava)
val res = brokerClient.uploadPactFile(pact, consumerVersion)
if(res.component2() == null) {
val res = brokerClient.uploadPactFile(pact, consumerVersion, tags.getOrElse(List()).asJava)
if( res.component2() == null) {
logger.debug("Pact succesfully shared. deleting file..")
removePact(pact)
new Response(200, ResponseUtils.CrossSiteHeaders.asJava, OptionalBody.body(res.component1().getBytes()))
Expand All @@ -68,6 +67,16 @@ object Publish extends StrictLogging {
}
}

private def getOptions(authToken: Option[String]): Map[String, _] = {
var options: Map[String, _]= Map()
if(authToken.isDefined) {
options = Map("authentication" -> List("bearer",authToken.get).asJava)
}
options
}



private def removePact(file: File): Unit = {
if (file.exists()) {
file.delete()
Expand All @@ -92,4 +101,12 @@ object Publish extends StrictLogging {
case _ => None
}

private def getListFromJson(variable: String, json: Any): Option[::[String]] = json match {
case map: Map[AnyRef, AnyRef] => {
if (map.contains(variable)) Some(map(variable).asInstanceOf[::[String]])
else None
}
case _ => None
}

}

0 comments on commit 16f1bb1

Please sign in to comment.