|
44 | 44 | import org.apache.cloudstack.api.command.QuotaCreditsListCmd; |
45 | 45 | import org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd; |
46 | 46 | import org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd; |
| 47 | +import org.apache.cloudstack.api.command.QuotaStatementCmd; |
47 | 48 | import org.apache.cloudstack.api.command.QuotaSummaryCmd; |
48 | 49 | import org.apache.cloudstack.api.command.QuotaValidateActivationRuleCmd; |
49 | 50 | import org.apache.cloudstack.context.CallContext; |
@@ -1007,4 +1008,109 @@ public void setStatementItemResourcesTestDoNotShowResourcesDoNothing() { |
1007 | 1008 |
|
1008 | 1009 | Assert.assertNull(item.getResources()); |
1009 | 1010 | } |
| 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 | + } |
1010 | 1116 | } |
0 commit comments