This commit is contained in:
2026-08-05 09:11:26 +08:00
parent d544bbd0c5
commit 3791fee285
45 changed files with 1187 additions and 126 deletions
@@ -0,0 +1,31 @@
package com.baiyue.warehouse.controller;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.RequestParam;
import java.lang.reflect.Method;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
class PlatformExpressCompanyAllTenantQueryContractTest {
@Test
void 分页租户参数可选但详情写入启停和导入仍然必填() {
Method page = method("page");
assertThat(page.getParameterTypes()[1]).isEqualTo(Long.class);
assertThat(page.getParameters()[1].getAnnotation(RequestParam.class).required()).isFalse();
for (String scopedMethod : new String[] {"detail", "create", "update", "changeStatus", "importCompanies"}) {
Method method = method(scopedMethod);
assertThat(method.getParameterTypes()[1]).isEqualTo(long.class);
assertThat(method.getParameters()[1].getAnnotation(RequestParam.class).required()).isTrue();
}
}
private static Method method(String name) {
return Arrays.stream(PlatformExpressCompanyController.class.getDeclaredMethods())
.filter(candidate -> candidate.getName().equals(name))
.findFirst()
.orElseThrow();
}
}
@@ -75,7 +75,7 @@ class PlatformMasterDataControllerContractTest {
Method page = controller.getDeclaredMethod("page",
com.baiyue.common.security.AuthenticatedUser.class,
long.class,
Long.class,
controller == PlatformPartnerController.class
? com.baiyue.warehouse.dto.MasterDataQueries.PartnerPageQuery.class
: com.baiyue.warehouse.dto.MasterDataQueries.ExpressCompanyPageQuery.class);
@@ -0,0 +1,64 @@
package com.baiyue.warehouse.controller;
import com.baiyue.warehouse.dto.MasterDataQueries.PartnerPageQuery;
import com.baiyue.warehouse.mapper.PartnerMapper;
import com.baiyue.warehouse.repository.PartnerRepository;
import com.baiyue.warehouse.service.PlatformPartnerQueryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Select;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.RequestParam;
import java.lang.reflect.Method;
import static org.assertj.core.api.Assertions.assertThat;
class PlatformPartnerAllTenantQueryContractTest {
@Test
void 平台分页租户可选但详情写入和导入仍要求租户() {
Method page = method(PlatformPartnerController.class, "page");
assertThat(page.getParameterTypes()[1]).isEqualTo(Long.class);
assertThat(page.getParameters()[1].getAnnotation(RequestParam.class).required()).isFalse();
for (String scopedMethod : new String[] {"importPartners", "detail", "create", "update", "changeStatus"}) {
Method method = method(PlatformPartnerController.class, scopedMethod);
assertThat(method.getParameterTypes()[1]).isEqualTo(long.class);
assertThat(method.getParameters()[1].getAnnotation(RequestParam.class).required()).isTrue();
}
}
@Test
void 分页链路使用可选租户而其他仓储方法保持原有强租户边界() throws Exception {
assertThat(PlatformPartnerQueryService.class.getMethod("page", Long.class, PartnerPageQuery.class))
.isNotNull();
assertThat(PartnerRepository.class.getMethod("page", Long.class, PartnerPageQuery.class))
.isNotNull();
assertThat(PartnerRepository.class.getMethod("findOwned", long.class, long.class))
.isNotNull();
assertThat(PartnerRepository.class.getMethod("existsCode", long.class, String.class, Long.class))
.isNotNull();
}
@Test
void 类型筛选在数据库内关联分页且租户条件可选() throws Exception {
Method method = PartnerMapper.class.getMethod("selectPartnerPageByType",
Page.class, Long.class, String.class, String.class, String.class);
String sql = String.join("\n", method.getAnnotation(Select.class).value());
assertThat(sql)
.contains("AND EXISTS (")
.contains("pt.partner_id = p.id")
.contains("pt.partner_type = #{type}")
.contains("pt.tenant_id = p.tenant_id")
.contains("<if test=\"tenantId != null\">")
.contains("AND p.tenant_id = #{tenantId}")
.doesNotContain("partner_id IN");
}
private static Method method(Class<?> type, String name) {
return java.util.Arrays.stream(type.getDeclaredMethods())
.filter(candidate -> candidate.getName().equals(name))
.findFirst()
.orElseThrow();
}
}
@@ -0,0 +1,24 @@
package com.baiyue.warehouse.mapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Select;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class PartnerMapperContractTest {
@Test
void bothTypeRequiresCustomerAndSupplierAssociations() throws Exception {
Select select = PartnerMapper.class.getDeclaredMethod("selectPartnerPageByType",
Page.class, Long.class, String.class, String.class, String.class)
.getAnnotation(Select.class);
String sql = String.join("\n", select.value());
assertThat(sql)
.contains("<when test=\"type == 'BOTH'\">")
.contains("customer_type.partner_type = 'CUSTOMER'")
.contains("supplier_type.partner_type = 'SUPPLIER'")
.contains("<otherwise>")
.contains("pt.partner_type = #{type}");
}
}
@@ -25,7 +25,7 @@ class ExpressCompanyRepositoryTest {
}
@Test
void 平台分页强制租户并支持筛选() {
void 平台分页传入租户时按租户并支持筛选() {
ExpressCompanyMapper mapper = mock(ExpressCompanyMapper.class);
when(mapper.selectPage(any(Page.class), any())).thenAnswer(invocation -> invocation.getArgument(0));
@@ -40,4 +40,22 @@ class ExpressCompanyRepositoryTest {
assertThat(wrapper.getValue().getParamNameValuePairs().values())
.contains(10001L, "ACTIVE", "%顺丰%");
}
@Test
void 平台分页不传租户时查询全部企业并保留其他筛选() {
ExpressCompanyMapper mapper = mock(ExpressCompanyMapper.class);
when(mapper.selectPage(any(Page.class), any())).thenAnswer(invocation -> invocation.getArgument(0));
new ExpressCompanyRepository(mapper).page(null,
new ExpressCompanyPageQuery(1, 20, "顺丰", "ACTIVE"));
@SuppressWarnings("unchecked")
ArgumentCaptor<LambdaQueryWrapper<ExpressCompanyEntity>> wrapper =
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(mapper).selectPage(any(Page.class), wrapper.capture());
wrapper.getValue().getSqlSegment();
assertThat(wrapper.getValue().getParamNameValuePairs().values())
.contains("ACTIVE", "%顺丰%")
.doesNotContain(10001L);
}
}
@@ -56,17 +56,27 @@ class PartnerRepositoryTest {
@Test void 平台分页强制租户并支持筛选() {
PartnerMapper mapper=mock(PartnerMapper.class);
when(mapper.selectPartnerIdsByType(10001L, "CUSTOMER")).thenReturn(List.of(9L));
when(mapper.selectPage(any(Page.class), any())).thenAnswer(invocation -> invocation.getArgument(0));
when(mapper.selectPartnerPageByType(any(Page.class), eq(10001L), eq("CUSTOMER"),
eq("ACTIVE"), eq("示例"))).thenAnswer(invocation -> invocation.getArgument(0));
new PartnerRepository(mapper).page(10001L,
new PartnerPageQuery(1, 20, "示例", "CUSTOMER", "ACTIVE"));
@SuppressWarnings("unchecked")
ArgumentCaptor<LambdaQueryWrapper<PartnerEntity>> wrapper=ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(mapper).selectPage(any(Page.class), wrapper.capture());
wrapper.getValue().getSqlSegment();
assertThat(wrapper.getValue().getParamNameValuePairs().values())
.contains(10001L, 9L, "ACTIVE", "%示例%");
verify(mapper).selectPartnerPageByType(any(Page.class), eq(10001L), eq("CUSTOMER"),
eq("ACTIVE"), eq("示例"));
verify(mapper, never()).selectPartnerIdsByType(anyLong(), anyString());
}
@Test void 平台分页不传租户时查询全部企业并支持承运商类型筛选() {
PartnerMapper mapper=mock(PartnerMapper.class);
when(mapper.selectPartnerPageByType(any(Page.class), isNull(), eq("CARRIER"),
eq("ACTIVE"), isNull())).thenAnswer(invocation -> invocation.getArgument(0));
new PartnerRepository(mapper).page(null,
new PartnerPageQuery(1, 20, null, "CARRIER", "ACTIVE"));
verify(mapper).selectPartnerPageByType(any(Page.class), isNull(), eq("CARRIER"),
eq("ACTIVE"), isNull());
verify(mapper, never()).selectPartnerIdsByType(anyLong(), anyString());
}
}