use of au.com.dius.pact.model.PactFragment in project pact-jvm by DiUS.
the class PerfTest method test.
@Test
public void test() {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// Define the test data:
String path = "/mypath/abc/";
//Header data:
Map<String, String> headerData = new HashMap<String, String>();
headerData.put("Content-Type", "application/json");
// Put as JSON object:
JSONObject bodyExpected = new JSONObject();
bodyExpected.put("name", "myName");
stopWatch.split();
System.out.println("Setup: " + stopWatch.getSplitTime());
PactFragment pactFragment = ConsumerPactBuilder.consumer("perf_test_consumer").hasPactWith("perf_test_provider").uponReceiving("a request to get values").path(path).method("GET").willRespondWith().status(200).headers(headerData).body(bodyExpected).toFragment();
stopWatch.split();
System.out.println("Setup Fragment: " + stopWatch.getSplitTime());
MockProviderConfig config = MockProviderConfig.createDefault();
PactVerificationResult result = runConsumerTest(pactFragment.toPact(), config, new PactTestRun() {
@Override
public void run(@NotNull MockServer mockServer) throws IOException {
try {
stopWatch.split();
System.out.println("In Test: " + stopWatch.getSplitTime());
new ConsumerClient(config.url()).getAsList(path);
} catch (IOException e) {
}
stopWatch.split();
System.out.println("After Test: " + stopWatch.getSplitTime());
}
});
stopWatch.split();
System.out.println("End of Test: " + stopWatch.getSplitTime());
stopWatch.stop();
System.out.println(stopWatch.toString());
}
use of au.com.dius.pact.model.PactFragment in project pact-jvm by DiUS.
the class PactRule method apply.
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
PactVerification pactDef = description.getAnnotation(PactVerification.class);
//no pactVerification? execute the test normally
if (pactDef == null) {
base.evaluate();
return;
}
PactFragment fragment = getPacts().get(pactDef.value());
if (fragment == null) {
throw new UnsupportedOperationException("Fragment not found: " + pactDef.value());
}
VerificationResult result = fragment.runConsumer(config, new TestRun() {
@Override
public void run(MockProviderConfig config) throws Throwable {
base.evaluate();
}
});
if (!result.equals(PACT_VERIFIED)) {
if (result instanceof PactError) {
throw new RuntimeException(((PactError) result).error());
}
if (result instanceof UserCodeFailed) {
throw new RuntimeException(((UserCodeFailed<RuntimeException>) result).error());
}
if (result instanceof PactMismatch) {
PactMismatch mismatch = (PactMismatch) result;
throw new PactMismatchException(mismatch);
}
}
}
};
}
use of au.com.dius.pact.model.PactFragment in project pact-jvm by DiUS.
the class PactRule method getPacts.
/**
* scan all methods for @Pact annotation and execute them, if not already initialized
* @return
*/
protected Map<String, PactFragment> getPacts() {
if (fragments == null) {
fragments = new HashMap<String, PactFragment>();
for (Method m : target.getClass().getMethods()) {
if (conformsToSigniture(m)) {
Pact pact = m.getAnnotation(Pact.class);
PactDslWithState dslBuilder = ConsumerPactBuilder.consumer(pact.consumer()).hasPactWith(pact.provider()).given(pact.state());
try {
fragments.put(pact.state(), (PactFragment) m.invoke(target, dslBuilder));
} catch (Exception e) {
LOGGER.error("Failed to invoke pact method", e);
throw new RuntimeException("Failed to invoke pact method", e);
}
}
}
}
return fragments;
}
use of au.com.dius.pact.model.PactFragment in project pact-jvm by DiUS.
the class ConsumerPactTest method testPact.
@Test
public void testPact() throws Throwable {
PactFragment fragment = createFragment(ConsumerPactBuilder.consumer(consumerName()).hasPactWith(providerName()));
final MockProviderConfig config = MockProviderConfig.createDefault(getSpecificationVersion());
VerificationResult result = fragment.runConsumer(config, config1 -> runTest(config1.url()));
if (!result.equals(PACT_VERIFIED)) {
if (result instanceof PactError) {
throw ((PactError) result).error();
}
if (result instanceof UserCodeFailed) {
throw ((UserCodeFailed<RuntimeException>) result).error();
}
if (result instanceof PactMismatch) {
PactMismatch mismatch = (PactMismatch) result;
throw new PactMismatchException(mismatch);
}
}
}
use of au.com.dius.pact.model.PactFragment in project pact-jvm by DiUS.
the class PactProviderRule method evaluatePactVerifications.
private void evaluatePactVerifications(PactVerifications pactVerifications, Statement base) throws Throwable {
Optional<PactVerification> possiblePactVerification = findPactVerification(pactVerifications);
if (!possiblePactVerification.isPresent()) {
base.evaluate();
return;
}
PactVerification pactVerification = possiblePactVerification.get();
Optional<Method> possiblePactMethod = findPactMethod(pactVerification);
if (!possiblePactMethod.isPresent()) {
throw new UnsupportedOperationException("Could not find method with @Pact for the provider " + provider);
}
Method method = possiblePactMethod.get();
Pact pact = method.getAnnotation(Pact.class);
PactDslWithProvider dslBuilder = ConsumerPactBuilder.consumer(pact.consumer()).hasPactWith(provider);
PactFragment pactFragment;
try {
pactFragment = (PactFragment) method.invoke(target, dslBuilder);
} catch (Exception e) {
throw new RuntimeException("Failed to invoke pact method", e);
}
VerificationResult result = runPactTest(base, pactFragment);
validateResult(result, pactVerification);
}
Aggregations