1515import static org .easymock .EasyMock .verify ;
1616import static org .junit .Assert .assertEquals ;
1717
18+ import java .math .BigDecimal ;
19+ import java .time .Instant ;
20+ import java .time .LocalDate ;
21+ import java .time .ZoneOffset ;
22+ import java .time .format .DateTimeFormatter ;
1823import java .util .ArrayList ;
1924import java .util .Date ;
25+ import java .util .List ;
2026
2127import org .duracloud .mill .db .repo .JpaSpaceStatsRepo ;
28+ import org .duracloud .reportdata .storage .StoreStatsDTO ;
2229import org .easymock .Capture ;
2330import org .easymock .Mock ;
24- import org .joda .time .format .DateTimeFormat ;
25- import org .joda .time .format .DateTimeFormatter ;
2631import org .junit .Test ;
2732
2833/**
@@ -33,14 +38,14 @@ public class StorageStatsResourceTest {
3338 @ Mock
3439 private JpaSpaceStatsRepo spaceStatsRepo ;
3540
36- @ Test
37- public void testGetStorageStats () throws Exception {
41+ final DateTimeFormatter dateTimeFormatter = DateTimeFormatter .ISO_INSTANT ;
3842
43+ @ Test
44+ public void testGetStorageStats () {
3945 spaceStatsRepo = mock (JpaSpaceStatsRepo .class );
4046 final String accountId = "account-id" ;
4147 final String storeId = "id" ;
42- DateTimeFormatter format = DateTimeFormat .forPattern ("YYYY-MM-dd HH:mm:ss z" ).withZoneUTC ();
43- final Date date = format .parseDateTime ("2019-12-31 12:00:00 UTC" ).toDate ();
48+ final Date date = Date .from (Instant .from (dateTimeFormatter .parse (("2019-12-31T12:00:00Z" ))));
4449
4550 final Capture <Date > captureStart = Capture .newInstance ();
4651 final Capture <Date > captureEnd = Capture .newInstance ();
@@ -57,10 +62,83 @@ public void testGetStorageStats() throws Exception {
5762 final Date end = captureEnd .getValue ();
5863 //assertEquals("Start date should be 00:00:00 GMT of the same day as input date", "2019-12-31 00:00:00 UTC",
5964 // format.print(start.getTime()));
60- assertEquals ("Start date should be 00:00:00 GMT of the next day as input date" , "2019-12-31 00:00:00 UTC" ,
61- format .print (start .getTime ()));
62- assertEquals ("End date should be 00:00:00 GMT of the next day as input date" , "2020-01-01 00:00:00 UTC" ,
63- format .print (end .getTime ()));
65+ assertEquals ("Start date should be 00:00:00 GMT of the next day as input date" , "2019-12-31T00:00:00Z" ,
66+ dateTimeFormatter .format (start .toInstant ()));
67+ assertEquals ("End date should be 00:00:00 GMT of the next day as input date" , "2020-01-01T00:00:00Z" ,
68+ dateTimeFormatter .format (end .toInstant ()));
69+
70+ }
71+
72+ @ Test
73+ public void testSortsStorageStats () {
74+ spaceStatsRepo = mock (JpaSpaceStatsRepo .class );
75+ final var accountId = "account-id" ;
76+ final var storeId = "id" ;
77+ final var byteCount = new BigDecimal (10000 );
78+ final var objectCount = new BigDecimal (100 );
79+
80+ final Date firstDate = Date .from (Instant .from (dateTimeFormatter .parse (("2019-12-31T12:00:00Z" ))));
81+ final Date lastDate = Date .from (Instant .from (dateTimeFormatter .parse (("2023-12-31T12:00:00Z" ))));
82+ final var now = Date .from (Instant .now ());
83+
84+ // A few quirks to note from the JpaSpaceStatsRepo:
85+ // - The time returned needs to be divided by 1000. See when creating new StoreStatsDTO/SpaceStatsDTO objects
86+ // - We get back a BigDecimal for the file and byte counts as they use the aggregate function `avg`
87+ final var unsorted = List .of (
88+ List .of (lastDate .getTime () / 1000 , accountId , storeId , byteCount , objectCount ).toArray (),
89+ List .of (firstDate .getTime () / 1000 , accountId , storeId , byteCount , objectCount ).toArray ());
90+
91+ expect (spaceStatsRepo .getByAccountIdAndStoreId (eq (accountId ), eq (storeId ), eq (firstDate ),
92+ eq (now ), eq (JpaSpaceStatsRepo .INTERVAL_DAY )))
93+ .andReturn (unsorted );
94+
95+ replay (spaceStatsRepo );
96+ final var resource = new StorageStatsResource (spaceStatsRepo );
97+ final List <StoreStatsDTO > storageProviderStats =
98+ resource .getStorageProviderStats (accountId , storeId , firstDate , now , StorageStatsResource .GroupBy .day );
99+ verify (spaceStatsRepo );
100+
101+ assertEquals (2 , storageProviderStats .size ());
102+ assertEquals (firstDate , storageProviderStats .get (0 ).getTimestamp ());
103+ assertEquals (lastDate , storageProviderStats .get (1 ).getTimestamp ());
104+ }
105+
106+ @ Test
107+ public void testSortsSpaceStats () {
108+ spaceStatsRepo = mock (JpaSpaceStatsRepo .class );
109+ final var accountId = "account-id" ;
110+ final var storeId = "id" ;
111+ final var spaceId = "space-id" ;
112+ final var byteCount = new BigDecimal (10000 );
113+ final var objectCount = new BigDecimal (100 );
114+
115+ final var firstDate = Date .from (Instant .from (dateTimeFormatter .parse ("2019-12-31T12:00:00Z" )));
116+ final var lastDate = Date .from (Instant .from (dateTimeFormatter .parse ("2023-12-31T12:00:00Z" )));
117+ final var now = Date .from (Instant .now ());
118+
119+ // A few quirks to note from the JpaSpaceStatsRepo:
120+ // - The time returned needs to be divided by 1000. See when creating new StoreStatsDTO/SpaceStatsDTO objects
121+ // - We get back a BigDecimal for the file and byte counts as they use the aggregate function `avg`
122+ // - The final column is the timestamp as a local date format (e.g. 2019-12-31)
123+ final List <Object []> unsorted = List .of (
124+ List .of (lastDate .getTime () / 1000 , accountId , storeId , spaceId , byteCount , objectCount ,
125+ LocalDate .ofInstant (lastDate .toInstant (), ZoneOffset .UTC ).toString ()).toArray (),
126+ List .of (firstDate .getTime () / 1000 , accountId , storeId , spaceId , byteCount , objectCount ,
127+ LocalDate .ofInstant (firstDate .toInstant (), ZoneOffset .UTC ).toString ()).toArray ());
128+
129+ expect (spaceStatsRepo .getByAccountIdAndStoreIdAndSpaceId (eq (accountId ), eq (storeId ), eq (spaceId ),
130+ eq (firstDate ), eq (now ),
131+ eq (JpaSpaceStatsRepo .INTERVAL_DAY )))
132+ .andReturn (unsorted );
133+
134+ replay (spaceStatsRepo );
135+ final var resource = new StorageStatsResource (spaceStatsRepo );
136+ final var storageProviderStats = resource .getSpaceStats (accountId , storeId , spaceId , firstDate , now ,
137+ StorageStatsResource .GroupBy .day );
138+ verify (spaceStatsRepo );
64139
140+ assertEquals (2 , storageProviderStats .size ());
141+ assertEquals (firstDate , storageProviderStats .get (0 ).getTimestamp ());
142+ assertEquals (lastDate , storageProviderStats .get (1 ).getTimestamp ());
65143 }
66144}
0 commit comments