@Test
  public void testRenameDatasetVersion() throws Exception {
    Account testAccount = makeTestAccountWithContainer();
    Container testContainer = new ArrayList<>(testAccount.getAllContainers()).get(0);
    long ttl = 36000L;
    Dataset dataset =
        new DatasetBuilder(testAccount.getName(), testContainer.getName(), DATASET_NAME_RENAME).setVersionSchema(
            Dataset.VersionSchema.SEMANTIC_LONG).setRetentionCount(1).setRetentionTimeInSeconds(ttl).build();
    Set<String> expectedFinalVersion = new HashSet<>();
    Set<String> allRenamedVersionsInDb = new HashSet<>();
    // Add a dataset to db
    mySqlAccountStore.addDataset(testAccount.getId(), testContainer.getId(), dataset);
    // add source dataset version
    String sourceVersion = "999.999.999.999";
    allRenamedVersionsInDb.add(sourceVersion);
    DatasetVersionRecord expectedDatasetVersionRecord =
        new DatasetVersionRecord(testAccount.getId(), testContainer.getId(), DATASET_NAME_RENAME, sourceVersion, -1,
            null);
    long creationTime = System.currentTimeMillis();
    mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME_RENAME, sourceVersion, -1, creationTime, false,
        DatasetVersionState.READY);
    // rename to a target dataset version
    // 999.999.999.999 -> 1.1.1.1(succeed)
    String targetVersion = "1.1.1.1";
    expectedFinalVersion.add(targetVersion);
    mySqlAccountStore.renameDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME_RENAME, sourceVersion, targetVersion);
    // get the renamed version.
    DatasetVersionRecord renamedDatasetVersion =
        mySqlAccountStore.getDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_RENAME, targetVersion);
    //verify each field
    assertEquals("Mismatch in dataset version's accountId", expectedDatasetVersionRecord.getAccountId(),
        renamedDatasetVersion.getAccountId());
    assertEquals("Mismatch in dataset version's containerId", expectedDatasetVersionRecord.getContainerId(),
        renamedDatasetVersion.getContainerId());
    assertEquals("Mismatch in dataset version's datasetName", expectedDatasetVersionRecord.getDatasetName(),
        renamedDatasetVersion.getDatasetName());
    assertEquals("Mismatch in dataset version's ", creationTime + dataset.getRetentionTimeInSeconds() * 1000,
        renamedDatasetVersion.getExpirationTimeMs());
    assertEquals("Mismatch in dataset version's ", targetVersion, renamedDatasetVersion.getVersion());

    //get previous version and should be deleted.
    try {
      mySqlAccountStore.getDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
          testContainer.getName(), DATASET_NAME_RENAME, sourceVersion);
      fail();
    } catch (AccountServiceException e) {
      assertEquals("Mismatch on error code", AccountServiceErrorCode.NotFound, e.getErrorCode());
    }

    // sourceVersion is auto incr version, should fail.
    sourceVersion = "MAJOR";
    try {
      mySqlAccountStore.renameDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
          testContainer.getName(), DATASET_NAME_RENAME, sourceVersion, targetVersion);
      fail();
    } catch (IllegalArgumentException e) {
      //no-op
    }

    // targetVersion is auto incr version, should fail.
    sourceVersion = "0.0.0.0";
    targetVersion = "MAJOR";

    try {
      mySqlAccountStore.renameDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
          testContainer.getName(), DATASET_NAME_RENAME, sourceVersion, targetVersion);
      fail();
    } catch (IllegalArgumentException e) {
      //no-op
    }

    // rename a version which does not exist, should fail
    // 2.2.2.2 -> 888.888.888.888(fail)
    sourceVersion = "2.2.2.2";
    targetVersion = "888.888.888.888";

    try {
      mySqlAccountStore.renameDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
          testContainer.getName(), DATASET_NAME_RENAME, sourceVersion, targetVersion);
      fail();
    } catch (AccountServiceException e) {
      assertEquals("Mismatch on error code", AccountServiceErrorCode.NotFound, e.getErrorCode());
    }

    // rename to an existing version, should throw conflict.
    // rename a version which does not exist, should fail
    // 1.1.1.1 -> 1.1.1.1(fail)
    sourceVersion = "1.1.1.1";
    targetVersion = "1.1.1.1";

    try {
      mySqlAccountStore.renameDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
          testContainer.getName(), DATASET_NAME_RENAME, sourceVersion, targetVersion);
      fail();
    } catch (AccountServiceException e) {
      assertEquals("Mismatch on error code", AccountServiceErrorCode.ResourceConflict, e.getErrorCode());
    }

    //Add a renamed version, should fail with conflict
    String renamedVersion = "1.1.1.1";
    try {
      mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
          testContainer.getName(), DATASET_NAME_RENAME, renamedVersion, -1, System.currentTimeMillis(), false,
          DatasetVersionState.READY);
      fail();
    } catch (AccountServiceException e) {
      assertEquals("Mismatch on error code", AccountServiceErrorCode.ResourceConflict, e.getErrorCode());
    }

    //rename a renamed version, should fail with not found
    //1.1.1.1 -> 888.888.888.888(fail)
    sourceVersion = "999.999.999.999";
    targetVersion = "888.888.888.888";
    try {
      mySqlAccountStore.renameDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
          testContainer.getName(), DATASET_NAME_RENAME, sourceVersion, targetVersion);
      fail();
    } catch (AccountServiceException e) {
      assertEquals("Mismatch on error code", AccountServiceErrorCode.NotFound, e.getErrorCode());
    }

    //rename twice, and it should point to the original version.
    //998.998.998.998 -> 997.997.997.997(succeed)
    String originalSourceVersion = "998.998.998.998";
    allRenamedVersionsInDb.add(originalSourceVersion);
    mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME_RENAME, originalSourceVersion, -1, System.currentTimeMillis(), false,
        DatasetVersionState.READY);

    targetVersion = "997.997.997.997";
    allRenamedVersionsInDb.add(targetVersion);
    mySqlAccountStore.renameDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME_RENAME, originalSourceVersion, targetVersion);
    DatasetVersionRecord renamedDatasetVersionFirstTime =
        mySqlAccountStore.getDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_RENAME, targetVersion);
    assertEquals("Rename from does not match", originalSourceVersion, renamedDatasetVersionFirstTime.getRenameFrom());

    //997.997.997.997 -> 3.3.3.3(succeed)
    String renamedSourceVersion = "997.997.997.997";
    targetVersion = "3.3.3.3";
    expectedFinalVersion.add(targetVersion);
    mySqlAccountStore.renameDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME_RENAME, renamedSourceVersion, targetVersion);
    DatasetVersionRecord renamedDatasetVersionSecondTime =
        mySqlAccountStore.getDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_RENAME, targetVersion);
    assertEquals("Rename from does not match", originalSourceVersion, renamedDatasetVersionSecondTime.getRenameFrom());

    //list the dataset version.
    Page<String> allDatasetVersions =
        mySqlAccountStore.listAllValidDatasetVersions(testAccount.getId(), testContainer.getId(), DATASET_NAME_RENAME,
            null);
    Set<String> allVersions = new HashSet<>(allDatasetVersions.getEntries());
    assertEquals("Final versions should match", allVersions, expectedFinalVersion);

    //get Valid version out of retention
    //at this moment, we have 1.1.1.1 and 3.3.3.3 which is the valid version
    //999.999.999.999, 998.998.998.998, 997.997.997.997 has been renamed.
    List<DatasetVersionRecord> datasetVersionOutOfRetention =
        mySqlAccountStore.getAllValidVersionsOutOfRetentionCount(testAccount.getId(), testContainer.getId(),
            testAccount.getName(), testContainer.getName(), DATASET_NAME_RENAME);
    assertEquals("Unexpected out of retention count", 1, datasetVersionOutOfRetention.size());

    //If I want to delete a dataset with all versions, I should  include the renamed state dataset version.
    List<DatasetVersionRecord> datasetVersionRecordsForDatasetDeletion =
        mySqlAccountStore.getAllValidVersionForDatasetDeletion(testAccount.getId(), testContainer.getId(),
            DATASET_NAME_RENAME);
    assertEquals("Unexpected delete numbers", 5, datasetVersionRecordsForDatasetDeletion.size());

    //Update dataset version
    for (String version : allRenamedVersionsInDb) {
      try {
        mySqlAccountStore.updateDatasetVersionTtl(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_RENAME, version);
        fail();
      } catch (AccountServiceException e) {
        assertEquals("Mismatch on error code", AccountServiceErrorCode.NotFound, e.getErrorCode());
      }
    }

    for (String version : expectedFinalVersion) {
      mySqlAccountStore.updateDatasetVersionTtl(testAccount.getId(), testContainer.getId(), testAccount.getName(),
          testContainer.getName(), DATASET_NAME_RENAME, version);
      DatasetVersionRecord datasetVersionRecordFromDb =
          mySqlAccountStore.getDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
              testContainer.getName(), DATASET_NAME_RENAME, version);
      assertEquals("The dataset version should be permanent after ttl update.", -1,
          datasetVersionRecordFromDb.getExpirationTimeMs());
      mySqlAccountStore.deleteDatasetVersion(testAccount.getId(), testContainer.getId(), DATASET_NAME_RENAME, version);
    }

    //DELETE a version which been renamed, should not be found?
    for (String version : allRenamedVersionsInDb) {
      try {
        mySqlAccountStore.deleteDatasetVersion(testAccount.getId(), testContainer.getId(), DATASET_NAME_RENAME,
            version);
        fail();
      } catch (AccountServiceException e) {
        assertEquals("Mismatch on error code", AccountServiceErrorCode.NotFound, e.getErrorCode());
      }
    }

    for (String version : allRenamedVersionsInDb) {
      //If the delete is to delete the dataset,
      mySqlAccountStore.deleteDatasetVersionForDatasetDelete(testAccount.getId(), testContainer.getId(),
          DATASET_NAME_RENAME, version);
      try {
        mySqlAccountStore.getDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_RENAME, version);
        fail();
      } catch (AccountServiceException e) {
        assertEquals("Mismatch on error code", AccountServiceErrorCode.NotFound, e.getErrorCode());
      }
    }

    //add a deleted version
    sourceVersion = "1.1.1.1";
    mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME_RENAME, renamedVersion, -1, System.currentTimeMillis(), false,
        DatasetVersionState.READY);
    DatasetVersionRecord datasetVersionRecord =
        mySqlAccountStore.getDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_RENAME, sourceVersion);
    assertNull("Rename from should be null", datasetVersionRecord.getRenameFrom());

    //rename a deleted version should return not found
    mySqlAccountStore.deleteDatasetVersion(testAccount.getId(), testContainer.getId(), DATASET_NAME_RENAME, sourceVersion);
    targetVersion = "5.5.5.5";
    try {
      mySqlAccountStore.renameDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME_RENAME, renamedSourceVersion, targetVersion);
      fail();
    } catch (AccountServiceException e) {
      assertEquals("Mismatch on error code", AccountServiceErrorCode.NotFound, e.getErrorCode());
    }
  }
Tests connection refresh for all non transient sql exceptions
ambry-account/src/integration-test/java/com/github/ambry/account/MySqlAccountServiceIntegrationTest.java