Search in sources :

Example 6 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 = "MessageProvider", consumer = "test_consumer_v3")
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 7 with MessagePact

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

the class AsyncMessageTest method createPact.

@Pact(consumer = "test_consumer_v3")
MessagePact createPact(MessagePactBuilder builder) {
    PactDslJsonBody body = new PactDslJsonBody();
    body.stringValue("testParam1", "value1");
    body.stringValue("testParam2", "value2");
    Map<String, Object> metadata = new HashMap<>();
    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 8 with MessagePact

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

the class ExampleMessageConsumerTest 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, String> metadata = new HashMap<String, String>();
    metadata.put("contentType", "application/json");
    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) Pact(au.com.dius.pact.core.model.annotations.Pact) MessagePact(au.com.dius.pact.core.model.messaging.MessagePact)

Example 9 with MessagePact

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

the class PactVerificationsForMultipleFragmentsTest method otherMessagePact.

@Pact(provider = MESSAGE_PROVIDER_NAME, consumer = PACT_VERIFICATIONS_CONSUMER_NAME)
public MessagePact otherMessagePact(MessagePactBuilder builder) {
    PactDslJsonBody body = new PactDslJsonBody();
    body.stringValue("testParamA", "valueA");
    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("contentType", "application/json");
    return builder.given("SomeOtherProviderState").expectsToReceive("another 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) RequestResponsePact(au.com.dius.pact.core.model.RequestResponsePact)

Example 10 with MessagePact

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

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