Skip to content

Commit 9669a0c

Browse files
committed
add junit test case for InstanceServiceImpl(deleteInstance)
1 parent c252937 commit 9669a0c

1 file changed

Lines changed: 68 additions & 8 deletions

File tree

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

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717

1818
package com.highgo.platform.apiserver.service.impl;
1919

20-
import static org.mockito.ArgumentMatchers.any;
21-
2220
import static org.junit.Assert.assertEquals;
2321
import static org.junit.Assert.assertThrows;
22+
import static org.mockito.ArgumentMatchers.any;
2423
import static org.mockito.Mockito.when;
2524

25+
import java.util.ArrayList;
26+
import java.util.List;
2627
import java.util.Optional;
2728

2829
import javax.persistence.EntityManager;
@@ -37,9 +38,12 @@
3738

3839
import com.highgo.cloud.enums.BackupStatus;
3940
import com.highgo.cloud.enums.InstanceStatus;
41+
import com.highgo.platform.apiserver.model.dto.InstanceDTO;
4042
import com.highgo.platform.apiserver.model.po.BackupPO;
43+
import com.highgo.platform.apiserver.model.po.ExtraMetaPO;
4144
import com.highgo.platform.apiserver.model.po.InstancePO;
4245
import com.highgo.platform.apiserver.model.vo.request.CreateInstanceVO;
46+
import com.highgo.platform.apiserver.model.vo.response.ActionResponse;
4347
import com.highgo.platform.apiserver.model.vo.response.InstanceVO;
4448
import com.highgo.platform.apiserver.repository.BackupPolicyRepository;
4549
import com.highgo.platform.apiserver.repository.BackupRepository;
@@ -125,7 +129,7 @@ class InstanceServiceImplTest {
125129
private String instanceName = "ivory";
126130

127131
@Test
128-
void testCreateInstanceCreateInstanceVO() {
132+
void testCreateInstance() {
129133
CreateInstanceVO createInstanceVO = new CreateInstanceVO();
130134
createInstanceVO.setClusterId(clusterId);
131135
createInstanceVO.setNamespace(namespace);
@@ -265,6 +269,7 @@ private Optional<InstancePO> getRunning_Instances() {
265269

266270
/**
267271
* 获取老实例, 其cpu 比新建实例的大
272+
*
268273
* @return
269274
*/
270275
private Optional<InstancePO> getCpuLargerThanNewInstance() {
@@ -279,6 +284,7 @@ private Optional<InstancePO> getCpuLargerThanNewInstance() {
279284

280285
/**
281286
* 获取老实例, 其memory 比新建实例的大
287+
*
282288
* @return
283289
*/
284290
private Optional<InstancePO> getMemoryLargerThanNewInstance() {
@@ -294,6 +300,7 @@ private Optional<InstancePO> getMemoryLargerThanNewInstance() {
294300

295301
/**
296302
* 获取老实例, 其storage 比新建实例的大
303+
*
297304
* @return
298305
*/
299306
private Optional<InstancePO> getStorageLargerThanNewInstance() {
@@ -317,18 +324,71 @@ private Optional<BackupPO> getBackupFiles_notAllowRestoreOptional() {
317324
}
318325

319326
@Test
320-
void testCreateInstanceInstanceDTO() {
327+
void testDeleteInstance() {
328+
String instanceId = "123456";
329+
// 测试实例不存在
330+
Optional<InstancePO> instancePo_empty = Optional.empty();
331+
332+
when(instanceRepository.findById(instanceId)).thenReturn(instancePo_empty);
333+
InstanceException exception = assertThrows(InstanceException.class, () -> {
334+
335+
instanceServiceImpl.deleteInstance(instanceId);
336+
});
337+
assertEquals(InstanceError.INSTANCE_NOT_EXIST.message(), exception.getMessage());
338+
339+
// 测试: 实例存在 但是实例状态不允许删除
340+
when(instanceRepository.findById(instanceId)).thenReturn(getNotAllowedDelete_Instances());
341+
when(extraMetaService.findAllByInstanceId(instanceId)).thenReturn(findExtrameta());
342+
InstanceException exception_not_allow_deleted = assertThrows(InstanceException.class, () -> {
321343

344+
instanceServiceImpl.deleteInstance(instanceId);
345+
});
346+
assertEquals(InstanceError.INSTANCE_NOT_ALLOW_OPERATE.message(), exception_not_allow_deleted.getMessage());
347+
348+
// 测试: 成功删除实例
349+
when(instanceRepository.findById(instanceId)).thenReturn(getAllowedDelete_Instances());
350+
when(crService.deleteCr(any(InstanceDTO.class))).thenReturn(true);
351+
ActionResponse actionResponse = instanceServiceImpl.deleteInstance(instanceId);
352+
353+
assertEquals(actionResponse.getCode(), ActionResponse.actionSuccess().getCode());
322354
}
323355

324-
@Test
325-
void testCreateInstanceCallback() {
356+
private List<ExtraMetaPO> findExtrameta() {
357+
ExtraMetaPO extraMetaPO = new ExtraMetaPO();
358+
extraMetaPO.setId("1");
359+
extraMetaPO.setName("name1");
360+
extraMetaPO.setValue("value1");
361+
List<ExtraMetaPO> extraMetaPOS = new ArrayList<ExtraMetaPO>();
362+
extraMetaPOS.add(extraMetaPO);
363+
364+
return extraMetaPOS;
326365

327366
}
328367

329-
@Test
330-
void testDeleteInstance() {
368+
/**
369+
* 获取不允许被删除的实例
370+
* @return
371+
*/
372+
private Optional<InstancePO> getNotAllowedDelete_Instances() {
373+
InstancePO instancePO = new InstancePO();
374+
instancePO.setIsDeleted(false);
375+
instancePO.setStatus(InstanceStatus.CREATING);
376+
Optional<InstancePO> optionalInstancePo = Optional.of(instancePO);
331377

378+
return optionalInstancePo;
379+
}
380+
381+
/**
382+
* 获取允许被删除的实例
383+
* @return
384+
*/
385+
private Optional<InstancePO> getAllowedDelete_Instances() {
386+
InstancePO instancePO = new InstancePO();
387+
instancePO.setIsDeleted(false);
388+
instancePO.setStatus(InstanceStatus.RUNNING);
389+
Optional<InstancePO> optionalInstancePo = Optional.of(instancePO);
390+
391+
return optionalInstancePo;
332392
}
333393

334394
@Test

0 commit comments

Comments
 (0)