Search in sources :

Example 76 with Container

use of com.github.ambry.account.Container in project ambry by linkedin.

the class AccountContainerTest method testAccountEqual.

/**
 * Tests {@link Account#equals(Object)} that checks equality of {@link Container}s.
 */
@Test
public void testAccountEqual() {
    // Check two accounts with same fields but no containers.
    Account accountNoContainer = new AccountBuilder(refAccountId, refAccountName, refAccountStatus).build();
    Account accountNoContainerDuplicate = new AccountBuilder(refAccountId, refAccountName, refAccountStatus).build();
    assertEquals("Two accounts should be equal.", accountNoContainer, accountNoContainerDuplicate);
    // Check two accounts with same fields and containers.
    Account accountWithContainers = accountFromJson(refAccountJson);
    Account accountWithContainersDuplicate = accountFromJson(refAccountJson);
    assertEquals("Two accounts should be equal.", accountWithContainers, accountWithContainersDuplicate);
    // Check two accounts with same fields but one has containers, the other one does not.
    assertFalse("Two accounts should not be equal.", accountNoContainer.equals(accountWithContainers));
    // Check two accounts with the same fields and the same number of containers. One container of one account has one
    // field different from the other one.
    Container updatedContainer = new ContainerBuilder(refContainerIds.get(0), refContainerNames.get(0), refContainerStatuses.get(0), "A changed container description", refAccountId).setEncrypted(refContainerEncryptionValues.get(0)).setPreviouslyEncrypted(refContainerPreviousEncryptionValues.get(0)).setCacheable(refContainerCachingValues.get(0)).setBackupEnabled(refContainerBackupEnabledValues.get(0)).setMediaScanDisabled(refContainerMediaScanDisabledValues.get(0)).setReplicationPolicy(refContainerReplicationPolicyValues.get(0)).setTtlRequired(refContainerTtlRequiredValues.get(0)).setSecurePathRequired(refContainerSignedPathRequiredValues.get(0)).setOverrideAccountAcl(refContainerOverrideAccountAcls.get(0)).setNamedBlobMode(refContainerNamedBlobModes.get(0)).setContentTypeWhitelistForFilenamesOnDownload(refContainerContentTypeAllowListForFilenamesOnDownloadValues.get(0)).setDeleteTriggerTime(refContainerDeleteTriggerTime.get(0)).setLastModifiedTime(refContainerLastModifiedTimes.get(0)).setSnapshotVersion(refContainerSnapshotVersions.get(0)).build();
    refContainers.remove(0);
    refContainers.add(updatedContainer);
    Account accountWithModifiedContainers = new AccountBuilder(refAccountId, refAccountName, refAccountStatus).build();
    assertFalse("Two accounts should not be equal.", accountWithContainers.equals(accountWithModifiedContainers));
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) Test(org.junit.Test)

Example 77 with Container

use of com.github.ambry.account.Container in project ambry by linkedin.

the class AccountContainerTest method testUpdateAccount.

/**
 * Tests update an {@link Account}.
 * @throws JSONException
 */
@Test
public void testUpdateAccount() throws JSONException {
    // set an account with different field value
    Account origin = accountFromJson(refAccountJson);
    AccountBuilder accountBuilder = new AccountBuilder(origin);
    short updatedAccountId = (short) (refAccountId + 1);
    String updatedAccountName = refAccountName + "-updated";
    Account.AccountStatus updatedAccountStatus = Account.AccountStatus.INACTIVE;
    accountBuilder.id(updatedAccountId);
    accountBuilder.name(updatedAccountName);
    accountBuilder.status(updatedAccountStatus);
    try {
        accountBuilder.build();
        fail("Should have thrown");
    } catch (IllegalStateException e) {
    // expected, as new account id does not match the parentAccountId of the two containers.
    }
    // remove all existing containers.
    for (Container container : origin.getAllContainers()) {
        accountBuilder.removeContainer(container);
    }
    // build the account and assert
    Account updatedAccount = accountBuilder.build();
    assertEquals(updatedAccountId, updatedAccount.getId());
    assertEquals(updatedAccountName, updatedAccount.getName());
    assertEquals(updatedAccountStatus, updatedAccount.getStatus());
    // add back the containers and assert
    for (Container container : origin.getAllContainers()) {
        accountBuilder.addOrUpdateContainer(container);
    }
    accountBuilder.id(refAccountId);
    updatedAccount = accountBuilder.build();
    assertEquals(origin.getAllContainers().toString(), updatedAccount.getAllContainers().toString());
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) Test(org.junit.Test)

