@Test
  public void testLatestVersionSupport() 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).build();

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

    // get a latest dataset version when no version exist, should fail.
    String version = "LATEST";
    DatasetVersionRecord datasetVersionRecordFromMysql;
    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());
    }

    // Add a LATEST dataset version
    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.READY);
    assertEquals("Mismatch in dataset", expectedDatasetVersionRecord, datasetVersionRecordFromMysql);

    // Add a new LATEST dataset version
    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 dataset version
    expectedDatasetVersionRecord =
        new DatasetVersionRecord(testAccount.getId(), testContainer.getId(), DATASET_NAME, "2", -1, null);
    datasetVersionRecordFromMysql =
        mySqlAccountStore.getDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME, version);
    assertEquals("Mismatch in dataset", expectedDatasetVersionRecord, datasetVersionRecordFromMysql);

    // Deleted version 2 and add new latest version
    mySqlAccountStore.deleteDatasetVersion(testAccount.getId(), testContainer.getId(), DATASET_NAME, "2");
    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);

    // Add a dataset with semantic version
    dataset =
        new DatasetBuilder(testAccount.getName(), testContainer.getName(), DATASET_NAME_WITH_SEMANTIC).setVersionSchema(
            Dataset.VersionSchema.SEMANTIC).build();
    mySqlAccountStore.addDataset(testAccount.getId(), testContainer.getId(), dataset);

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

    // add a major version for timestamp schema, should fail.

    // add a major version.
    version = "MAJOR";
    expectedDatasetVersionRecord =
        new DatasetVersionRecord(testAccount.getId(), testContainer.getId(), DATASET_NAME_WITH_SEMANTIC, "1.0.0", -1,
            null);
    datasetVersionRecordFromMysql =
        mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_WITH_SEMANTIC, version, -1, System.currentTimeMillis(), false,
            DatasetVersionState.READY);
    assertEquals("Mismatch in dataset", expectedDatasetVersionRecord, datasetVersionRecordFromMysql);
    // add second major version
    expectedDatasetVersionRecord =
        new DatasetVersionRecord(testAccount.getId(), testContainer.getId(), DATASET_NAME_WITH_SEMANTIC, "2.0.0", -1,
            null);
    datasetVersionRecordFromMysql =
        mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_WITH_SEMANTIC, version, -1, System.currentTimeMillis(), false,
            DatasetVersionState.READY);
    assertEquals("Mismatch in dataset", expectedDatasetVersionRecord, datasetVersionRecordFromMysql);

    // add a patch version.
    version = "PATCH";
    expectedDatasetVersionRecord =
        new DatasetVersionRecord(testAccount.getId(), testContainer.getId(), DATASET_NAME_WITH_SEMANTIC, "2.0.1", -1,
            null);
    datasetVersionRecordFromMysql =
        mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_WITH_SEMANTIC, version, -1, System.currentTimeMillis(), false,
            DatasetVersionState.READY);
    assertEquals("Mismatch in dataset", expectedDatasetVersionRecord, datasetVersionRecordFromMysql);
    // add second path version
    expectedDatasetVersionRecord =
        new DatasetVersionRecord(testAccount.getId(), testContainer.getId(), DATASET_NAME_WITH_SEMANTIC, "2.0.2", -1,
            null);
    datasetVersionRecordFromMysql =
        mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_WITH_SEMANTIC, version, -1, System.currentTimeMillis(), false,
            DatasetVersionState.READY);
    assertEquals("Mismatch in dataset", expectedDatasetVersionRecord, datasetVersionRecordFromMysql);

    // add a minor version.
    version = "MINOR";
    expectedDatasetVersionRecord =
        new DatasetVersionRecord(testAccount.getId(), testContainer.getId(), DATASET_NAME_WITH_SEMANTIC, "2.1.0", -1,
            null);
    datasetVersionRecordFromMysql =
        mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_WITH_SEMANTIC, version, -1, System.currentTimeMillis(), false,
            DatasetVersionState.READY);
    assertEquals("Mismatch in dataset", expectedDatasetVersionRecord, datasetVersionRecordFromMysql);
    // add second minor version
    expectedDatasetVersionRecord =
        new DatasetVersionRecord(testAccount.getId(), testContainer.getId(), DATASET_NAME_WITH_SEMANTIC, "2.2.0", -1,
            null);
    datasetVersionRecordFromMysql =
        mySqlAccountStore.addDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_WITH_SEMANTIC, version, -1, System.currentTimeMillis(), false,
            DatasetVersionState.READY);
    assertEquals("Mismatch in dataset", expectedDatasetVersionRecord, datasetVersionRecordFromMysql);

    //get the latest version.
    version = "LATEST";
    expectedDatasetVersionRecord =
        new DatasetVersionRecord(testAccount.getId(), testContainer.getId(), DATASET_NAME_WITH_SEMANTIC, "2.2.0", -1,
            null);
    datasetVersionRecordFromMysql =
        mySqlAccountStore.getDatasetVersion(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_WITH_SEMANTIC, version);
    assertEquals("Mismatch in dataset", expectedDatasetVersionRecord, datasetVersionRecordFromMysql);

    //list all dataset versions
    Page<String> allDatasetVersions =
        mySqlAccountStore.listAllValidDatasetVersions(testAccount.getId(), testContainer.getId(),
            DATASET_NAME_WITH_SEMANTIC, null);
    List<String> expectedDatasetVersions = Arrays.asList("1.0.0", "2.0.0", "2.0.1", "2.0.2", "2.1.0", "2.2.0");
    assertEquals("Mismatch for dataset version list", expectedDatasetVersions, allDatasetVersions.getEntries());

    //update the listDatasetVersionsMaxResult
    mySqlConfigProps.setProperty(LIST_DATASET_VERSIONS_MAX_RESULT, "3");
    mySqlAccountStore = spy(new MySqlAccountStoreFactory(new VerifiableProperties(mySqlConfigProps),
        new MetricRegistry()).getMySqlAccountStore());
    when(mockMySqlAccountStoreFactory.getMySqlAccountStore()).thenReturn(mySqlAccountStore);
    mySqlAccountService = getAccountService();
    Page<String> partialDatasetVersions =
        mySqlAccountStore.listAllValidDatasetVersions(testAccount.getId(), testContainer.getId(),
            DATASET_NAME_WITH_SEMANTIC, null);
    expectedDatasetVersions = Arrays.asList("1.0.0", "2.0.0", "2.0.1");
    assertEquals("Mismatch for datasets list", expectedDatasetVersions, partialDatasetVersions.getEntries());
    assertEquals("Mismatch on next page token", "2.0.2", partialDatasetVersions.getNextPageToken());

    //listing dataset version with page token provided.
    partialDatasetVersions =
        mySqlAccountStore.listAllValidDatasetVersions(testAccount.getId(), testContainer.getId(),
            DATASET_NAME_WITH_SEMANTIC, "2.0.0");
    expectedDatasetVersions = Arrays.asList("2.0.0", "2.0.1", "2.0.2");
    assertEquals("Mismatch for datasets list", expectedDatasetVersions, partialDatasetVersions.getEntries());
    assertEquals("Mismatch on next page token", "2.1.0", partialDatasetVersions.getNextPageToken());
  }
Test add and get latest version.
@throws Exception
ambry-account/src/integration-test/java/com/github/ambry/account/MySqlAccountServiceIntegrationTest.java