-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
messaging: add consumer implementation example
- Loading branch information
1 parent
475c0b6
commit 79c591d
Showing
5 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
consumer/src/main/kotlin/com/example/demo/messaging/UserCreateEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
6 changes: 6 additions & 0 deletions
6
consumer/src/main/kotlin/com/example/demo/messaging/UserDeleteEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
60
consumer/src/test/kotlin/com/example/demo/MessagingContractTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.