接口1
import static org.mockito.Mockito.;
import static org.junit.jupiter.api.Assertions.;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ExtendWith(MockitoExtension.class)
public class DepartmentServiceTest {
@Mock
private DepartmentMapper departmentMapper;
@Mock
private UserDepartmentMapper userDepartmentMapper;
@Mock
private DepartmentPermissionMapper permissionMapper;
@Mock
private FinTechDataProviderService finTechDataProviderService;
@Mock
private DepartmentPermissionServiceImpl permissionService;
@Mock
private IndexPatternServiceImpl indexPatternService;
@InjectMocks
private DepartmentServiceImpl departmentService;
@BeforeEach
void setUp() {
// 初始化 mock 对象和注入的 service
}
@Test
void testAddDepartmentPermission() {
AddDepartmentPermissionRequest request = new AddDepartmentPermissionRequest();
request.setDepartmentId("123");
request.setDepartmentPermissionDTOList(new ArrayList<>());
AddDepartmentPermissionResponse expectedResponse = new AddDepartmentPermissionResponse();
when(permissionService.addPermissionForDepartment("123", new ArrayList<>())).thenReturn(true);
AddDepartmentPermissionResponse actualResponse = departmentService.addDepartmentPermission(request);
assertNotNull(actualResponse);
verify(permissionService).addPermissionForDepartment("123", new ArrayList<>());
}
@Test
void testChangeGlobalQuery() {
ChangeDepartmentGlobalQueryRequest request = new ChangeDepartmentGlobalQueryRequest();
request.setDepartmentId("123");
request.setGlobalQuery(true);
request.setPermanentFlag(true);
request.setEffectiveDate(new Date());
ChangeDepartmentGlobalQueryResponse expectedResponse = new ChangeDepartmentGlobalQueryResponse();
when(departmentMapper.selectByPrimaryKey("123")).thenReturn(null);
ChangeDepartmentGlobalQueryResponse actualResponse = departmentService.changeGlobalQuery(request);
assertNotNull(actualResponse);
verify(departmentMapper).insert(any(Department.class));
}
@Test
void testDeleteDepartmentPermission() {
DelDepartmentPermissionRequest request = new DelDepartmentPermissionRequest();
request.setDepartmentId("123");
request.setIndexPatternNameList(new ArrayList<>());
DelDepartmentPermissionResponse expectedResponse = new DelDepartmentPermissionResponse();
when(permissionService.deletePermissionForDepartment("123", new ArrayList<>())).thenReturn(true);
DelDepartmentPermissionResponse actualResponse = departmentService.deleteDepartmentPermission(request);
assertNotNull(actualResponse);
verify(permissionService).deletePermissionForDepartment("123", new ArrayList<>());
}
@Test
void testIsGlobalQuery() {
QueryDepartmentGlobalQueryRequest request = new QueryDepartmentGlobalQueryRequest();
request.setDepartmentId("123");
Department department = new Department();
department.setDepartmentId("123");
department.setGlobalQuery(true);
department.setQueryEffectiveDate(new Date());
department.setQueryPermanentFlag(true);
when(departmentMapper.selectByPrimaryKey("123")).thenReturn(department);
QueryDepartmentGlobalQueryResponse actualResponse = departmentService.isGlobalQuery(request);
assertNotNull(actualResponse);
assertTrue(actualResponse.getGlobalQueryFlag());
assertEquals(new Date(), actualResponse.getEffectiveDate());
assertTrue(actualResponse.getPermanentFlag());
}
@Test
void testQueryIndexPatternByDepartmentId() {
QueryDepartmentIndexPatternRequest request = new QueryDepartmentIndexPatternRequest();
request.setDepartmentId("123");
List<UserDepartmentIndexPatternItem> indexPatternList = new ArrayList<>();
when(indexPatternService.getDepartmentIndexPatternByDepartmentId("123")).thenReturn(indexPatternList);
QueryDepartmentIndexPatternResponse actualResponse = departmentService.queryIndexPatternByDepartmentId(request);
assertNotNull(actualResponse);
assertEquals(indexPatternList, actualResponse.getIndexPatternItemList());
}
}