Search in sources :

Example 1 with PactFolder

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

Example 2 with PactFolder

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

Example 3 with PactFolder

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

the class MessagePactProviderRule method apply.

/* (non-Javadoc)
	 * @see org.junit.rules.ExternalResource#apply(org.junit.runners.model.Statement, org.junit.runner.Description)
	 */
@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, description);
                return;
            }
            PactVerification pactDef = description.getAnnotation(PactVerification.class);
            // no pactVerification? execute the test normally
            if (pactDef == null) {
                base.evaluate();
                return;
            }
            Message providedMessage = null;
            Map<String, Message> pacts;
            if (StringUtils.isNoneEmpty(pactDef.fragment())) {
                Optional<Method> possiblePactMethod = findPactMethod(pactDef);
                if (possiblePactMethod.isEmpty()) {
                    base.evaluate();
                    return;
                }
                pacts = new HashMap<>();
                Method method = possiblePactMethod.get();
                Pact pact = method.getAnnotation(Pact.class);
                MessagePactBuilder builder = new MessagePactBuilder().consumer(Objects.toString(ep.parseExpression(pact.consumer(), DataType.RAW))).hasPactWith(provider);
                messagePact = (MessagePact) method.invoke(testClassInstance, builder);
                for (Message message : messagePact.getMessages()) {
                    pacts.put(message.getProviderStates().stream().map(ProviderState::getName).collect(Collectors.joining()), message);
                }
            } else {
                pacts = parsePacts();
            }
            if (pactDef.value().length == 2 && !pactDef.value()[1].trim().isEmpty()) {
                providedMessage = pacts.get(pactDef.value()[1].trim());
            } else if (!pacts.isEmpty()) {
                providedMessage = pacts.values().iterator().next();
            }
            if (providedMessage == null) {
                base.evaluate();
                return;
            }
            setMessage(providedMessage, description);
            Metrics.INSTANCE.sendMetrics(new MetricEvent.ConsumerTestRun(messagePact.getMessages().size(), "junit"));
            try {
                base.evaluate();
                PactFolder pactFolder = testClassInstance.getClass().getAnnotation(PactFolder.class);
                PactDirectory pactDirectory = testClassInstance.getClass().getAnnotation(PactDirectory.class);
                if (pactFolder != null) {
                    messagePact.write(pactFolder.value(), PactSpecVersion.V3);
                } else if (pactDirectory != null) {
                    messagePact.write(pactDirectory.value(), PactSpecVersion.V3);
                } else {
                    messagePact.write(BuiltToolConfig.INSTANCE.getPactDirectory(), PactSpecVersion.V3);
                }
            } catch (Throwable t) {
                throw t;
            }
        }
    };
}
Also used : Message(au.com.dius.pact.core.model.messaging.Message) Statement(org.junit.runners.model.Statement) MessagePact(au.com.dius.pact.core.model.messaging.MessagePact) Pact(au.com.dius.pact.core.model.annotations.Pact) MetricEvent(au.com.dius.pact.core.support.MetricEvent) PactDirectory(au.com.dius.pact.core.model.annotations.PactDirectory) MessagePactBuilder(au.com.dius.pact.consumer.MessagePactBuilder) Method(java.lang.reflect.Method) PactFolder(au.com.dius.pact.core.model.annotations.PactFolder) ProviderState(au.com.dius.pact.core.model.ProviderState)

Aggregations

PactDirectory (au.com.dius.pact.core.model.annotations.PactDirectory)3 PactFolder (au.com.dius.pact.core.model.annotations.PactFolder)3 PactVerificationResult (au.com.dius.pact.consumer.PactVerificationResult)2 BasePact (au.com.dius.pact.core.model.BasePact)2 Pact (au.com.dius.pact.core.model.annotations.Pact)2 MetricEvent (au.com.dius.pact.core.support.MetricEvent)2 Method (java.lang.reflect.Method)2 Statement (org.junit.runners.model.Statement)2 MessagePactBuilder (au.com.dius.pact.consumer.MessagePactBuilder)1 PactDslWithProvider (au.com.dius.pact.consumer.dsl.PactDslWithProvider)1 ProviderState (au.com.dius.pact.core.model.ProviderState)1 RequestResponsePact (au.com.dius.pact.core.model.RequestResponsePact)1 Message (au.com.dius.pact.core.model.messaging.Message)1 MessagePact (au.com.dius.pact.core.model.messaging.MessagePact)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Objects (java.util.Objects)1