Example 78 with Container

use of com.github.ambry.account.Container in project ambry by linkedin.

the class AccountContainerTest method testConstructContainerFromJson.

/**
 * Tests constructing a {@link Container} from json object.
 */
@Test
public void testConstructContainerFromJson() throws JSONException {
    for (int i = 0; i < CONTAINER_COUNT; i++) {
        Container containerFromJson = containerFromJson(containerJsonList.get(i), refAccountId);
        assertContainer(containerFromJson, i);
    }
}
Also used : Container(com.github.ambry.account.Container) Test(org.junit.Test)

Example 79 with Container

use of com.github.ambry.account.Container in project ambry by linkedin.

the class AccountContainerTest method initializeRefContainers.

/**
 * Initializes reference containers.
 * @throws JSONException
 */
private void initializeRefContainers() throws JSONException {
    refContainerIds = new ArrayList<>();
    refContainerNames = new ArrayList<>();
    refContainerStatuses = new ArrayList<>();
    refContainerDescriptions = new ArrayList<>();
    refContainerEncryptionValues = new ArrayList<>();
    refContainerPreviousEncryptionValues = new ArrayList<>();
    refContainerCachingValues = new ArrayList<>();
    refContainerBackupEnabledValues = new ArrayList<>();
    refContainerMediaScanDisabledValues = new ArrayList<>();
    refContainerReplicationPolicyValues = new ArrayList<>();
    refContainerTtlRequiredValues = new ArrayList<>();
    refContainerDeleteTriggerTime = new ArrayList<>();
    refContainerSignedPathRequiredValues = new ArrayList<>();
    refContainerOverrideAccountAcls = new ArrayList<>();
    refContainerNamedBlobModes = new ArrayList<>();
    refContainerContentTypeAllowListForFilenamesOnDownloadValues = new ArrayList<>();
    refContainerLastModifiedTimes = new ArrayList<>();
    refContainerSnapshotVersions = new ArrayList<>();
    containerJsonList = new ArrayList<>();
    refContainers = new ArrayList<>();
    refAccessControlAllowOriginValues = new ArrayList<>();
    Set<Short> containerIdSet = new HashSet<>();
    Set<String> containerNameSet = new HashSet<>();
    for (int i = 0; i < CONTAINER_COUNT; i++) {
        short containerId = Utils.getRandomShort(random);
        String containerName = UUID.randomUUID().toString();
        if (!containerIdSet.add(containerId) || !containerNameSet.add(containerName)) {
            i--;
            continue;
        }
        refContainerIds.add(containerId);
        refContainerNames.add(containerName);
        refContainerStatuses.add(random.nextBoolean() ? ContainerStatus.ACTIVE : ContainerStatus.INACTIVE);
        refContainerDescriptions.add(UUID.randomUUID().toString());
        boolean encrypted = (i % 2 == 0);
        boolean previouslyEncrypted = encrypted || (i % 4 < 2);
        refContainerEncryptionValues.add(encrypted);
        refContainerPreviousEncryptionValues.add(previouslyEncrypted);
        refContainerCachingValues.add(random.nextBoolean());
        refContainerBackupEnabledValues.add(random.nextBoolean());
        refContainerMediaScanDisabledValues.add(random.nextBoolean());
        if (refContainerReplicationPolicyValues.contains(null)) {
            refContainerReplicationPolicyValues.add(TestUtils.getRandomString(10));
        } else {
            refContainerReplicationPolicyValues.add(null);
        }
        refContainerTtlRequiredValues.add(random.nextBoolean());
        refContainerSignedPathRequiredValues.add(random.nextBoolean());
        refContainerOverrideAccountAcls.add(random.nextBoolean());
        refContainerNamedBlobModes.add(random.nextBoolean() ? NamedBlobMode.DISABLED : NamedBlobMode.OPTIONAL);
        refContainerDeleteTriggerTime.add((long) 0);
        if (i == 0) {
            refContainerContentTypeAllowListForFilenamesOnDownloadValues.add(null);
        } else if (i == 1) {
            refContainerContentTypeAllowListForFilenamesOnDownloadValues.add(Collections.emptySet());
        } else {
            refContainerContentTypeAllowListForFilenamesOnDownloadValues.add(getRandomContentTypeAllowListForFilenamesOnDownload());
        }
        refContainerLastModifiedTimes.add(System.currentTimeMillis());
        refContainerSnapshotVersions.add(random.nextInt());
        if (i == 0) {
            refAccessControlAllowOriginValues.add("*");
        } else if (i == 1) {
            refAccessControlAllowOriginValues.add("");
        } else {
            refAccessControlAllowOriginValues.add("https://" + TestUtils.getRandomString(10) + ".com");
        }
        refContainers.add(new Container(refContainerIds.get(i), refContainerNames.get(i), refContainerStatuses.get(i), refContainerDescriptions.get(i), refContainerEncryptionValues.get(i), refContainerPreviousEncryptionValues.get(i), refContainerCachingValues.get(i), refContainerMediaScanDisabledValues.get(i), refContainerReplicationPolicyValues.get(i), refContainerTtlRequiredValues.get(i), refContainerSignedPathRequiredValues.get(i), refContainerContentTypeAllowListForFilenamesOnDownloadValues.get(i), refContainerBackupEnabledValues.get(i), refContainerOverrideAccountAcls.get(i), refContainerNamedBlobModes.get(i), refAccountId, refContainerDeleteTriggerTime.get(i), refContainerLastModifiedTimes.get(i), refContainerSnapshotVersions.get(i), refAccessControlAllowOriginValues.get(i)));
        containerJsonList.add(buildContainerJson(refContainers.get(i)));
    }
}
Also used : Container(com.github.ambry.account.Container) HashSet(java.util.HashSet)

