@Test
  public void testFetchMonthlyStorageUsage() throws Exception {
    MockTime mockTime = new MockTime(SystemTime.getInstance().milliseconds());
    MySqlStorageUsageRefresher.time = mockTime;
    try {
      String currentMonth = MySqlStorageUsageRefresher.getCurrentMonth();

      AggregatedAccountStorageStats aggregatedAccountStorageStats = new AggregatedAccountStorageStats(
          StorageStatsUtilTest.generateRandomAggregatedAccountStorageStats((short) 0, 10, 10, 10000L, 2, 10));
      accountStatsMySqlStore.storeAggregatedAccountStorageStats(aggregatedAccountStorageStats);
      accountStatsMySqlStore.takeSnapshotOfAggregatedAccountStatsAndUpdateMonth(currentMonth);
      properties.remove(StorageQuotaConfig.REFRESHER_POLLING_INTERVAL_MS);
      StorageQuotaConfig storageQuotaConfig = new StorageQuotaConfig(new VerifiableProperties(properties));
      AccountStatsMySqlStore newAccountStatsMySqlStore = createAccountStatsMySqlStore();
      MySqlStorageUsageRefresher refresher =
          new MySqlStorageUsageRefresher(newAccountStatsMySqlStore, scheduler, storageQuotaConfig, metrics);

      // Fetch monthly storage usage
      refresher.fetchStorageUsageMonthlyBase();
      assertEquals(
          MySqlStorageUsageRefresher.convertAggregatedAccountStorageStatsToMap(aggregatedAccountStorageStats, false),
          refresher.getContainerStorageUsageMonthlyBase());

      // Change the month
      String notCurrentMonth = "1970-01";
      AggregatedAccountStorageStats newAggregatedAccountStorageStats = new AggregatedAccountStorageStats(
          StorageStatsUtilTest.generateRandomAggregatedAccountStorageStats((short) 0, 10, 10, 10000L, 2, 10));
      accountStatsMySqlStore.storeAggregatedAccountStorageStats(newAggregatedAccountStorageStats);
      accountStatsMySqlStore.takeSnapshotOfAggregatedAccountStatsAndUpdateMonth(notCurrentMonth);
      refresher.fetchStorageUsageMonthlyBase();
      // Monthly storage usage still the old one
      assertEquals(
          MySqlStorageUsageRefresher.convertAggregatedAccountStorageStatsToMap(aggregatedAccountStorageStats, false),
          refresher.getContainerStorageUsageMonthlyBase());

      // Change the month back to the current month
      accountStatsMySqlStore.takeSnapshotOfAggregatedAccountStatsAndUpdateMonth(currentMonth);
      // Wait for schedule to retry
      Thread.sleep(MYSQL_RETRY_BACKOFF_MS * 2);
      assertEquals(
          MySqlStorageUsageRefresher.convertAggregatedAccountStorageStatsToMap(newAggregatedAccountStorageStats, false),
          refresher.getContainerStorageUsageMonthlyBase());

      // Forward the time to next month
      mockTime.sleep((MySqlStorageUsageRefresher.secondsToNextMonthTick(currentMonth,
          storageQuotaConfig.mysqlMonthlyBaseFetchOffsetSec) + 10) * 1000);
      String nextMonth = MySqlStorageUsageRefresher.getCurrentMonth();

      Function<String, Integer> stringMonthToInteger = (monthInStr) -> {
        String[] parts = monthInStr.split("-");
        int year = Integer.parseInt(parts[0]);
        int month = Integer.parseInt(parts[1]);
        return year * 12 + month;
      };
      assertEquals(stringMonthToInteger.apply(currentMonth) + 1, (int) stringMonthToInteger.apply(nextMonth));
      // Update the month to next month

      AggregatedAccountStorageStats nextAggregatedAccountStorageStats = new AggregatedAccountStorageStats(
          StorageStatsUtilTest.generateRandomAggregatedAccountStorageStats((short) 0, 10, 10, 10000L, 2, 10));
      accountStatsMySqlStore.storeAggregatedAccountStorageStats(nextAggregatedAccountStorageStats);
      accountStatsMySqlStore.takeSnapshotOfAggregatedAccountStatsAndUpdateMonth(nextMonth);
      refresher.fetchStorageUsageMonthlyBase();
      assertEquals(
          MySqlStorageUsageRefresher.convertAggregatedAccountStorageStatsToMap(nextAggregatedAccountStorageStats,
              false), refresher.getContainerStorageUsageMonthlyBase());
      // A backup file should be create as well
      assertEquals(
          MySqlStorageUsageRefresher.convertAggregatedAccountStorageStatsToMap(nextAggregatedAccountStorageStats,
              false), refresher.getBackupFileManager().getBackupFileContent(nextMonth));
    } finally {
      MySqlStorageUsageRefresher.time = SystemTime.getInstance();
    }
  }
Test to update container storage usage monthly base.
ambry-quota/src/integration-test/java/com/github/ambry/quota/storage/MySqlStorageUsageRefresherTest.java