Skip to content

Commit

Permalink
Refactor gradle (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogervinas committed Jun 20, 2024
1 parent 0d27093 commit 83a0793
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 110 deletions.
16 changes: 15 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
version: 2
updates:
- package-ecosystem: gradle
directory: "/"
directory: "/sample-api-client"
schedule:
interval: "weekly"
groups:
dependencies:
applies-to: version-updates
patterns:
- "*"
- package-ecosystem: gradle
directory: "/sample-api-server"
schedule:
interval: "weekly"
groups:
dependencies:
applies-to: version-updates
patterns:
- "*"
- package-ecosystem: github-actions
directory: /
schedule:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[![CI](https://github.com/rogervinas/contract-testing-with-pact/actions/workflows/ci.yml/badge.svg)](https://github.com/rogervinas/contract-testing-with-pact/actions/workflows/ci.yml)
![Java](https://img.shields.io/badge/Java-21-blue?labelColor=black)
![Kotlin](https://img.shields.io/badge/Kotlin-1.9.20-blue?labelColor=black)
![Kotlin](https://img.shields.io/badge/Kotlin-2.0.0-blue?labelColor=black)
![SpringBoot](https://img.shields.io/badge/SpringBoot-3.1.5-blue?labelColor=black)
![Pact](https://img.shields.io/badge/Pact-4.6.3-blue?labelColor=black)
![Pact](https://img.shields.io/badge/Pact-4.6.10-blue?labelColor=black)

# Contract Testing with Pact

Expand Down
3 changes: 0 additions & 3 deletions sample-api-client/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@ charset=utf-8
insert_final_newline=true
trim_trailing_whitespace=true
max_line_length=120

[*.{kt,kts}]
disabled_rules=import-ordering
15 changes: 11 additions & 4 deletions sample-api-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED
import org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED

plugins {
id("org.jetbrains.kotlin.jvm") version "1.9.20"
id("au.com.dius.pact") version "4.6.3"
id("org.jetbrains.kotlin.jvm") version "2.0.0"
id("au.com.dius.pact") version "4.6.10"
id("org.jlleitschuh.gradle.ktlint") version "12.1.1"
application
}

Expand Down Expand Up @@ -35,9 +36,15 @@ dependencies {
testImplementation("au.com.dius.pact.consumer:junit5:4.6.3")
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(21))
compilerOptions {
freeCompilerArgs.addAll("-Xjsr305=strict")
}
}

Expand Down
Binary file modified sample-api-client/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion sample-api-client/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
17 changes: 9 additions & 8 deletions sample-api-client/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -144,15 +145,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -201,11 +202,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import java.time.LocalDate
data class SampleThing(
val name: String,
val value: Double,
@JsonFormat(pattern = "yyyy-MM-dd") val date: LocalDate
@JsonFormat(pattern = "yyyy-MM-dd") val date: LocalDate,
)

data class SampleThingId(
val id: Int
val id: Int,
)

interface SampleApiClient {
suspend fun create(thing: SampleThing): SampleThingId?

suspend fun get(thingId: SampleThingId): SampleThing?
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ import io.ktor.http.contentType
import io.ktor.serialization.jackson.jackson

class SampleApiKtorClient(private val serverUrl: String) : SampleApiClient {

private val client = HttpClient(CIO) {
install(ContentNegotiation) {
jackson {
registerModule(JavaTimeModule())
private val client =
HttpClient(CIO) {
install(ContentNegotiation) {
jackson {
registerModule(JavaTimeModule())
}
}
}
}

override suspend fun create(thing: SampleThing): SampleThingId? {
val response = client.post("$serverUrl/thing") {
contentType(ContentType.Application.Json)
setBody(thing)
}
val response =
client.post("$serverUrl/thing") {
contentType(ContentType.Application.Json)
setBody(thing)
}
return when (response.status) {
HttpStatusCode.Created -> response.body<SampleThingId>()
else -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
import org.junit.jupiter.api.extension.ExtendWith
import java.time.LocalDate


@ExtendWith(PactConsumerTestExt::class)
@TestInstance(PER_CLASS)
class SampleApiClientContractTest {

companion object {
private val THING123 = SampleThing("Foo", 123.45, LocalDate.of(2022, 10, 13))
private val THING123_JSON_PACT = PactDslJsonBody()
.stringMatcher("name", "\\w+", "Foo")
.decimalType("value", 123.45)
.localDate("date", "yyyy-MM-dd", LocalDate.of(2022, 10, 13))
private val THING123_JSON_PACT =
PactDslJsonBody()
.stringMatcher("name", "\\w+", "Foo")
.decimalType("value", 123.45)
.localDate("date", "yyyy-MM-dd", LocalDate.of(2022, 10, 13))

private val THING123_ID = SampleThingId(123)
private val THING123_ID_JSON_PACT = PactDslJsonBody()
.integerType("id", 123L)
private val THING123_ID_JSON_PACT =
PactDslJsonBody()
.integerType("id", 123L)
}

@Pact(provider = "Sample API Server", consumer = "Sample API Client")
Expand All @@ -55,9 +55,10 @@ class SampleApiClientContractTest {
@PactTestFor(providerName = "Sample API Server", pactMethod = "create", providerType = SYNCH)
fun `should create thing`(mockServer: MockServer) {
val client = SampleApiKtorClient(mockServer.getUrl())
val thingId = runBlocking {
client.create(THING123)
}
val thingId =
runBlocking {
client.create(THING123)
}
assertThat(thingId)
.isEqualTo(THING123_ID)
}
Expand All @@ -80,9 +81,10 @@ class SampleApiClientContractTest {
@PactTestFor(providerName = "Sample API Server", pactMethod = "getExistingThing", providerType = SYNCH)
fun `should get thing 123 when it exists`(mockServer: MockServer) {
val client = SampleApiKtorClient(mockServer.getUrl())
val thing = runBlocking {
client.get(THING123_ID)
}
val thing =
runBlocking {
client.get(THING123_ID)
}
assertThat(thing)
.isEqualTo(THING123)
}
Expand All @@ -103,9 +105,10 @@ class SampleApiClientContractTest {
@PactTestFor(providerName = "Sample API Server", pactMethod = "getNonExistingThing", providerType = SYNCH)
fun `should not get thing 123 when it does not exist`(mockServer: MockServer) {
val client = SampleApiKtorClient(mockServer.getUrl())
val thing = runBlocking {
client.get(THING123_ID)
}
val thing =
runBlocking {
client.get(THING123_ID)
}
assertThat(thing).isNull()
}
}
3 changes: 0 additions & 3 deletions sample-api-server/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@ charset=utf-8
insert_final_newline=true
trim_trailing_whitespace=true
max_line_length=120

[*.{kt,kts}]
disabled_rules=import-ordering
79 changes: 43 additions & 36 deletions sample-api-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED
import org.gradle.api.tasks.testing.logging.TestLogEvent.SKIPPED

plugins {
id("org.springframework.boot") version "3.1.5"
id("io.spring.dependency-management") version "1.1.3"
kotlin("jvm") version "1.9.20"
kotlin("plugin.spring") version "1.9.20"
id("au.com.dius.pact") version "4.6.3"
id("org.springframework.boot") version "3.1.5"
id("io.spring.dependency-management") version "1.1.3"
kotlin("jvm") version "2.0.0"
kotlin("plugin.spring") version "2.0.0"
id("au.com.dius.pact") version "4.6.10"
id("org.jlleitschuh.gradle.ktlint") version "12.1.1"
}

version = "1.0"
Expand All @@ -18,48 +19,54 @@ project.extra["pacticipant"] = "Sample API Server"
project.extra["pacticipantVersion"] = version

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

kotlin {
compilerOptions {
freeCompilerArgs.addAll("-Xjsr305=strict")
}
}

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
dependencies {
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")

testImplementation("au.com.dius.pact.provider:spring6:4.6.3")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")

testImplementation("com.ninja-squad:springmockk:4.0.2")
}
testImplementation("au.com.dius.pact.provider:spring6:4.6.3")

kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(21))
}
testImplementation("com.ninja-squad:springmockk:4.0.2")
}

tasks.withType<Test>().configureEach {
useJUnitPlatform()
testLogging {
events(PASSED, SKIPPED, FAILED)
exceptionFormat = FULL
showExceptions = true
showCauses = true
showStackTraces = true
}
useJUnitPlatform()
testLogging {
events(PASSED, SKIPPED, FAILED)
exceptionFormat = FULL
showExceptions = true
showCauses = true
showStackTraces = true
}

systemProperties["pactbroker.url"] = "${project.extra["pactbroker.url"]}"
systemProperties["pact.provider.version"] = version
systemProperties["pact.verifier.publishResults"] = "true"
systemProperties["pactbroker.url"] = "${project.extra["pactbroker.url"]}"
systemProperties["pact.provider.version"] = version
systemProperties["pact.verifier.publishResults"] = "true"
}

pact {
broker {
pactBrokerUrl = "${project.extra["pactbroker.url"]}"
}
broker {
pactBrokerUrl = "${project.extra["pactbroker.url"]}"
}
}
Binary file modified sample-api-server/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion sample-api-server/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Loading

0 comments on commit 83a0793

Please sign in to comment.