Skip to content

Commit f17d27d

Browse files
kansaljayapalu
authored andcommitted
CLOUDSTACK-8886: Limitations is listUsageRecords output, listUsageRecords does not return domain - Fixed and tests added
1 parent e02003d commit f17d27d

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

api/src/org/apache/cloudstack/api/response/UsageRecordResponse.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,8 @@ public void setCpuSpeed(Long cpuSpeed) {
253253
public void setMemory(Long memory) {
254254
this.memory = memory;
255255
}
256+
257+
public String getDomainName(){
258+
return domainName;
259+
}
256260
}

server/src/com/cloud/api/ApiResponseHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,6 +3173,7 @@ public UsageRecordResponse createUsageResponse(Usage usageRecord) {
31733173
Domain domain = ApiDBUtils.findDomainById(usageRecord.getDomainId());
31743174
if (domain != null) {
31753175
usageRecResponse.setDomainId(domain.getUuid());
3176+
usageRecResponse.setDomainName(domain.getName());
31763177
}
31773178

31783179
if (usageRecord.getZoneId() != null) {

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

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,32 @@
1616
// under the License.
1717
package com.cloud.api;
1818

19-
import java.lang.reflect.Field;
20-
import java.text.ParseException;
21-
import java.text.SimpleDateFormat;
22-
import java.util.TimeZone;
23-
19+
import com.cloud.domain.DomainVO;
20+
import com.cloud.usage.UsageVO;
21+
import com.cloud.user.AccountVO;
22+
import org.apache.cloudstack.api.response.UsageRecordResponse;
2423
import org.apache.cloudstack.usage.UsageService;
25-
import org.junit.Assert;
2624
import org.junit.Before;
2725
import org.junit.Test;
2826
import org.junit.runner.RunWith;
2927
import org.mockito.Mock;
3028
import org.mockito.Mockito;
31-
import org.mockito.runners.MockitoJUnitRunner;
29+
import org.powermock.api.mockito.PowerMockito;
30+
import org.powermock.core.classloader.annotations.PrepareForTest;
31+
import org.powermock.modules.junit4.PowerMockRunner;
32+
33+
import java.lang.reflect.Field;
34+
import java.text.ParseException;
35+
import java.text.SimpleDateFormat;
36+
import java.util.Date;
37+
import java.util.TimeZone;
38+
39+
import static org.junit.Assert.assertEquals;
40+
import static org.mockito.Matchers.anyLong;
41+
import static org.mockito.Mockito.when;
3242

33-
@RunWith(MockitoJUnitRunner.class)
43+
@RunWith(PowerMockRunner.class)
44+
@PrepareForTest(ApiDBUtils.class)
3445
public class ApiResponseHelperTest {
3546

3647
@Mock
@@ -54,15 +65,48 @@ public void injectMocks() throws SecurityException, NoSuchFieldException,
5465
public void getDateStringInternal() throws ParseException {
5566
Mockito.when(usageService.getUsageTimezone()).thenReturn(
5667
TimeZone.getTimeZone("UTC"));
57-
Assert.assertEquals("2014-06-29'T'23:45:00+00:00", helper
68+
assertEquals("2014-06-29'T'23:45:00+00:00", helper
5869
.getDateStringInternal(dateFormat.parse("2014-06-29 23:45:00 UTC")));
59-
Assert.assertEquals("2014-06-29'T'23:45:01+00:00", helper
70+
assertEquals("2014-06-29'T'23:45:01+00:00", helper
6071
.getDateStringInternal(dateFormat.parse("2014-06-29 23:45:01 UTC")));
61-
Assert.assertEquals("2014-06-29'T'23:45:11+00:00", helper
72+
assertEquals("2014-06-29'T'23:45:11+00:00", helper
6273
.getDateStringInternal(dateFormat.parse("2014-06-29 23:45:11 UTC")));
63-
Assert.assertEquals("2014-06-29'T'23:05:11+00:00", helper
74+
assertEquals("2014-06-29'T'23:05:11+00:00", helper
6475
.getDateStringInternal(dateFormat.parse("2014-06-29 23:05:11 UTC")));
65-
Assert.assertEquals("2014-05-29'T'08:45:11+00:00", helper
76+
assertEquals("2014-05-29'T'08:45:11+00:00", helper
6677
.getDateStringInternal(dateFormat.parse("2014-05-29 08:45:11 UTC")));
6778
}
79+
80+
@Test
81+
public void testUsageRecordResponse(){
82+
//Creating the usageVO object to be passed to the createUsageResponse.
83+
Long zoneId = null;
84+
Long accountId = null;
85+
Long domainId = null;
86+
String Description = "Test Object";
87+
String usageDisplay = " ";
88+
int usageType = -1;
89+
Double rawUsage = null;
90+
Long vmId = null;
91+
String vmName = " ";
92+
Long offeringId = null;
93+
Long templateId = null;
94+
Long usageId = null;
95+
Date startDate = null;
96+
Date endDate = null;
97+
String type = " ";
98+
UsageVO usage = new UsageVO(zoneId,accountId,domainId,Description,usageDisplay,usageType,rawUsage,vmId,vmName,offeringId,templateId,usageId,startDate,endDate,type);
99+
100+
DomainVO domain = new DomainVO();
101+
domain.setName("DomainName");
102+
103+
AccountVO account = new AccountVO();
104+
105+
PowerMockito.mockStatic(ApiDBUtils.class);
106+
when(ApiDBUtils.findAccountById(anyLong())).thenReturn(account);
107+
when(ApiDBUtils.findDomainById(anyLong())).thenReturn(domain);
108+
109+
UsageRecordResponse MockResponse = helper.createUsageResponse(usage);
110+
assertEquals("DomainName",MockResponse.getDomainName());
111+
}
68112
}

0 commit comments

Comments
 (0)