Search in sources :

Example 1 with MessagePact

use of au.com.dius.pact.model.v3.messaging.MessagePact 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.isPresent()) {
                    base.evaluate();
                    return;
                }
                pacts = new HashMap<>();
                Method method = possiblePactMethod.get();
                Pact pact = method.getAnnotation(Pact.class);
                MessagePactBuilder builder = MessagePactBuilder.consumer(pact.consumer()).hasPactWith(provider);
                messagePact = (MessagePact) method.invoke(testClassInstance, builder);
                for (Message message : messagePact.getMessages()) {
                    pacts.put(message.getProviderState(), 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.contentsAsBytes(), description);
            try {
                base.evaluate();
                messagePact.write(PactConsumerConfig$.MODULE$.pactRootDir());
            } catch (Throwable t) {
                throw t;
            }
        }
    };
}
Also used : Message(au.com.dius.pact.model.v3.messaging.Message) Statement(org.junit.runners.model.Statement) MessagePact(au.com.dius.pact.model.v3.messaging.MessagePact) Method(java.lang.reflect.Method)

Example 2 with MessagePact

use of au.com.dius.pact.model.v3.messaging.MessagePact 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.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);
    MessagePactBuilder builder = MessagePactBuilder.consumer(pact.consumer()).hasPactWith(provider);
    MessagePact messagePact = (MessagePact) method.invoke(testClassInstance, builder);
    setMessage(messagePact.getMessages().get(0).contentsAsBytes(), description);
    base.evaluate();
    messagePact.write(PactConsumerConfig$.MODULE$.pactRootDir());
}
Also used : MessagePact(au.com.dius.pact.model.v3.messaging.MessagePact) MessagePact(au.com.dius.pact.model.v3.messaging.MessagePact) Method(java.lang.reflect.Method)

Example 3 with MessagePact

use of au.com.dius.pact.model.v3.messaging.MessagePact 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.model.v3.messaging.MessagePact) MessagePact(au.com.dius.pact.model.v3.messaging.MessagePact)

Example 4 with MessagePact

use of au.com.dius.pact.model.v3.messaging.MessagePact in project pact-jvm by DiUS.

the class MessagePactProviderRule method parsePacts.

@SuppressWarnings("unchecked")
private Map<String, Message> parsePacts() {
    if (providerStateMessages == null) {
        providerStateMessages = new HashMap<String, Message>();
        for (Method m : testClassInstance.getClass().getMethods()) {
            if (conformsToSignature(m)) {
                Pact pact = m.getAnnotation(Pact.class);
                if (pact != null) {
                    String provider = pact.provider();
                    if (provider != null && !provider.trim().isEmpty()) {
                        MessagePactBuilder builder = MessagePactBuilder.consumer(pact.consumer()).hasPactWith(provider);
                        List<Message> messages = null;
                        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) {
                            providerStateMessages.put(message.getProviderState(), message);
                        }
                    }
                }
            }
        }
    }
    return providerStateMessages;
}
Also used : Message(au.com.dius.pact.model.v3.messaging.Message) MessagePact(au.com.dius.pact.model.v3.messaging.MessagePact) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with MessagePact

use of au.com.dius.pact.model.v3.messaging.MessagePact in project pact-jvm by DiUS.

the class AsyncMessageTest method createPact2.

@Pact(provider = "test_provider", consumer = "test_consumer_v3")
public MessagePact createPact2(MessagePactBuilder builder) {
    PactDslJsonBody body = new PactDslJsonBody();
    body.stringValue("testParam1", "value3");
    body.stringValue("testParam2", "value4");
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("contentType", "application/json");
    return builder.given("SomeProviderState2").expectsToReceive("a test message").withMetadata(metadata).withContent(body).toPact();
}
Also used : HashMap(java.util.HashMap) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) Pact(au.com.dius.pact.consumer.Pact) MessagePact(au.com.dius.pact.model.v3.messaging.MessagePact)

Aggregations

MessagePact (au.com.dius.pact.model.v3.messaging.MessagePact)17 Pact (au.com.dius.pact.consumer.Pact)13 HashMap (java.util.HashMap)13 PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)12 RequestResponsePact (au.com.dius.pact.model.RequestResponsePact)5 Method (java.lang.reflect.Method)3 Message (au.com.dius.pact.model.v3.messaging.Message)2 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Statement (org.junit.runners.model.Statement)1