au.com.dius.pact.core.model.RequestResponsePact Java Examples

The following examples show how to use au.com.dius.pact.core.model.RequestResponsePact. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PollingTest.java    From microservice-istio with Apache License 2.0 6 votes vote down vote up
@Pact(consumer = "Shipping")
public RequestResponsePact createFragment(PactDslWithProvider builder) {
	Map<String, String> headers = new HashMap<String, String>();
	headers.put("Content-Type", "application/json");
	Date now = new Date();
	return builder	.uponReceiving("Request for order feed")
					.method("GET")
					.path("/feed")
					.willRespondWith()
					.status(200)
					.headers(headers)
					.body(feedBody(now))
					.uponReceiving("Request for an order")
					.method("GET")
					.path("/order/1")
					.willRespondWith()
					.status(200)
					.headers(headers)
					.body(order(now))
					.toPact();
}
 
Example #2
Source File: PollingTest.java    From microservice-istio with Apache License 2.0 6 votes vote down vote up
@Pact(consumer = "Invoice")
public RequestResponsePact createFragment(PactDslWithProvider builder) {
	Map<String, String> headers = new HashMap<String, String>();
	headers.put("Content-Type", "application/json");
	Date now = new Date();
	return builder	.uponReceiving("Request for order feed")
					.method("GET")
					.path("/feed")
					.willRespondWith()
					.status(200)
					.headers(headers)
					.body(feedBody(now))
					.uponReceiving("Request for an order")
					.method("GET")
					.path("/order/1")
					.willRespondWith()
					.status(200)
					.headers(headers)
					.body(order(now))
					.toPact();
}
 
Example #3
Source File: ClientPactTest.java    From pact-workshop-jvm with Apache License 2.0 6 votes vote down vote up
@Pact(provider = "Our Provider", consumer = "Our Little Consumer")
public RequestResponsePact pact(PactDslWithProvider builder) {
  dateTime = LocalDateTime.now();
  dateResult = OffsetDateTime.now().truncatedTo(ChronoUnit.SECONDS);
  return builder
    .given("data count > 0")
    .uponReceiving("a request for json data")
    .path("/provider.json")
    .method("GET")
    .query("validDate=" + dateTime.toString())
    .willRespondWith()
    .status(200)
    .body(
        new PactDslJsonBody()
            .stringValue("test", "NO")
            .datetime("validDate", "yyyy-MM-dd'T'HH:mm:ssXX", dateResult.toInstant())
            .integerType("count", 100)
    )
    .toPact();
}
 
Example #4
Source File: PollingTest.java    From microservice-istio with Apache License 2.0 6 votes vote down vote up
@Pact(consumer = "Bonus")
public RequestResponsePact createFragment(PactDslWithProvider builder) {
	Map<String, String> headers = new HashMap<String, String>();
	headers.put("Content-Type", "application/json");
	Date now = new Date();
	return builder	.uponReceiving("Request for order feed")
					.method("GET")
					.path("/feed")
					.willRespondWith()
					.status(200)
					.headers(headers)
					.body(feedBody(now))
					.uponReceiving("Request for an order")
					.method("GET")
					.path("/order/1")
					.willRespondWith()
					.status(200)
					.headers(headers)
					.body(order(now))
					.toPact();
}
 
Example #5
Source File: PactJunitRuleTest.java    From Pact-JVM-Example with MIT License 6 votes vote down vote up
@Pact(consumer = "JunitRuleConsumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json;charset=UTF-8");

    return builder
            .given("")
            .uponReceiving("Pact JVM example Pact interaction")
            .path("/information")
            .query("name=Miku")
            .method("GET")
            .willRespondWith()
            .headers(headers)
            .status(200)
            .body("{\n" +
                    "    \"salary\": 45000,\n" +
                    "    \"name\": \"Hatsune Miku\",\n" +
                    "    \"nationality\": \"Japan\",\n" +
                    "    \"contact\": {\n" +
                    "        \"Email\": \"[email protected]\",\n" +
                    "        \"Phone Number\": \"9090950\"\n" +
                    "    }\n" +
                    "}")
            .toPact();
}
 
