Search in sources :

Example 1 with Message

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

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

the class MessagePactBuilder method withContent.

public MessagePactBuilder withContent(DslPart body) {
    if (messages == null || messages.isEmpty()) {
        throw new InvalidPactException("expectsToReceive is required before withMetaData");
    }
    Message message = messages.get(messages.size() - 1);
    @SuppressWarnings("unchecked") Map<String, String> metadata = message.getMetaData();
    if (metadata == null) {
        metadata = new HashMap<String, String>(1);
        metadata.put(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
    } else if (!metadata.containsKey(CONTENT_TYPE)) {
        metadata.put(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
    }
    message.setContents(OptionalBody.body(body.toString()));
    message.setMatchingRules(body.getMatchers());
    return this;
}
Also used : Message(au.com.dius.pact.model.v3.messaging.Message) InvalidPactException(au.com.dius.pact.model.InvalidPactException)

Example 3 with Message

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

the class MessagePactBuilder method expectsToReceive.

/**
   * Adds a message expectation in the pact.
   *
   * @param description message description.
   */
public MessagePactBuilder expectsToReceive(String description) {
    Message message = new Message(description, providerState);
    if (messages == null) {
        messages = new ArrayList<Message>();
    }
    messages.add(message);
    return this;
}
Also used : Message(au.com.dius.pact.model.v3.messaging.Message)

Example 4 with Message

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

use of au.com.dius.pact.model.v3.messaging.Message 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)14 Pact (au.com.dius.pact.consumer.Pact)12 HashMap (java.util.HashMap)12 PactDslJsonBody (au.com.dius.pact.consumer.dsl.PactDslJsonBody)11 RequestResponsePact (au.com.dius.pact.model.RequestResponsePact)5 Message (au.com.dius.pact.model.v3.messaging.Message)4 Method (java.lang.reflect.Method)2 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)1 InvalidPactException (au.com.dius.pact.model.InvalidPactException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Statement (org.junit.runners.model.Statement)1