|
20 | 20 | import static org.junit.Assert.assertEquals; |
21 | 21 | import static org.junit.Assert.assertThrows; |
22 | 22 | import static org.mockito.ArgumentMatchers.any; |
| 23 | +import static org.mockito.Mockito.mock; |
23 | 24 | import static org.mockito.Mockito.when; |
24 | 25 |
|
| 26 | +import java.math.BigInteger; |
25 | 27 | import java.util.ArrayList; |
| 28 | +import java.util.Arrays; |
26 | 29 | import java.util.List; |
27 | 30 | import java.util.Optional; |
28 | 31 |
|
29 | 32 | import javax.persistence.EntityManager; |
| 33 | +import javax.persistence.Query; |
30 | 34 |
|
31 | 35 | import org.junit.jupiter.api.Test; |
32 | 36 | import org.junit.jupiter.api.extension.ExtendWith; |
|
41 | 45 | import org.springframework.data.domain.Sort; |
42 | 46 | import org.springframework.data.jpa.domain.Specification; |
43 | 47 |
|
| 48 | +import com.highgo.cloud.constant.InstanceAllowConstant; |
44 | 49 | import com.highgo.cloud.enums.BackupStatus; |
45 | 50 | import com.highgo.cloud.enums.InstanceStatus; |
46 | 51 | import com.highgo.platform.apiserver.model.dto.InstanceDTO; |
|
51 | 56 | import com.highgo.platform.apiserver.model.po.K8sClusterInfoPO; |
52 | 57 | import com.highgo.platform.apiserver.model.vo.request.CreateInstanceVO; |
53 | 58 | import com.highgo.platform.apiserver.model.vo.response.ActionResponse; |
| 59 | +import com.highgo.platform.apiserver.model.vo.response.InstanceCountVO; |
54 | 60 | import com.highgo.platform.apiserver.model.vo.response.InstanceVO; |
55 | 61 | import com.highgo.platform.apiserver.repository.BackupPolicyRepository; |
56 | 62 | import com.highgo.platform.apiserver.repository.BackupRepository; |
@@ -514,6 +520,24 @@ private Page<InstancePO> getInstancePOByPage() { |
514 | 520 | @Test |
515 | 521 | void testGetInstanceCount() { |
516 | 522 |
|
| 523 | + Query query = mock(Query.class); |
| 524 | + |
| 525 | + // 模拟查询结果 |
| 526 | + Object[] row1 = {InstanceStatus.RUNNING.name(), BigInteger.valueOf(2)}; |
| 527 | + Object[] row2 = {InstanceStatus.CREATING.name(), BigInteger.valueOf(3)}; |
| 528 | + Object[] row3 = {InstanceStatus.ERROR.name(), BigInteger.valueOf(1)}; |
| 529 | + List<Object> resultList = Arrays.asList(row1, row2, row3); |
| 530 | + |
| 531 | + // 模拟EntityManager和Query行为 |
| 532 | + when(entityManager.createNativeQuery(any(String.class))).thenReturn(query); |
| 533 | + when(query.getResultList()).thenReturn(resultList); |
| 534 | + |
| 535 | + InstanceCountVO result = instanceServiceImpl.getInstanceCount(); |
| 536 | + |
| 537 | + // 验证结果是否符合预期 |
| 538 | + assertEquals(2, result.getRunningCount()); |
| 539 | + assertEquals(3, result.getStartingCount()); |
| 540 | + assertEquals(1, result.getErrorCount()); |
517 | 541 | } |
518 | 542 |
|
519 | 543 | @Test |
|
0 commit comments