Skip to content

Commit

Permalink
feat(JUnit5): Support verification tests for sync request/response me…
Browse files Browse the repository at this point in the history
…ssages with MessageTestTarget #1681
  • Loading branch information
rholshausen committed Mar 30, 2023
1 parent 1721cb0 commit c07f707
Show file tree
Hide file tree
Showing 9 changed files with 453 additions and 23 deletions.
3 changes: 1 addition & 2 deletions config/detekt-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ naming:
ignoreOverridden: true
FunctionParameterNaming:
active: true
parameterPattern: '[a-z][A-Za-z0-9]*'
parameterPattern: '_?[a-z][A-Za-z0-9]*'
excludeClassPattern: '$^'
ignoreOverridden: true
InvalidPackageDeclaration:
Expand Down Expand Up @@ -521,7 +521,6 @@ style:
values:
- 'FIXME:'
- 'STOPSHIP:'
- 'TODO:'
allowedPatterns: ''
customMessage: ''
ForbiddenImport:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import mu.KLogging
import org.apache.commons.codec.binary.Base64
import org.apache.commons.lang3.StringUtils

interface MessageInteraction {
val description: String
val interactionId: String?
interface MessageInteraction: Interaction {
override var description: String
override val interactionId: String?
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,17 @@ open class HttpsTestTarget @JvmOverloads constructor (
}

/**
* Test target for use with asynchronous providers (like with message queues).
* Test target for use with asynchronous providers (like with message queues) and synchronous request/response message
* flows (like gRPC or Kafka request/reply strategies).
*
* This target will look for methods with a @PactVerifyProvider annotation where the value is the description of the
* interaction.
* interaction. For asynchronous messages, these functions must take no parameter and return the message
* (or message + metadata), while for synchronous messages they can receive the request message then must return the
* response message (or message + metadata).
*
* @property packagesToScan List of packages to scan for methods with @PactVerifyProvider annotations. Defaults to the
* full test classpath.
* @property classLoader (Optional) ClassLoader to use to scan for packages
*/
open class MessageTestTarget @JvmOverloads constructor(
private val packagesToScan: List<String> = emptyList(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package au.com.dius.pact.provider.junit5;

import au.com.dius.pact.core.model.Interaction;
import au.com.dius.pact.core.model.Pact;
import au.com.dius.pact.core.model.v4.MessageContents;
import au.com.dius.pact.provider.PactVerifyProvider;
import au.com.dius.pact.provider.junitsupport.Provider;
import au.com.dius.pact.provider.junitsupport.loader.PactFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;

@Provider("KafkaRequestReplyProvider")
@PactFolder("src/test/resources/amqp_pacts")
class SynchronousMessageContractTest {
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void testTemplate(Pact pact, Interaction interaction, PactVerificationContext context) {
context.verifyInteraction();
}

@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new MessageTestTarget());
}

@PactVerifyProvider("a test message")
public String verifyMessageForOrder(MessageContents request) {
return "{\"name\": \"Fred\", \"testParam1\": \"value1\",\"testParam2\": \"value2\"}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"consumer": {
"name": "KafkaRequestReplyConsumer"
},
"interactions": [
{
"comments": {
"testname": "com.techgalery.springkafkaclient.PactTest.test(SynchronousMessages)"
},
"description": "a test message",
"key": "c13464bf",
"pending": false,
"request": {
"contents": {
"content": {
"name": "abcd123"
},
"contentType": "application/json",
"encoded": false
},
"matchingRules": {
"body": {
"$.name": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
}
}
},
"metadata": {
"contentType": "application/json"
}
},
"response": [
{
"contents": {
"content": {
"name": "321dcba"
},
"contentType": "application/json",
"encoded": false
},
"matchingRules": {
"body": {
"$.name": {
"combine": "AND",
"matchers": [
{
"match": "type"
}
]
}
}
},
"metadata": {
"contentType": "application/json"
}
}
],
"type": "Synchronous/Messages"
}
],
"metadata": {
"pact-jvm": {
"version": "4.5.3"
},
"pactSpecification": {
"version": "4.0"
}
},
"provider": {
"name": "KafkaRequestReplyProvider"
}
}

0 comments on commit c07f707

Please sign in to comment.