|
| 1 | +package au.com.dius.pact.consumer.junit.xml; |
| 2 | + |
| 3 | +import au.com.dius.pact.consumer.dsl.PactDslWithProvider; |
| 4 | +import au.com.dius.pact.consumer.junit.PactProviderRule; |
| 5 | +import au.com.dius.pact.consumer.junit.PactVerification; |
| 6 | +import au.com.dius.pact.consumer.xml.PactXmlBuilder; |
| 7 | +import au.com.dius.pact.core.model.RequestResponsePact; |
| 8 | +import au.com.dius.pact.core.model.annotations.Pact; |
| 9 | +import org.apache.commons.collections4.MapUtils; |
| 10 | +import org.junit.Rule; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +import static au.com.dius.pact.consumer.dsl.Matchers.bool; |
| 19 | +import static au.com.dius.pact.consumer.dsl.Matchers.integer; |
| 20 | +import static au.com.dius.pact.consumer.dsl.Matchers.string; |
| 21 | +import static au.com.dius.pact.consumer.dsl.Matchers.timestamp; |
| 22 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | +import static org.hamcrest.Matchers.greaterThan; |
| 24 | +import static org.hamcrest.Matchers.hasSize; |
| 25 | +import static org.hamcrest.Matchers.is; |
| 26 | +import static org.hamcrest.Matchers.isEmptyString; |
| 27 | +import static org.hamcrest.Matchers.not; |
| 28 | + |
| 29 | +public class TodoXmlTest { |
| 30 | + @Rule |
| 31 | + public PactProviderRule provider = new PactProviderRule("TodoProvider", "localhost", 1234, this); |
| 32 | + |
| 33 | + // body: <?xml version="1.0" encoding="UTF-8"?> |
| 34 | + // <projects foo="bar"> |
| 35 | + // <project id="1" name="Project 1" due="2016-02-11T09:46:56.023Z"> |
| 36 | + // <tasks> |
| 37 | + // <task id="1" name="Do the laundry" done="true"/> |
| 38 | + // <task id="2" name="Do the dishes" done="false"/> |
| 39 | + // <task id="3" name="Do the backyard" done="false"/> |
| 40 | + // <task id="4" name="Do nothing" done="false"/> |
| 41 | + // </tasks> |
| 42 | + // </project> |
| 43 | + // <project/> |
| 44 | + // </projects> |
| 45 | + |
| 46 | + @Pact(provider = "TodoProvider", consumer = "TodoConsumer") |
| 47 | + public RequestResponsePact projects(PactDslWithProvider builder) { |
| 48 | + return builder |
| 49 | + .given("i have a list of projects") |
| 50 | + .uponReceiving("a request for projects in XML") |
| 51 | + .path("/projects") |
| 52 | + .query("from=today") |
| 53 | + .headers(mapOf("Accept", "application/xml")) |
| 54 | + .willRespondWith() |
| 55 | + .headers(mapOf("Content-Type", "application/xml")) |
| 56 | + .status(200) |
| 57 | + .body( |
| 58 | + new PactXmlBuilder("projects", "http://some.namespace/and/more/stuff").build(root -> { |
| 59 | + root.setAttributes(mapOf("id", "1234")); |
| 60 | + root.eachLike("project", 2, mapOf( |
| 61 | + "id", integer(), |
| 62 | + "type", "activity", |
| 63 | + "name", string("Project 1"), |
| 64 | + "due", timestamp("yyyy-MM-dd'T'HH:mm:ss.SSSX", "2016-02-11T09:46:56.023Z") |
| 65 | + ), project -> { |
| 66 | + project.appendElement("tasks", Collections.emptyMap(), task -> { |
| 67 | + task.eachLike("task", 5, mapOf( |
| 68 | + "id", integer(), |
| 69 | + "name", string("Task 1"), |
| 70 | + "done", bool(true) |
| 71 | + )); |
| 72 | + }); |
| 73 | + }); |
| 74 | + }) |
| 75 | + ) |
| 76 | + .toPact(); |
| 77 | + } |
| 78 | + |
| 79 | + @PactVerification("TodoProvider") |
| 80 | + @Test |
| 81 | + public void testGeneratesAListOfTODOsForTheMainScreen() throws IOException { |
| 82 | + Projects projects = new TodoApp() |
| 83 | + .setUrl(provider.getMockServer().getUrl()) |
| 84 | + .getProjects("xml"); |
| 85 | + assertThat(projects.getId(), is("1234")); |
| 86 | + assertThat(projects.getProjects(), hasSize(2)); |
| 87 | + projects.getProjects().forEach(project -> { |
| 88 | + assertThat(project.getId(), is(greaterThan(0))); |
| 89 | + assertThat(project.getType(), is("activity")); |
| 90 | + assertThat(project.getName(), is("Project 1")); |
| 91 | + assertThat(project.getDue(), not(isEmptyString())); |
| 92 | + assertThat(project.getTasks().getTasks(), hasSize(5)); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + private <T> Map<String, T> mapOf(String key, T value) { |
| 97 | + return MapUtils.putAll(new HashMap<>(), new Object[] { key, value }); |
| 98 | + } |
| 99 | + |
| 100 | + private Map<String, Object> mapOf(String key1, Object value1, String key2, Object value2) { |
| 101 | + return MapUtils.putAll(new HashMap<>(), new Object[] { key1, value1, key2, value2 }); |
| 102 | + } |
| 103 | + |
| 104 | + private Map<String, Object> mapOf(String key1, Object value1, String key2, Object value2, String key3, Object value3) { |
| 105 | + return MapUtils.putAll(new HashMap<>(), new Object[] { key1, value1, key2, value2, key3, value3 }); |
| 106 | + } |
| 107 | + |
| 108 | + private Map<String, Object> mapOf(String key1, Object value1, String key2, Object value2, String key3, Object value3, |
| 109 | + String key4, Object value4) { |
| 110 | + return MapUtils.putAll(new HashMap<>(), new Object[] { key1, value1, key2, value2, key3, value3, key4, value4 }); |
| 111 | + } |
| 112 | +} |
0 commit comments