|
|
@@ -0,0 +1,82 @@
|
|
|
+package com.yimeng.okr.service;
|
|
|
+
|
|
|
+import com.yimeng.okr.dto.WorkspaceOverviewDto;
|
|
|
+import com.yimeng.okr.entity.AssessmentPeriod;
|
|
|
+import com.yimeng.okr.enums.PeriodStatus;
|
|
|
+import com.yimeng.okr.mapper.AssessmentPeriodMapper;
|
|
|
+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 org.mockito.junit.jupiter.MockitoSettings;
|
|
|
+import org.mockito.quality.Strictness;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+@ExtendWith(MockitoExtension.class)
|
|
|
+@MockitoSettings(strictness = Strictness.LENIENT)
|
|
|
+class WorkspaceServiceTest {
|
|
|
+
|
|
|
+ @Mock JdbcTemplate jdbcTemplate;
|
|
|
+ @Mock AssessmentPeriodMapper periodMapper;
|
|
|
+ @InjectMocks WorkspaceService workspaceService;
|
|
|
+
|
|
|
+ private AssessmentPeriod period;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ period = new AssessmentPeriod();
|
|
|
+ period.setId(1L);
|
|
|
+ period.setName("测试周期");
|
|
|
+ period.setStatus(PeriodStatus.EXECUTING);
|
|
|
+ lenient().when(periodMapper.selectById(1L)).thenReturn(period);
|
|
|
+ lenient().when(periodMapper.selectList(any()))
|
|
|
+ .thenReturn(List.of(period));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void buildRisksFiltersByUserId() {
|
|
|
+ // One blocked KR for the user's team
|
|
|
+ Map<String, Object> blockedKr = new HashMap<>();
|
|
|
+ blockedKr.put("title", "Blocked KR");
|
|
|
+ blockedKr.put("real_name", "Subordinate A");
|
|
|
+ List<Map<String, Object>> blockedList = new ArrayList<>();
|
|
|
+ blockedList.add(blockedKr);
|
|
|
+
|
|
|
+ // Mock: blocked KRs returned for the query parameters
|
|
|
+ lenient().when(jdbcTemplate.queryForList(
|
|
|
+ contains("SELECT k.title, u.real_name FROM kr_key_result"),
|
|
|
+ eq(1L), eq(2L), eq(2L)))
|
|
|
+ .thenReturn((List) blockedList);
|
|
|
+ // Default: empty for other queries
|
|
|
+ lenient().when(jdbcTemplate.queryForList(anyString(), any(), any(), any()))
|
|
|
+ .thenReturn((List) List.of());
|
|
|
+ lenient().when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class), any()))
|
|
|
+ .thenReturn(0);
|
|
|
+
|
|
|
+ WorkspaceOverviewDto dto = workspaceService.getOverview(2L, 1L);
|
|
|
+ assertNotNull(dto.getRisks());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void getOverviewReturnsActions() {
|
|
|
+ lenient().when(jdbcTemplate.queryForList(anyString(), any(), any(), any()))
|
|
|
+ .thenReturn((List) List.of());
|
|
|
+ lenient().when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class), any()))
|
|
|
+ .thenReturn(0);
|
|
|
+
|
|
|
+ WorkspaceOverviewDto dto = workspaceService.getOverview(2L, 1L);
|
|
|
+ assertNotNull(dto.getAllowedActions());
|
|
|
+ assertTrue(dto.getAllowedActions().size() >= 3);
|
|
|
+ }
|
|
|
+}
|