Search in sources :

Example 1 with BasePact

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

the class PactQueryParameterTest method testMatchQueryWithComplexQueryParameter.

@Test
public void testMatchQueryWithComplexQueryParameter() throws Throwable {
    // Given a pact expecting GET /hello?q=query%20containing%20%26%20and%20%3F%20characters
    // created using the .matchQuery(...) method
    String path = "/hello";
    String parameterName = "q";
    String decodedValue = "query containing & and ? characters";
    String encodedValue = "query%20containing%20%26%20and%20%3F%20characters";
    String encodedFullPath = path + "?" + parameterName + "=" + encodedValue;
    BasePact pact = ConsumerPactBuilder.consumer("Some Consumer").hasPactWith("Some Provider").uponReceiving(encodedFullPath).path(path).matchQuery(parameterName, ".+", decodedValue).method("GET").willRespondWith().status(200).toPact();
    // When sending the request, we expect no errors
    verifyRequestMatches(pact, encodedFullPath);
}
Also used : BasePact(au.com.dius.pact.core.model.BasePact) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 2 with BasePact

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

the class PactQueryParameterTest method testMatchQueryWithSimpleQueryParameter.

@Test
public void testMatchQueryWithSimpleQueryParameter() throws Throwable {
    // Given a pact expecting GET /hello?q=simple created using the .matchQuery(...) method
    String path = "/hello";
    String parameterName = "q";
    String decodedValue = "simple";
    String encodedValue = "simple";
    String encodedFullPath = path + "?" + parameterName + "=" + encodedValue;
    BasePact pact = ConsumerPactBuilder.consumer("Some Consumer").hasPactWith("Some Provider").uponReceiving(encodedFullPath).path(path).matchQuery(parameterName, ".+", decodedValue).method("GET").willRespondWith().status(200).toPact();
    // When sending the request, we expect no errors
    verifyRequestMatches(pact, encodedFullPath);
}
Also used : BasePact(au.com.dius.pact.core.model.BasePact) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 3 with BasePact

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

the class PactQueryParameterTest method testEncodedQueryWithComplexQueryParameter.

@Test
public void testEncodedQueryWithComplexQueryParameter() throws Throwable {
    // Given a pact expecting GET /hello?q=query%20containing%20%26%20and%20%3F%20characters,
    // created using the .query(...) method
    String path = "/hello";
    String parameterName = "q";
    String encodedValue = "query%20containing%20%26%20and%20%3F%20characters";
    String encodedQuery = parameterName + "=" + encodedValue;
    String encodedFullPath = path + "?" + encodedQuery;
    BasePact pact = ConsumerPactBuilder.consumer("Some Consumer").hasPactWith("Some Provider").uponReceiving(encodedFullPath).path(path).encodedQuery(encodedQuery).method("GET").willRespondWith().status(200).toPact();
    // When sending the request, we expect no errors
    verifyRequestMatches(pact, encodedFullPath);
}
Also used : BasePact(au.com.dius.pact.core.model.BasePact) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 4 with BasePact

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

the class PactQueryParameterTest method testEncodedQueryWithSimpleQueryParameter.

@Test
public void testEncodedQueryWithSimpleQueryParameter() throws Throwable {
    // Given a pact expecting GET /hello?q=simple, created using the .query(...) method
    String path = "/hello";
    String parameterName = "q";
    String encodedValue = "simple";
    String encodedQuery = parameterName + "=" + encodedValue;
    String encodedFullPath = path + "?" + encodedQuery;
    BasePact pact = ConsumerPactBuilder.consumer("Some Consumer").hasPactWith("Some Provider").uponReceiving(encodedFullPath).path(path).encodedQuery(encodedQuery).method("GET").willRespondWith().status(200).toPact();
    // When sending the request, we expect no errors
    verifyRequestMatches(pact, encodedFullPath);
}
Also used : BasePact(au.com.dius.pact.core.model.BasePact) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 5 with BasePact

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

the class BaseProviderRule method evaluatePactVerifications.

private void evaluatePactVerifications(PactVerifications pactVerifications, Statement base) throws Throwable {
    List<PactVerification> possiblePactVerifications = findPactVerifications(pactVerifications, this.provider);
    if (possiblePactVerifications.isEmpty()) {
        base.evaluate();
        return;
    }
    final BasePact[] pact = { null };
    possiblePactVerifications.forEach(pactVerification -> {
        Optional<Method> possiblePactMethod = findPactMethod(pactVerification);
        if (possiblePactMethod.isEmpty()) {
            throw new UnsupportedOperationException("Could not find method with @Pact for the provider " + provider);
        }
        Method method = possiblePactMethod.get();
        Pact pactAnnotation = method.getAnnotation(Pact.class);
        PactDslWithProvider dslBuilder = ConsumerPactBuilder.consumer(ep.parseExpression(pactAnnotation.consumer(), DataType.RAW).toString()).pactSpecVersion(config.getPactVersion()).hasPactWith(provider);
        updateAnyDefaultValues(dslBuilder);
        try {
            BasePact pactFromMethod = (BasePact) method.invoke(target, dslBuilder);
            if (pact[0] == null) {
                pact[0] = pactFromMethod;
            } else {
                pact[0].mergeInteractions(pactFromMethod.getInteractions());
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to invoke pact method", e);
        }
    });
    PactFolder pactFolder = target.getClass().getAnnotation(PactFolder.class);
    PactDirectory pactDirectory = target.getClass().getAnnotation(PactDirectory.class);
    PactVerificationResult result = runPactTest(base, pact[0], pactFolder, pactDirectory);
    JUnitTestSupport.validateMockServerResult(result);
}
Also used : BasePact(au.com.dius.pact.core.model.BasePact) Pact(au.com.dius.pact.core.model.annotations.Pact) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact) PactDirectory(au.com.dius.pact.core.model.annotations.PactDirectory) Method(java.lang.reflect.Method) PactDslWithProvider(au.com.dius.pact.consumer.dsl.PactDslWithProvider) InvocationTargetException(java.lang.reflect.InvocationTargetException) BasePact(au.com.dius.pact.core.model.BasePact) PactVerificationResult(au.com.dius.pact.consumer.PactVerificationResult) PactFolder(au.com.dius.pact.core.model.annotations.PactFolder)

Aggregations

BasePact (au.com.dius.pact.core.model.BasePact)7 ConsumerPactRunnerKt.runConsumerTest (au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest)4 Test (org.junit.Test)4 PactVerificationResult (au.com.dius.pact.consumer.PactVerificationResult)2 PactDslWithProvider (au.com.dius.pact.consumer.dsl.PactDslWithProvider)2 RequestResponsePact (au.com.dius.pact.core.model.RequestResponsePact)2 Pact (au.com.dius.pact.core.model.annotations.Pact)2 PactDirectory (au.com.dius.pact.core.model.annotations.PactDirectory)2 PactFolder (au.com.dius.pact.core.model.annotations.PactFolder)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 MetricEvent (au.com.dius.pact.core.support.MetricEvent)1 Objects (java.util.Objects)1 Statement (org.junit.runners.model.Statement)1