use of au.com.dius.pact.core.model.BasePact 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.BasePact in project pact-jvm by DiUS.
the class BaseProviderRule method apply.
@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);
return;
}
PactVerification pactDef = description.getAnnotation(PactVerification.class);
// no pactVerification? execute the test normally
if (pactDef == null) {
base.evaluate();
return;
}
Map<String, BasePact> pacts = getPacts(pactDef.fragment());
Optional<BasePact> pact;
if (pactDef.value().length == 1 && StringUtils.isEmpty(pactDef.value()[0])) {
pact = pacts.values().stream().findFirst();
} else {
pact = Arrays.stream(pactDef.value()).map(pacts::get).filter(Objects::nonNull).findFirst();
}
if (pact.isEmpty()) {
base.evaluate();
return;
}
if (config.getPactVersion() == PactSpecVersion.V4) {
pact.get().asV4Pact().component1().getInteractions().forEach(i -> i.getComments().put("testname", Json.toJson(description.getDisplayName())));
}
PactFolder pactFolder = target.getClass().getAnnotation(PactFolder.class);
PactDirectory pactDirectory = target.getClass().getAnnotation(PactDirectory.class);
BasePact basePact = pact.get();
Metrics.INSTANCE.sendMetrics(new MetricEvent.ConsumerTestRun(basePact.getInteractions().size(), "junit"));
PactVerificationResult result = runPactTest(base, basePact, pactFolder, pactDirectory);
validateResult(result, pactDef);
}
};
}
Aggregations