Search in sources :

Example 21 with Account

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

the class AccountContainerTest method testAccountAndContainerSerDe.

@Test
public void testAccountAndContainerSerDe() throws IOException {
    assumeTrue(Container.getCurrentJsonVersion() == JSON_VERSION_2);
    // Make sure the JSONObject string can be deserialized to jackson object
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
        refAccountJson.write(writer);
    }
    Account deserialized = objectMapper.readValue(outputStream.toByteArray(), Account.class);
    Account fromJson = accountFromJson(refAccountJson);
    assertTrue(deserialized.equals(fromJson));
    // Make sure jackson string can be deserialized to JSONObject object
    String serialized = objectMapper.writer(new DefaultPrettyPrinter()).writeValueAsString(deserialized);
    JSONObject jsonObject = new JSONObject(serialized);
    fromJson = accountFromJson(jsonObject);
    assertTrue(deserialized.equals(fromJson));
    @JsonIgnoreProperties({ "containers" })
    abstract class AccountMixIn {
    }
    ObjectMapper newObjectMapper = new ObjectMapper();
    newObjectMapper.addMixIn(Account.class, AccountMixIn.class);
    serialized = newObjectMapper.writeValueAsString(deserialized);
    assertFalse(serialized.contains("containers"));
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) Account(com.github.ambry.account.Account) JSONObject(org.json.JSONObject) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) Test(org.junit.Test)

Example 22 with Account

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

the class AccountContainerTest method testConstructAccountAndContainerFromArguments.

/**
 * Tests constructing {@link Account} and {@link Container} using individual arguments.
 */
@Test
public void testConstructAccountAndContainerFromArguments() throws JSONException {
    Account accountFromArguments = new Account(refAccountId, refAccountName, refAccountStatus, refAccountAclInheritedByContainer, refAccountSnapshotVersion, refContainers, refQuotaResourceType);
    assertAccountAgainstReference(accountFromArguments, true, true);
}
Also used : Account(com.github.ambry.account.Account) Test(org.junit.Test)

Example 23 with Account

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

the class AccountContainerTest method testToString.

// Tests for builders
/**
 * Tests {@code toString()} methods.
 * @throws JSONException
 */
@Test
public void testToString() throws JSONException {
    Account account = accountFromJson(refAccountJson);
    assertEquals("Account[" + account.getId() + "," + account.getSnapshotVersion() + "]", account.toString());
    for (int i = 0; i < CONTAINER_COUNT; i++) {
        Container container = containerFromJson(containerJsonList.get(i), refAccountId);
        assertEquals("Container[" + account.getId() + ":" + container.getId() + "]", container.toString());
    }
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) Test(org.junit.Test)

Example 24 with Account

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

the class AccountContainerTest method testRemoveNonExistContainer.

/**
 * Tests removing a non-existent container from accountBuilder.
 * @throws JSONException
 */
@Test
public void testRemoveNonExistContainer() throws JSONException {
    Account origin = accountFromJson(refAccountJson);
    AccountBuilder accountBuilder = new AccountBuilder(origin);
    ContainerBuilder containerBuilder = new ContainerBuilder((short) -999, refContainerNames.get(0), refContainerStatuses.get(0), refContainerDescriptions.get(0), 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)).setContentTypeWhitelistForFilenamesOnDownload(refContainerContentTypeAllowListForFilenamesOnDownloadValues.get(0)).setDeleteTriggerTime(refContainerDeleteTriggerTime.get(0)).setAccessControlAllowOrigin(refAccessControlAllowOriginValues.get(0));
    Container container = containerBuilder.build();
    accountBuilder.removeContainer(container);
    accountBuilder.removeContainer(null);
    Account account = accountBuilder.build();
    assertAccountAgainstReference(account, true, true);
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) Test(org.junit.Test)

Example 25 with Account

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

the class RestUtils method buildBlobProperties.

/**
 * Builds {@link BlobProperties} given the arguments associated with a request.
 * @param args the arguments associated with the request. Cannot be {@code null}.
 * @return the {@link BlobProperties} extracted from the arguments.
 * @throws RestServiceException if required arguments aren't present or if they aren't in the format or number
 *                                    expected.
 */
public static BlobProperties buildBlobProperties(Map<String, Object> args) throws RestServiceException {
    Account account = getAccountFromArgs(args);
    Container container = getContainerFromArgs(args);
    String serviceId = getHeader(args, Headers.SERVICE_ID, true);
    String contentType = getHeader(args, Headers.AMBRY_CONTENT_TYPE, true);
    String ownerId = getHeader(args, Headers.OWNER_ID, false);
    String externalAssetTag = getHeader(args, Headers.EXTERNAL_ASSET_TAG, false);
    String contentEncoding = getHeader(args, Headers.AMBRY_CONTENT_ENCODING, false);
    String filename = getHeader(args, Headers.AMBRY_FILENAME, false);
    long ttl = getTtlFromRequestHeader(args);
    // This field should not matter on newly created blobs, because all privacy/cacheability decisions should be made
    // based on the container properties and ACLs. For now, BlobProperties still includes this field, though.
    boolean isPrivate = !container.isCacheable();
    return new BlobProperties(-1, serviceId, ownerId, contentType, isPrivate, ttl, account.getId(), container.getId(), container.isEncrypted(), externalAssetTag, contentEncoding, filename);
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) BlobProperties(com.github.ambry.messageformat.BlobProperties)

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