Example #6
Source File: CreditScoreServicePactContracts.java    From microservices-testing-examples with MIT License 5 votes vote down vote up
@Pact(provider = CREDIT_SCORE_SERVICE, consumer = SPECIAL_MEMBERSHIP_SERVICE)
public RequestResponsePact hawleyGriffinCreditScore(PactDslWithProvider pact) {
  return pact.given("There is not a [email protected]")
      .uponReceiving("A credit score request for [email protected]")
      .path("/credit-scores/[email protected]").method("GET")
      .willRespondWith()
      .status(404)
      .toPact();
}
 
Example #7
Source File: CreditScoreServicePactContracts.java    From microservices-testing-examples with MIT License 5 votes vote down vote up
@Pact(provider = CREDIT_SCORE_SERVICE, consumer = SPECIAL_MEMBERSHIP_SERVICE)
public RequestResponsePact tonyStarkCreditScore(PactDslWithProvider pact) {
  return pact.given("There is a [email protected]")
      .uponReceiving("A credit score request for [email protected]")
      .path("/credit-scores/[email protected]").method("GET")
      .willRespondWith()
      .status(200)
      .headers(singletonMap(CONTENT_TYPE, APPLICATION_JSON))
      .body(new PactDslJsonBody().integerType("creditScore", 850))
      .toPact();
}
 
Example #8
Source File: NationalityPactTest.java    From Pact-JVM-Example with MIT License 5 votes vote down vote up
@Test
public void testNoNationality() {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json;charset=UTF-8");

    DslPart body = newJsonBody((root) -> {
        root.numberType("salary");
        root.stringValue("name", "Takamachi Nanoha");
        root.stringValue("nationality", null);
        root.object("contact", (contactObject) -> {
            contactObject.stringMatcher("Email", ".*@ariman.com", "[email protected]");
            contactObject.stringType("Phone Number", "9090940");
        });
    }).build();

    RequestResponsePact pact = ConsumerPactBuilder
            .consumer("ConsumerNanohaNoNationality")
            .hasPactWith("ExampleProvider")
            .given("No nationality")
            .uponReceiving("Query name is Nanoha")
            .path("/information")
            .query("name=Nanoha")
            .method("GET")
            .willRespondWith()
            .headers(headers)
            .status(200)
            .body(body)
            .toPact();

    MockProviderConfig config = MockProviderConfig.createDefault(PactSpecVersion.V3);
    PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> {
        providerService.setBackendURL(mockServer.getUrl());
        Information information = providerService.getInformation();
        assertEquals(information.getName(), "Takamachi Nanoha");
        assertNull(information.getNationality());
        return null;
    });

    checkResult(result);
}
 
Example #9
Source File: NationalityPactTest.java    From Pact-JVM-Example with MIT License 5 votes vote down vote up
@Test
public void testWithNationality() {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json;charset=UTF-8");

    DslPart body = newJsonBody((root) -> {
        root.numberType("salary");
        root.stringValue("name", "Takamachi Nanoha");
        root.stringValue("nationality", "Japan");
        root.object("contact", (contactObject) -> {
            contactObject.stringMatcher("Email", ".*@ariman.com", "[email protected]");
            contactObject.stringType("Phone Number", "9090940");
        });
    }).build();

    RequestResponsePact pact = ConsumerPactBuilder
            .consumer("ConsumerNanohaWithNationality")
            .hasPactWith("ExampleProvider")
            .given("")
            .uponReceiving("Query name is Nanoha")
            .path("/information")
            .query("name=Nanoha")
            .method("GET")
            .willRespondWith()
            .headers(headers)
            .status(200)
            .body(body)
            .toPact();

    MockProviderConfig config = MockProviderConfig.createDefault(PactSpecVersion.V3);
    PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> {
        providerService.setBackendURL(mockServer.getUrl());
        Information information = providerService.getInformation();
        assertEquals(information.getName(), "Takamachi Nanoha");
        assertEquals(information.getNationality(), "Japan");
        return null;
    });

    checkResult(result);
}
 
