Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
library-service: upgraded to PACT 4.0 and removed custom messaging co…
…ntract tests
- Loading branch information
Showing
4 changed files
with
34 additions
and
19 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
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
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
46 changes: 31 additions & 15 deletions
46
library-service/src/test/kotlin/pact/MessageContractTest.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 |
---|---|---|
@@ -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) | ||
} | ||
|
||
} |