Skip to content

Commit

Permalink
add a gradle task to publish pact files to a pact broker #120
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jul 6, 2015
1 parent 11adca2 commit 5fe1698
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pact-jvm-provider-gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ apply plugin: 'maven-publish'
apply plugin: "com.gradle.plugin-publish"

dependencies {
compile gradleApi(), localGroovy()
compile gradleApi(), localGroovy(), 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
compile(project(":pact-jvm-provider_${project.scalaVersion}")) {
exclude module: 'groovy-all'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ class PactPlugin implements Plugin<Project> {
// Create and install the extension object
project.extensions.create("pact", PactPluginExtension, project.container(ProviderInfo))

project.task('pactVerify', description: 'Verify your pacts against your providers')
project.task('pactVerify', description: 'Verify your pacts against your providers', group: 'Pact')
project.task('pactPublish', description: 'Publish your pacts to a pact broker', type: PactPublishTask, group: 'Pact')

project.afterEvaluate {
project.pact.serviceProviders.all { ProviderInfo provider ->
def providerTask = project.task("pactVerify_${provider.name}",
description: "Verify the pacts against ${provider.name}", type: PactVerificationTask) {
description: "Verify the pacts against ${provider.name}", type: PactVerificationTask, group: 'Pact') {
providerToVerify = provider
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package au.com.dius.pact.provider.gradle

import org.gradle.api.NamedDomainObjectContainer
import org.gradle.util.ConfigureUtil

class PactPluginExtension {

final NamedDomainObjectContainer<ProviderInfo> serviceProviders

PactPublish publish

public PactPluginExtension(serviceProviders) {
this.serviceProviders = serviceProviders
}

def serviceProviders(Closure closure) {
serviceProviders.configure(closure)
}

def publish(Closure closure) {
publish = new PactPublish()
ConfigureUtil.configure(closure, publish)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package au.com.dius.pact.provider.gradle

import groovy.transform.ToString

@ToString
class PactPublish {
def pactDirectory
String pactBrokerUrl
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package au.com.dius.pact.provider.gradle

import groovy.io.FileType
import groovy.json.JsonSlurper
import groovyx.net.http.HTTPBuilder
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.Method.PUT

class PactPublishTask extends DefaultTask {

@TaskAction
void publishPacts() {
if (!project.pact.publish) {
throw new RuntimeException('You must add a pact publish configuration to your build before you can ' +
'use the pactPublish task')
}

PactPublish pactPublish = project.pact.publish
if (pactPublish.pactDirectory == null) {
pactPublish.pactDirectory = project.file("${project.buildDir}/pacts")
}

def http = new HTTPBuilder(pactPublish.pactBrokerUrl)
File pactDirectory = pactPublish.pactDirectory as File
pactDirectory.eachFileMatch(FileType.FILES, ~/.*\.json/) { pactFile ->
print "Publishing ${pactFile.name} ... "
def pact = new JsonSlurper().parse(pactFile)
http.request(PUT) {
uri.path = "/pacts/provider/${pact.provider.name}/consumer/${pact.consumer.name}/version/${project.version}"
requestContentType = JSON
body = pactFile.text

response.success = { resp ->
println resp.statusLine
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@ class PactPluginTest {
private Project project

@Before
public void setup() {
void setup() {
project = ProjectBuilder.builder().build()
plugin = new PactPlugin()
plugin.apply(project)
}

@Test
public void 'defines a pactVerify task'() {
void 'defines a pactVerify task'() {
assert project.tasks.pactVerify
}

@Test
public void 'defines a task for each defined provider'() {
void 'defines a pactPublish task'() {
assert project.tasks.pactPublish
}

@Test
void 'defines a task for each defined provider'() {
project.pact {
serviceProviders {
provider1 {
Expand All @@ -43,7 +48,7 @@ class PactPluginTest {
}

@Test
public void 'defines a task for each file in the pact file directory'() {
void 'defines a task for each file in the pact file directory'() {
def resource = getClass().classLoader.getResource('pacts/foo_pact.json')
File pactFileDirectory = new File(resource.file).parentFile
project.pact {
Expand All @@ -66,7 +71,7 @@ class PactPluginTest {
}

@Test
public void 'configures the providers and consumers correctly'() {
void 'configures the providers and consumers correctly'() {
def pactFileUrl = 'http://localhost:8000/pacts/provider/prividera/consumer/consumera/latest'
def stateChangeUrl = 'http://localhost:8080/stateChange'
project.pact {
Expand Down Expand Up @@ -99,7 +104,7 @@ class PactPluginTest {
}

@Test
public void 'do not set the state change url automatically'() {
void 'do not set the state change url automatically'() {
def pactFileUrl = 'http://localhost:8000/pacts/provider/prividera/consumer/consumera/latest'
project.pact {
serviceProviders {
Expand All @@ -117,4 +122,19 @@ class PactPluginTest {
assert consumer.pactFile == new URL(pactFileUrl)
assert consumer.stateChange == null
}

@Test
void 'configures the publish task correctly'() {
project.pact {
publish {
pactDirectory = '/pact/dir'
pactBrokerUrl = 'http://pactbroker:1234'
}
}

project.evaluate()

assert project.pact.publish.pactDirectory == '/pact/dir'
assert project.pact.publish.pactBrokerUrl == 'http://pactbroker:1234'
}
}

0 comments on commit 5fe1698

Please sign in to comment.