Aurelia
04/20/2022, 1:53 PMuglyog
Aurelia
04/21/2022, 12:58 PM@ExtendWith(PactConsumerTestExt.class)
@SpringBootTest
public class ProductWithAccentPactTest {
@Autowired
private ObjectMapper objectMapper;
@Pact(provider = "one-catalog_product_with_accents", consumer = "product-display")
public RequestResponsePact createPact(PactDslWithProvider builder) {
return builder.given("an hybrid product")
.uponReceiving("a request for product 100001789")
.path("/one-catalog/public/v1/catalog-items")
.query(
"ids=100001090&warehouseId=1&ongoingOfferDate=2022-04-19T00:00&fields=(id, product)&activeOffersOnly=true")
.method("GET")
.willRespondWith()
.status(200)
.body(jsonAsString("/pact/product-with-accents.json"))
.headers(Map.of("content-type", "application/json"))
.toPact();
}
@PactTestFor(providerName = "one-catalog_product_with_accents")
@Test
void validatePact(MockServer mockServer) throws IOException {
// when
var actual = oneCatalogClient(mockServer.getUrl()).findOneCatalogItems(
ProductFilter.builder().uids(List.of(100001090l)).build(),
OfferFilter.builder()
.storeType(<http://StoreType.ONLINE|StoreType.ONLINE>)
.warehouseId(1)
.ongoingOfferDate(LocalDateTime.of(2022, 4, 19, 0, 0, 0))
.build(), "(id, product)");
// then
var expected = objectMapper.readValue(
ResourceReader.readResource("pact/product-with-accents.json"),
OneCatalogItem[].class);
assertThat(actual).hasSize(1).first().usingRecursiveComparison().isEqualTo(
Arrays.stream(expected).findFirst().get());
}
private String jsonAsString(String path) {
return removeWhiteSpaces(IOUtils.toString(
this.getClass().getResourceAsStream(path),
StandardCharsets.UTF_8));
}
private String removeWhiteSpaces(String input) {
return input.replaceAll("\r", "").replaceAll("\n", "").replaceAll("\t", "");
}
private RestOneCatalogClient oneCatalogClient(String uri) {
return new RestOneCatalogClient(URI.create(uri), new RestTemplateBuilder());
}
@SpringBootConfiguration
public static class TestContext {
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper()
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule())
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
}
}
}
Aurelia
04/21/2022, 1:00 PM{
"consumer": {
"name": "product-display"
},
"interactions": [
{
"description": "a request for product 100001789",
"providerStates": [
{
"name": "an hybrid product"
}
],
"request": {
"method": "GET",
"path": "/one-catalog/public/v1/catalog-items",
"query": {
"activeOffersOnly": [
"true"
],
"fields": [
"(id, product)"
],
"ids": [
"100001090"
],
"ongoingOfferDate": [
"2022-04-19T00:00"
],
"warehouseId": [
"1"
]
}
},
"response": {
"body": [
{
"id": 100001090,
"product": {
"brand": "Jura-Sel",
"categoryBreadcrumbs": [
{
"categoryId": "3850252",
"categoryName": {
"de": "Salzige Lebensmittel",
"en": "Salty Groceries",
"fr": "Epicerie sal\u00E9e",
"it": "Alimenti salati"
},
"level": 2,
"slug": {
"de": "salzige-lebensmittel",
"en": "salty-groceries",
"fr": "epicerie-salee",
"it": "alimenti-salati"
}
},
{
"categoryId": "3860189",
"categoryName": {
"de": "Gew\u00FCrze & Saucen",
"en": "Spices & Sauces",
"fr": "Condiments & Sauces",
"it": "Spezie e salse"
},
"level": 3,
"slug": {
"de": "gewurze-saucen",
"en": "spices-sauces",
"fr": "condiments-sauces",
"it": "spezie-e-salse"
}
},
{
"categoryId": "3860200",
"categoryName": {
"de": "Salz, Pfeffer & W\u00FCrze",
"en": "Spices, Salt & Pepper",
"fr": "Sels, Poivres & Assaisonnements",
"it": "Sale, pepe e condimenti"
},
"level": 4,
"slug": {
"de": "salz-pfeffer-wurze",
"en": "spices-salt-pepper",
"fr": "sels-poivres-assaisonnements",
"it": "sale-pepe-e-condimenti"
}
}
],
Aurelia
04/21/2022, 1:25 PMuglyog
Aurelia
04/26/2022, 5:42 AM