Search in sources :

Example 11 with MessagePact

use of au.com.dius.pact.core.model.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<>();
        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 12 with MessagePact

use of au.com.dius.pact.core.model.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.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 13 with MessagePact

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

the class AsyncMessageTest method createPact.

@Pact(provider = "test_provider", consumer = "test_consumer_v3")
public MessagePact createPact(MessagePactBuilder builder) {
    PactDslJsonBody body = new PactDslJsonBody();
    body.stringValue("testParam1", "value1");
    body.stringValue("testParam2", "value2");
    Map<String, Object> metadata = new HashMap<>();
    metadata.put("Content-Type", "application/json");
    metadata.put("destination", Matchers.regexp("\\w+\\d+", "X001"));
    return builder.given("SomeProviderState").expectsToReceive("a test message").withMetadata(metadata).withContent(body).toPact();
}
Also used : HashMap(java.util.HashMap) PactDslJsonBody(au.com.dius.pact.consumer.dsl.PactDslJsonBody) MessagePact(au.com.dius.pact.core.model.messaging.MessagePact) Pact(au.com.dius.pact.core.model.annotations.Pact)

Example 14 with MessagePact

use of au.com.dius.pact.core.model.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("Content-Type", "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) MessagePact(au.com.dius.pact.core.model.messaging.MessagePact) Pact(au.com.dius.pact.core.model.annotations.Pact)

Example 15 with MessagePact

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

the class ExampleMessageConsumerWithRootArrayTest method createPact.

@Pact(provider = "test_provider", consumer = "test_consumer_v3")
public MessagePact createPact(MessagePactBuilder builder) {
    PactDslJsonArray body = new PactDslJsonArray().decimalType(100.10).stringType("Should be in an array");
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("contentType", "application/json");
    return builder.given("SomeProviderState").expectsToReceive("a test message with an array").withMetadata(metadata).withContent(body).toPact();
}
Also used : PactDslJsonArray(au.com.dius.pact.consumer.dsl.PactDslJsonArray) HashMap(java.util.HashMap) MessagePact(au.com.dius.pact.core.model.messaging.MessagePact) Pact(au.com.dius.pact.core.model.annotations.Pact)

Aggregations

Pact (au.com.dius.pact.core.model.annotations.Pact)21 MessagePact (au.com.dius.pact.core.model.messaging.MessagePact)21 PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)16 HashMap (java.util.HashMap)16 RequestResponsePact (au.com.dius.pact.core.model.RequestResponsePact)5 MessagePactBuilder (au.com.dius.pact.consumer.MessagePactBuilder)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 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)1 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