-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a message metadata builder DSL #1409
- Loading branch information
Ronald Holshausen
committed
Jul 31, 2021
1 parent
647ec0d
commit 08c639e
Showing
4 changed files
with
538 additions
and
31 deletions.
There are no files selected for viewing
70 changes: 39 additions & 31 deletions
70
...unit5/src/test/java/au/com/dius/pact/consumer/junit5/MessageWithMetadataConsumerTest.java
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,49 +1,57 @@ | ||
package au.com.dius.pact.consumer.junit5; | ||
|
||
import au.com.dius.pact.consumer.MessagePactBuilder; | ||
import au.com.dius.pact.core.model.annotations.Pact; | ||
import au.com.dius.pact.consumer.dsl.PactDslJsonBody; | ||
import au.com.dius.pact.core.model.annotations.Pact; | ||
import au.com.dius.pact.core.model.matchingrules.RegexMatcher; | ||
import au.com.dius.pact.core.model.messaging.Message; | ||
import au.com.dius.pact.core.model.messaging.MessagePact; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.util.HashMap; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.empty; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
|
||
@ExtendWith(PactConsumerTestExt.class) | ||
@PactTestFor(providerName = "MessageProvider", providerType = ProviderType.ASYNCH) | ||
public class MessageWithMetadataConsumerTest { | ||
|
||
@Pact(consumer = "test_consumer_v3") | ||
public MessagePact createPact(MessagePactBuilder builder) { | ||
PactDslJsonBody body = new PactDslJsonBody(); | ||
body.stringValue("testParam1", "value1"); | ||
body.stringValue("testParam2", "value2"); | ||
|
||
Map<String, Object> metadata = new HashMap<>(); | ||
metadata.put("metadata1", "metadataValue1"); | ||
metadata.put("metadata2", "metadataValue2"); | ||
metadata.put("metadata3", 10L); | ||
|
||
return builder.given("SomeProviderState") | ||
.expectsToReceive("a test message with metadata") | ||
.withMetadata(metadata) | ||
.withContent(body) | ||
.toPact(); | ||
} | ||
|
||
@Test | ||
void test(List<Message> messages) { | ||
assertTrue(!messages.isEmpty()); | ||
assertTrue(!messages.get(0).getMetaData().isEmpty()); | ||
assertEquals("metadataValue1", messages.get(0).getMetaData().get("metadata1")); | ||
assertEquals("metadataValue2", messages.get(0).getMetaData().get("metadata2")); | ||
assertEquals(10L, messages.get(0).getMetaData().get("metadata3")); | ||
} | ||
|
||
@Pact(consumer = "test_consumer_v3") | ||
public MessagePact createPact(MessagePactBuilder builder) { | ||
PactDslJsonBody body = new PactDslJsonBody(); | ||
body.stringValue("testParam1", "value1"); | ||
body.stringValue("testParam2", "value2"); | ||
|
||
return builder.given("SomeProviderState") | ||
.expectsToReceive("a test message with metadata") | ||
.withMetadata(md -> { | ||
md.add("metadata1", "metadataValue1"); | ||
md.add("metadata2", "metadataValue2"); | ||
md.add("metadata3", 10L); | ||
md.matchRegex("partitionKey", "[A-Z]{3}\\d{2}", "ABC01"); | ||
}) | ||
.withContent(body) | ||
.toPact(); | ||
} | ||
|
||
@Test | ||
void test(List<Message> messages) { | ||
assertThat(messages, is(not(empty()))); | ||
Message message = messages.get(0); | ||
Map<String, Object> metaData = message.getMetaData(); | ||
assertThat(metaData.entrySet(), is(not(empty()))); | ||
assertThat(metaData.get("metadata1"), is("metadataValue1")); | ||
assertThat(metaData.get("metadata2"), is("metadataValue2")); | ||
assertThat(metaData.get("metadata3"), is(10L)); | ||
assertThat(metaData.get("partitionKey"), is("ABC01")); | ||
|
||
assertThat(message.getMatchingRules().rulesForCategory("metadata").allMatchingRules(), | ||
is(Collections.singletonList(new RegexMatcher("[A-Z]{3}\\d{2}")))); | ||
} | ||
} |
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
Oops, something went wrong.