Example 80 with Container

use of com.github.ambry.account.Container in project ambry by linkedin.

the class AccountContainerTest method testUpdateContainerInAccount.

/**
 * Tests updating containers in an account.
 * @throws JSONException
 */
@Test
public void testUpdateContainerInAccount() throws JSONException {
    Account account = accountFromJson(refAccountJson);
    AccountBuilder accountBuilder = new AccountBuilder(account);
    // updating with different containers
    for (int i = 0; i < CONTAINER_COUNT; i++) {
        Container container = account.getContainerById(refContainerIds.get(i));
        accountBuilder.removeContainer(container);
        ContainerBuilder containerBuilder = new ContainerBuilder(container);
        short updatedContainerId = (short) (-1 * (container.getId()));
        String updatedContainerName = container.getName() + "-updated";
        Container.ContainerStatus updatedContainerStatus = Container.ContainerStatus.INACTIVE;
        String updatedContainerDescription = container.getDescription() + "--updated";
        boolean updatedEncrypted = !container.isEncrypted();
        boolean updatedPreviouslyEncrypted = updatedEncrypted || container.wasPreviouslyEncrypted();
        boolean updatedCacheable = !container.isCacheable();
        boolean updatedMediaScanDisabled = !container.isMediaScanDisabled();
        String updatedReplicationPolicy = container.getReplicationPolicy() + "---updated";
        boolean updatedTtlRequired = !container.isTtlRequired();
        boolean updatedSignedPathRequired = !container.isSecurePathRequired();
        String updatedAccessControlAllowOrigin = container.getAccessControlAllowOrigin() + "---updated";
        Set<String> updatedContentTypeAllowListForFilenamesOnDownloadValues = container.getContentTypeWhitelistForFilenamesOnDownload().stream().map(contentType -> contentType + "--updated").collect(Collectors.toSet());
        containerBuilder.setId(updatedContainerId).setName(updatedContainerName).setStatus(updatedContainerStatus).setDescription(updatedContainerDescription).setEncrypted(updatedEncrypted).setCacheable(updatedCacheable).setMediaScanDisabled(updatedMediaScanDisabled).setReplicationPolicy(updatedReplicationPolicy).setTtlRequired(updatedTtlRequired).setSecurePathRequired(updatedSignedPathRequired).setContentTypeWhitelistForFilenamesOnDownload(updatedContentTypeAllowListForFilenamesOnDownloadValues).setAccessControlAllowOrigin(updatedAccessControlAllowOrigin);
        accountBuilder.addOrUpdateContainer(containerBuilder.build());
        // build account and assert
        Account updatedAccount = accountBuilder.build();
        Container updatedContainer = updatedAccount.getContainerById(updatedContainerId);
        assertEquals("container id is not correctly updated", updatedContainerId, updatedContainer.getId());
        assertEquals("container name is not correctly updated", updatedContainerName, updatedContainer.getName());
        assertEquals("container status is not correctly updated", updatedContainerStatus, updatedContainer.getStatus());
        assertEquals("container description is not correctly updated", updatedContainerDescription, updatedContainer.getDescription());
        assertEquals("cacheable is not correctly updated", updatedCacheable, updatedContainer.isCacheable());
        switch(Container.getCurrentJsonVersion()) {
            case Container.JSON_VERSION_1:
                assertEquals("Wrong encryption setting", ENCRYPTED_DEFAULT_VALUE, updatedContainer.isEncrypted());
                assertEquals("Wrong previous encryption setting", PREVIOUSLY_ENCRYPTED_DEFAULT_VALUE, updatedContainer.wasPreviouslyEncrypted());
                assertEquals("Wrong media scan disabled setting", MEDIA_SCAN_DISABLED_DEFAULT_VALUE, updatedContainer.isMediaScanDisabled());
                assertNull("Wrong replication policy", updatedContainer.getReplicationPolicy());
                assertEquals("Wrong ttl required setting", TTL_REQUIRED_DEFAULT_VALUE, updatedContainer.isTtlRequired());
                assertEquals("Wrong secure required setting", SECURE_PATH_REQUIRED_DEFAULT_VALUE, updatedContainer.isSecurePathRequired());
                assertEquals("Wrong content type allow list for filenames on download value", CONTENT_TYPE_WHITELIST_FOR_FILENAMES_ON_DOWNLOAD_DEFAULT_VALUE, updatedContainer.getContentTypeWhitelistForFilenamesOnDownload());
                assertEquals("Wrong accessControlAllowOrigin", "", updatedContainer.getAccessControlAllowOrigin());
                break;
            case Container.JSON_VERSION_2:
                assertEquals("Wrong encryption setting", updatedEncrypted, updatedContainer.isEncrypted());
                assertEquals("Wrong previous encryption setting", updatedPreviouslyEncrypted, updatedContainer.wasPreviouslyEncrypted());
                assertEquals("Wrong media scan disabled setting", updatedMediaScanDisabled, updatedContainer.isMediaScanDisabled());
                assertEquals("Wrong replication policy", updatedReplicationPolicy, updatedContainer.getReplicationPolicy());
                assertEquals("Wrong ttl required setting", updatedTtlRequired, updatedContainer.isTtlRequired());
                assertEquals("Wrong secure path required setting", updatedSignedPathRequired, updatedContainer.isSecurePathRequired());
                assertEquals("Wrong content type allow list for filenames on download value", updatedContentTypeAllowListForFilenamesOnDownloadValues, updatedContainer.getContentTypeWhitelistForFilenamesOnDownload());
                assertEquals("Wrong accessControlAllowOrigin", updatedAccessControlAllowOrigin, updatedContainer.getAccessControlAllowOrigin());
                break;
            default:
                throw new IllegalStateException("Unsupported version: " + Container.getCurrentJsonVersion());
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RunWith(org.junit.runner.RunWith) Random(java.util.Random) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) Parameterized(org.junit.runners.Parameterized) ValueInstantiationException(com.fasterxml.jackson.databind.exc.ValueInstantiationException) Container(com.github.ambry.account.Container) QuotaResourceType(com.github.ambry.quota.QuotaResourceType) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) Utils(com.github.ambry.utils.Utils) IOException(java.io.IOException) Test(org.junit.Test) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Writer(java.io.Writer) Account(com.github.ambry.account.Account) Assert(org.junit.Assert) Collections(java.util.Collections) JSONArray(org.json.JSONArray) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) Test(org.junit.Test)

Aggregations

Container (com.github.ambry.account.Container)119 Account (com.github.ambry.account.Account)88 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)30 RestServiceException (com.github.ambry.rest.RestServiceException)20 ContainerBuilder (com.github.ambry.account.ContainerBuilder)17 JSONObject (org.json.JSONObject)17 VerifiableProperties (com.github.ambry.config.VerifiableProperties)16 HashSet (java.util.HashSet)15 HashMap (java.util.HashMap)14 Properties (java.util.Properties)14 AccountBuilder (com.github.ambry.account.AccountBuilder)13 RestRequest (com.github.ambry.rest.RestRequest)13 ByteBuffer (java.nio.ByteBuffer)13 Map (java.util.Map)13 MetricRegistry (com.codahale.metrics.MetricRegistry)12 TestUtils (com.github.ambry.utils.TestUtils)12 Collections (java.util.Collections)12 List (java.util.List)12 Assert (org.junit.Assert)12