Skip to content

Commit

Permalink
Changed logics for publishing pact to broker. Now using PactBrokerCli…
Browse files Browse the repository at this point in the history
…ent.
  • Loading branch information
markozz committed Aug 31, 2020
1 parent b6e81b7 commit 1fa7ad7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 57 deletions.
1 change: 0 additions & 1 deletion pact-jvm-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ dependencies {
compile("com.typesafe.scala-logging:scala-logging_2.12:3.7.2") {
exclude group: 'org.scala-lang'
}
compile "com.lihaoyi:requests_2.12:0.6.5"
compile "ws.unfiltered:unfiltered-netty-server_2.12:0.9.1"
implementation 'org.apache.commons:commons-io:1.3.2'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import scala.collection.JavaConverters._
import scala.io.Source
import java.io.{File, IOException}

import requests.{RequestAuth, RequestFailedException, headers}
import au.com.dius.pact.core.pactbroker.{PactBrokerClient, RequestFailedException}

object Publish extends StrictLogging {

Expand Down Expand Up @@ -37,7 +37,8 @@ object Publish extends StrictLogging {
}

private def publishPact(consumer: String, consumerVersion: String, provider: String, broker: String, authToken: Option[String]) = {
def fileName: String = s"${consumer}-${provider}.json"
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)
Expand All @@ -46,33 +47,28 @@ object Publish extends StrictLogging {
logger.debug("Pact Broker: " + broker)

try {
val content = readContract(fileName)
def url = s"${broker}/pacts/provider/${provider}/consumer/${consumer}/version/${consumerVersion}"
var auth: RequestAuth = RequestAuth.Empty
if (authToken.isDefined) {
auth = RequestAuth.Bearer(authToken.get)
var options: Map[String, _]= Map()
if(authToken.isDefined) {
options = Map("authentication" -> List("bearer",authToken.get).asJava)
}
def response = requests.put(
url,
auth,
headers = Map("Content-Type" -> "application/json", "Accept" -> "application/hal+json, application/json, */*; q=0.01"),
data = content
)
logger.debug("Statuscode from broker: " + response.statusCode)
if(response.statusCode > 199 && response.statusCode < 400) {
val brokerClient: PactBrokerClient = new PactBrokerClient(broker, options.asJava)
val res = brokerClient.uploadPactFile(pact, consumerVersion)
if(res.component2() == null) {
logger.debug("Pact succesfully shared. deleting file..")
removePact(fileName)
removePact(pact)
new Response(200, ResponseUtils.CrossSiteHeaders.asJava, OptionalBody.body(res.component1().getBytes()))
} else {
new Response(500, ResponseUtils.CrossSiteHeaders.asJava, OptionalBody.body(res.component2().getLocalizedMessage.getBytes()))
}
new Response(response.statusCode, ResponseUtils.CrossSiteHeaders.asJava, OptionalBody.body(response.data.array))

} catch {
case e: IOException => new Response(500, ResponseUtils.CrossSiteHeaders.asJava, OptionalBody.body(s"""{"error": "Got IO Exception while reading file. ${e.getMessage}"}""".getBytes()))
case e: RequestFailedException => new Response(e.response.statusCode, ResponseUtils.CrossSiteHeaders.asJava, OptionalBody.body(e.response.data.array))
case _ => new Response(500, ResponseUtils.CrossSiteHeaders.asJava, OptionalBody.body("Something unknown happened..".getBytes()))
case e: RequestFailedException => new Response(e.getStatus.getStatusCode, ResponseUtils.CrossSiteHeaders.asJava, OptionalBody.body(e.getBody.getBytes()))
case t: Throwable => new Response(500, ResponseUtils.CrossSiteHeaders.asJava, OptionalBody.body(t.getMessage.getBytes()))
}
}

private def removePact(fileName: String): Unit = {
def file = new File(s"${System.getProperty("pact.rootDir", "target/pacts")}/$fileName")
private def removePact(file: File): Unit = {
if (file.exists()) {
file.delete()
}
Expand All @@ -96,12 +92,4 @@ object Publish extends StrictLogging {
case _ => None
}

def readContract(fileName: String): String = {
def filePath = s"${System.getProperty("pact.rootDir", "target/pacts")}/$fileName"
def fileReader = Source.fromFile(filePath)
def content = fileReader.getLines().mkString
fileReader.close()
logger.debug(content)
content
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,4 @@ class PublishSpec extends Specification {
then:
result.defined
}

def 'successful read on valid file'() {
given:
def content = '{"consumer": "testconsumer", "provider": "testprovider"}'
def fileName = 'test.json'
def rootDir = System.getProperty('pact.rootDir', 'target/pacts')
def file = new File("${rootDir}/$fileName")
new File(rootDir).mkdirs()
file.write(content)

when:
def result = Publish.readContract(fileName)

then:
content == result

cleanup:
new File(System.getProperty('pact.rootDir', 'target')).deleteDir()
}

def 'unsuccessful read on invalid file'() {
when:
Publish.readContract('invalid')

then:
thrown(IOException)
}
}

0 comments on commit 1fa7ad7

Please sign in to comment.