Search in sources :

Example 1 with PactFragment

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

the class PerfTest method test.

@Test
public void test() {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    // Define the test data:
    String path = "/mypath/abc/";
    //Header data:
    Map<String, String> headerData = new HashMap<String, String>();
    headerData.put("Content-Type", "application/json");
    // Put as JSON object:
    JSONObject bodyExpected = new JSONObject();
    bodyExpected.put("name", "myName");
    stopWatch.split();
    System.out.println("Setup: " + stopWatch.getSplitTime());
    PactFragment pactFragment = ConsumerPactBuilder.consumer("perf_test_consumer").hasPactWith("perf_test_provider").uponReceiving("a request to get values").path(path).method("GET").willRespondWith().status(200).headers(headerData).body(bodyExpected).toFragment();
    stopWatch.split();
    System.out.println("Setup Fragment: " + stopWatch.getSplitTime());
    MockProviderConfig config = MockProviderConfig.createDefault();
    PactVerificationResult result = runConsumerTest(pactFragment.toPact(), config, new PactTestRun() {

        @Override
        public void run(@NotNull MockServer mockServer) throws IOException {
            try {
                stopWatch.split();
                System.out.println("In Test: " + stopWatch.getSplitTime());
                new ConsumerClient(config.url()).getAsList(path);
            } catch (IOException e) {
            }
            stopWatch.split();
            System.out.println("After Test: " + stopWatch.getSplitTime());
        }
    });
    stopWatch.split();
    System.out.println("End of Test: " + stopWatch.getSplitTime());
    stopWatch.stop();
    System.out.println(stopWatch.toString());
}
Also used : PactFragment(au.com.dius.pact.model.PactFragment) HashMap(java.util.HashMap) MockProviderConfig(au.com.dius.pact.model.MockProviderConfig) IOException(java.io.IOException) StopWatch(org.apache.commons.lang3.time.StopWatch) JSONObject(org.json.JSONObject) ConsumerPactRunnerKt.runConsumerTest(au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest) Test(org.junit.Test)

Example 2 with PactFragment

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

the class PactRule method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            PactVerification pactDef = description.getAnnotation(PactVerification.class);
            //no pactVerification? execute the test normally
            if (pactDef == null) {
                base.evaluate();
                return;
            }
            PactFragment fragment = getPacts().get(pactDef.value());
            if (fragment == null) {
                throw new UnsupportedOperationException("Fragment not found: " + pactDef.value());
            }
            VerificationResult result = fragment.runConsumer(config, new TestRun() {

                @Override
                public void run(MockProviderConfig config) throws Throwable {
                    base.evaluate();
                }
            });
            if (!result.equals(PACT_VERIFIED)) {
                if (result instanceof PactError) {
                    throw new RuntimeException(((PactError) result).error());
                }
                if (result instanceof UserCodeFailed) {
                    throw new RuntimeException(((UserCodeFailed<RuntimeException>) result).error());
                }
                if (result instanceof PactMismatch) {
                    PactMismatch mismatch = (PactMismatch) result;
                    throw new PactMismatchException(mismatch);
                }
            }
        }
    };
}
Also used : PactFragment(au.com.dius.pact.model.PactFragment) Statement(org.junit.runners.model.Statement) MockProviderConfig(au.com.dius.pact.model.MockProviderConfig)

Example 3 with PactFragment

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

the class PactRule method getPacts.

/**
     * scan all methods for @Pact annotation and execute them, if not already initialized
     * @return
     */
protected Map<String, PactFragment> getPacts() {
    if (fragments == null) {
        fragments = new HashMap<String, PactFragment>();
        for (Method m : target.getClass().getMethods()) {
            if (conformsToSigniture(m)) {
                Pact pact = m.getAnnotation(Pact.class);
                PactDslWithState dslBuilder = ConsumerPactBuilder.consumer(pact.consumer()).hasPactWith(pact.provider()).given(pact.state());
                try {
                    fragments.put(pact.state(), (PactFragment) m.invoke(target, dslBuilder));
                } catch (Exception e) {
                    LOGGER.error("Failed to invoke pact method", e);
                    throw new RuntimeException("Failed to invoke pact method", e);
                }
            }
        }
    }
    return fragments;
}
Also used : PactFragment(au.com.dius.pact.model.PactFragment) PactDslWithState(au.com.dius.pact.consumer.dsl.PactDslWithState) Method(java.lang.reflect.Method)

Example 4 with PactFragment

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

the class ConsumerPactTest method testPact.

@Test
public void testPact() throws Throwable {
    PactFragment fragment = createFragment(ConsumerPactBuilder.consumer(consumerName()).hasPactWith(providerName()));
    final MockProviderConfig config = MockProviderConfig.createDefault(getSpecificationVersion());
    VerificationResult result = fragment.runConsumer(config, config1 -> runTest(config1.url()));
    if (!result.equals(PACT_VERIFIED)) {
        if (result instanceof PactError) {
            throw ((PactError) result).error();
        }
        if (result instanceof UserCodeFailed) {
            throw ((UserCodeFailed<RuntimeException>) result).error();
        }
        if (result instanceof PactMismatch) {
            PactMismatch mismatch = (PactMismatch) result;
            throw new PactMismatchException(mismatch);
        }
    }
}
Also used : PactFragment(au.com.dius.pact.model.PactFragment) MockProviderConfig(au.com.dius.pact.model.MockProviderConfig) Test(org.junit.Test)

Example 5 with PactFragment

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

the class PactProviderRule method evaluatePactVerifications.

private void evaluatePactVerifications(PactVerifications pactVerifications, Statement base) throws Throwable {
    Optional<PactVerification> possiblePactVerification = findPactVerification(pactVerifications);
    if (!possiblePactVerification.isPresent()) {
        base.evaluate();
        return;
    }
    PactVerification pactVerification = possiblePactVerification.get();
    Optional<Method> possiblePactMethod = findPactMethod(pactVerification);
    if (!possiblePactMethod.isPresent()) {
        throw new UnsupportedOperationException("Could not find method with @Pact for the provider " + provider);
    }
    Method method = possiblePactMethod.get();
    Pact pact = method.getAnnotation(Pact.class);
    PactDslWithProvider dslBuilder = ConsumerPactBuilder.consumer(pact.consumer()).hasPactWith(provider);
    PactFragment pactFragment;
    try {
        pactFragment = (PactFragment) method.invoke(target, dslBuilder);
    } catch (Exception e) {
        throw new RuntimeException("Failed to invoke pact method", e);
    }
    VerificationResult result = runPactTest(base, pactFragment);
    validateResult(result, pactVerification);
}
Also used : PactFragment(au.com.dius.pact.model.PactFragment) Method(java.lang.reflect.Method) PactDslWithProvider(au.com.dius.pact.consumer.dsl.PactDslWithProvider) SocketException(java.net.SocketException)

Aggregations

PactFragment (au.com.dius.pact.model.PactFragment)5 MockProviderConfig (au.com.dius.pact.model.MockProviderConfig)3 Method (java.lang.reflect.Method)2 Test (org.junit.Test)2 ConsumerPactRunnerKt.runConsumerTest (au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest)1 PactDslWithProvider (au.com.dius.pact.consumer.dsl.PactDslWithProvider)1 PactDslWithState (au.com.dius.pact.consumer.dsl.PactDslWithState)1 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1 HashMap (java.util.HashMap)1 StopWatch (org.apache.commons.lang3.time.StopWatch)1 JSONObject (org.json.JSONObject)1 Statement (org.junit.runners.model.Statement)1