use of au.com.dius.pact.core.model.annotations.Pact 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();
}
use of au.com.dius.pact.core.model.annotations.Pact in project pact-jvm by DiUS.
the class BaseProviderRule method evaluatePactVerifications.
private void evaluatePactVerifications(PactVerifications pactVerifications, Statement base) throws Throwable {
List<PactVerification> possiblePactVerifications = findPactVerifications(pactVerifications, this.provider);
if (possiblePactVerifications.isEmpty()) {
base.evaluate();
return;
}
final BasePact[] pact = { null };
possiblePactVerifications.forEach(pactVerification -> {
Optional<Method> possiblePactMethod = findPactMethod(pactVerification);
if (possiblePactMethod.isEmpty()) {
throw new UnsupportedOperationException("Could not find method with @Pact for the provider " + provider);
}
Method method = possiblePactMethod.get();
Pact pactAnnotation = method.getAnnotation(Pact.class);
PactDslWithProvider dslBuilder = ConsumerPactBuilder.consumer(ep.parseExpression(pactAnnotation.consumer(), DataType.RAW).toString()).pactSpecVersion(config.getPactVersion()).hasPactWith(provider);
updateAnyDefaultValues(dslBuilder);
try {
BasePact pactFromMethod = (BasePact) method.invoke(target, dslBuilder);
if (pact[0] == null) {
pact[0] = pactFromMethod;
} else {
pact[0].mergeInteractions(pactFromMethod.getInteractions());
}
} catch (Exception e) {
throw new RuntimeException("Failed to invoke pact method", e);
}
});
PactFolder pactFolder = target.getClass().getAnnotation(PactFolder.class);
PactDirectory pactDirectory = target.getClass().getAnnotation(PactDirectory.class);
PactVerificationResult result = runPactTest(base, pact[0], pactFolder, pactDirectory);
JUnitTestSupport.validateMockServerResult(result);
}
use of au.com.dius.pact.core.model.annotations.Pact in project pact-jvm by DiUS.
the class BaseProviderRule method getPacts.
/**
* scan all methods for @Pact annotation and execute them, if not already initialized
* @param fragment
*/
protected Map<String, BasePact> getPacts(String fragment) {
if (pacts == null) {
pacts = new HashMap<>();
for (Method m : target.getClass().getMethods()) {
if (JUnitTestSupport.conformsToSignature(m, config.getPactVersion()) && methodMatchesFragment(m, fragment)) {
Pact pactAnnotation = m.getAnnotation(Pact.class);
String provider = ep.parseExpression(pactAnnotation.provider(), DataType.RAW).toString();
if (StringUtils.isEmpty(provider) || this.provider.equals(provider)) {
PactDslWithProvider dslBuilder = ConsumerPactBuilder.consumer(ep.parseExpression(pactAnnotation.consumer(), DataType.RAW).toString()).pactSpecVersion(config.getPactVersion()).hasPactWith(this.provider);
updateAnyDefaultValues(dslBuilder);
try {
BasePact pact = (BasePact) m.invoke(target, dslBuilder);
pacts.put(this.provider, pact);
} catch (Exception e) {
throw new RuntimeException("Failed to invoke pact method", e);
}
}
}
}
}
return pacts;
}
use of au.com.dius.pact.core.model.annotations.Pact 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;
}
}
};
}
use of au.com.dius.pact.core.model.annotations.Pact 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;
}
Aggregations