|
17 | 17 |
|
18 | 18 | import java.math.BigDecimal; |
19 | 19 | import java.time.Instant; |
| 20 | +import java.time.LocalDate; |
| 21 | +import java.time.ZoneOffset; |
20 | 22 | import java.time.format.DateTimeFormatter; |
21 | 23 | import java.util.ArrayList; |
22 | 24 | import java.util.Date; |
@@ -66,4 +68,77 @@ public void testGetStorageStats() { |
66 | 68 | dateTimeFormatter.format(end.toInstant())); |
67 | 69 |
|
68 | 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); |
| 139 | + |
| 140 | + assertEquals(2, storageProviderStats.size()); |
| 141 | + assertEquals(firstDate, storageProviderStats.get(0).getTimestamp()); |
| 142 | + assertEquals(lastDate, storageProviderStats.get(1).getTimestamp()); |
| 143 | + } |
69 | 144 | } |
0 commit comments