From 7d84f60af829d2fc830cfdf36f5ecb4d8d20f198 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Sat, 18 Apr 2020 16:49:33 +1000 Subject: [PATCH] chore: add test to try replicate Github issue --- .../pact/consumer/junit5/Defect1070Test.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 consumer/pact-jvm-consumer-junit5/src/test/java/au/com/dius/pact/consumer/junit5/Defect1070Test.java diff --git a/consumer/pact-jvm-consumer-junit5/src/test/java/au/com/dius/pact/consumer/junit5/Defect1070Test.java b/consumer/pact-jvm-consumer-junit5/src/test/java/au/com/dius/pact/consumer/junit5/Defect1070Test.java new file mode 100644 index 0000000000..f7eb048242 --- /dev/null +++ b/consumer/pact-jvm-consumer-junit5/src/test/java/au/com/dius/pact/consumer/junit5/Defect1070Test.java @@ -0,0 +1,65 @@ +package au.com.dius.pact.consumer.junit5; + +import au.com.dius.pact.consumer.MockServer; +import au.com.dius.pact.consumer.dsl.PactDslWithProvider; +import au.com.dius.pact.core.model.RequestResponsePact; +import au.com.dius.pact.core.model.annotations.Pact; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpResponse; +import org.apache.http.client.fluent.Request; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import static io.pactfoundation.consumer.dsl.LambdaDsl.newJsonArrayMinLike; +import static java.lang.String.format; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +@ExtendWith(PactConsumerTestExt.class) +@PactTestFor(providerName = "ApiProvider", port = "1234") +public class Defect1070Test { + private Map headers = MapUtils.putAll(new HashMap<>(), new String[] { + "Content-Type", "application/json" + }); + + @BeforeEach + public void setUp(MockServer mockServer) { + assertThat(mockServer, is(notNullValue())); + } + + @Pact(consumer = "ApiConsumer") + public RequestResponsePact articles(PactDslWithProvider builder) { + return builder + .given("This is a test") + .uponReceiving("GET request to retrieve default values") + .matchPath(format("/api/test/%s", "\\d{1,8}")) + .method("GET") + .willRespondWith() + .status(200) + .headers(headers) + .body(newJsonArrayMinLike(1, values -> values.object(value -> { + value.numberType("id", 32432); + value.stringType("name", "testId254"); + value.numberType("size", 1445211); + } + )).build()) + .toPact(); + } + + @Test + @PactTestFor + void testApi(MockServer mockServer) throws IOException { + HttpResponse httpResponse = Request.Get(mockServer.getUrl() + "/api/test/1234").execute().returnResponse(); + assertThat(httpResponse.getStatusLine().getStatusCode(), is(equalTo(200))); + assertThat(IOUtils.toString(httpResponse.getEntity().getContent()), + is(equalTo("[{\"size\":1445211,\"name\":\"testId254\",\"id\":32432}]"))); + } +}