/
AsyncMessageTest.java
69 lines (57 loc) · 2.44 KB
/
AsyncMessageTest.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
package au.com.dius.pact.consumer.junit5;
import au.com.dius.pact.consumer.MessagePactBuilder;
import au.com.dius.pact.core.model.PactSpecVersion;
import au.com.dius.pact.core.model.annotations.Pact;
import au.com.dius.pact.consumer.dsl.Matchers;
import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
import au.com.dius.pact.core.model.messaging.Message;
import au.com.dius.pact.core.model.messaging.MessagePact;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.core.Is.is;
@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "MessageProvider", providerType = ProviderType.ASYNCH, pactVersion = PactSpecVersion.V3)
public class AsyncMessageTest {
@Pact(consumer = "test_consumer_v3")
MessagePact createPact(MessagePactBuilder builder) {
PactDslJsonBody body = new PactDslJsonBody();
body.stringValue("testParam1", "value1");
body.stringValue("testParam2", "value2");
Map<String, Object> metadata = new HashMap<>();
metadata.put("destination", Matchers.regexp("\\w+\\d+", "X001"));
return builder.given("SomeProviderState")
.expectsToReceive("a test message")
.withMetadata(metadata)
.withContent(body)
.toPact();
}
@Pact(provider = "MessageProvider", consumer = "test_consumer_v3")
MessagePact createPact2(MessagePactBuilder builder) {
PactDslJsonBody body = new PactDslJsonBody();
body.stringValue("testParam1", "value3");
body.stringValue("testParam2", "value4");
Map<String, String> metadata = new HashMap<String, String>();
metadata.put("Content-Type", "application/json");
return builder.given("SomeProviderState2")
.expectsToReceive("a test message")
.withMetadata(metadata)
.withContent(body)
.toPact();
}
@Test
@PactTestFor(pactMethod = "createPact")
void test(List<Message> messages) {
assertThat(new String(messages.get(0).contentsAsBytes()), is("{\"testParam1\":\"value1\",\"testParam2\":\"value2\"}"));
assertThat(messages.get(0).getMetadata(), hasEntry("destination", "X001"));
}
@Test
@PactTestFor(pactMethod = "createPact2")
void test2(MessagePact pact) {
assertThat(new String(pact.getMessages().get(0).contentsAsBytes()), is("{\"testParam1\":\"value3\",\"testParam2\":\"value4\"}"));
}
}