Files
baiyue-firstleg/firstleg/src/test/java/com/baiyue/firstleg/controller/InternalInboundOrderReferenceControllerTest.java
T

147 lines
7.0 KiB
Java

package com.baiyue.firstleg.controller;
import com.baiyue.common.core.exception.BizException;
import com.baiyue.firstleg.dto.InboundOrderReference;
import com.baiyue.firstleg.dto.OrderApprovedInboundCommand;
import com.baiyue.firstleg.service.InboundOrderCreationService;
import com.baiyue.firstleg.service.InboundCartonLabelService;
import com.baiyue.firstleg.service.UnifiedInboundQueryService;
import com.baiyue.firstleg.service.OrderDeclarationSyncService;
import com.baiyue.firstleg.service.OrderRemarksSyncService;
import com.baiyue.firstleg.dto.UnifiedInboundQuery;
import com.baiyue.firstleg.dto.OrderDeclarationSync;
import com.baiyue.common.core.api.PageResult;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import java.util.List;
import java.time.LocalDateTime;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@SpringJUnitConfig(InternalInboundOrderController.class)
@TestPropertySource(properties = "baiyue.security.internal-service-token=internal-token")
class InternalInboundOrderReferenceControllerTest {
@MockBean
private InboundOrderCreationService service;
@MockBean
private InboundCartonLabelService cartonLabels;
@MockBean
private UnifiedInboundQueryService unifiedQueryService;
@MockBean
private OrderDeclarationSyncService declarationSync;
@MockBean
private OrderRemarksSyncService remarksSync;
@MockBean private com.baiyue.firstleg.service.OrderFulfillmentSnapshotService fulfillmentSnapshot;
@MockBean private com.baiyue.firstleg.service.MerchantWorkbenchFilterService workbenchFilters;
@Resource
private InternalInboundOrderController controller;
@Test void workbenchFiltersRejectMissingAndWrongInternalIdentity() {
var request = new com.baiyue.firstleg.dto.MerchantWorkbenchFilters.Request(11L, "LOADED");
assertThatThrownBy(() -> controller.workbenchFilters(null, request)).isInstanceOf(BizException.class);
assertThatThrownBy(() -> controller.workbenchFilters("wrong", request)).isInstanceOf(BizException.class);
org.mockito.Mockito.verifyNoInteractions(workbenchFilters);
var response = new com.baiyue.firstleg.dto.MerchantWorkbenchFilters.Response(List.of(1L), List.of(), List.of());
when(workbenchFilters.read(request)).thenReturn(response);
assertThat(controller.workbenchFilters("internal-token", request).data()).isEqualTo(response);
}
@Test
void completionSnapshotRequiresInternalTokenAndDelegatesExactCommand() {
var command = new com.baiyue.firstleg.dto.OrderFulfillmentCompleted(11, 21,
"FL-21", "IN-21", LocalDateTime.of(2026, 9, 30, 12, 0));
assertThatThrownBy(() -> controller.completeFulfillmentSnapshot("wrong", command)).isInstanceOf(BizException.class);
org.mockito.Mockito.verifyNoInteractions(fulfillmentSnapshot);
when(fulfillmentSnapshot.complete(command)).thenReturn(0);
assertThat(controller.completeFulfillmentSnapshot("internal-token", command).data()).isZero();
verify(fulfillmentSnapshot).complete(command);
}
@Test
void shouldHandleCase01() {
var request = new InboundOrderReference.BatchRequest(10001L, List.of(77L, 77L, 78L));
when(service.findReferences(10001L, List.of(77L, 78L))).thenReturn(List.of(
new InboundOrderReference.Item(77L, 88L, "RK-77", "WAIT_MEASURE")));
var response = controller.references("internal-token", request);
assertThat(response.data()).extracting(InboundOrderReference.Item::inboundCode)
.containsExactly("RK-77");
verify(service).findReferences(10001L, List.of(77L, 78L));
}
@Test
void shouldHandleCase02() {
var request = new InboundOrderReference.BatchRequest(10001L, List.of(77L));
assertThatThrownBy(() -> controller.references("bad-token", request))
.isInstanceOf(BizException.class);
}
@Test
void unifiedQueryRequiresInternalTokenAndForwardsBatchRequest() {
UnifiedInboundQuery request = new UnifiedInboundQuery(
10001L, null, null, "WAIT_SPLIT", null, null, null,
null, null, null, null, null, null, null, null, null, null, null, null,
null, null, 1, 20, null);
when(unifiedQueryService.page(request)).thenReturn(new PageResult<>(List.of(), 1, 20, 0, 0));
assertThat(controller.unifiedPage("internal-token", request).data().records()).isEmpty();
assertThatThrownBy(() -> controller.unifiedPage("bad-token", request))
.isInstanceOf(BizException.class);
verify(unifiedQueryService).page(request);
}
@Test
void approvedOrderRequiresInboundCode() {
assertThatThrownBy(() -> controller.createFromApprovedOrder(
"internal-token", approvedCommand(" ", null)))
.isInstanceOf(BizException.class);
}
@Test
void approvedOrderAllowsMissingCarrier() {
OrderApprovedInboundCommand command = approvedCommand("RK-77", null);
when(service.createFromApprovedOrder(command)).thenReturn(88L);
assertThat(controller.createFromApprovedOrder("internal-token", command).data()).isEqualTo(88L);
verify(service).createFromApprovedOrder(command);
verify(cartonLabels).getOrCreate(10001L, 88L);
}
@Test
void exposesExactOrderChannelValidationContract() {
var request = new OrderDeclarationSync.ChannelChangeValidationRequest(10001L, 77L, 4L, "MATSON_NEW");
var result = new OrderDeclarationSync.ChannelChangeValidationResponse(77L, 4L, "MATSON_NEW", "WAIT_SPLIT");
when(declarationSync.validateChannelChange(request)).thenReturn(result);
assertThat(controller.validateOrderChannelChange("internal-token", request).data()).isEqualTo(result);
verify(declarationSync).validateChannelChange(request);
}
@Test
void exposesExactDeclarationEventConsumerContract() {
var event = new OrderDeclarationSync.UpdatedEvent(1, "event-4", 10001L, 77L, "FL-77", "RK-77",
4, 9L, LocalDateTime.of(2026, 9, 2, 10, 0), null, null, "MATSON_NEW", "新渠道",
true, List.of(), List.of());
assertThat(controller.updateOrderDeclaration("internal-token", event).successful()).isTrue();
verify(declarationSync).consume(event);
}
private static OrderApprovedInboundCommand approvedCommand(String inboundCode, Long carrierId) {
return new OrderApprovedInboundCommand(
10001L, 77L, "FL-77", inboundCode, 7L, "测试客户", 9L, "义乌仓",
null, null, null, "FBA", "AIR", "空运", carrierId,
carrierId == null ? null : "承运商", null, null, null, null,
null, null, null, null, null, null);
}
}