Skip to content

Commit 99e048a

Browse files
committed
add junit test case for testListByFilter
1 parent 9669a0c commit 99e048a

1 file changed

Lines changed: 112 additions & 12 deletions

File tree

cloudnative/src/test/java/com/highgo/platform/apiserver/service/impl/InstanceServiceImplTest.java

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,20 @@
3535
import org.mockito.junit.jupiter.MockitoExtension;
3636
import org.mockito.junit.jupiter.MockitoSettings;
3737
import org.mockito.quality.Strictness;
38+
import org.springframework.data.domain.Page;
39+
import org.springframework.data.domain.PageImpl;
40+
import org.springframework.data.domain.PageRequest;
41+
import org.springframework.data.domain.Sort;
42+
import org.springframework.data.jpa.domain.Specification;
3843

3944
import com.highgo.cloud.enums.BackupStatus;
4045
import com.highgo.cloud.enums.InstanceStatus;
4146
import com.highgo.platform.apiserver.model.dto.InstanceDTO;
4247
import com.highgo.platform.apiserver.model.po.BackupPO;
4348
import com.highgo.platform.apiserver.model.po.ExtraMetaPO;
49+
import com.highgo.platform.apiserver.model.po.InstanceNetworkPO;
4450
import com.highgo.platform.apiserver.model.po.InstancePO;
51+
import com.highgo.platform.apiserver.model.po.K8sClusterInfoPO;
4552
import com.highgo.platform.apiserver.model.vo.request.CreateInstanceVO;
4653
import com.highgo.platform.apiserver.model.vo.response.ActionResponse;
4754
import com.highgo.platform.apiserver.model.vo.response.InstanceVO;
@@ -141,7 +148,6 @@ void testCreateInstance() {
141148
// namespace, instanceName);
142149

143150
InstanceException exception = assertThrows(InstanceException.class, () -> {
144-
// 在这里调用该方法
145151
instanceServiceImpl.createInstance(createInstanceVO);
146152
});
147153

@@ -161,7 +167,6 @@ void testCreateInstance() {
161167
// 实例不存在或已删除
162168
createInstanceVO.setOriginalInstanceId("originalInstanceId");
163169
InstanceException exception_instance_not_exist = assertThrows(InstanceException.class, () -> {
164-
// 在这里调用该方法
165170
instanceServiceImpl.createInstance(createInstanceVO);
166171
});
167172

@@ -170,15 +175,13 @@ void testCreateInstance() {
170175
// 实例已删除
171176
when(instanceRepository.findById(any(String.class))).thenReturn(getDeletedInstances());
172177
InstanceException exception_instance_is_deleted = assertThrows(InstanceException.class, () -> {
173-
// 在这里调用该方法
174178
instanceServiceImpl.createInstance(createInstanceVO);
175179
});
176180
assertEquals(InstanceError.INSTANCE_NOT_EXIST.message(), exception_instance_is_deleted.getMessage());
177181

178182
// 实例存在,但是当前状态不允许执行恢复
179183
when(instanceRepository.findById(any(String.class))).thenReturn(getNotDeleted_creating_Instances());
180184
InstanceException exception_instance_nofile_not_allow_restore = assertThrows(InstanceException.class, () -> {
181-
// 在这里调用该方法
182185
instanceServiceImpl.createInstance(createInstanceVO);
183186
});
184187

@@ -188,7 +191,6 @@ void testCreateInstance() {
188191
// 实例存在,但是备份不存在
189192
when(instanceRepository.findById(any(String.class))).thenReturn(getRunning_Instances());
190193
BackupException exception_instance_backupfile_not_exist = assertThrows(BackupException.class, () -> {
191-
// 在这里调用该方法
192194
instanceServiceImpl.createInstance(createInstanceVO);
193195
});
194196
assertEquals(BackupError.BACKUP_NOT_EXIST.message(), exception_instance_backupfile_not_exist.getMessage());
@@ -198,7 +200,6 @@ void testCreateInstance() {
198200
Optional<BackupPO> optionalBackupPO = getBackupFiles_notAllowRestoreOptional();
199201
when(backupRepository.findById(any(String.class))).thenReturn(optionalBackupPO);
200202
BackupException exception_instance_not_allow_restore = assertThrows(BackupException.class, () -> {
201-
// 在这里调用该方法
202203
instanceServiceImpl.createInstance(createInstanceVO);
203204
});
204205
assertEquals(BackupError.BACKUP_NOT_ALLOW_OPERATE.message(), exception_instance_not_allow_restore.getMessage());
@@ -209,7 +210,6 @@ void testCreateInstance() {
209210
optionalBackupPO.get().setStatus(BackupStatus.COMPLETED);
210211
when(instanceRepository.findById(any(String.class))).thenReturn(getCpuLargerThanNewInstance());
211212
InstanceException exception_cpu = assertThrows(InstanceException.class, () -> {
212-
// 在这里调用该方法
213213
instanceServiceImpl.createInstance(createInstanceVO);
214214
});
215215
assertEquals(InstanceError.INSTANCE_NOT_ALLOW_DEMOTE.message(), exception_cpu.getMessage());
@@ -221,7 +221,6 @@ void testCreateInstance() {
221221
optionalBackupPO.get().setStatus(BackupStatus.COMPLETED);
222222
when(instanceRepository.findById(any(String.class))).thenReturn(getMemoryLargerThanNewInstance());
223223
InstanceException exception_memory = assertThrows(InstanceException.class, () -> {
224-
// 在这里调用该方法
225224
instanceServiceImpl.createInstance(createInstanceVO);
226225
});
227226
assertEquals(InstanceError.INSTANCE_NOT_ALLOW_DEMOTE.message(), exception_memory.getMessage());
@@ -234,7 +233,6 @@ void testCreateInstance() {
234233
optionalBackupPO.get().setStatus(BackupStatus.COMPLETED);
235234
when(instanceRepository.findById(any(String.class))).thenReturn(getStorageLargerThanNewInstance());
236235
InstanceException exception_storage = assertThrows(InstanceException.class, () -> {
237-
// 在这里调用该方法
238236
instanceServiceImpl.createInstance(createInstanceVO);
239237
});
240238
assertEquals(InstanceError.INSTANCE_NOT_ALLOW_DEMOTE.message(), exception_storage.getMessage());
@@ -392,23 +390,125 @@ private Optional<InstancePO> getAllowedDelete_Instances() {
392390
}
393391

394392
@Test
395-
void testDeleteInstanceCallback() {
393+
void testGetVO() {
394+
String id = "123456";
395+
when(instanceRepository.findById(id)).thenReturn(getWithClusterId_Instances());
396+
397+
List<InstanceNetworkPO> instanceNetworkPOs = getInstanceNetworkPOById();
398+
when(instanceNetworkRepository.listByInstanceId(id)).thenReturn(instanceNetworkPOs);
399+
400+
K8sClusterInfoPO k8sClusterInfoPO = new K8sClusterInfoPO();
401+
k8sClusterInfoPO.setClusterName("clusterName");
402+
when(k8sClusterService.getInfoByClusterId(any(String.class))).thenReturn(k8sClusterInfoPO);
403+
InstanceVO instanceVO = instanceServiceImpl.getVO(id);
396404

405+
assertEquals(instanceVO.getClusterName(), k8sClusterInfoPO.getClusterName());
406+
assertEquals(instanceVO.getNetwork().size(), instanceNetworkPOs.size());
407+
assertEquals(instanceVO.getNetwork().get(0).getNodePort(), instanceNetworkPOs.get(0).getNodePort());
397408
}
398409

399-
@Test
400-
void testGetVO() {
410+
/**
411+
* 获取允许被删除的实例
412+
* @return
413+
*/
414+
private Optional<InstancePO> getWithClusterId_Instances() {
415+
InstancePO instancePO = new InstancePO();
416+
instancePO.setIsDeleted(false);
417+
instancePO.setStatus(InstanceStatus.RUNNING);
418+
instancePO.setClusterId("clusterId");
419+
Optional<InstancePO> optionalInstancePo = Optional.of(instancePO);
401420

421+
return optionalInstancePo;
402422
}
403423

424+
/**
425+
* get instance network
426+
* @return
427+
*/
428+
public List<InstanceNetworkPO> getInstanceNetworkPOById() {
429+
InstanceNetworkPO instanceNetworkPO = new InstanceNetworkPO();
430+
instanceNetworkPO.setId("1");
431+
instanceNetworkPO.setNodePort(6060);
432+
List<InstanceNetworkPO> instanceNetworkPOs = new ArrayList<InstanceNetworkPO>();
433+
instanceNetworkPOs.add(instanceNetworkPO);
434+
435+
return instanceNetworkPOs;
436+
}
404437
@Test
405438
void testGetDTO() {
439+
String instanceId = "123456";
440+
// 测试实例不存在
441+
Optional<InstancePO> instancePo_empty = Optional.empty();
442+
443+
when(instanceRepository.findById(instanceId)).thenReturn(instancePo_empty);
444+
InstanceException exception = assertThrows(InstanceException.class, () -> {
445+
446+
instanceServiceImpl.getDTO(instanceId);
447+
});
448+
assertEquals(InstanceError.INSTANCE_NOT_EXIST.message(), exception.getMessage());
449+
450+
// 测试 获取产品的属性
451+
when(instanceRepository.findById(instanceId)).thenReturn(getWithClusterId_Instances());
452+
when(extraMetaService.findAllByInstanceId(instanceId)).thenReturn(findExtrameta());
453+
InstanceDTO instanceDTO = instanceServiceImpl.getDTO(instanceId);
454+
assertEquals("value1", instanceDTO.getExtraMeta().get("name1"));
406455

407456
}
408457

409458
@Test
410459
void testListByFilter() {
460+
// List<InstanceNetworkPO> instanceNetworkPOList = instanceNetworkRepository.list();
461+
when(instanceNetworkRepository.list()).thenReturn(getInstanceNetworkPOList());
462+
463+
// Page<InstancePO> page = instanceRepository.findAll(specification, pageable);
464+
Specification<InstancePO> spec = any(Specification.class);
465+
PageRequest pageable = any(PageRequest.class);
466+
467+
// 设置mock行为
468+
// Page<InstancePO> expectedPage = mock(Page.class);
469+
when(instanceRepository.findAll(spec, pageable)).thenReturn(getInstancePOByPage());
470+
471+
instanceServiceImpl.listByFilter(1, "filter", "clusterId", 10, 20);
472+
}
473+
474+
private List<InstanceNetworkPO> getInstanceNetworkPOList() {
475+
InstanceNetworkPO instanceNetworkPO1 = new InstanceNetworkPO();
476+
instanceNetworkPO1.setInstanceId("0001");
477+
instanceNetworkPO1.setNodePort(6060);
478+
479+
InstanceNetworkPO instanceNetworkPO2 = new InstanceNetworkPO();
480+
instanceNetworkPO2.setInstanceId("0002");
481+
instanceNetworkPO2.setNodePort(6061);
482+
483+
List<InstanceNetworkPO> instanceNetworkPOList = new ArrayList<InstanceNetworkPO>();
484+
instanceNetworkPOList.add(instanceNetworkPO1);
485+
instanceNetworkPOList.add(instanceNetworkPO2);
486+
487+
return instanceNetworkPOList;
488+
}
489+
490+
private Page<InstancePO> getInstancePOByPage() {
491+
// 创建PageRequest对象,指定页码、每页大小和排序规则(可选)
492+
PageRequest pageRequest = PageRequest.of(10, 20, Sort.by("name"));
493+
494+
InstancePO instancePO1 = new InstancePO();
495+
instancePO1.setId("0001");
496+
instancePO1.setClusterId("0001");
497+
instancePO1.setName("name1");
498+
499+
InstancePO instancePO2 = new InstancePO();
500+
instancePO1.setId("0002");
501+
instancePO2.setClusterId("0002");
502+
instancePO2.setName("name2");
503+
504+
List<InstancePO> instanceList = new ArrayList<InstancePO>();
505+
instanceList.add(instancePO1);
506+
instanceList.add(instancePO2);
507+
508+
long totalElements = 100;
509+
Page<InstancePO> page = new PageImpl<>(instanceList, pageRequest, totalElements);
411510

511+
return page;
412512
}
413513

414514
@Test

0 commit comments

Comments
 (0)