@Test
  public void testSizeLimitInList() throws Exception {
    RouterStore store =
        new RouterStore(accountServiceMetrics, backup, helixStore, new AtomicReference<>(router), forBackfill,
            TOTAL_NUMBER_OF_VERSION_TO_KEEP, config);

    Map<Short, Account> idToRefAccountMap = new HashMap<>();
    Map<Short, Map<Short, Container>> idtoRefContainerMap = new HashMap<>();
    Set<Short> accountIDSet = new HashSet<>();
    for (int i = 0; i < TOTAL_NUMBER_OF_VERSION_TO_KEEP; i++) {
      // generate an new account and test update and fetch on this account
      Map<Short, Account> newIdToRefAccountMap = new HashMap<>();
      AccountTestUtils.generateRefAccounts(newIdToRefAccountMap, idtoRefContainerMap, accountIDSet, 1, 1);
      if (!forBackfill) {
        for (Map.Entry<Short, Account> entry : newIdToRefAccountMap.entrySet()) {
          idToRefAccountMap.put(entry.getKey(), entry.getValue());
        }
      } else {
        idToRefAccountMap = newIdToRefAccountMap;
      }
      assertUpdateAndFetch(store, idToRefAccountMap, newIdToRefAccountMap, i + 1, i + 1);
    }
    // Now we already have maximum number of versions in the list, adding a new version should remove the oldest one.
    List<RouterStore.BlobIDAndVersion> blobIDAndVersions = getBlobIDAndVersionInHelix(TOTAL_NUMBER_OF_VERSION_TO_KEEP);
    Collections.sort(blobIDAndVersions, Comparator.comparing(RouterStore.BlobIDAndVersion::getVersion));
    assertEquals(1, blobIDAndVersions.get(0).getVersion());

    Map<Short, Account> newIdToRefAccountMap = new HashMap<>();
    AccountTestUtils.generateRefAccounts(newIdToRefAccountMap, idtoRefContainerMap, accountIDSet, 1, 1);
    if (!forBackfill) {
      for (Map.Entry<Short, Account> entry : newIdToRefAccountMap.entrySet()) {
        idToRefAccountMap.put(entry.getKey(), entry.getValue());
      }
    } else {
      idToRefAccountMap = newIdToRefAccountMap;
    }
    assertUpdateAndFetch(store, idToRefAccountMap, newIdToRefAccountMap, TOTAL_NUMBER_OF_VERSION_TO_KEEP + 1,
        TOTAL_NUMBER_OF_VERSION_TO_KEEP);

    // Get the list again and compare the blob ids
    List<RouterStore.BlobIDAndVersion> blobIDAndVersionsAfterUpdate =
        getBlobIDAndVersionInHelix(TOTAL_NUMBER_OF_VERSION_TO_KEEP);
    Collections.sort(blobIDAndVersionsAfterUpdate, Comparator.comparing(RouterStore.BlobIDAndVersion::getVersion));
    assertEquals("First version should be removed", 2, blobIDAndVersionsAfterUpdate.get(0).getVersion());
    assertEquals("Version mismatch", TOTAL_NUMBER_OF_VERSION_TO_KEEP + 1,
        blobIDAndVersionsAfterUpdate.get(TOTAL_NUMBER_OF_VERSION_TO_KEEP - 1).getVersion());

    for (int i = 1; i < TOTAL_NUMBER_OF_VERSION_TO_KEEP; i++) {
      assertEquals("BlobIDAndVersion mismatch at index " + i, blobIDAndVersions.get(i),
          blobIDAndVersionsAfterUpdate.get(i - 1));
    }
    try {
      router.getBlob(blobIDAndVersions.get(0).getBlobID(), new GetBlobOptionsBuilder().build()).get();
      fail("Expecting not found exception");
    } catch (ExecutionException e) {
      Throwable t = e.getCause();
      assertTrue("Cause should be RouterException", t instanceof RouterException);
      assertEquals("ErrorCode mismatch", RouterErrorCode.BlobDoesNotExist, ((RouterException) t).getErrorCode());
    }
  }
Test when the number of updates exceeds the maximum number of versions to save. RouterStore should start removing
the old blobs.
ambry-account/src/test/java/com/github/ambry/account/RouterStoreTest.java