Skip to content

Commit fdeb202

Browse files
committed
Change accountid and domainid logic + add unit tests
1 parent d05ab01 commit fdeb202

File tree

2 files changed

+130
-15
lines changed

2 files changed

+130
-15
lines changed

plugins/database/quota/src/main/java/org/apache/cloudstack/api/response/QuotaResponseBuilderImpl.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -425,16 +425,11 @@ public int compare(QuotaBalanceVO o1, QuotaBalanceVO o2) {
425425
@Override
426426
public QuotaStatementResponse createQuotaStatementResponse(QuotaStatementCmd cmd) {
427427
Long accountId = getAccountIdForQuotaStatement(cmd);
428-
long domainId = getDomainIdForQuotaStatement(cmd);
428+
Long domainId = getDomainIdForQuotaStatement(cmd, accountId);
429429
List<QuotaUsageJoinVO> quotaUsages = _quotaService.getQuotaUsage(accountId, null, domainId, cmd.getUsageType(), cmd.getStartDate(), cmd.getEndDate());
430430

431431
logger.debug("Creating quota statement from [{}] usage records for parameters [{}].", quotaUsages.size(),
432432
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(cmd, "accountName", "accountId", "projectId", "domainId", "startDate", "endDate", "usageType", "showResources"));
433-
434-
if (CollectionUtils.isEmpty(quotaUsages)) {
435-
throw new InvalidParameterValueException("There is no usage data for the provided parameters.");
436-
}
437-
438433
createDummyRecordForEachQuotaTypeIfUsageTypeIsNotInformed(quotaUsages, cmd.getUsageType());
439434

440435
Map<Integer, List<QuotaUsageJoinVO>> recordsPerUsageTypes = quotaUsages.stream()
@@ -451,34 +446,48 @@ public QuotaStatementResponse createQuotaStatementResponse(QuotaStatementCmd cmd
451446
statement.setObjectName("statement");
452447

453448
if (accountId != null) {
454-
Account account = _accountDao.findAccountIncludingRemoved(cmd.getAccountName(), cmd.getDomainId());
449+
Account account = _accountDao.findByIdIncludingRemoved(accountId);
455450
statement.setAccountId(account.getUuid());
456451
statement.setAccountName(account.getAccountName());
452+
domainId = account.getDomainId();
453+
}
454+
if (domainId != null) {
455+
DomainVO domain = domainDao.findByIdIncludingRemoved(domainId);
456+
statement.setDomainId(domain.getUuid());
457457
}
458-
DomainVO domain = domainDao.findByIdIncludingRemoved(domainId);
459-
statement.setDomainId(domain.getUuid());
460458

461459
return statement;
462460
}
463461

464-
protected Long getAccountIdForQuotaStatement(QuotaStatementCmd cmd) { // TODO: tests pra esse e pro debaixo
462+
protected Long getAccountIdForQuotaStatement(QuotaStatementCmd cmd) {
463+
if (Account.Type.NORMAL.equals(CallContext.current().getCallingAccount().getType())) {
464+
logger.debug("Limiting the Quota statement for the calling Account, as they are a User Account.");
465+
return CallContext.current().getCallingAccountId();
466+
}
467+
465468
long accountId = cmd.getEntityOwnerId();
466469
if (accountId != -1) {
467470
return accountId;
468471
}
469472

470-
if (Account.Type.NORMAL.equals(CallContext.current().getCallingAccount().getType())) {
471-
logger.debug("Limiting the Quota statement for the calling Cccount, as they are a User Account.");
473+
if (cmd.getDomainId() == null) {
474+
logger.debug("Limiting the Quota statement for the calling Account, as 'domainid' was not informed.");
472475
return CallContext.current().getCallingAccountId();
473476
}
474477

475478
logger.debug("Allowing admin/domain admin to generate the Quota statement for the provided Domain.");
476479
return null;
477480
}
478481

479-
protected long getDomainIdForQuotaStatement(QuotaStatementCmd cmd) {
480-
long domainId = cmd.getDomainId();
481-
if (domainId != -1) {
482+
protected Long getDomainIdForQuotaStatement(QuotaStatementCmd cmd, Long accountId) {
483+
if (accountId != null) {
484+
logger.debug("Quota statement is already limited to Account [{}].", accountId);
485+
Account account = _accountDao.findByIdIncludingRemoved(accountId);
486+
return account.getDomainId();
487+
}
488+
489+
Long domainId = cmd.getDomainId();
490+
if (domainId != null) {
482491
return domainId;
483492
}
484493

plugins/database/quota/src/test/java/org/apache/cloudstack/api/response/QuotaResponseBuilderImplTest.java

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.apache.cloudstack.api.command.QuotaCreditsListCmd;
4545
import org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd;
4646
import org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd;
47+
import org.apache.cloudstack.api.command.QuotaStatementCmd;
4748
import org.apache.cloudstack.api.command.QuotaSummaryCmd;
4849
import org.apache.cloudstack.api.command.QuotaValidateActivationRuleCmd;
4950
import org.apache.cloudstack.context.CallContext;
@@ -1007,4 +1008,109 @@ public void setStatementItemResourcesTestDoNotShowResourcesDoNothing() {
10071008

10081009
Assert.assertNull(item.getResources());
10091010
}
1011+
1012+
@Test
1013+
public void getAccountIdForQuotaStatementTestLimitsToCallingAccountForNormalUser() {
1014+
QuotaStatementCmd cmd = Mockito.mock(QuotaStatementCmd.class);
1015+
1016+
Mockito.doReturn(accountMock).when(callContextMock).getCallingAccount();
1017+
Mockito.doReturn(Account.Type.NORMAL).when(accountMock).getType();
1018+
1019+
try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
1020+
callContextMocked.when(CallContext::current).thenReturn(callContextMock);
1021+
1022+
Long result = quotaResponseBuilderSpy.getAccountIdForQuotaStatement(cmd);
1023+
1024+
Assert.assertEquals(Long.valueOf(callerAccountMock.getAccountId()), result);
1025+
}
1026+
}
1027+
1028+
@Test
1029+
public void getAccountIdForQuotaStatementTestReturnsEntityOwnerIdWhenProvided() {
1030+
QuotaStatementCmd cmd = Mockito.mock(QuotaStatementCmd.class);
1031+
1032+
Mockito.doReturn(42L).when(cmd).getEntityOwnerId();
1033+
1034+
Long result = quotaResponseBuilderSpy.getAccountIdForQuotaStatement(cmd);
1035+
1036+
Assert.assertEquals(Long.valueOf(42L), result);
1037+
}
1038+
1039+
@Test
1040+
public void getAccountIdForQuotaStatementTestLimitsToCallingAccountWhenCallerIsAdminAndDomainIsNotProvided() {
1041+
QuotaStatementCmd cmd = Mockito.mock(QuotaStatementCmd.class);
1042+
1043+
Mockito.doReturn(accountMock).when(callContextMock).getCallingAccount();
1044+
Mockito.doReturn(Account.Type.ADMIN).when(accountMock).getType();
1045+
Mockito.doReturn(-1L).when(cmd).getEntityOwnerId();
1046+
Mockito.doReturn(null).when(cmd).getDomainId();
1047+
1048+
try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
1049+
callContextMocked.when(CallContext::current).thenReturn(callContextMock);
1050+
1051+
Long result = quotaResponseBuilderSpy.getAccountIdForQuotaStatement(cmd);
1052+
1053+
Assert.assertEquals(Long.valueOf(callerAccountMock.getAccountId()), result);
1054+
}
1055+
}
1056+
1057+
@Test
1058+
public void getAccountIdForQuotaStatementTestReturnsNullWhenCallerIsAdminAndDomainIsProvided() {
1059+
QuotaStatementCmd cmd = Mockito.mock(QuotaStatementCmd.class);
1060+
1061+
Mockito.doReturn(accountMock).when(callContextMock).getCallingAccount();
1062+
Mockito.doReturn(Account.Type.ADMIN).when(accountMock).getType();
1063+
Mockito.doReturn(-1L).when(cmd).getEntityOwnerId();
1064+
Mockito.doReturn(10L).when(cmd).getDomainId();
1065+
1066+
try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
1067+
callContextMocked.when(CallContext::current).thenReturn(callContextMock);
1068+
1069+
Long result = quotaResponseBuilderSpy.getAccountIdForQuotaStatement(cmd);
1070+
1071+
Assert.assertNull(result);
1072+
}
1073+
}
1074+
1075+
@Test
1076+
public void getDomainIdForQuotaStatementTestReturnsAccountDomainIdWhenAccountIdIsProvided() {
1077+
QuotaStatementCmd cmd = Mockito.mock(QuotaStatementCmd.class);
1078+
AccountVO account = Mockito.mock(AccountVO.class);
1079+
1080+
Mockito.doReturn(account).when(accountDaoMock).findByIdIncludingRemoved(55L);
1081+
Mockito.doReturn(77L).when(account).getDomainId();
1082+
1083+
Long result = quotaResponseBuilderSpy.getDomainIdForQuotaStatement(cmd, 55L);
1084+
1085+
Assert.assertEquals(Long.valueOf(77L), result);
1086+
}
1087+
1088+
@Test
1089+
public void getDomainIdForQuotaStatementTestReturnsProvidedDomainIdWhenAccountIdIsNull() {
1090+
QuotaStatementCmd cmd = Mockito.mock(QuotaStatementCmd.class);
1091+
1092+
Mockito.doReturn(99L).when(cmd).getDomainId();
1093+
1094+
Long result = quotaResponseBuilderSpy.getDomainIdForQuotaStatement(cmd, null);
1095+
1096+
Assert.assertEquals(Long.valueOf(99L), result);
1097+
}
1098+
1099+
@Test
1100+
public void getDomainIdForQuotaStatementTestFallsBackToCallingAccountDomainIdWhenNeitherAccountNorDomainIsProvided() {
1101+
QuotaStatementCmd cmd = Mockito.mock(QuotaStatementCmd.class);
1102+
Account account = Mockito.mock(Account.class);
1103+
1104+
Mockito.doReturn(null).when(cmd).getDomainId();
1105+
Mockito.doReturn(123L).when(account).getDomainId();
1106+
Mockito.doReturn(account).when(callContextMock).getCallingAccount();
1107+
1108+
try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
1109+
callContextMocked.when(CallContext::current).thenReturn(callContextMock);
1110+
1111+
Long result = quotaResponseBuilderSpy.getDomainIdForQuotaStatement(cmd, null);
1112+
1113+
Assert.assertEquals(123L, result.longValue());
1114+
}
1115+
}
10101116
}

0 commit comments

Comments
 (0)