use of au.com.dius.pact.model.MockProviderConfig 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.MockProviderConfig 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.MockProviderConfig 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.MockProviderConfig in project pact-jvm by DiUS.
the class DirectDSLConsumerPactTest method testPact.
@Test
public void testPact() {
RequestResponsePact pact = ConsumerPactBuilder.consumer("Some Consumer").hasPactWith("Some Provider").uponReceiving("a request to say Hello").path("/hello").method("POST").body("{\"name\": \"harry\"}").willRespondWith().status(200).body("{\"hello\": \"harry\"}").toPact();
MockProviderConfig config = MockProviderConfig.createDefault();
PactVerificationResult result = runConsumerTest(pact, config, mockServer -> {
Map expectedResponse = new HashMap();
expectedResponse.put("hello", "harry");
try {
assertEquals(new ProviderClient(mockServer.getUrl()).hello("{\"name\": \"harry\"}"), expectedResponse);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
if (result instanceof PactVerificationResult.Error) {
throw new RuntimeException(((PactVerificationResult.Error) result).getError());
}
assertEquals(PactVerificationResult.Ok.INSTANCE, result);
}
use of au.com.dius.pact.model.MockProviderConfig in project pact-jvm by DiUS.
the class MimeTypeTest method runTest.
private void runTest(RequestResponsePact pact, final String body, final String expectedResponse, final ContentType mimeType) {
MockProviderConfig config = MockProviderConfig.createDefault(PactSpecVersion.V2);
PactVerificationResult result = runConsumerTest(pact, config, new PactTestRun() {
@Override
public void run(@NotNull MockServer mockServer) throws IOException {
try {
assertEquals(new ConsumerClient(config.url()).postBody("/hello", body, mimeType), expectedResponse);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
});
if (result instanceof PactVerificationResult.Error) {
throw new RuntimeException(((PactVerificationResult.Error) result).getError());
}
Assert.assertEquals(PactVerificationResult.Ok.INSTANCE, result);
}
Aggregations