Search in sources :

Example 6 with BasePact

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

the class BaseProviderRule method getPacts.

/**
 * scan all methods for @Pact annotation and execute them, if not already initialized
 * @param fragment
 */
protected Map<String, BasePact> getPacts(String fragment) {
    if (pacts == null) {
        pacts = new HashMap<>();
        for (Method m : target.getClass().getMethods()) {
            if (JUnitTestSupport.conformsToSignature(m, config.getPactVersion()) && methodMatchesFragment(m, fragment)) {
                Pact pactAnnotation = m.getAnnotation(Pact.class);
                String provider = ep.parseExpression(pactAnnotation.provider(), DataType.RAW).toString();
                if (StringUtils.isEmpty(provider) || this.provider.equals(provider)) {
                    PactDslWithProvider dslBuilder = ConsumerPactBuilder.consumer(ep.parseExpression(pactAnnotation.consumer(), DataType.RAW).toString()).pactSpecVersion(config.getPactVersion()).hasPactWith(this.provider);
                    updateAnyDefaultValues(dslBuilder);
                    try {
                        BasePact pact = (BasePact) m.invoke(target, dslBuilder);
                        pacts.put(this.provider, pact);
                    } catch (Exception e) {
                        throw new RuntimeException("Failed to invoke pact method", e);
                    }
                }
            }
        }
    }
    return pacts;
}
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) 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)

Example 7 with BasePact

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

the class BaseProviderRule method apply.

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

        @Override
        public void evaluate() throws Throwable {
            PactVerifications pactVerifications = description.getAnnotation(PactVerifications.class);
            if (pactVerifications != null) {
                evaluatePactVerifications(pactVerifications, base);
                return;
            }
            PactVerification pactDef = description.getAnnotation(PactVerification.class);
            // no pactVerification? execute the test normally
            if (pactDef == null) {
                base.evaluate();
                return;
            }
            Map<String, BasePact> pacts = getPacts(pactDef.fragment());
            Optional<BasePact> pact;
            if (pactDef.value().length == 1 && StringUtils.isEmpty(pactDef.value()[0])) {
                pact = pacts.values().stream().findFirst();
            } else {
                pact = Arrays.stream(pactDef.value()).map(pacts::get).filter(Objects::nonNull).findFirst();
            }
            if (pact.isEmpty()) {
                base.evaluate();
                return;
            }
            if (config.getPactVersion() == PactSpecVersion.V4) {
                pact.get().asV4Pact().component1().getInteractions().forEach(i -> i.getComments().put("testname", Json.toJson(description.getDisplayName())));
            }
            PactFolder pactFolder = target.getClass().getAnnotation(PactFolder.class);
            PactDirectory pactDirectory = target.getClass().getAnnotation(PactDirectory.class);
            BasePact basePact = pact.get();
            Metrics.INSTANCE.sendMetrics(new MetricEvent.ConsumerTestRun(basePact.getInteractions().size(), "junit"));
            PactVerificationResult result = runPactTest(base, basePact, pactFolder, pactDirectory);
            validateResult(result, pactDef);
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) PactDirectory(au.com.dius.pact.core.model.annotations.PactDirectory) MetricEvent(au.com.dius.pact.core.support.MetricEvent) BasePact(au.com.dius.pact.core.model.BasePact) PactVerificationResult(au.com.dius.pact.consumer.PactVerificationResult) Objects(java.util.Objects) 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