|
1 |
| -package net.nautsch.invoice.adapters.address; |
| 1 | +package net.nautsch.invoice.pact; |
2 | 2 |
|
3 | 3 | import au.com.dius.pact.consumer.ConsumerPactBuilder;
|
4 | 4 | import au.com.dius.pact.consumer.PactError;
|
5 |
| -import au.com.dius.pact.consumer.TestRun; |
6 | 5 | import au.com.dius.pact.consumer.VerificationResult;
|
7 | 6 | import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
|
8 | 7 | import au.com.dius.pact.model.MockProviderConfig;
|
9 | 8 | import au.com.dius.pact.model.PactFragment;
|
10 | 9 | import au.com.dius.pact.model.PactSpecVersion;
|
| 10 | +import java.util.Collections; |
| 11 | +import java.util.function.Consumer; |
11 | 12 | import net.nautsch.invoice.Address;
|
12 |
| -import org.apache.http.entity.ContentType; |
13 |
| -import org.junit.Test; |
| 13 | +import net.nautsch.invoice.adapters.address.AddressServiceAdapter; |
14 | 14 |
|
15 |
| -import java.util.HashMap; |
16 |
| -import java.util.Map; |
| 15 | +import org.apache.http.HttpStatus; |
| 16 | +import org.junit.Test; |
17 | 17 |
|
18 | 18 | import static au.com.dius.pact.consumer.ConsumerPactTest.PACT_VERIFIED;
|
| 19 | +import static org.apache.http.entity.ContentType.APPLICATION_JSON; |
19 | 20 | import static org.hamcrest.CoreMatchers.is;
|
20 | 21 | import static org.junit.Assert.assertThat;
|
21 | 22 |
|
22 | 23 | /**
|
23 |
| - * integration test. |
| 24 | + * Pact consumer test, generates the consumer contract. |
24 | 25 | */
|
25 | 26 | public class AddressServiceAdapterTest {
|
26 | 27 |
|
27 | 28 | private static final String ADDRESS = "address_service";
|
28 | 29 | private static final String INVOICE = "invoice_service";
|
29 | 30 |
|
30 |
| - |
31 |
| - // tag::shouldDeliverAnAddress[] |
| 31 | + // tag::shouldDeliverValidAddress[] |
32 | 32 | @Test
|
33 |
| - public void shouldDeliverAnAddress() { |
| 33 | + public void shouldDeliverValidAddress() { |
34 | 34 | PactDslJsonBody responseBody = new PactDslJsonBody()
|
35 | 35 | .stringType("firstName", "Jan")
|
36 | 36 | .stringType("surname", "Wloka");
|
37 | 37 |
|
38 |
| - PactFragment fragment = buildPactFragment(responseBody, "get an address"); |
| 38 | + PactFragment pact = buildPact("", responseBody, "get an address"); |
39 | 39 |
|
40 |
| - runTest(fragment); |
41 |
| - } |
42 |
| - // end::shouldDeliverAnAddress[] |
| 40 | + assertPact(pact, config -> { |
| 41 | + Address actual = new AddressServiceAdapter(config.url()).getAddress("1"); |
43 | 42 |
|
44 |
| - private PactFragment buildPactFragment(PactDslJsonBody responseBody, String description) { |
45 |
| - return buildPactFragment("", responseBody, description); |
| 43 | + assertThat(actual.getFirstName(), is("Jan")); |
| 44 | + assertThat(actual.getSurname(), is("Wloka")); |
| 45 | + }); |
46 | 46 | }
|
| 47 | + // end::shouldDeliverValidAddress[] |
47 | 48 |
|
48 |
| - private PactFragment buildPactFragment(String requestBody, PactDslJsonBody responseBody, String description) { |
49 |
| - Map<String, String> headers = new HashMap<>(); |
50 |
| - headers.put("Content-Type", ContentType.APPLICATION_JSON.toString()); |
| 49 | + private PactFragment buildPact(String requestBody, PactDslJsonBody responseBody, String description) { |
51 | 50 | return ConsumerPactBuilder
|
52 | 51 | .consumer(INVOICE)
|
53 | 52 | .hasPactWith(ADDRESS)
|
54 | 53 | .uponReceiving(description)
|
55 | 54 | .path("/addresses/1")
|
56 | 55 | .method("GET")
|
57 | 56 | .body(requestBody)
|
58 |
| - .headers(new HashMap<>()) |
| 57 | + .headers(Collections.emptyMap()) |
59 | 58 | .willRespondWith()
|
60 |
| - .status(200) |
| 59 | + .status(HttpStatus.SC_OK) |
61 | 60 | .body(responseBody)
|
62 |
| - .headers(headers) |
| 61 | + .headers(Collections.singletonMap("Content-Type", APPLICATION_JSON.toString())) |
63 | 62 | .toFragment();
|
64 | 63 | }
|
65 | 64 |
|
66 |
| - private void runTest(final PactFragment pactFragment) { |
| 65 | + private void assertPact(PactFragment pact, Consumer<MockProviderConfig> consumer) { |
67 | 66 | MockProviderConfig config = MockProviderConfig.createDefault(PactSpecVersion.V3);
|
68 |
| - VerificationResult result = pactFragment.runConsumer(config, new TestRun() { |
69 |
| - @Override |
70 |
| - public void run(MockProviderConfig config) { |
71 |
| - Address result = new AddressServiceAdapter(config.url()).getAddress("1"); |
72 |
| - assertThat(result.getFirstName(), is("Jan")); |
73 |
| - assertThat(result.getSurname(), is("Wloka")); |
74 |
| - } |
75 |
| - }); |
| 67 | + VerificationResult result = pact.runConsumer(config, consumer::accept); |
76 | 68 |
|
77 | 69 | if (result instanceof PactError) {
|
78 | 70 | throw new RuntimeException(((PactError)result).error());
|
|
0 commit comments