Skip to content

Commit

Permalink
Merge pull request #189 from ITV/publisher-refactor
Browse files Browse the repository at this point in the history
combine all broker interactions in single class
  • Loading branch information
jbwheatley authored Oct 31, 2020
2 parents bd4e977 + c55751e commit ff864e2
Show file tree
Hide file tree
Showing 31 changed files with 406 additions and 474 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@
"name" : "scala-pact-consumer"
},
"interactions" : [
{
"providerState" : "Results: Bob, Fred, Harry",
"description" : "Fetching results",
"request" : {
"method" : "GET",
"path" : "/results"
},
"response" : {
"status" : 200,
"headers" : {
"Pact" : "modifiedRequest"
},
"body" : {
"count" : 3,
"results" : [
"Bob",
"Fred",
"Harry"
]
}
}
},
{
"description" : "Fetching least secure auth token ever",
"request" : {
Expand Down Expand Up @@ -37,28 +59,6 @@
}
}
}
},
{
"providerState" : "Results: Bob, Fred, Harry",
"description" : "Fetching results",
"request" : {
"method" : "GET",
"path" : "/results"
},
"response" : {
"status" : 200,
"headers" : {
"Pact" : "modifiedRequest"
},
"body" : {
"count" : 3,
"results" : [
"Bob",
"Fred",
"Harry"
]
}
}
}
],
"metadata" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@
"name" : "scala-pact-consumer"
},
"interactions" : [
{
"providerState" : "Results: Bob, Fred, Harry",
"description" : "Fetching results",
"request" : {
"method" : "GET",
"path" : "/results"
},
"response" : {
"status" : 200,
"headers" : {
"Pact" : "modifiedRequest"
},
"body" : {
"count" : 3,
"results" : [
"Bob",
"Fred",
"Harry"
]
}
}
},
{
"description" : "Fetching least secure auth token ever",
"request" : {
Expand Down Expand Up @@ -37,28 +59,6 @@
}
}
}
},
{
"providerState" : "Results: Bob, Fred, Harry",
"description" : "Fetching results",
"request" : {
"method" : "GET",
"path" : "/results"
},
"response" : {
"status" : 200,
"headers" : {
"Pact" : "modifiedRequest"
},
"body" : {
"count" : 3,
"results" : [
"Bob",
"Fred",
"Harry"
]
}
}
}
],
"metadata" : {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.itv.scalapact.plugin

import com.itv.scalapact.shared.ScalaPactSettings
import com.itv.scalapact.shared.typeclasses.BrokerPublishData
import com.itv.scalapact.shared.{BrokerPublishData, ScalaPactSettings}

import scala.concurrent.duration._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package com.itv.scalapact.plugin.shared

import java.io.{File, PrintWriter}

//todo make defs
object PactContractWriter {

private val simplifyName: String => String = name => "[^a-zA-Z0-9-]".r.replaceAllIn(name.replace(" ", "-"), "")

val writePactContracts: String => String => String => String => Unit = dirPath =>
val writePactToFile: String => String => String => String => Unit = dirPath =>
provider =>
consumer =>
contents => {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.itv.scalapact.plugin.shared

import com.itv.scalapact.shared.ColourOutput._
import com.itv.scalapact.shared.typeclasses.{IPactReader, IPactWriter, IScalaPactHttpClientBuilder}
import com.itv.scalapact.shared._
import com.itv.scalapactcore.common.LocalPactFileLoader
import com.itv.scalapact.shared.typeclasses.{IPactReader, IPactWriter, IScalaPactHttpClientBuilder}
import com.itv.scalapactcore.publisher.{PublishFailed, PublishResult, PublishSuccess, Publisher}

import scala.concurrent.duration._

object ScalaPactPublishCommand {

def doPactPublish[F[_]](
def doPactPublish(
scalaPactSettings: ScalaPactSettings,
pactBrokerAddress: String,
providerBrokerPublishMap: Map[String, String],
Expand All @@ -20,10 +20,7 @@ object ScalaPactPublishCommand {
pactBrokerAuthorization: Option[PactBrokerAuthorization],
pactBrokerClientTimeout: Duration,
sslContextName: Option[String]
)(implicit pactReader: IPactReader, pactWriter: IPactWriter, httpClientBuilder: IScalaPactHttpClientBuilder[F]): Unit = {
import Publisher._

val httpClient = httpClientBuilder.build(pactBrokerClientTimeout, sslContextName)
)(implicit pactReader: IPactReader, pactWriter: IPactWriter, httpClientBuilder: IScalaPactHttpClientBuilder): Unit = {

PactLogger.message("*************************************".white.bold)
PactLogger.message("** ScalaPact: Publishing Contracts **".white.bold)
Expand All @@ -37,32 +34,19 @@ object ScalaPactPublishCommand {
PactLogger.error("Pact broker does not cope well with snapshot contracts.".yellow)
PactLogger.error("To enable this feature, add \"allowSnapshotPublish := true\" to your pact.sbt file.".yellow)
} else {
val configAndPactFiles =
LocalPactFileLoader.loadPactFiles(pactReader)(false)(scalaPactSettings.giveOutputPath)(scalaPactSettings)

// Publish all to main broker
val mainPublishResults: List[PublishResult] = publishToBroker(
httpClient.doRequestSync,
val publishSettings = PactPublishSettings(
pactBrokerAddress,
providerBrokerPublishMap,
versionToPublishAs,
tagsToPublishWith,
pactBrokerAuthorization
)(pactWriter)(configAndPactFiles)

// Publish to other specified brokers
val otherPublishResults: List[PublishResult] = for {
pactContract <- configAndPactFiles
broker <- providerBrokerPublishMap.get(pactContract.provider.name).toList
publishResult <- publishToBroker(
httpClient.doRequestSync,
broker,
versionToPublishAs,
tagsToPublishWith,
pactBrokerAuthorization
)(pactWriter)(List(pactContract))
} yield publishResult

evaluatePublishResults(mainPublishResults ++ otherPublishResults)
pactContractVersion,
allowSnapshotPublish,
tagsToPublishWith.toList,
pactBrokerAuthorization,
pactBrokerClientTimeout,
sslContextName
)
val publishResults = Publisher.apply.publishPacts(publishSettings, scalaPactSettings)
evaluatePublishResults(publishResults)
}
}

Expand All @@ -72,9 +56,7 @@ object ScalaPactPublishCommand {
case result: PublishFailed => PactLogger.error(result.renderAsString)
}

val noErrors = publishResults.collectFirst { case _: PublishFailed => () }.isEmpty

if (noErrors) exitSuccess()
if (publishResults.forall(_.isSuccess)) exitSuccess()
else exitFailure()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object ScalaPactTestCommand {
.collect { case Some(pact) => pact }
.reduceOption(combinePacts)
.foreach { combined =>
PactContractWriter.writePactContracts(outputPath)(combined.provider.name)(combined.consumer.name)(
PactContractWriter.writePactToFile(outputPath)(combined.provider.name)(combined.consumer.name)(
pactWriter.pactToJsonString(combined, BuildInfo.version)
)
}
Expand All @@ -68,7 +68,9 @@ object ScalaPactTestCommand {

private def fileToJsonString(file: File): Option[String] =
Try {
val contents = Source.fromFile(file).getLines().mkString
val source = Source.fromFile(file)
val contents = source.getLines().mkString
source.close()
file.delete()
contents
}.toOption.orElse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package com.itv.scalapact.plugin.shared

import com.itv.scalapact.shared.ColourOutput._
import com.itv.scalapact.shared._
import com.itv.scalapact.shared.typeclasses.{IPactReader, IPactWriter, IResultPublisherBuilder, IScalaPactHttpClientBuilder}
import com.itv.scalapact.shared.typeclasses.{IPactReader, IPactWriter, IScalaPactHttpClientBuilder}
import com.itv.scalapact.shared.ProviderStateResult.SetupProviderState
import com.itv.scalapactcore.verifier.Verifier

import scala.concurrent.duration._

object ScalaPactVerifyCommand {

def doPactVerify[F[_]](
def doPactVerify(
scalaPactSettings: ScalaPactSettings,
providerStates: Seq[(String, SetupProviderState)],
providerStateMatcher: PartialFunction[String, ProviderStateResult],
Expand All @@ -27,8 +27,7 @@ object ScalaPactVerifyCommand {
includePendingStatus: Boolean
)(implicit pactReader: IPactReader,
pactWriter: IPactWriter,
httpClientBuilder: IScalaPactHttpClientBuilder[F],
publisher: IResultPublisherBuilder): Unit = {
httpClientBuilder: IScalaPactHttpClientBuilder): Unit = {
PactLogger.message("*************************************".white.bold)
PactLogger.message("** ScalaPact: Running Verifier **".white.bold)
PactLogger.message("*************************************".white.bold)
Expand Down Expand Up @@ -66,7 +65,7 @@ object ScalaPactVerifyCommand {
}
}

val successfullyVerified = Verifier[F].verify(pactVerifySettings, scalaPactSettings)
val successfullyVerified = Verifier.apply.verify(pactVerifySettings, scalaPactSettings)

if (successfullyVerified) sys.exit(0) else sys.exit(1)

Expand Down
Loading

0 comments on commit ff864e2

Please sign in to comment.