Skip to content

Commit

Permalink
feat: add a message metadata builder DSL #1409
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jul 31, 2021
1 parent 647ec0d commit 08c639e
Show file tree
Hide file tree
Showing 4 changed files with 538 additions and 31 deletions.
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}"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package au.com.dius.pact.consumer

import au.com.dius.pact.consumer.dsl.DslPart
import au.com.dius.pact.consumer.dsl.Matcher
import au.com.dius.pact.consumer.dsl.MetadataBuilder
import au.com.dius.pact.consumer.xml.PactXmlBuilder
import au.com.dius.pact.core.model.Consumer
import au.com.dius.pact.core.model.ContentType
import au.com.dius.pact.core.model.InvalidPactException
import au.com.dius.pact.core.model.OptionalBody
import au.com.dius.pact.core.model.Provider
import au.com.dius.pact.core.model.ProviderState
import au.com.dius.pact.core.model.generators.Category
import au.com.dius.pact.core.model.messaging.Message
import au.com.dius.pact.core.model.messaging.MessagePact

Expand Down Expand Up @@ -116,6 +118,23 @@ class MessagePactBuilder(
return this
}

/**
* Adds the expected metadata to the message using a builder
*/
fun withMetadata(consumer: java.util.function.Consumer<MetadataBuilder>): MessagePactBuilder {
if (messages.isEmpty()) {
throw InvalidPactException("expectsToReceive is required before withMetaData")
}

val message = messages.last()
val metadataBuilder = MetadataBuilder()
consumer.accept(metadataBuilder)
message.metaData = metadataBuilder.values
message.matchingRules.addCategory(metadataBuilder.matchers)
message.generators.addGenerators(Category.METADATA, metadataBuilder.generators)
return this
}

/**
* Adds the JSON body as the message content
*/
Expand Down
Loading

0 comments on commit 08c639e

Please sign in to comment.