Example #10
Source File: PactJunitDSLTest.java    From Pact-JVM-Example with MIT License 5 votes vote down vote up
@Test
public void testPact2() {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json;charset=UTF-8");

    RequestResponsePact pact = ConsumerPactBuilder
            .consumer("JunitDSLConsumer2")
            .hasPactWith("ExampleProvider")
            .given("")
            .uponReceiving("Query name is Nanoha")
            .path("/information")
            .query("name=Nanoha")
            .method("GET")
            .willRespondWith()
            .headers(headers)
            .status(200)
            .body("{\n" +
                    "    \"salary\": 80000,\n" +
                    "    \"name\": \"Takamachi Nanoha\",\n" +
                    "    \"nationality\": \"Japan\",\n" +
                    "    \"contact\": {\n" +
                    "        \"Email\": \"[email protected]\",\n" +
                    "        \"Phone Number\": \"9090940\"\n" +
                    "    }\n" +
                    "}")
            .toPact();

    MockProviderConfig config = MockProviderConfig.createDefault();
    PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> {
        providerService.setBackendURL(mockServer.getUrl(), "Nanoha");
        Information information = providerService.getInformation();
        assertEquals(information.getName(), "Takamachi Nanoha");
        return null;
    });

    checkResult(result);
}
 
Example #11
Source File: PactJunitDSLTest.java    From Pact-JVM-Example with MIT License 5 votes vote down vote up
@Test
public void testPact1() {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json;charset=UTF-8");

    RequestResponsePact pact = ConsumerPactBuilder
            .consumer("JunitDSLConsumer1")
            .hasPactWith("ExampleProvider")
            .given("")
            .uponReceiving("Query name is Miku")
            .path("/information")
            .query("name=Miku")
            .method("GET")
            .willRespondWith()
            .headers(headers)
            .status(200)
            .body("{\n" +
                    "    \"salary\": 45000,\n" +
                    "    \"name\": \"Hatsune Miku\",\n" +
                    "    \"nationality\": \"Japan\",\n" +
                    "    \"contact\": {\n" +
                    "        \"Email\": \"[email protected]\",\n" +
                    "        \"Phone Number\": \"9090950\"\n" +
                    "    }\n" +
                    "}")
            .toPact();

    MockProviderConfig config = MockProviderConfig.createDefault();
    PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> {
        providerService.setBackendURL(mockServer.getUrl(), "Miku");
        Information information = providerService.getInformation();
        assertEquals(information.getName(), "Hatsune Miku");
        return null;
    });

    checkResult(result);
}
 
Example #12
Source File: PactBaseConsumerTest.java    From Pact-JVM-Example with MIT License 5 votes vote down vote up
@Override
@Pact(provider="ExampleProvider", consumer="BaseConsumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json;charset=UTF-8");

    return builder
            .given("")
            .uponReceiving("Pact JVM example Pact interaction")
            .path("/information")
            .query("name=Miku")
            .method("GET")
            .willRespondWith()
            .headers(headers)
            .status(200)
            .body("{\n" +
                    "    \"salary\": 45000,\n" +
                    "    \"name\": \"Hatsune Miku\",\n" +
                    "    \"nationality\": \"Japan\",\n" +
                    "    \"contact\": {\n" +
                    "        \"Email\": \"[email protected]\",\n" +
                    "        \"Phone Number\": \"9090950\"\n" +
                    "    }\n" +
                    "}")

            .toPact();
}
 
Example #13
Source File: PactJunitDSLJsonBodyTest.java    From Pact-JVM-Example with MIT License 5 votes vote down vote up
@Test
public void testWithLambdaDSLJsonBody() {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json;charset=UTF-8");

    DslPart body = newJsonBody((root) -> {
        root.numberValue("salary", 45000);
        root.stringValue("name", "Hatsune Miku");
        root.stringValue("nationality", "Japan");
        root.object("contact", (contactObject) -> {
            contactObject.stringMatcher("Email", ".*@ariman.com", "[email protected]");
            contactObject.stringType("Phone Number", "9090950");
        });
    }).build();

    RequestResponsePact pact = ConsumerPactBuilder
            .consumer("JunitDSLLambdaJsonBodyConsumer")
            .hasPactWith("ExampleProvider")
            .given("")
            .uponReceiving("Query name is Miku")
            .path("/information")
            .query("name=Miku")
            .method("GET")
            .willRespondWith()
            .headers(headers)
            .status(200)
            .body(body)
            .toPact();

    MockProviderConfig config = MockProviderConfig.createDefault(PactSpecVersion.V3);
    PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> {
        providerService.setBackendURL(mockServer.getUrl());
        Information information = providerService.getInformation();
        assertEquals(information.getName(), "Hatsune Miku");
        return null;
    });

    checkResult(result);
}
 
