Skip to content

Commit

Permalink
messaging: add consumer implementation example
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-draeger committed Dec 7, 2018
1 parent 475c0b6 commit 79c591d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions consumer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<artifactId>khttp</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>net.wuerl.kotlin</groupId>
<artifactId>assertj-core-kotlin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.demo.messaging

import java.util.*

data class UserCreateEvent(
val firstName: String,
val lastName: String,
val age: Int,
val active: Boolean,
val expiryDate: Date,
// val timestamp: Long,
val ids: Identifiers
)

data class Identifiers(
val id: Long,
val uuid: UUID
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.demo.messaging

data class UserDeleteEvent(
// val timestamp: Long,
val id: Long
)
60 changes: 60 additions & 0 deletions consumer/src/test/kotlin/com/example/demo/MessagingContractTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.example.demo

import au.com.dius.pact.consumer.MessagePactBuilder
import au.com.dius.pact.consumer.MessagePactProviderRule
import au.com.dius.pact.consumer.Pact
import au.com.dius.pact.consumer.PactVerification
import au.com.dius.pact.model.v3.messaging.MessagePact
import com.example.demo.messaging.UserCreateEvent
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.pactfoundation.consumer.dsl.LambdaDsl.newJsonBody
import org.assertj.core.api.KotlinAssertions.assertThat
import org.junit.Rule
import org.junit.Test
import java.text.SimpleDateFormat

class MessagingContractTest {

@get:Rule
val pactRule = MessagePactProviderRule("messaging-provider", this)

private val datePattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX"
private val sdf = SimpleDateFormat(datePattern)
private val expiryDate = sdf.parse("2017-09-16T05:25:25.000+02:00")

@Pact(provider = "messaging-provider", consumer = "messaging-consumer")
fun createEvent(builder: MessagePactBuilder): MessagePact {

val body = newJsonBody { body ->
body.stringType("firstName", "Christian")
body.stringValue("lastName", "Dräger")
body.numberType("age", 30)
body.booleanType("active")
//body.timestamp()
body.time("expiryDate", datePattern, expiryDate)

body.`object`("ids") {
it.numberType("id", 1)
it.uuid("uuid")
}
}.build()

return builder.given("a user was created")
.expectsToReceive("a create event")
.withContent(body)
.toPact()
}

@Test
@PactVerification("messaging-provider", fragment = "createEvent")
fun canParseCreateEvent() {
val result = jacksonObjectMapper().readValue(pactRule.message, UserCreateEvent::class.java)
assertThat(result.firstName).isEqualTo("Christian")
assertThat(result.lastName).isEqualTo("Dräger")
assertThat(result.age).isEqualTo(30)
assertThat(result.active).isTrue()
assertThat(result.expiryDate).isEqualTo(expiryDate)
assertThat(result.ids.id).isEqualTo(1)
//assertThat(result.ids.uuid).isEqualTo(1)
}
}
Binary file added ui-uploaded-and-verified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 79c591d

Please sign in to comment.