Search in sources :

Example 66 with Account

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

the class AccountContainerTest method testRemovingContainers.

/**
 * Tests removing containers in AccountBuilder.
 */
@Test
public void testRemovingContainers() throws JSONException {
    Account origin = accountFromJson(refAccountJson);
    AccountBuilder accountBuilder = new AccountBuilder(origin);
    // first, remove 10 containers
    ArrayList<Container> containers = new ArrayList<>(origin.getAllContainers());
    Set<Container> removed = new HashSet<>();
    while (removed.size() < 10) {
        Container container = containers.get(random.nextInt(containers.size()));
        removed.add(container);
        accountBuilder.removeContainer(container);
    }
    Account account = accountBuilder.build();
    assertEquals("Wrong number of containers", CONTAINER_COUNT - 10, account.getAllContainers().size());
    for (Container removedContainer : removed) {
        assertNull("Container not removed ", account.getContainerById(removedContainer.getId()));
        assertNull("Container not removed ", account.getContainerByName(removedContainer.getName()));
    }
    // then, remove the rest containers
    for (Container container : origin.getAllContainers()) {
        accountBuilder.removeContainer(container);
    }
    account = accountBuilder.build();
    assertEquals("Wrong container number.", 0, account.getAllContainers().size());
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 67 with Account

use of com.github.ambry.account.Account 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 68 with Account

use of com.github.ambry.account.Account 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 69 with Account

use of com.github.ambry.account.Account 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)

Example 70 with Account

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

the class RestUtils method convertToStr.

/**
 * Convert the specified {@link RestRequest} object to a string representation.
 * @param restRequest {@link RestRequest} object.
 * @return String representation of the {@link RestRequest} object.
 */
public static String convertToStr(RestRequest restRequest) {
    if (Objects.isNull(restRequest)) {
        return "RestRequest: null";
    }
    StringBuilder sb = new StringBuilder();
    sb.append("RestRequest: [");
    sb.append("Method: " + ((restRequest.getRestMethod() == null) ? "null" : restRequest.getRestMethod().name()));
    sb.append(", Path: " + ((restRequest.getPath() == null) ? "null" : restRequest.getPath()));
    sb.append(", Uri: " + ((restRequest.getUri() == null) ? "null" : restRequest.getUri()));
    Account account = null;
    try {
        account = RestUtils.getAccountFromArgs(restRequest.getArgs());
    } catch (RestServiceException restServiceException) {
    }
    sb.append(", Account: " + ((account == null) ? "null" : account.toString()));
    Container container = null;
    try {
        container = RestUtils.getContainerFromArgs(restRequest.getArgs());
    } catch (RestServiceException restServiceException) {
    }
    sb.append(", Container: " + ((container == null) ? "null" : container.toString()));
    sb.append("]");
    return sb.toString();
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container)

Aggregations

Account (com.github.ambry.account.Account)114 Container (com.github.ambry.account.Container)87 Test (org.junit.Test)67 RestServiceException (com.github.ambry.rest.RestServiceException)24 ArrayList (java.util.ArrayList)22 RestRequest (com.github.ambry.rest.RestRequest)18 JSONObject (org.json.JSONObject)18 MockRestRequest (com.github.ambry.rest.MockRestRequest)17 VerifiableProperties (com.github.ambry.config.VerifiableProperties)16 HashMap (java.util.HashMap)15 HashSet (java.util.HashSet)15 AccountBuilder (com.github.ambry.account.AccountBuilder)14 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)14 ContainerBuilder (com.github.ambry.account.ContainerBuilder)13 Properties (java.util.Properties)13 MetricRegistry (com.codahale.metrics.MetricRegistry)12 InMemAccountService (com.github.ambry.account.InMemAccountService)12 ByteBuffer (java.nio.ByteBuffer)12 RestMethod (com.github.ambry.rest.RestMethod)11 Map (java.util.Map)11