Skip to content

Commit 139a92a

Browse files
Test public usage response dispatcher
1 parent e5a4855 commit 139a92a

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

‎server/src/test/java/com/cloud/api/ApiResponseHelperTest.java‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.junit.jupiter.api.BeforeEach;
3535
import org.junit.jupiter.api.DisplayName;
3636
import org.junit.jupiter.api.Test;
37+
import org.junit.jupiter.params.ParameterizedTest;
38+
import org.junit.jupiter.params.provider.ValueSource;
3739
import org.junit.jupiter.api.extension.ExtendWith;
3840
import org.mockito.InjectMocks;
3941
import org.mockito.Mock;
@@ -1413,6 +1415,71 @@ public void populateVmDiskBytesWriteUsageResponseTest() throws Exception {
14131415
verify(entityManagerMock).findByIdIncludingRemoved(VolumeVO.class, 28L);
14141416
}
14151417

1418+
@ParameterizedTest
1419+
@ValueSource(ints = {
1420+
UsageTypes.RUNNING_VM,
1421+
UsageTypes.IP_ADDRESS,
1422+
UsageTypes.NETWORK_BYTES_SENT,
1423+
UsageTypes.VM_DISK_IO_READ,
1424+
UsageTypes.VOLUME,
1425+
UsageTypes.TEMPLATE,
1426+
UsageTypes.SNAPSHOT,
1427+
UsageTypes.LOAD_BALANCER_POLICY,
1428+
UsageTypes.PORT_FORWARDING_RULE,
1429+
UsageTypes.NETWORK_OFFERING,
1430+
UsageTypes.VPN_USERS,
1431+
UsageTypes.SECURITY_GROUP,
1432+
UsageTypes.BACKUP,
1433+
UsageTypes.VM_SNAPSHOT,
1434+
UsageTypes.VOLUME_SECONDARY,
1435+
UsageTypes.VM_SNAPSHOT_ON_PRIMARY,
1436+
UsageTypes.BUCKET
1437+
})
1438+
public void createUsageResponseDispatchesAllUsageTypes(int usageType) {
1439+
UsageVO usageRecord = mock(UsageVO.class);
1440+
Account account = mock(Account.class);
1441+
DomainVO domain = new DomainVO();
1442+
1443+
when(usageRecord.getAccountId()).thenReturn(1L);
1444+
when(usageRecord.getZoneId()).thenReturn(null);
1445+
when(usageRecord.getDescription()).thenReturn("usage description");
1446+
when(usageRecord.getUsageDisplay()).thenReturn("usage");
1447+
when(usageRecord.getUsageType()).thenReturn(usageType);
1448+
when(usageRecord.getVmInstanceId()).thenReturn(null);
1449+
when(usageRecord.getVmName()).thenReturn(null);
1450+
when(usageRecord.getTemplateId()).thenReturn(null);
1451+
when(usageRecord.getOfferingId()).thenReturn(1L);
1452+
when(usageRecord.getUsageId()).thenReturn(1L);
1453+
when(usageRecord.getNetworkId()).thenReturn(null);
1454+
when(usageRecord.getRawUsage()).thenReturn(null);
1455+
when(usageRecord.getSize()).thenReturn(null);
1456+
when(usageRecord.getVirtualSize()).thenReturn(null);
1457+
when(usageRecord.getType()).thenReturn("UserVm");
1458+
when(usageRecord.getStartDate()).thenReturn(null);
1459+
when(usageRecord.getEndDate()).thenReturn(null);
1460+
1461+
when(account.getType()).thenReturn(Account.Type.NORMAL);
1462+
when(account.getId()).thenReturn(1L);
1463+
when(account.getUuid()).thenReturn("account-uuid");
1464+
when(account.getAccountName()).thenReturn("account");
1465+
when(account.getDomainId()).thenReturn(2L);
1466+
domain.setId(2L);
1467+
domain.setUuid("domain-uuid");
1468+
domain.setName("domain");
1469+
domain.setPath("/domain/");
1470+
1471+
try (MockedStatic<ApiDBUtils> ignored = Mockito.mockStatic(ApiDBUtils.class)) {
1472+
when(ApiDBUtils.findAccountById(1L)).thenReturn(account);
1473+
when(ApiDBUtils.findDomainById(2L)).thenReturn(domain);
1474+
1475+
UsageRecordResponse response = helper.createUsageResponse(usageRecord, null, false);
1476+
1477+
assertEquals(usageType, response.getUsageType());
1478+
assertEquals("account-uuid", response.getAccountId());
1479+
assertEquals("domain-uuid", response.getDomainId());
1480+
}
1481+
}
1482+
14161483
private Object invokeUsageDetailsHelper(String methodName, Class<?>[] parameterTypes, Object... args) throws Exception {
14171484
Method method = ApiResponseHelper.class.getDeclaredMethod(methodName, parameterTypes);
14181485
method.setAccessible(true);

0 commit comments

Comments
 (0)