Michael Branders
01/21/2022, 1:49 PMuglyog
Michael Branders
01/24/2022, 7:45 AM@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "wg-sector-service", providerType = ProviderType.ASYNCH)
class ParitairComiteChangedEventContractTest {
public static final String PARITAIR_COMITE_ID = "01234567890123456789012345678901";
@Pact(consumer = "dimona-service")
V4Pact paritairComiteChangedEventPact(MessagePactBuilder builder) {
PactDslJsonBody body = new PactDslJsonBody();
body.stringMatcher("id", "^[a-zA-Z0-9]{32}$", PARITAIR_COMITE_ID);
Map<String, Object> metadata = new HashMap<>();
metadata.put("Content-Type", "application/json");
return builder
.expectsToReceive("valid ParitairComiteChangedEvent from wg-sector-service")
.withMetadata(metadata)
.withContent(body)
.toPact(V4Pact.class);
}
@Test
@PactTestFor(pactMethod = "paritairComiteChangedEventPact")
void test(List<Message> messages) {
assertThat(new String(messages.get(0).contentsAsBytes()), is("{\"id\":\"" + PARITAIR_COMITE_ID + "\"}"));
}
}
Michael Branders
01/24/2022, 7:52 AM@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "wg-sector-service", providerType = ProviderType.ASYNCH, pactVersion = PactSpecVersion.V3)
class ParitairComiteChangedEventContractTest {
public static final String PARITAIR_COMITE_ID = "01234567890123456789012345678901";
@Pact(consumer = "dimona-service")
MessagePact paritairComiteChangedEventPact(MessagePactBuilder builder) {
PactDslJsonBody body = new PactDslJsonBody();
body.stringMatcher("id", "^[a-zA-Z0-9]{32}$", PARITAIR_COMITE_ID);
Map<String, Object> metadata = new HashMap<>();
metadata.put("Content-Type", "application/json");
return builder
.expectsToReceive("valid ParitairComiteChangedEvent from wg-sector-service")
.withMetadata(metadata)
.withContent(body)
.toPact();
}
@Test
@PactTestFor(pactMethod = "paritairComiteChangedEventPact")
void test(List<Message> messages) {
assertThat(new String(messages.get(0).contentsAsBytes()), is("{\"id\":\"" + PARITAIR_COMITE_ID + "\"}"));
}
}
Michael Branders
01/24/2022, 10:50 AMMichael Branders
01/24/2022, 3:24 PMMichael Branders
01/24/2022, 3:24 PM@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "wg-sector-service", providerType = ProviderType.ASYNCH)
class ParitairComiteChangedEventContractTest {
public static final String PARITAIR_COMITE_ID = "01234567890123456789012345678901";
@Pact(consumer = "dimona-service")
V4Pact paritairComiteChangedEventPact(PactBuilder builder) {
PactDslJsonBody body = new PactDslJsonBody();
body.stringMatcher("id", "^[a-zA-Z0-9]{32}$", PARITAIR_COMITE_ID);
Map<String, Object> metadata = new HashMap<>();
metadata.put("Content-Type", "application/json");
return builder.usingLegacyMessageDsl()
.expectsToReceive("valid ParitairComiteChangedEvent from wg-sector-service")
.withMetadata(metadata)
.withContent(body)
.toPact();
}
@Test
@PactTestFor(pactMethod = "paritairComiteChangedEventPact")
void test(V4Pact pact) {
V4Interaction.AsynchronousMessage message = (V4Interaction.AsynchronousMessage) pact.getInteractions()
.stream().filter(i -> i instanceof V4Interaction.AsynchronousMessage).findFirst().get();
assertThat(message.getContents().getContents().valueAsString(), is("{\"id\":\"" + PARITAIR_COMITE_ID + "\"}"));
}
}
uglyog
Michael Branders
01/25/2022, 7:10 AMTobias Friedrich
07/06/2022, 5:45 PMV4Pact
for messages and also came up with a similar solution like @Michael Branders. What confused me about that was the use of usingLegacyMessageDsl
though🤔 Maybe it's because of the naming, but to me this method kind of sounds like it should not be used 🤷♂️ Is there also any "non-legacy" way of creating V4Pacts for messages?
Sorry for reviving this old thread!uglyog