feat(mcp): 新增站点列表与详情工具 (REQ-20260908-0006)
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
package me.iiv.agent.mcp.tool;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import me.iiv.agent.entity.m0.Agent;
|
||||
import me.iiv.agent.entity.m0.DistrictDistrictStrategy;
|
||||
import me.iiv.agent.entity.m0.Properyt;
|
||||
import me.iiv.agent.entity.m0.User;
|
||||
import me.iiv.agent.mapper.m0.AgentMapper;
|
||||
import me.iiv.agent.mapper.m0.DistrictDistrictStrategyMapper;
|
||||
import me.iiv.agent.mapper.m0.ProperytMapper;
|
||||
import me.iiv.agent.mapper.m0.UserMapper;
|
||||
import me.iiv.agent.permission.PermissionService;
|
||||
import me.iiv.agent.security.AuthContext;
|
||||
import me.iiv.agent.security.AuthContextHolder;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.ai.tool.ToolCallback;
|
||||
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class DistrictToolsTest {
|
||||
|
||||
@Mock
|
||||
DistrictDistrictStrategyMapper districtMapper;
|
||||
@Mock
|
||||
ProperytMapper properytMapper;
|
||||
@Mock
|
||||
AgentMapper agentMapper;
|
||||
@Mock
|
||||
UserMapper userMapper;
|
||||
@Mock
|
||||
PermissionService permissionService;
|
||||
@Mock
|
||||
StringRedisTemplate redisTemplate;
|
||||
@Mock
|
||||
ValueOperations<String, String> valueOperations;
|
||||
|
||||
DistrictTools tools;
|
||||
User user;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
||||
DistrictDistrictStrategy.class);
|
||||
tools = new DistrictTools(districtMapper, properytMapper, agentMapper,
|
||||
userMapper, permissionService, redisTemplate);
|
||||
user = new User();
|
||||
user.setId(7L);
|
||||
AuthContextHolder.set(new AuthContext(null, user));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
AuthContextHolder.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void listUsesReviewedContractAndTruncatesRows() {
|
||||
DistrictDistrictStrategy district = new DistrictDistrictStrategy();
|
||||
district.setId(9L);
|
||||
district.setDistrictName("测试站点");
|
||||
district.setDistrictNumber("D-009");
|
||||
district.setPropertyId(11L);
|
||||
district.setAgentId(12L);
|
||||
district.setProvince("广东省");
|
||||
district.setCity("深圳市");
|
||||
district.setCountyseat("南山区");
|
||||
district.setAddress("科技园");
|
||||
district.setDistrictState(1);
|
||||
district.setOnlineState(1);
|
||||
district.setHardwareCount(6);
|
||||
district.setIsSmart(1);
|
||||
when(districtMapper.selectCount(any())).thenReturn(1L);
|
||||
when(districtMapper.selectList(any())).thenReturn(List.of(district));
|
||||
|
||||
Properyt property = new Properyt();
|
||||
property.setId(11L);
|
||||
property.setName("测试物业");
|
||||
when(properytMapper.selectBatchIds(any())).thenReturn(List.of(property));
|
||||
Agent agent = new Agent();
|
||||
agent.setId(12L);
|
||||
agent.setName("测试代理");
|
||||
when(agentMapper.selectBatchIds(any())).thenReturn(List.of(agent));
|
||||
|
||||
Map<String, Object> result = tools.districtListQuery(
|
||||
null, null, null, null, null, null, null, 0, 500);
|
||||
|
||||
assertEquals(1, result.get("page"));
|
||||
assertEquals(100, result.get("rows"));
|
||||
assertEquals("rows 超过上限 100,已截断为 100", result.get("notice"));
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> record = ((List<Map<String, Object>>) result.get("records")).get(0);
|
||||
assertEquals(List.of("id", "districtName", "districtNumber", "propertyId", "propertyName",
|
||||
"agentId", "agentName", "province", "city", "countyseat", "address",
|
||||
"districtState", "onlineState", "hardwareCount", "isSmart"),
|
||||
record.keySet().stream().toList());
|
||||
assertEquals("测试物业", record.get("propertyName"));
|
||||
assertEquals("测试代理", record.get("agentName"));
|
||||
verify(permissionService).applyDistrictRowFilter(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void detailDoesNotDistinguishMissingFromUnauthorized() {
|
||||
when(districtMapper.selectOne(any())).thenReturn(null);
|
||||
|
||||
Map<String, Object> result = tools.districtDetailGet(99L);
|
||||
|
||||
assertFalse((Boolean) result.get("found"));
|
||||
assertEquals("站点不存在或无权访问", result.get("message"));
|
||||
verify(permissionService).applyDistrictRowFilter(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void detailReturnsExpandedWhitelistWithoutInternalFields() {
|
||||
DistrictDistrictStrategy district = new DistrictDistrictStrategy();
|
||||
district.setId(9L);
|
||||
when(districtMapper.selectOne(any())).thenReturn(district);
|
||||
when(redisTemplate.opsForValue()).thenReturn(valueOperations);
|
||||
|
||||
Map<String, Object> result = tools.districtDetailGet(9L);
|
||||
|
||||
assertTrue((Boolean) result.get("found"));
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> data = (Map<String, Object>) result.get("data");
|
||||
assertTrue(data.keySet().containsAll(List.of(
|
||||
"districtName", "propertyName", "agentName", "businessFormat", "districtState",
|
||||
"deviceSalesType", "pointState", "lowerPaperState", "warning", "billTypeStr",
|
||||
"districtRefresh", "forbidFunction", "createName", "modifyTime")));
|
||||
assertFalse(data.containsKey("softDelete"));
|
||||
assertFalse(data.containsKey("enterpriseId"));
|
||||
assertFalse(data.containsKey("modifyId"));
|
||||
assertFalse(data.containsKey("publicServiceUrl"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void detailRequiresPositiveId() {
|
||||
assertThrows(IllegalArgumentException.class, () -> tools.districtDetailGet(null));
|
||||
assertThrows(IllegalArgumentException.class, () -> tools.districtDetailGet(0L));
|
||||
verifyNoInteractions(districtMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void springAiPublishesBothReviewedToolSchemas() throws Exception {
|
||||
ToolCallback[] callbacks = MethodToolCallbackProvider.builder().toolObjects(tools).build().getToolCallbacks();
|
||||
Map<String, String> schemas = Arrays.stream(callbacks).collect(Collectors.toMap(
|
||||
callback -> callback.getToolDefinition().name(),
|
||||
callback -> callback.getToolDefinition().inputSchema()));
|
||||
|
||||
assertEquals(2, schemas.size());
|
||||
assertTrue(schemas.get("district.list.query").contains("districtName"));
|
||||
assertTrue(schemas.get("district.list.query").contains("propertyId"));
|
||||
assertTrue(new ObjectMapper().readTree(schemas.get("district.detail.get"))
|
||||
.path("required").toString().contains("\"id\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listRejectsUnknownDistrictStateBeforeQuerying() {
|
||||
assertThrows(IllegalArgumentException.class, () -> tools.districtListQuery(
|
||||
null, null, 4, null, null, null, null, 1, 20));
|
||||
|
||||
verifyNoInteractions(districtMapper);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,9 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import me.iiv.agent.entity.m0.Agent;
|
||||
import me.iiv.agent.entity.m0.AgentManageDistrictAuth;
|
||||
import me.iiv.agent.entity.m0.Authority;
|
||||
import me.iiv.agent.entity.m0.DistrictDistrictStrategy;
|
||||
import me.iiv.agent.entity.m0.Properyt;
|
||||
import me.iiv.agent.entity.m0.User;
|
||||
import me.iiv.agent.mapper.m0.AgentManageDistrictAuthMapper;
|
||||
@@ -22,6 +24,7 @@ import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@@ -51,6 +54,8 @@ class PermissionServiceTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), Agent.class);
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), Properyt.class);
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), DistrictDistrictStrategy.class);
|
||||
permissionService = new PermissionService(authorityMapper, agentMapper,
|
||||
warehouseAgentAuthMapper, agentManageDistrictAuthMapper, properytMapper, redisTemplate);
|
||||
}
|
||||
@@ -140,6 +145,61 @@ class PermissionServiceTest {
|
||||
() -> permissionService.isSalesNonLeader(user));
|
||||
}
|
||||
|
||||
@Test
|
||||
void districtFilterKeepsAgentOrAuthorizedPropertyBoundary() {
|
||||
User user = user(1000L, 108L, 1L);
|
||||
when(authorityMapper.selectById(108L)).thenReturn(authority(108L, 1));
|
||||
when(agentMapper.selectList(org.mockito.ArgumentMatchers.any())).thenReturn(List.of());
|
||||
when(warehouseAgentAuthMapper.selectList(org.mockito.ArgumentMatchers.any())).thenReturn(List.of());
|
||||
AgentManageDistrictAuth auth = new AgentManageDistrictAuth();
|
||||
auth.setPropertyId(44L);
|
||||
when(agentManageDistrictAuthMapper.selectList(org.mockito.ArgumentMatchers.any())).thenReturn(List.of(auth));
|
||||
|
||||
LambdaQueryWrapper<DistrictDistrictStrategy> wrapper = new LambdaQueryWrapper<>();
|
||||
permissionService.applyDistrictRowFilter(wrapper, user);
|
||||
|
||||
String sql = wrapper.getSqlSegment();
|
||||
Collection<Object> values = wrapper.getParamNameValuePairs().values();
|
||||
assertTrue(sql.contains("agent_id IN"));
|
||||
assertTrue(sql.contains("property_id IN"));
|
||||
assertTrue(values.contains(1L));
|
||||
assertTrue(values.contains(44L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void headquartersDistrictFilterAddsNoBoundary() {
|
||||
User user = user(46L, 1L, 0L);
|
||||
when(authorityMapper.selectById(1L)).thenReturn(authority(1L, 0));
|
||||
|
||||
LambdaQueryWrapper<DistrictDistrictStrategy> wrapper = new LambdaQueryWrapper<>();
|
||||
permissionService.applyDistrictRowFilter(wrapper, user);
|
||||
|
||||
assertTrue(wrapper.getSqlSegment().isBlank());
|
||||
}
|
||||
|
||||
@Test
|
||||
void salesDistrictFilterUsesOnlyVisiblePropertyIds() {
|
||||
User user = user(1000L, 108L, 1L);
|
||||
user.setDeptId(5L);
|
||||
user.setPostId(7L);
|
||||
when(authorityMapper.selectById(108L)).thenReturn(authority(108L, 1));
|
||||
when(agentMapper.selectList(org.mockito.ArgumentMatchers.any())).thenReturn(List.of());
|
||||
when(warehouseAgentAuthMapper.selectList(org.mockito.ArgumentMatchers.any())).thenReturn(List.of());
|
||||
when(redisTemplate.opsForHash()).thenReturn(hashOperations);
|
||||
when(hashOperations.get("Dictionary", "authDeptId")).thenReturn(",5,");
|
||||
when(hashOperations.get("Dictionary", "authDeptPostId")).thenReturn("8,");
|
||||
Properyt property = new Properyt();
|
||||
property.setId(55L);
|
||||
when(properytMapper.selectList(org.mockito.ArgumentMatchers.any())).thenReturn(List.of(property));
|
||||
|
||||
LambdaQueryWrapper<DistrictDistrictStrategy> wrapper = new LambdaQueryWrapper<>();
|
||||
permissionService.applyDistrictRowFilter(wrapper, user);
|
||||
|
||||
assertTrue(wrapper.getSqlSegment().contains("property_id IN"));
|
||||
assertFalse(wrapper.getSqlSegment().contains("agent_id IN"));
|
||||
assertTrue(wrapper.getParamNameValuePairs().values().contains(55L));
|
||||
}
|
||||
|
||||
private static Authority authority(Long id, int isAgent) {
|
||||
Authority authority = new Authority();
|
||||
authority.setId(id);
|
||||
|
||||
Reference in New Issue
Block a user