@Test
  public void testAddGetAndUpdateDatasets() throws Exception {
    Account testAccount = makeTestAccountWithContainer();
    Container testContainer = new ArrayList<>(testAccount.getAllContainers()).get(0);
    //add dataset with must provide info
    Dataset dataset =
        new DatasetBuilder(testAccount.getName(), testContainer.getName(), DATASET_NAME_BASIC).setVersionSchema(
            Dataset.VersionSchema.MONOTONIC).build();
    mySqlAccountStore.addDataset(testAccount.getId(), testContainer.getId(), dataset);
    Dataset datasetFromMysql =
        mySqlAccountStore.getDataset(testAccount.getId(), testContainer.getId(), testAccount.getName(),
            testContainer.getName(), DATASET_NAME_BASIC);
    //do not check retentionPolicy as the default will be written into db.
    verifyDatasetProperties(dataset, datasetFromMysql);

    Map<String, String> userTags = new HashMap<>();
    userTags.put("userTag", "tagValue");
    int retentionCount = 5;
    dataset = new DatasetBuilder(testAccount.getName(), testContainer.getName(), DATASET_NAME).setVersionSchema(
            Dataset.VersionSchema.TIMESTAMP)
        .setRetentionTimeInSeconds((long) -1)
        .setUserTags(userTags)
        .setRetentionCount(retentionCount)
        .build();
    // Add dataset to db
    mySqlAccountStore.addDataset(testAccount.getId(), testContainer.getId(), dataset);

    // query db and check
    datasetFromMysql = mySqlAccountStore.getDataset(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME);

    verifyDatasetProperties(dataset, datasetFromMysql);
    assertEquals("Mismatch in retentionPolicy", DEFAULT_RETENTION_POLICY, datasetFromMysql.getRetentionPolicy());

    // Add dataset again, should fail due to already exist.
    try {
      mySqlAccountStore.addDataset(testAccount.getId(), testContainer.getId(), dataset);
      fail("Should fail due to dataset already exist");
    } catch (AccountServiceException e) {
      assertEquals("Unexpected ErrorCode", AccountServiceErrorCode.ResourceConflict, e.getErrorCode());
    }

    //update the retention count only.
    int newRetentionCount = 10;
    Dataset datasetForUpdate =
        new DatasetBuilder(testAccount.getName(), testContainer.getName(), DATASET_NAME).setRetentionCount(10).build();
    mySqlAccountStore.updateDataset(testAccount.getId(), testContainer.getId(), datasetForUpdate);
    datasetFromMysql = mySqlAccountStore.getDataset(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME);
    assertEquals("Mismatch in updated retention count", (Integer) newRetentionCount,
        datasetFromMysql.getRetentionCount());

    //update the expiration time.
    long datasetTtl = 30;
    datasetForUpdate =
        new DatasetBuilder(testAccount.getName(), testContainer.getName(), DATASET_NAME).setRetentionTimeInSeconds(
            datasetTtl).build();
    mySqlAccountStore.updateDataset(testAccount.getId(), testContainer.getId(), datasetForUpdate);
    datasetFromMysql = mySqlAccountStore.getDataset(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME);
    assertEquals("Mismatch in updated dataset ttl", (Long) datasetTtl, datasetFromMysql.getRetentionTimeInSeconds());

    //update the user tags
    userTags.clear();
    userTags.put("userTagNew", "tagValueNew");
    datasetForUpdate =
        new DatasetBuilder(testAccount.getName(), testContainer.getName(), DATASET_NAME).setUserTags(userTags).build();
    mySqlAccountStore.updateDataset(testAccount.getId(), testContainer.getId(), datasetForUpdate);
    datasetFromMysql = mySqlAccountStore.getDataset(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME);
    assertEquals("Mismatch in user tags", userTags, datasetFromMysql.getUserTags());

    //update dataset if the dataset does not exist (should fail)
    datasetForUpdate =
        new DatasetBuilder(testAccount.getName(), testContainer.getName(), DATASET_NAME_NOT_EXIST).setUserTags(userTags)
            .build();
    try {
      mySqlAccountStore.updateDataset(testAccount.getId(), testContainer.getId(), datasetForUpdate);
      fail("should fail due to this dataset does not exist");
    } catch (AccountServiceException e) {
      assertEquals("Unexpected ErrorCode", AccountServiceErrorCode.NotFound, e.getErrorCode());
    }

    //Add a dataset with limited ttl
    dataset =
        new DatasetBuilder(testAccount.getName(), testContainer.getName(), DATASET_NAME_WITH_TTL).setVersionSchema(
                Dataset.VersionSchema.TIMESTAMP)
            .setRetentionTimeInSeconds(datasetTtl)
            .setUserTags(userTags)
            .setRetentionCount(retentionCount)
            .build();
    mySqlAccountStore.addDataset(testAccount.getId(), testContainer.getId(), dataset);

    //update expiration to permanent.
    datasetTtl = -1;
    datasetForUpdate = new DatasetBuilder(testAccount.getName(), testContainer.getName(),
        DATASET_NAME_WITH_TTL).setRetentionTimeInSeconds(datasetTtl).build();
    mySqlAccountStore.updateDataset(testAccount.getId(), testContainer.getId(), datasetForUpdate);
    datasetFromMysql = mySqlAccountStore.getDataset(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME_WITH_TTL);
    assertEquals("Mismatch in updated dataset ttl", (Long) datasetTtl, datasetFromMysql.getRetentionTimeInSeconds());

    //list all datasets
    Page<String> allDatasets = mySqlAccountStore.listAllValidDatasets(testAccount.getId(), testContainer.getId(), null);
    List<String> expectedDatasets = Arrays.asList(DATASET_NAME, DATASET_NAME_BASIC, DATASET_NAME_WITH_TTL);
    assertEquals("Mismatch for datasets list", expectedDatasets, allDatasets.getEntries());

    //update the listDatasetsMaxResult
    mySqlConfigProps.setProperty(LIST_DATASETS_MAX_RESULT, "2");
    mySqlAccountStore = spy(new MySqlAccountStoreFactory(new VerifiableProperties(mySqlConfigProps),
        new MetricRegistry()).getMySqlAccountStore());
    when(mockMySqlAccountStoreFactory.getMySqlAccountStore()).thenReturn(mySqlAccountStore);
    mySqlAccountService = getAccountService();
    Page<String> partialDatasets =
        mySqlAccountStore.listAllValidDatasets(testAccount.getId(), testContainer.getId(), null);
    expectedDatasets = Arrays.asList(DATASET_NAME, DATASET_NAME_BASIC);
    assertEquals("Mismatch for datasets list", expectedDatasets, partialDatasets.getEntries());
    assertEquals("Mismatch on next page token", DATASET_NAME_WITH_TTL, partialDatasets.getNextPageToken());

    partialDatasets =
        mySqlAccountStore.listAllValidDatasets(testAccount.getId(), testContainer.getId(), DATASET_NAME_WITH_TTL);
    expectedDatasets = Arrays.asList(DATASET_NAME_WITH_TTL);
    assertEquals("Mismatch for datasets list", expectedDatasets, partialDatasets.getEntries());

    //delete a dataset
    mySqlAccountStore.deleteDataset(testAccount.getId(), testContainer.getId(), DATASET_NAME);
    try {
      mySqlAccountStore.getDataset(testAccount.getId(), testContainer.getId(), testAccount.getName(),
          testContainer.getName(), DATASET_NAME);
      fail("Should fail due to the dataset already expired");
    } catch (AccountServiceException e) {
      assertEquals("Mistmatch on error code", AccountServiceErrorCode.Deleted, e.getErrorCode());
    }

    //add new dataset with same primary key after it has been deleted.
    dataset = new DatasetBuilder(testAccount.getName(), testContainer.getName(), DATASET_NAME).setVersionSchema(
        Dataset.VersionSchema.TIMESTAMP).setRetentionTimeInSeconds((long) -1).setRetentionCount(retentionCount).build();
    mySqlAccountStore.addDataset(testAccount.getId(), testContainer.getId(), dataset);
    datasetFromMysql = mySqlAccountStore.getDataset(testAccount.getId(), testContainer.getId(), testAccount.getName(),
        testContainer.getName(), DATASET_NAME);
    verifyDatasetProperties(dataset, datasetFromMysql);
  }
Test add, get and update dataset.
@throws Exception
ambry-account/src/integration-test/java/com/github/ambry/account/MySqlAccountServiceIntegrationTest.java