@Test
  public void testDatasetVersionState() throws Exception {
    Account testAccount = makeTestAccountWithContainer();
    Container testContainer = new ArrayList<>(testAccount.getAllContainers()).get(0);
    Dataset dataset = new DatasetBuilder(testAccount.getName(), testContainer.getName(), DATASET_NAME).setVersionSchema(
        Dataset.VersionSchema.MONOTONIC).setRetentionCount(1).build();

    // Add a dataset to db
    mySqlAccountStore.addDataset(testAccount.getId(), testContainer.getId(), dataset);

    // add dataset version which is in progress state.
    DatasetVersionRecord datasetVersionRecordFromMysql;
    String version = "LATEST";
    DatasetVersionRecord expectedDatasetVersionRecord =
        new DatasetVersionRecord(testAccount.getId(), testContainer.getId(), DATASET_NAME, "1", -1, null);
    datasetVersionRecordFromMysql =
        mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME, version, -1, System.currentTimeMillis(), false,
            DatasetVersionState.IN_PROGRESS);
    assertEquals("Mismatch in dataset", expectedDatasetVersionRecord, datasetVersionRecordFromMysql);

    // get a latest dataset version when no version exist, should fail.
    try {
      mySqlAccountStore.getDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
          testContainer.getName(), DATASET_NAME, version);
      fail();
    } catch (AccountServiceException e) {
      assertEquals("Mismatch on error code", AccountServiceErrorCode.NotFound, e.getErrorCode());
    }

    //Upload a new latest version, should be version 2 since we have version 1 in progress
    expectedDatasetVersionRecord =
        new DatasetVersionRecord(testAccount.getId(), testContainer.getId(), DATASET_NAME, "2", -1, null);
    datasetVersionRecordFromMysql =
        mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME, version, -1, System.currentTimeMillis(), false,
            DatasetVersionState.READY);
    assertEquals("Mismatch in dataset", expectedDatasetVersionRecord, datasetVersionRecordFromMysql);

    //get the latest version which is version2.
    datasetVersionRecordFromMysql =
        mySqlAccountStore.getDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME, version);
    assertEquals("Mismatch in dataset", expectedDatasetVersionRecord, datasetVersionRecordFromMysql);

    //retention add version 3 with in progress, should not be considered as a valid version, so retention count still meet requirement.
    datasetVersionRecordFromMysql =
        mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME, version, -1, System.currentTimeMillis(), false,
            DatasetVersionState.IN_PROGRESS);
    List<DatasetVersionRecord> datasetVersionRecordList =
        mySqlAccountStore.getAllValidVersionsOutOfRetentionCount(testAccount.getId(), testContainer.getId(),
            testAccount.getName(), testContainer.getName(), DATASET_NAME);
    assertEquals("Mismatch in datasetVersionRecordList size", 0, datasetVersionRecordList.size());

    //update the version3 states to ready, should be considered as a valid version and delete version 2.
    mySqlAccountStore.updateDatasetVersionState(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME, "3", DatasetVersionState.READY);
    datasetVersionRecordList =
        mySqlAccountStore.getAllValidVersionsOutOfRetentionCount(testAccount.getId(), testContainer.getId(),
            testAccount.getName(), testContainer.getName(), DATASET_NAME);
    assertEquals("Mismatch in datasetVersionRecordList size", 1, datasetVersionRecordList.size());

    //delete the dataset, and can't get the in progress dataset version.
    mySqlAccountStore.deleteDataset(testAccount.getId(), testContainer.getId(), DATASET_NAME);
    version = "1";
    try {
      mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
          testContainer.getName(), DATASET_NAME, version, -1, System.currentTimeMillis(), false, null);
      fail("Should fail due to the dataset already gone");
    } catch (AccountServiceException e) {
      assertEquals("Unexpected error code", AccountServiceErrorCode.Deleted, e.getErrorCode());
    }
  }
Tests connection refresh for all non transient sql exceptions
ambry-account/src/integration-test/java/com/github/ambry/account/MySqlAccountServiceIntegrationTest.java