forked from pact-foundation/pact-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PactMultiProviderTest.java
183 lines (168 loc) · 8.69 KB
/
PactMultiProviderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package au.com.dius.pact.consumer.pactproviderrule;
import au.com.dius.pact.consumer.Pact;
import au.com.dius.pact.consumer.PactVerification;
import au.com.dius.pact.consumer.PactVerificationResult;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.exampleclients.ConsumerClient;
import au.com.dius.pact.matchers.BodyMismatch;
import au.com.dius.pact.matchers.Mismatch;
import au.com.dius.pact.model.RequestResponsePact;
import org.junit.Rule;
import org.junit.Test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
public class PactMultiProviderTest {
private static final String NAME_LARRY_JSON = "{\"name\": \"larry\"}";
@Rule
public TestFailureProviderRule mockTestProvider = new TestFailureProviderRule("test_provider", this);
@Rule
public TestFailureProviderRule mockTestProvider2 = new TestFailureProviderRule("test_provider2", this);
@Pact(provider="test_provider", consumer="test_consumer")
public RequestResponsePact createFragment(PactDslWithProvider builder) {
Map<String, String> headers = new HashMap<String, String>();
headers.put("testreqheader", "testreqheadervalue");
return builder
.given("good state")
.uponReceiving("PactProviderTest test interaction")
.path("/")
.method("GET")
.headers(headers)
.willRespondWith()
.status(200)
.headers(headers)
.body("{\"responsetest\": true, \"name\": \"harry\"}")
.uponReceiving("PactProviderTest second test interaction")
.method("OPTIONS")
.headers(headers)
.path("/second")
.body("")
.willRespondWith()
.status(200)
.headers(headers)
.body("")
.toPact();
}
@Pact(provider="test_provider2", consumer="test_consumer")
public RequestResponsePact createFragment2(PactDslWithProvider builder) {
return builder
.given("good state")
.uponReceiving("PactProviderTest test interaction")
.path("/")
.method("PUT")
.body(NAME_LARRY_JSON)
.willRespondWith()
.status(200)
.body("{\"responsetest\": true, \"name\": \"larry\"}")
.toPact();
}
@Test
@PactVerification({"test_provider", "test_provider2"})
public void allPass() throws IOException {
mockTestProvider.validateResultWith((result, t) -> {
assertThat(t, is(nullValue()));
assertThat(result, is(PactVerificationResult.Ok.INSTANCE));
});
doTest("/", NAME_LARRY_JSON);
}
@Test
@PactVerification({"test_provider", "test_provider2"})
public void consumerTestFails() throws IOException, InterruptedException {
mockTestProvider.validateResultWith((result, t) -> {
assertThat(t, is(instanceOf(AssertionError.class)));
assertThat(t.getMessage(), is("Pact Test function failed with an exception: Oops"));
assertThat(result, is(instanceOf(PactVerificationResult.Error.class)));
PactVerificationResult.Error error = (PactVerificationResult.Error) result;
assertThat(error.getError(), is(instanceOf(RuntimeException.class)));
assertThat(error.getError().getMessage(), is("Oops"));
assertThat(error.getMockServerState(), is(PactVerificationResult.Ok.INSTANCE));
});
doTest("/", NAME_LARRY_JSON);
throw new RuntimeException("Oops");
}
@Test
@PactVerification(value = {"test_provider", "test_provider2"})
public void provider1Fails() throws IOException, InterruptedException {
mockTestProvider.validateResultWith((result, t) -> {
assertThat(t, is(instanceOf(AssertionError.class)));
assertThat(t.getMessage(), startsWith("The following mismatched requests occurred:\nUnexpected Request:\n\tmethod: GET\n\tpath: /abc"));
assertThat(result, is(instanceOf(PactVerificationResult.Mismatches.class)));
PactVerificationResult.Mismatches error = (PactVerificationResult.Mismatches) result;
assertThat(error.getMismatches(), hasSize(1));
PactVerificationResult result1 = error.getMismatches().get(0);
assertThat(result1, is(instanceOf(PactVerificationResult.UnexpectedRequest.class)));
PactVerificationResult.UnexpectedRequest unexpectedRequest = (PactVerificationResult.UnexpectedRequest) result1;
assertThat(unexpectedRequest.getRequest().getPath(), is("/abc"));
});
doTest("/abc", NAME_LARRY_JSON);
}
@Test
@PactVerification(value = {"test_provider", "test_provider2"})
public void provider2Fails() throws IOException, InterruptedException {
mockTestProvider2.validateResultWith((result, t) -> {
assertThat(t, is(instanceOf(AssertionError.class)));
assertThat(t.getMessage(), is("The following mismatched requests occurred:\n" +
"PartialMismatch(mismatches=[BodyMismatch(expected=larry, actual=farry, mismatch=Expected 'larry'" +
" but received 'farry', path=$.name, diff=null)])"));
assertThat(result, is(instanceOf(PactVerificationResult.Mismatches.class)));
PactVerificationResult.Mismatches error = (PactVerificationResult.Mismatches) result;
assertThat(error.getMismatches(), hasSize(1));
PactVerificationResult result1 = error.getMismatches().get(0);
assertThat(result1, is(instanceOf(PactVerificationResult.PartialMismatch.class)));
PactVerificationResult.PartialMismatch error1 = (PactVerificationResult.PartialMismatch) result1;
assertThat(error1.getMismatches(), hasSize(1));
Mismatch mismatch = error1.getMismatches().get(0);
assertThat(mismatch, is(instanceOf(BodyMismatch.class)));
});
doTest("/", "{\"name\": \"farry\"}");
}
@Test
@PactVerification(value = {"test_provider", "test_provider2"})
public void bothprovidersFail() throws IOException, InterruptedException {
mockTestProvider.validateResultWith((result, t) -> {
assertThat(t, is(instanceOf(AssertionError.class)));
assertThat(t.getMessage(), startsWith("The following mismatched requests occurred:\nUnexpected Request:\n\tmethod: GET\n\tpath: /abc"));
assertThat(result, is(instanceOf(PactVerificationResult.Mismatches.class)));
PactVerificationResult.Mismatches error = (PactVerificationResult.Mismatches) result;
assertThat(error.getMismatches(), hasSize(1));
PactVerificationResult result1 = error.getMismatches().get(0);
assertThat(result1, is(instanceOf(PactVerificationResult.UnexpectedRequest.class)));
PactVerificationResult.UnexpectedRequest unexpectedRequest = (PactVerificationResult.UnexpectedRequest) result1;
assertThat(unexpectedRequest.getRequest().getPath(), is("/abc"));
});
mockTestProvider2.validateResultWith((result, t) -> {
assertThat(t, is(instanceOf(AssertionError.class)));
assertThat(t.getMessage(), is("The following mismatched requests occurred:\n" +
"PartialMismatch(mismatches=[BodyMismatch(expected=larry, actual=farry, mismatch=Expected 'larry' " +
"but received 'farry', path=$.name, diff=null)])"));
assertThat(result, is(instanceOf(PactVerificationResult.Mismatches.class)));
PactVerificationResult.Mismatches error = (PactVerificationResult.Mismatches) result;
assertThat(error.getMismatches(), hasSize(1));
PactVerificationResult result1 = error.getMismatches().get(0);
assertThat(result1, is(instanceOf(PactVerificationResult.PartialMismatch.class)));
PactVerificationResult.PartialMismatch error1 = (PactVerificationResult.PartialMismatch) result1;
assertThat(error1.getMismatches(), hasSize(1));
Mismatch mismatch = error1.getMismatches().get(0);
assertThat(mismatch, is(instanceOf(BodyMismatch.class)));
});
doTest("/abc", "{\"name\": \"farry\"}");
}
private void doTest(String path, String json) throws IOException {
ConsumerClient consumerClient = new ConsumerClient(mockTestProvider.getUrl());
consumerClient.options("/second");
try {
consumerClient.getAsMap(path, "");
} catch (IOException e) {
}
try {
new ConsumerClient(mockTestProvider2.getUrl()).putAsMap("/", json);
} catch (IOException e) {
}
}
}