This commit is contained in:
2026-08-04 21:01:53 +08:00
parent 7e544780b4
commit 9b7b08665e
21 changed files with 268 additions and 32 deletions
@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@@ -29,13 +30,30 @@ class PlatformLocationControllerContractTest {
assertMethod("delete", DeleteMapping.class, "platform:location:delete");
}
@Test
void 分页查询的企业租户参数可选但写接口仍然必填() {
Method page = method("page");
assertThat(page.getParameterTypes()[1]).isEqualTo(Long.class);
assertThat(page.getParameters()[1].getAnnotation(RequestParam.class).required()).isFalse();
for (String writeMethod : new String[] {"create", "update", "changeStatus", "delete"}) {
Method method = method(writeMethod);
assertThat(method.getParameterTypes()[1]).isEqualTo(long.class);
assertThat(method.getParameters()[1].getAnnotation(RequestParam.class).required()).isTrue();
}
}
private static void assertMethod(String name, Class<? extends Annotation> mapping, String permission) {
Method method = java.util.Arrays.stream(PlatformLocationController.class.getDeclaredMethods())
.filter(candidate -> candidate.getName().equals(name))
.findFirst().orElseThrow();
Method method = method(name);
assertThat(method.getAnnotation(mapping)).isNotNull();
assertThat(method.getAnnotation(PreAuthorize.class).value())
.isEqualTo("hasAuthority('" + permission + "')");
assertThat(method.getAnnotation(Operation.class).summary()).isNotBlank();
}
private static Method method(String name) {
return java.util.Arrays.stream(PlatformLocationController.class.getDeclaredMethods())
.filter(candidate -> candidate.getName().equals(name))
.findFirst().orElseThrow();
}
}
@@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@@ -24,15 +25,28 @@ class PlatformPalletControllerContractTest {
assertMethod("create", PostMapping.class, "platform:pallet:create");
assertMethod("update", PutMapping.class, "platform:pallet:edit");
assertMethod("delete", DeleteMapping.class, "platform:pallet:delete");
Method page = method("page");
assertThat(page.getParameterTypes()[1]).isEqualTo(Long.class);
assertThat(page.getParameters()[1].getAnnotation(RequestParam.class).required()).isFalse();
for (String writeMethod : new String[]{"detail", "create", "update", "delete"}) {
Method method = method(writeMethod);
assertThat(method.getParameterTypes()[1]).isEqualTo(long.class);
assertThat(method.getParameters()[1].getAnnotation(RequestParam.class).required()).isTrue();
}
assertThat(Arrays.stream(PlatformPalletController.class.getDeclaredMethods())
.noneMatch(method -> method.getName().equals("status"))).isTrue();
}
private static void assertMethod(String name, Class<? extends Annotation> mapping, String authority) {
Method method = Arrays.stream(PlatformPalletController.class.getDeclaredMethods())
.filter(candidate -> candidate.getName().equals(name)).findFirst().orElseThrow();
Method method = method(name);
assertThat(method.getAnnotation(mapping)).isNotNull();
assertThat(method.getAnnotation(PreAuthorize.class).value())
.isEqualTo("hasAuthority('" + authority + "')");
}
private static Method method(String name) {
return Arrays.stream(PlatformPalletController.class.getDeclaredMethods())
.filter(candidate -> candidate.getName().equals(name)).findFirst().orElseThrow();
}
}
@@ -1,5 +1,6 @@
package com.baiyue.warehouse.mapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.junit.jupiter.api.Test;
@@ -9,6 +10,22 @@ import java.lang.reflect.Method;
import static org.assertj.core.api.Assertions.assertThat;
class LocationMapperContractTest {
@Test
void 平台分页的企业和仓库条件均为可选且仍返回仓库快照() throws NoSuchMethodException {
Method page = LocationMapper.class.getMethod("selectLocationPage", Page.class,
Long.class, Long.class, String.class, String.class, String.class, String.class);
String pageSql = sql(page.getAnnotation(Select.class).value());
assertThat(pageSql)
.contains("SELECT l.*, w.warehouse_code, w.warehouse_name")
.contains("w.id = l.warehouse_id AND w.tenant_id = l.tenant_id")
.contains("<if test=\"tenantId != null\">")
.contains("AND l.tenant_id = #{tenantId}")
.contains("<if test=\"warehouseId != null\">")
.contains("AND l.warehouse_id = #{warehouseId}")
.contains("ORDER BY l.created_at DESC, l.id DESC");
}
@Test
void 可用库位按库区和库位数字自然顺序临近分配() throws NoSuchMethodException {
String selectSql = sql(LocationMapper.class.getMethod(
@@ -0,0 +1,78 @@
package com.baiyue.warehouse.repository;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baiyue.warehouse.dto.PalletRequests;
import com.baiyue.warehouse.entity.PalletEntity;
import com.baiyue.warehouse.mapper.PalletMapper;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.util.Collection;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class PalletRepositoryTest {
@BeforeAll
static void 初始化实体字段缓存() {
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new Configuration(), "test"), PalletEntity.class);
}
@Test
void 平台分页默认不限制企业和仓库() {
Collection<Object> values = queryValues(null, null);
assertThat(values).isEmpty();
}
@Test
void 平台分页可单独按企业筛选() {
Collection<Object> values = queryValues(10001L, null);
assertThat(values).containsExactly(10001L);
}
@Test
void 平台分页可同时按企业和仓库筛选() {
Collection<Object> values = queryValues(10001L, 20L);
assertThat(values).containsExactlyInAnyOrder(10001L, 20L);
}
@Test
void 平台分页可按全局唯一仓库筛选且写查询仍保留联合边界() {
Collection<Object> pageValues = queryValues(null, 20L);
assertThat(pageValues).containsExactly(20L);
PalletMapper mapper = mapper();
new PalletRepository(mapper).findOwned(10001L, 20L, 30L);
@SuppressWarnings("unchecked")
ArgumentCaptor<LambdaQueryWrapper<PalletEntity>> wrapper = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(mapper).selectOne(wrapper.capture());
wrapper.getValue().getSqlSegment();
assertThat(wrapper.getValue().getParamNameValuePairs().values())
.containsExactlyInAnyOrder(10001L, 20L, 30L);
}
private static Collection<Object> queryValues(Long tenantId, Long warehouseId) {
PalletMapper mapper = mapper();
new PalletRepository(mapper).page(tenantId,
new PalletRequests.PageQuery(1, 20, warehouseId, null, null));
@SuppressWarnings("unchecked")
ArgumentCaptor<LambdaQueryWrapper<PalletEntity>> wrapper = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(mapper).selectPage(any(Page.class), wrapper.capture());
wrapper.getValue().getSqlSegment();
return wrapper.getValue().getParamNameValuePairs().values();
}
private static PalletMapper mapper() {
PalletMapper mapper = mock(PalletMapper.class);
when(mapper.selectPage(any(Page.class), any())).thenAnswer(invocation -> invocation.getArgument(0));
return mapper;
}
}
@@ -1,6 +1,7 @@
package com.baiyue.warehouse.service;
import com.baiyue.common.core.exception.BizException;
import com.baiyue.warehouse.dto.LocationDtos.PageQuery;
import com.baiyue.warehouse.dto.LocationDtos.SaveRequest;
import com.baiyue.warehouse.entity.LocationEntity;
import com.baiyue.warehouse.entity.WarehouseEntity;
@@ -14,9 +15,44 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
class LocationApplicationServiceTest {
@Test
void 平台分页不传企业和仓库时查询全部库位() {
LocationRepository repository = mock(LocationRepository.class);
LocationApplicationService service = new LocationApplicationService(repository);
PageQuery query = pageQuery(null);
service.page(null, query);
verify(repository).page(null, query);
verifyNoMoreInteractions(repository);
}
@Test
void 平台分页可只按企业筛选全部仓库() {
LocationRepository repository = mock(LocationRepository.class);
LocationApplicationService service = new LocationApplicationService(repository);
PageQuery query = pageQuery(null);
service.page(10001L, query);
verify(repository).page(10001L, query);
}
@Test
void 平台分页可同时按企业和仓库筛选() {
LocationRepository repository = mock(LocationRepository.class);
LocationApplicationService service = new LocationApplicationService(repository);
PageQuery query = pageQuery(88L);
service.page(10001L, query);
verify(repository).page(10001L, query);
}
@Test
void 新增时仓库必须属于当前企业租户() {
LocationRepository repository = mock(LocationRepository.class);
@@ -79,6 +115,10 @@ class LocationApplicationServiceTest {
"PALLET", new BigDecimal("500"), status, "备注", version);
}
private static PageQuery pageQuery(Long warehouseId) {
return new PageQuery(1, 20, warehouseId, null, null, null, null);
}
private static WarehouseEntity warehouse(String status) {
WarehouseEntity warehouse = new WarehouseEntity();
warehouse.setId(88L);
@@ -1,6 +1,8 @@
package com.baiyue.warehouse.service;
import com.baiyue.common.core.exception.BizException;
import com.baiyue.common.core.api.PageResult;
import com.baiyue.warehouse.dto.PalletRequests;
import com.baiyue.warehouse.entity.PalletEntity;
import com.baiyue.warehouse.repository.PalletRepository;
import com.baiyue.warehouse.repository.WarehouseRepository;
@@ -8,6 +10,7 @@ import org.junit.jupiter.api.Test;
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;
class PalletApplicationServiceTest {
@@ -28,6 +31,17 @@ class PalletApplicationServiceTest {
assertThatThrownBy(() -> service.detail(10001, 21, 30)).isInstanceOf(BizException.class);
}
@Test
void 平台分页允许不指定企业并保留分页结果() {
PalletRequests.PageQuery query = new PalletRequests.PageQuery(1, 20, null, null, null);
when(repository.page(null, query)).thenReturn(new PageResult<>(java.util.List.of(pallet("AVAILABLE")),
1, 20, 1, 1));
org.assertj.core.api.Assertions.assertThat(service.page(null, query).records())
.singleElement().extracting("tenantId", "warehouseId").containsExactly(10001L, 20L);
verify(repository).page(null, query);
}
private static PalletEntity pallet(String status) {
PalletEntity entity = new PalletEntity();
entity.setId(30L);