Example #14
Source File: PactJunitDSLJsonBodyTest.java    From Pact-JVM-Example with MIT License 5 votes vote down vote up
@Test
public void testWithPactDSLJsonBody() {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json;charset=UTF-8");

    DslPart body = new PactDslJsonBody()
            .numberType("salary", 45000)
            .stringType("name", "Hatsune Miku")
            .stringType("nationality", "Japan")
            .object("contact")
            .stringValue("Email", "[email protected]")
            .stringValue("Phone Number", "9090950")
            .closeObject();

    RequestResponsePact pact = ConsumerPactBuilder
            .consumer("JunitDSLJsonBodyConsumer")
            .hasPactWith("ExampleProvider")
            .given("")
            .uponReceiving("Query name is Miku")
            .path("/information")
            .query("name=Miku")
            .method("GET")
            .willRespondWith()
            .headers(headers)
            .status(200)
            .body(body)
            .toPact();

    MockProviderConfig config = MockProviderConfig.createDefault(PactSpecVersion.V3);
    PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> {
        providerService.setBackendURL(mockServer.getUrl());
        Information information = providerService.getInformation();
        assertEquals(information.getName(), "Hatsune Miku");
        return null;
    });

    checkResult(result);
}
 
Example #15
Source File: ClientPactTest.java    From pact-workshop-jvm with Apache License 2.0 5 votes vote down vote up
@Pact(provider = "Our Provider", consumer = "Our Little Consumer")
public RequestResponsePact pactForWhenThereIsNoData(PactDslWithProvider builder) {
  dateTime = LocalDateTime.now();
  return builder
          .given("data count == 0")
          .uponReceiving("a request for json data")
          .path("/provider.json")
          .method("GET")
          .query("validDate=" + dateTime.toString())
          .willRespondWith()
          .status(404)
          .toPact();
}
 
Example #16
Source File: ClientPactTest.java    From pact-workshop-jvm with Apache License 2.0 5 votes vote down vote up
@Pact(provider = "Our Provider", consumer = "Our Little Consumer")
public RequestResponsePact pactForInvalidDateParameter(PactDslWithProvider builder) {
  return builder
          .given("data count > 0")
          .uponReceiving("a request with an invalid date parameter")
          .path("/provider.json")
          .method("GET")
          .query("validDate=This is not a date")
          .willRespondWith()
          .status(400)
          .body(
               new PactDslJsonBody().stringValue("error", "'This is not a date' is not a date")
          )
          .toPact();
}
 
Example #17
Source File: ClientPactTest.java    From pact-workshop-jvm with Apache License 2.0 5 votes vote down vote up
@Pact(provider = "Our Provider", consumer = "Our Little Consumer")
public RequestResponsePact pactForMissingDateParameter(PactDslWithProvider builder) {
  return builder
          .given("data count > 0")
          .uponReceiving("a request with a missing date parameter")
          .path("/provider.json")
          .method("GET")
          .willRespondWith()
          .status(400)
          .body(
              new PactDslJsonBody().stringValue("error", "validDate is required")
          )
          .toPact();
}
 
Example #18
Source File: PactJunitRuleMultipleInteractionsTest.java    From Pact-JVM-Example with MIT License 4 votes vote down vote up
@Pact(consumer="JunitRuleMultipleInteractionsConsumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json;charset=UTF-8");

    return builder
            .given("")
            .uponReceiving("Miku")
            .path("/information")
            .query("name=Miku")
            .method("GET")
            .willRespondWith()
            .headers(headers)
            .status(200)
            .body("{\n" +
                    "    \"salary\": 45000,\n" +
                    "    \"name\": \"Hatsune Miku\",\n" +
                    "    \"nationality\": \"Japan\",\n" +
                    "    \"contact\": {\n" +
                    "        \"Email\": \"[email protected]\",\n" +
                    "        \"Phone Number\": \"9090950\"\n" +
                    "    }\n" +
                    "}")
            .given("")
            .uponReceiving("Nanoha")
            .path("/information")
            .query("name=Nanoha")
            .method("GET")
            .willRespondWith()
            .headers(headers)
            .status(200)
            .body("{\n" +
                    "    \"salary\": 80000,\n" +
                    "    \"name\": \"Takamachi Nanoha\",\n" +
                    "    \"nationality\": \"Japan\",\n" +
                    "    \"contact\": {\n" +
                    "        \"Email\": \"[email protected]\",\n" +
                    "        \"Phone Number\": \"9090940\"\n" +
                    "    }\n" +
                    "}")
            .toPact();
}