package com.baiyue.lastleg.delivery; import com.baiyue.common.web.LongIdJacksonConfiguration; import com.baiyue.lastleg.delivery.dto.DeliveryRequests; import com.baiyue.lastleg.delivery.dto.DeliveryResponses; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @DisplayName("派送单长整型 ID JSON 契约") class DeliveryLongIdJsonContractTest { private static final long DELIVERY_ID = 19_000_000_000_000_001L; private static final long FEE_ID = 19_000_000_000_000_002L; private static final long OPERATION_ID = 19_000_000_000_000_003L; private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of( JacksonAutoConfiguration.class, LongIdJacksonConfiguration.class)); @Test void deliveryResponseIdsShouldBeSerializedAsJsonStrings() { contextRunner.run(context -> { ObjectMapper objectMapper = context.getBean(ObjectMapper.class); DeliveryResponses.DeliveryDetail detail = new DeliveryResponses.DeliveryDetail( deliveryRow(), List.of(), List.of(new DeliveryResponses.DeliveryFeeRow( FEE_ID, "RECEIVABLE", "DELIVERY_FEE", "USD", null, null)), List.of(new DeliveryResponses.DeliveryOperationRow( OPERATION_ID, null, "CREATED", "tester", "success"))); JsonNode json = objectMapper.readTree(objectMapper.writeValueAsString(detail)); assertThat(json.at("/order/id").isTextual()).isTrue(); assertThat(json.at("/order/id").textValue()).isEqualTo(Long.toString(DELIVERY_ID)); assertThat(json.at("/fees/0/id").textValue()).isEqualTo(Long.toString(FEE_ID)); assertThat(json.at("/operations/0/id").textValue()).isEqualTo(Long.toString(OPERATION_ID)); }); } @Test void quotedRequestIdShouldBeDeserializedToLong() { contextRunner.run(context -> { ObjectMapper objectMapper = context.getBean(ObjectMapper.class); DeliveryRequests.OneClickItem item = objectMapper.readValue( "{\"inventoryPositionId\":\"19000000000000001\",\"packages\":\"2\",\"sku\":\"SKU-1\"}", DeliveryRequests.OneClickItem.class); assertThat(item.inventoryPositionId()).isEqualTo(DELIVERY_ID); assertThat(item.packages()).isEqualTo(2L); }); } private DeliveryResponses.DeliveryRow deliveryRow() { return new DeliveryResponses.DeliveryRow( DELIVERY_ID, null, null, "PS-1", null, null, null, null, "CREATED", null, null, null, null, null, null, null, null, null, null, null, null, 0, 0, null, null, null, null, null, null, null, null, 0, java.util.List.of(), null, com.baiyue.lastleg.delivery.model.DeliveryDocumentSnapshot.summary(null), null, null, null, null, null, null, null, null); } }