1616// under the License.
1717package 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 ;
2423import org .apache .cloudstack .usage .UsageService ;
25- import org .junit .Assert ;
2624import org .junit .Before ;
2725import org .junit .Test ;
2826import org .junit .runner .RunWith ;
2927import org .mockito .Mock ;
3028import 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 )
3445public 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