Search in sources :

Example 1 with RequestResponsePact

use of au.com.dius.pact.model.RequestResponsePact in project pact-jvm by DiUS.

the class Defect266Test method getUsersFragment.

@Pact(provider = "266_provider", consumer = "test_consumer")
public RequestResponsePact getUsersFragment(PactDslWithProvider builder) {
    Map<String, Map<String, Object>> matchers = (Map<String, Map<String, Object>>) new JsonSlurper().parseText("{" + "\"$.body[0][*].userName\": {\"match\": \"type\"}," + "\"$.body[0][*].id\": {\"match\": \"regex\", \"regex\": \"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\"}," + "\"$.body[0]\": {\"match\": \"type\", \"max\": 5}," + "\"$.body[0][*].email\": {\"match\": \"type\"}" + "}");
    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();
    assertThat(pact.getInteractions().get(0).getResponse().getMatchingRules(), is(equalTo(matchers)));
    return pact;
}
Also used : JsonSlurper(groovy.json.JsonSlurper) PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) DslPart(au.com.dius.pact.consumer.dsl.DslPart) Map(java.util.Map) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact)

Example 2 with RequestResponsePact

use of au.com.dius.pact.model.RequestResponsePact in project pact-jvm by DiUS.

the class Defect266Test method getUsersFragment2.

@Pact(provider = "266_provider", consumer = "test_consumer")
public RequestResponsePact getUsersFragment2(PactDslWithProvider builder) {
    Map<String, Map<String, Object>> matchers = (Map<String, Map<String, Object>>) new JsonSlurper().parseText("{" + "\"$.body[0][*].userName\": {\"match\": \"type\"}," + "\"$.body[0][*].id\": {\"match\": \"regex\", \"regex\": \"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\"}," + "\"$.body[0]\": {\"match\": \"type\", \"min\": 5}," + "\"$.body[0][*].email\": {\"match\": \"type\"}" + "}");
    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();
    assertThat(pact.getInteractions().get(0).getResponse().getMatchingRules(), is(equalTo(matchers)));
    return pact;
}
Also used : JsonSlurper(groovy.json.JsonSlurper) PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) DslPart(au.com.dius.pact.consumer.dsl.DslPart) Map(java.util.Map) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact)

Example 3 with RequestResponsePact

use of au.com.dius.pact.model.RequestResponsePact in project pact-jvm by DiUS.

the class PactDslJsonArrayTemplateTest method createPact.

@Override
protected RequestResponsePact createPact(PactDslWithProvider builder) {
    DslPart personTemplate = new PactDslJsonBody().id().stringType("name").date("dob");
    DslPart body = new PactDslJsonArray().template(personTemplate, 3);
    RequestResponsePact pact = builder.uponReceiving("java test interaction with a DSL array body with templates").path("/").method("GET").willRespondWith().status(200).body(body).toPact();
    MatcherTestUtils.assertResponseMatcherKeysEqualTo(pact, "$.body[0].id", "$.body[0].name", "$.body[0].dob", "$.body[1].id", "$.body[1].name", "$.body[1].dob", "$.body[2].id", "$.body[2].name", "$.body[2].dob");
    return pact;
}
Also used : PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) DslPart(au.com.dius.pact.consumer.dsl.DslPart)

Example 4 with RequestResponsePact

use of au.com.dius.pact.model.RequestResponsePact in project pact-jvm by DiUS.

the class BaseProviderRule method conformsToSignature.

/**
   * validates method signature as described at {@link Pact}
   */
private boolean conformsToSignature(Method m) {
    Pact pact = m.getAnnotation(Pact.class);
    boolean conforms = pact != null && RequestResponsePact.class.isAssignableFrom(m.getReturnType()) && m.getParameterTypes().length == 1 && m.getParameterTypes()[0].isAssignableFrom(PactDslWithProvider.class);
    if (!conforms && pact != null) {
        throw new UnsupportedOperationException("Method " + m.getName() + " does not conform required method signature 'public RequestResponsePact xxx(PactDslWithProvider builder)'");
    }
    return conforms;
}
Also used : RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) PactDslWithProvider(au.com.dius.pact.consumer.dsl.PactDslWithProvider)

Example 5 with RequestResponsePact

use of au.com.dius.pact.model.RequestResponsePact in project pact-jvm by DiUS.

the class DirectDSLConsumerPactTest method testPact.

@Test
public void testPact() {
    RequestResponsePact pact = ConsumerPactBuilder.consumer("Some Consumer").hasPactWith("Some Provider").uponReceiving("a request to say Hello").path("/hello").method("POST").body("{\"name\": \"harry\"}").willRespondWith().status(200).body("{\"hello\": \"harry\"}").toPact();
    MockProviderConfig config = MockProviderConfig.createDefault();
    PactVerificationResult result = runConsumerTest(pact, config, mockServer -> {
        Map expectedResponse = new HashMap();
        expectedResponse.put("hello", "harry");
        try {
            assertEquals(new ProviderClient(mockServer.getUrl()).hello("{\"name\": \"harry\"}"), expectedResponse);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    if (result instanceof PactVerificationResult.Error) {
        throw new RuntimeException(((PactVerificationResult.Error) result).getError());
    }
    assertEquals(PactVerificationResult.Ok.INSTANCE, result);
}
Also used : ProviderClient(au.com.dius.pact.consumer.exampleclients.ProviderClient) HashMap(java.util.HashMap) MockProviderConfig(au.com.dius.pact.model.MockProviderConfig) RequestResponsePact(au.com.dius.pact.model.RequestResponsePact) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) PactVerificationResult(au.com.dius.pact.consumer.PactVerificationResult) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Aggregations

RequestResponsePact (au.com.dius.pact.model.RequestResponsePact)13 DslPart (au.com.dius.pact.consumer.dsl.DslPart)7 Map (java.util.Map)5 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)4 PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)4 MockProviderConfig (au.com.dius.pact.model.MockProviderConfig)4 ConsumerPactRunnerKt.runConsumerTest (au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 PactDslWithProvider (au.com.dius.pact.consumer.dsl.PactDslWithProvider)2 JsonSlurper (groovy.json.JsonSlurper)2 HashMap (java.util.HashMap)2 PactVerificationResult (au.com.dius.pact.consumer.PactVerificationResult)1 ProviderClient (au.com.dius.pact.consumer.exampleclients.ProviderClient)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1