Search in sources :

Example 1 with MatchingRuleGroup

use of au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup 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 2 with MatchingRuleGroup

use of au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup 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 3 with MatchingRuleGroup

use of au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup 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)

Example 4 with MatchingRuleGroup

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

the class EventsRepositoryDictionaryNestedArrayConsumerTest method createPact.

@Pact(provider = "EventsProvider", consumer = "EventsConsumerDictionaryNestedArray")
public RequestResponsePact createPact(PactDslWithProvider builder) {
    DslPart body = new PactDslJsonBody().object("events").eachKeyMappedToAnArrayLike(// broken, see pact-jvm issue 401
    "ant").stringType("title", "ant");
    RequestResponsePact pact = builder.given("initialStateForEventsTest").uponReceiving("a request to get events keyed by title").path("/dictionaryNestedArray").headers("Accept", ContentType.APPLICATION_JSON.toString()).method("GET").willRespondWith().status(200).body(body).toPact();
    MatcherTestUtils.assertResponseMatcherKeysEqualTo(pact, "body", "$.events", "$.events.*[*].title");
    HashMap<String, MatchingRuleGroup> matchingRules = new HashMap<>();
    matchingRules.put("$.events", new MatchingRuleGroup(Collections.singletonList(ValuesMatcher.INSTANCE)));
    matchingRules.put("$.events.*[*].title", new MatchingRuleGroup(Collections.singletonList(TypeMatcher.INSTANCE)));
    assertThat(pact.getInteractions().get(0).asSynchronousRequestResponse().getResponse().getMatchingRules().rulesForCategory("body").getMatchingRules(), is(equalTo(matchingRules)));
    return pact;
}
Also used : MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) HashMap(java.util.HashMap) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) 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 5 with MatchingRuleGroup

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

the class MatcherTestUtils method assertMessageMatcherKeysEqualTo.

public static void assertMessageMatcherKeysEqualTo(MessagePact messagePact, String category, String... matcherKeys) {
    MatchingRules matchingRules = messagePact.getMessages().get(0).getMatchingRules();
    Map<String, MatchingRuleGroup> matchers = matchingRules.rulesForCategory(category).getMatchingRules();
    assertEquals(asSet(matcherKeys), new TreeSet<String>(matchers.keySet()));
}
Also used : MatchingRuleGroup(au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup) MatchingRules(au.com.dius.pact.core.model.matchingrules.MatchingRules)

Aggregations

MatchingRuleGroup (au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup)6 RequestResponsePact (au.com.dius.pact.core.model.RequestResponsePact)4 MatchingRules (au.com.dius.pact.core.model.matchingrules.MatchingRules)4 DslPart (au.com.dius.pact.consumer.dsl.DslPart)3 Pact (au.com.dius.pact.core.model.annotations.Pact)3 RegexMatcher (au.com.dius.pact.core.model.matchingrules.RegexMatcher)3 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)2 HashMap (java.util.HashMap)2 PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)1 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 Test (org.junit.Test)1