Search in sources :

Example 1 with MessagePactBuilder

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

Example 2 with MessagePactBuilder

use of au.com.dius.pact.consumer.MessagePactBuilder in project pact-jvm by DiUS.

the class MessagePactProviderRule method parsePacts.

@SuppressWarnings("unchecked")
private Map<String, Message> parsePacts() {
    if (providerStateMessages == null) {
        providerStateMessages = new HashMap<>();
        for (Method m : testClassInstance.getClass().getMethods()) {
            if (conformsToSignature(m)) {
                Pact pact = m.getAnnotation(Pact.class);
                if (pact != null) {
                    String provider = Objects.toString(ep.parseExpression(pact.provider(), DataType.RAW));
                    if (provider != null && !provider.trim().isEmpty()) {
                        MessagePactBuilder builder = new MessagePactBuilder().consumer(pact.consumer()).hasPactWith(provider);
                        List<Message> messages;
                        try {
                            messagePact = (MessagePact) m.invoke(testClassInstance, builder);
                            messages = messagePact.getMessages();
                        } catch (Exception e) {
                            throw new RuntimeException("Failed to invoke pact method", e);
                        }
                        for (Message message : messages) {
                            if (message.getProviderStates().isEmpty()) {
                                providerStateMessages.put("", message);
                            } else {
                                for (ProviderState state : message.getProviderStates()) {
                                    providerStateMessages.put(state.getName(), message);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return providerStateMessages;
}
Also used : Message(au.com.dius.pact.core.model.messaging.Message) MessagePact(au.com.dius.pact.core.model.messaging.MessagePact) Pact(au.com.dius.pact.core.model.annotations.Pact) MessagePactBuilder(au.com.dius.pact.consumer.MessagePactBuilder) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProviderState(au.com.dius.pact.core.model.ProviderState)

Example 3 with MessagePactBuilder

use of au.com.dius.pact.consumer.MessagePactBuilder in project pact-jvm by DiUS.

the class MessagePactProviderRule method conformsToSignature.

/**
 * validates method signature as described at {@link Pact}
 */
private boolean conformsToSignature(Method m) {
    Pact pact = m.getAnnotation(Pact.class);
    boolean conforms = pact != null && MessagePact.class.isAssignableFrom(m.getReturnType()) && m.getParameterTypes().length == 1 && m.getParameterTypes()[0].isAssignableFrom(MessagePactBuilder.class);
    if (!conforms && pact != null) {
        throw new UnsupportedOperationException("Method " + m.getName() + " does not conform required method signature 'public MessagePact xxx(MessagePactBuilder builder)'");
    }
    return conforms;
}
Also used : MessagePact(au.com.dius.pact.core.model.messaging.MessagePact) MessagePact(au.com.dius.pact.core.model.messaging.MessagePact) Pact(au.com.dius.pact.core.model.annotations.Pact) MessagePactBuilder(au.com.dius.pact.consumer.MessagePactBuilder)

Example 4 with MessagePactBuilder

use of au.com.dius.pact.consumer.MessagePactBuilder in project pact-jvm by DiUS.

the class MessagePactProviderRule method evaluatePactVerifications.

private void evaluatePactVerifications(PactVerifications pactVerifications, Statement base, Description description) throws Throwable {
    if (provider == null) {
        throw new UnsupportedOperationException("This provider name cannot be null when using @PactVerifications");
    }
    Optional<PactVerification> possiblePactVerification = findPactVerification(pactVerifications);
    if (possiblePactVerification.isEmpty()) {
        base.evaluate();
        return;
    }
    PactVerification pactVerification = possiblePactVerification.get();
    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 pact = method.getAnnotation(Pact.class);
    MessagePactBuilder builder = new MessagePactBuilder().consumer(Objects.toString(ep.parseExpression(pact.consumer(), DataType.RAW))).hasPactWith(provider);
    MessagePact messagePact = (MessagePact) method.invoke(testClassInstance, builder);
    setMessage(messagePact.getMessages().get(0), description);
    base.evaluate();
    messagePact.write(BuiltToolConfig.INSTANCE.getPactDirectory(), PactSpecVersion.V3);
}
Also used : MessagePact(au.com.dius.pact.core.model.messaging.MessagePact) MessagePact(au.com.dius.pact.core.model.messaging.MessagePact) Pact(au.com.dius.pact.core.model.annotations.Pact) MessagePactBuilder(au.com.dius.pact.consumer.MessagePactBuilder) Method(java.lang.reflect.Method)

Aggregations

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