Skip to content

Commit

Permalink
library-service: upgraded to PACT 4.0 and removed custom messaging co…
Browse files Browse the repository at this point in the history
…ntract tests
  • Loading branch information
slu-it committed Oct 4, 2019
1 parent fe2940c commit 742e751
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
3 changes: 1 addition & 2 deletions library-service/build.gradle
Expand Up @@ -68,9 +68,8 @@ dependencies {
testImplementation("com.tngtech.archunit:archunit:${archunitVersion}")
testImplementation("io.mockk:mockk:${mockkVersion}")
testImplementation("info.novatec.testit:testutils-logrecorder-logback:${logRecorderVersion}")
testImplementation("info.novatec.testit:pact-provider-junit5:${pactProviderVersion}")
testImplementation("org.testcontainers:testcontainers:${testContainersVersion}")
testImplementation("au.com.dius:pact-jvm-provider-junit5_2.12:${pactVersion}")
testImplementation("au.com.dius:pact-jvm-provider-junit5:${pactVersion}")

testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
Expand Down
2 changes: 1 addition & 1 deletion library-service/gradle.properties
Expand Up @@ -4,7 +4,7 @@ asciidoctorVersion = 1.5.8
dockerPluginVersion = 1.2
kotlinVersion = 1.3.50
logRecorderVersion = 0.3.4
pactVersion = 3.5.24
pactVersion = 4.0.0
pactProviderVersion = 0.4.0
springBootVersion = 2.1.8.RELEASE
testContainersVersion = 1.12.2
Expand Down
2 changes: 1 addition & 1 deletion library-service/src/test/kotlin/pact/HttpContractTest.kt
Expand Up @@ -41,7 +41,6 @@ import utils.classification.ContractTest
properties = ["application.secured=false"]
)
@TestInstance(PER_CLASS) // PACT needs this ... for some reason ...
@ExtendWith(PactVerificationInvocationContextProvider::class)
class HttpContractTest(
@Autowired val dataStore: BookDataStore
) {
Expand All @@ -57,6 +56,7 @@ class HttpContractTest(
}

@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider::class)
fun pactVerificationTestTemplate(context: PactVerificationContext) {
context.verifyInteraction()
}
Expand Down
46 changes: 31 additions & 15 deletions library-service/src/test/kotlin/pact/MessageContractTest.kt
@@ -1,43 +1,59 @@
package pact

import au.com.dius.pact.provider.PactVerifyProvider
import au.com.dius.pact.provider.junit.Provider
import au.com.dius.pact.provider.junit.VerificationReports
import au.com.dius.pact.provider.junit.loader.PactFolder
import au.com.dius.pact.provider.junit5.AmpqTestTarget
import au.com.dius.pact.provider.junit5.PactVerificationContext
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider
import com.fasterxml.jackson.databind.ObjectMapper
import library.service.business.books.domain.BookRecord
import library.service.business.books.domain.events.BookAdded
import library.service.business.books.domain.types.BookId
import library.service.messaging.MessagingConfiguration
import org.junit.jupiter.api.TestFactory
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
import org.junit.jupiter.api.TestTemplate
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.amqp.core.MessageProperties
import org.testit.pact.provider.junit.PactTestFactory
import org.testit.pact.provider.message.ActualMessage
import org.testit.pact.provider.message.MessagePacts
import org.testit.pact.provider.message.MessageProducer
import org.testit.pact.provider.sources.LocalFiles
import utils.Books
import utils.classification.ContractTest
import java.time.OffsetDateTime
import java.util.*

@ContractTest
@Provider("library-service")
@PactFolder("src/test/pacts/message")
@VerificationReports("console")
@TestInstance(PER_CLASS) // PACT needs this ... for some reason ...
class MessageContractTest {

val configuration = MessagingConfiguration()
val objectMapper = ObjectMapper().apply { findAndRegisterModules() }
val messageConverter = configuration.messageConverter(objectMapper)

val pacts = MessagePacts(LocalFiles("src/test/pacts/message"), "library-service")
@BeforeEach
fun setTarget(context: PactVerificationContext) {
context.target = AmpqTestTarget(listOf("pact"))
}

@TestFactory fun `library-enrichment consumer contract tests`() =
PactTestFactory.createTests(pacts, "library-enrichment", this)
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider::class)
fun pactVerificationTestTemplate(context: PactVerificationContext) {
context.verifyInteraction()
}

@MessageProducer("'The Martian' was added event")
fun `verify The Martian was added event`(): ActualMessage {
@PactVerifyProvider("'The Martian' was added event")
fun `verify 'The Martian' was added event`(): String {
val event = BookAdded(
id = UUID.randomUUID(),
timestamp = OffsetDateTime.now(),
bookRecord = BookRecord(BookId.generate(), Books.THE_MARTIAN)
id = UUID.randomUUID(),
timestamp = OffsetDateTime.now(),
bookRecord = BookRecord(BookId.generate(), Books.THE_MARTIAN)
)
val message = messageConverter.toMessage(event, MessageProperties())
return ActualMessage(message.body)
return String(message.body)
}

}

0 comments on commit 742e751

Please sign in to comment.