Hello, I a Java project using pact junit5 4.2.11,...
# pact-jvm
a
Hello, I a Java project using pact junit5 4.2.11, and I have encoding issues. When generating a Pact, the response body is read from a json file containing accented characters (é, è, ü, etc) When the pact is generated, those characters are replaced with their unicode encoding. Then on the producer side, when running the test, the pact verification fails due to those encoded characters. Do you know why the pact can’t just be written with the unicode characters ? I found nothing helpful when searching on Internet, but I’m surely not the only person with accented characters in the pact payload. Thanks in advance for your help 🙏
u
Are you able to provide your test code, pact file and any logs? Can you check what charset is being used, and whether that charset is configured with the content type header?
a
Sure, here is the test code :
Copy code
@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);
      }
   }
}
And here is an extract of the generated pact with encoded characters :
Copy code
{
  "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"
                  }
                }
              ],
While debugging I can see that until this line https://github.com/pact-foundation/pact-jvm/blob/4712d32575856d4fc5796ded5685caeca[…]model/src/main/kotlin/au/com/dius/pact/core/model/PactWriter.kt the characters look fine. Therefore I guess something happens in the Json.prettyPrint() function, but I can’t debug inside.
u
Ok, can you raise a GH issue for this?
a