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());
}
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));
}
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());
}
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());
}
}
}
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();
}
Aggregations