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;
}
}
};
}
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;
}
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;
}
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;
}
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();
}
Aggregations