Search in sources :

Example 1 with MockProviderConfig

use of au.com.dius.pact.model.MockProviderConfig 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 MockProviderConfig

use of au.com.dius.pact.model.MockProviderConfig 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 MockProviderConfig

use of au.com.dius.pact.model.MockProviderConfig 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 4 with MockProviderConfig

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

Example 5 with MockProviderConfig

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

the class MimeTypeTest method runTest.

private void runTest(RequestResponsePact pact, final String body, final String expectedResponse, final ContentType mimeType) {
    MockProviderConfig config = MockProviderConfig.createDefault(PactSpecVersion.V2);
    PactVerificationResult result = runConsumerTest(pact, config, new PactTestRun() {

        @Override
        public void run(@NotNull MockServer mockServer) throws IOException {
            try {
                assertEquals(new ConsumerClient(config.url()).postBody("/hello", body, mimeType), expectedResponse);
            } catch (IOException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    });
    if (result instanceof PactVerificationResult.Error) {
        throw new RuntimeException(((PactVerificationResult.Error) result).getError());
    }
    Assert.assertEquals(PactVerificationResult.Ok.INSTANCE, result);
}
Also used : MockProviderConfig(au.com.dius.pact.model.MockProviderConfig) IOException(java.io.IOException)

Aggregations

MockProviderConfig (au.com.dius.pact.model.MockProviderConfig)9 IOException (java.io.IOException)6 Test (org.junit.Test)5 ConsumerPactRunnerKt.runConsumerTest (au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest)4 RequestResponsePact (au.com.dius.pact.model.RequestResponsePact)4 PactFragment (au.com.dius.pact.model.PactFragment)3 HashMap (java.util.HashMap)3 Map (java.util.Map)2 PactVerificationResult (au.com.dius.pact.consumer.PactVerificationResult)1 ProviderClient (au.com.dius.pact.consumer.exampleclients.ProviderClient)1 URL (java.net.URL)1 StopWatch (org.apache.commons.lang3.time.StopWatch)1 JSONObject (org.json.JSONObject)1 Statement (org.junit.runners.model.Statement)1