Search in sources :

Example 1 with RegexMatcher

use of au.com.dius.pact.core.model.matchingrules.RegexMatcher in project pact-jvm by DiUS.

the class MessageWithMetadataConsumerTest method test.

@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}"))));
}
Also used : Message(au.com.dius.pact.core.model.messaging.Message) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) Test(org.junit.jupiter.api.Test)

Example 2 with RegexMatcher

use of au.com.dius.pact.core.model.matchingrules.RegexMatcher in project pact-jvm by DiUS.

the class Defect266Test method getUsersFragment2.

@Pact(provider = "266_provider", consumer = "test_consumer")
public RequestResponsePact getUsersFragment2(PactDslWithProvider builder) {
    DslPart body = new PactDslJsonArray().minArrayLike(5).uuid("id").stringType("userName").stringType("email").closeObject();
    RequestResponsePact pact = builder.given("a user with an id named 'user' exists").uponReceiving("get all users for min").path("/idm/user").method("GET").willRespondWith().status(200).body(body).toPact();
    MatchingRules matchingRules = pact.getInteractions().get(0).asSynchronousRequestResponse().getResponse().getMatchingRules();
    Map<String, MatchingRuleGroup> bodyMatchingRules = matchingRules.rulesForCategory("body").getMatchingRules();
    assertThat(bodyMatchingRules.keySet(), is(equalTo(Sets.newHashSet("$[0][*].userName", "$[0][*].id", "$[0]", "$[0][*].email"))));
    assertThat(bodyMatchingRules.get("$[0][*].userName").getRules().get(0), is(equalTo(TypeMatcher.INSTANCE)));
    assertThat(bodyMatchingRules.get("$[0][*].id").getRules().get(0), is(equalTo(new RegexMatcher("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"))));
    assertThat(bodyMatchingRules.get("$[0]").getRules().get(0), is(equalTo(new MinTypeMatcher(5))));
    assertThat(bodyMatchingRules.get("$[0][*].email").getRules().get(0), is(equalTo(TypeMatcher.INSTANCE)));
    return pact;
}
Also used : MinTypeMatcher(au.com.dius.pact.core.model.matchingrules.MinTypeMatcher) MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) MatchingRules(au.com.dius.pact.core.model.matchingrules.MatchingRules) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) DslPart(au.com.dius.pact.consumer.dsl.DslPart) Pact(au.com.dius.pact.core.model.annotations.Pact) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact)

Example 3 with RegexMatcher

use of au.com.dius.pact.core.model.matchingrules.RegexMatcher in project pact-jvm by DiUS.

the class Defect266Test method getUsersFragment.

@Pact(provider = "266_provider", consumer = "test_consumer")
public RequestResponsePact getUsersFragment(PactDslWithProvider builder) {
    DslPart body = new PactDslJsonArray().maxArrayLike(5).uuid("id").stringType("userName").stringType("email").closeObject();
    RequestResponsePact pact = builder.given("a user with an id named 'user' exists").uponReceiving("get all users for max").path("/idm/user").method("GET").willRespondWith().status(200).body(body).toPact();
    MatchingRules matchingRules = pact.getInteractions().get(0).asSynchronousRequestResponse().getResponse().getMatchingRules();
    Map<String, MatchingRuleGroup> bodyMatchingRules = matchingRules.rulesForCategory("body").getMatchingRules();
    assertThat(bodyMatchingRules.keySet(), is(equalTo(Sets.newHashSet("$[0][*].userName", "$[0][*].id", "$[0]", "$[0][*].email"))));
    assertThat(bodyMatchingRules.get("$[0][*].userName").getRules().get(0), is(equalTo(TypeMatcher.INSTANCE)));
    assertThat(bodyMatchingRules.get("$[0][*].id").getRules().get(0), is(equalTo(new RegexMatcher("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"))));
    assertThat(bodyMatchingRules.get("$[0]").getRules().get(0), is(equalTo(new MaxTypeMatcher(5))));
    assertThat(bodyMatchingRules.get("$[0][*].email").getRules().get(0), is(equalTo(TypeMatcher.INSTANCE)));
    return pact;
}
Also used : MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) MaxTypeMatcher(au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) MatchingRules(au.com.dius.pact.core.model.matchingrules.MatchingRules) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) DslPart(au.com.dius.pact.consumer.dsl.DslPart) Pact(au.com.dius.pact.core.model.annotations.Pact) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact)

Example 4 with RegexMatcher

use of au.com.dius.pact.core.model.matchingrules.RegexMatcher in project pact-jvm by DiUS.

the class PactDslRootValueTest method rootValueTest.

@Test
public void rootValueTest() {
    PactDslRootValue rootValueBody = new PactDslRootValue();
    rootValueBody.setValue("Brent");
    rootValueBody.setMatcher(new RegexMatcher(".{5}"));
    PactDslWithProvider dsl = new PactDslWithProvider(new ConsumerPactBuilder("consumer"), "provider");
    Map<String, String> headers = new HashMap<String, String>() {

        {
            put("Content-Type", "text/plain");
        }
    };
    RequestResponsePact frag = dsl.given("I am testing root values").uponReceiving("A request for text/plain").path("/some/blah/path").headers(headers).willRespondWith().headers(headers).status(200).body(rootValueBody).toPact().asRequestResponsePact().component1();
    Assert.assertEquals(1, frag.getInteractions().size());
    assertThat(frag.getInteractions().get(0).asSynchronousRequestResponse().getResponse().getBody().valueAsString(), is("Brent"));
    Map<String, MatchingRuleGroup> matchingGroups = frag.getInteractions().get(0).asSynchronousRequestResponse().getResponse().getMatchingRules().rulesForCategory("body").getMatchingRules();
    List<MatchingRule> rules = matchingGroups.get("$").getRules();
    Assert.assertEquals(1, rules.size());
    Assert.assertEquals(".{5}", rules.get(0).toMap(PactSpecVersion.V3).get("regex"));
}
Also used : MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) PactDslRootValue(au.com.dius.pact.consumer.dsl.PactDslRootValue) HashMap(java.util.HashMap) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) RegexMatcher(au.com.dius.pact.core.model.matchingrules.RegexMatcher) PactDslWithProvider(au.com.dius.pact.consumer.dsl.PactDslWithProvider) MatchingRule(au.com.dius.pact.core.model.matchingrules.MatchingRule) Test(org.junit.Test)

Aggregations

RegexMatcher (au.com.dius.pact.core.model.matchingrules.RegexMatcher)4 RequestResponsePact (au.com.dius.pact.core.model.RequestResponsePact)3 MatchingRuleGroup (au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup)3 DslPart (au.com.dius.pact.consumer.dsl.DslPart)2 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)2 Pact (au.com.dius.pact.core.model.annotations.Pact)2 MatchingRules (au.com.dius.pact.core.model.matchingrules.MatchingRules)2 PactDslRootValue (au.com.dius.pact.consumer.dsl.PactDslRootValue)1 PactDslWithProvider (au.com.dius.pact.consumer.dsl.PactDslWithProvider)1 MatchingRule (au.com.dius.pact.core.model.matchingrules.MatchingRule)1 MaxTypeMatcher (au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher)1 MinTypeMatcher (au.com.dius.pact.core.model.matchingrules.MinTypeMatcher)1 Message (au.com.dius.pact.core.model.messaging.Message)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1