Skip to content

Commit

Permalink
Merge pull request #1199 from markozz/feature/publish-to-broker
Browse files Browse the repository at this point in the history
Feature/publish to broker
  • Loading branch information
uglyog authored Sep 5, 2020
2 parents ddb7600 + f1c8d6c commit ec3c777
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 3 deletions.
19 changes: 19 additions & 0 deletions pact-jvm-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The server implements a `JSON` `REST` Admin API with the following endpoints.
/ -> For diagnostics, currently returns a list of ports of the running mock servers.
/create -> For initialising a test server and submitting the JSON interactions. It returns a port
/complete -> For finalising and verifying the interactions with the server. It writes the `JSON` pact file to disk.
/publish -> For publishing contracts. It takes a contract from disk and publishes it to the configured broker

## Running the server

Expand Down Expand Up @@ -40,6 +41,10 @@ Usage: pact-jvm-server [options] [port]
Keystore password
-s <value> | --ssl-port <value>
Ssl port the mock server should run on. lower and upper bounds are ignored
-b <value> | --broker <value>
The baseUrl of the broker to publish contracts to (for example https://organization.broker.com
-t <value | --token <value>
API token for authentication to the pact broker
--debug
run with debug logging
```
Expand Down Expand Up @@ -87,6 +92,7 @@ The following actions are expected to occur
* Once finished, the client will call `/complete' on the Admin API, posting the port number
* The pact server will verify the interactions and write the `JSON` `pact` file to disk under `/target`
* The mock server running on the supplied port will be shutdown.
* The client will call `/publish` to publish the created contract to the configured pact broker

## Endpoints

Expand Down Expand Up @@ -120,6 +126,19 @@ For example:
This will cause the Pact server to verify the interactions, shutdown the mock server running on that port and writing
the pact `JSON` file to disk under the `target` directory.

### /publish

Once all interactions have been tested the `/publish` endpoint can be called to publish the created pact to the pact broker
For this it is required to run the pact-jvm-server with the -b parameter to configure the pact broker to publish the pacts to.
Optionaly an authentication token can be used for authentication against the broker.

For example:

POST http://localhost:29999/publish '{ "consumer": "Zoo", "consumerVersion": "0.0.1", "provider": "Animal_Service" }'

This will cause the Pact server to check for the pact `Zoo-Animal_Service.json` on disk under `target` and publish it to
the configured pact broker. After a successful publish the pact will be removed from disk.

### /

The `/` endpoint is for diagnostics and to check that the pact server is running. It will return all the currently
Expand Down
111 changes: 111 additions & 0 deletions pact-jvm-server/src/main/scala/au/com/dius/pact/server/Publish.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package au.com.dius.pact.server

import au.com.dius.pact.core.model.{OptionalBody, Request, Response}
import com.typesafe.scalalogging.StrictLogging

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

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

object Publish extends StrictLogging {

def apply(request: Request, oldState: ServerState, config: Config): Result = {
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, tags)
} else {
def errorJson: String = "{\"error\": \"body should contain consumer, consumerVersion and provider.\"}"
def body: OptionalBody = OptionalBody.body(errorJson.getBytes())
response = new Response(400, ResponseUtils.CrossSiteHeaders.asJava, body)
}
} else {
def errorJson: String = "{\"error\" : \"Broker url not correctly configured please run server with -b or --broker 'http://pact-broker.adomain.com' option\" }"
def body: OptionalBody = OptionalBody.body(errorJson.getBytes())
response = new Response(500, ResponseUtils.CrossSiteHeaders.asJava, body)
}
Result(response, oldState)
}

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 {
val options = getOptions(authToken)
val brokerClient: PactBrokerClient = new PactBrokerClient(broker, options.asJava)
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()))
} else {
new Response(500, ResponseUtils.CrossSiteHeaders.asJava, OptionalBody.body(res.component2().getLocalizedMessage.getBytes()))
}

} 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.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 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()
}
}

private def getVarFromConfig(variable: String) = {
if (!variable.isEmpty) Some(variable)
else None
}

def getBrokerUrlFromConfig(config: Config): Option[String] = {
if (!config.broker.isEmpty && config.broker.startsWith("http")) Some(config.broker)
else None
}

private def getVarFromJson(variable: String, json: Any): Option[String] = json match {
case map: Map[AnyRef, AnyRef] => {
if (map.contains(variable)) Some(map(variable).toString)
else None
}
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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ object RequestRouter {
action match {
case "create" => Create(request, oldState, config)
case "complete" => Complete(request, oldState)
case "publish" => Publish(request, oldState, config)
case "" => ListServers(oldState)
case _ => Result(pactDispatch(request, oldState), oldState)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ case class Config(port: Int = 29999,
pactVersion: Int = 2,
keystorePath: String = "",
keystorePassword: String = "",
sslPort : Int = 8443)
sslPort : Int = 8443,
broker: String = "",
authToken: String = ""
)

object Server extends App {

Expand All @@ -43,6 +46,8 @@ object Server extends App {
opt[String]('k', "keystore-path") action { (x, c) => c.copy(keystorePath = x) } text("Path to keystore")
opt[String]('p', "keystore-password") action { (x, c) => c.copy(keystorePassword = x) } text("Keystore password")
opt[Int]('s', "ssl-port") action { (x, c) => c.copy(sslPort = x) } text("Ssl port the mock server should run on. lower and upper bounds are ignored")
opt[String]('b', "broker") action {(x, c) => c.copy(broker = x)} text("URL of broker where to publish contracts to")
opt[String]('t', "token") action {(x, c) => c.copy(authToken = x)} text("Auth token for publishing the pact to broker")
}

parser.parse(args, Config()) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CreateSpec extends Specification {
JavaConverters.asScalaBuffer(['/data']).toList(),
pact, new scala.collection.immutable.HashMap(),
new au.com.dius.pact.server.Config(4444, 'localhost', false, 20000, 40000, true,
2, '', '', 8444))
2, '', '', 8444, '', ''))

then:
result.response().status == 201
Expand All @@ -44,7 +44,7 @@ class CreateSpec extends Specification {
JavaConverters.asScalaBuffer([]).toList(),
pact, new scala.collection.immutable.HashMap(),
new au.com.dius.pact.server.Config(4444, 'localhost', false, 20000, 40000, true,
2, keystorePath, password, 8444))
2, keystorePath, password, 8444, '', ''))

then:
result.response().status == 201
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package au.com.dius.pact.server

import spock.lang.Specification

class PublishSpec extends Specification {

def 'invalid broker url in config will not set broker'() {
given:
def config = new Config(80, '0.0.0.0', false, 100, 200, false, 3, '', '', 0, 'invalid', 'abc#3')
// def pact = PublishSpec.getResourceAsStream('/create-pact.json').text

when:
def result = Publish.getBrokerUrlFromConfig(config)

then:
!result.defined
}

def 'valid broker url will set broker'() {
given:
def config = new Config(80,
'0.0.0.0',
false,
100,
200,
false,
3,
'',
'',
0,
'https://valid.broker.com',
'abc#3'
)

when:
def result = Publish.getBrokerUrlFromConfig(config)

then:
result.defined
}
}

0 comments on commit ec3c777

Please sign in to comment.