use of com.github.ambry.clustermap.MockClusterMap in project ambry by linkedin.
the class MockReadableStreamChannel method verifyBlob.
/**
* Verifies that the blob associated with the blob id returned by a successful put operation has exactly the same
* data as the original object that was put.
* @param blobId the blobId of the blob that is to be verified.
* @param properties the {@link BlobProperties} of the blob that is to be verified
* @param originalPutContent original content of the blob
* @param originalUserMetadata original user-metadata of the blob
* @param serializedRequests the mapping from blob ids to their corresponding serialized {@link PutRequest}.
*/
private void verifyBlob(String blobId, BlobProperties properties, byte[] originalPutContent, byte[] originalUserMetadata, HashMap<String, ByteBuffer> serializedRequests) throws Exception {
ByteBuffer serializedRequest = serializedRequests.get(blobId);
PutRequest.ReceivedPutRequest request = deserializePutRequest(serializedRequest);
NotificationBlobType notificationBlobType;
if (request.getBlobType() == BlobType.MetadataBlob) {
notificationBlobType = NotificationBlobType.Composite;
byte[] data = Utils.readBytesFromStream(request.getBlobStream(), (int) request.getBlobSize());
CompositeBlobInfo compositeBlobInfo = MetadataContentSerDe.deserializeMetadataContentRecord(ByteBuffer.wrap(data), new BlobIdFactory(mockClusterMap));
Assert.assertEquals("Wrong max chunk size in metadata", chunkSize, compositeBlobInfo.getChunkSize());
Assert.assertEquals("Wrong total size in metadata", originalPutContent.length, compositeBlobInfo.getTotalSize());
List<StoreKey> dataBlobIds = compositeBlobInfo.getKeys();
Assert.assertEquals("Number of chunks is not as expected", RouterUtils.getNumChunksForBlobAndChunkSize(originalPutContent.length, chunkSize), dataBlobIds.size());
// verify user-metadata
if (properties.isEncrypted()) {
ByteBuffer userMetadata = request.getUsermetadata();
BlobId origBlobId = new BlobId(blobId, mockClusterMap);
// reason to directly call run() instead of spinning up a thread instead of calling start() is that, any exceptions or
// assertion failures in non main thread will not fail the test.
new DecryptJob(origBlobId, request.getBlobEncryptionKey().duplicate(), null, userMetadata, cryptoService, kms, new CryptoJobMetricsTracker(metrics.decryptJobMetrics), (result, exception) -> {
Assert.assertNull("Exception should not be thrown", exception);
Assert.assertEquals("BlobId mismatch", origBlobId, result.getBlobId());
Assert.assertArrayEquals("UserMetadata mismatch", originalUserMetadata, result.getDecryptedUserMetadata().array());
}).run();
} else {
Assert.assertArrayEquals("UserMetadata mismatch", originalUserMetadata, request.getUsermetadata().array());
}
verifyCompositeBlob(properties, originalPutContent, originalUserMetadata, dataBlobIds, request, serializedRequests);
} else {
notificationBlobType = NotificationBlobType.Simple;
byte[] content = Utils.readBytesFromStream(request.getBlobStream(), (int) request.getBlobSize());
if (!properties.isEncrypted()) {
Assert.assertArrayEquals("Input blob and written blob should be the same", originalPutContent, content);
Assert.assertArrayEquals("UserMetadata mismatch for simple blob", originalUserMetadata, request.getUsermetadata().array());
notificationSystem.verifyNotification(blobId, notificationBlobType, request.getBlobProperties());
} else {
ByteBuffer userMetadata = request.getUsermetadata();
BlobId origBlobId = new BlobId(blobId, mockClusterMap);
// reason to directly call run() instead of spinning up a thread instead of calling start() is that, any exceptions or
// assertion failures in non main thread will not fail the test.
new DecryptJob(origBlobId, request.getBlobEncryptionKey().duplicate(), ByteBuffer.wrap(content), userMetadata, cryptoService, kms, new CryptoJobMetricsTracker(metrics.decryptJobMetrics), new Callback<DecryptJob.DecryptJobResult>() {
@Override
public void onCompletion(DecryptJob.DecryptJobResult result, Exception exception) {
Assert.assertNull("Exception should not be thrown", exception);
Assert.assertEquals("BlobId mismatch", origBlobId, result.getBlobId());
Assert.assertArrayEquals("Content mismatch", originalPutContent, result.getDecryptedBlobContent().array());
Assert.assertArrayEquals("UserMetadata mismatch", originalUserMetadata, result.getDecryptedUserMetadata().array());
}
}).run();
}
}
notificationSystem.verifyNotification(blobId, notificationBlobType, request.getBlobProperties());
}
use of com.github.ambry.clustermap.MockClusterMap in project ambry by linkedin.
the class AmbryBlobStorageServiceFactoryTest method getAmbryBlobStorageServiceFactoryWithBadInputTest.
/**
* Tests instantiation of {@link AmbryBlobStorageServiceFactory} with bad input.
* @throws Exception
*/
@Test
public void getAmbryBlobStorageServiceFactoryWithBadInputTest() throws Exception {
// dud properties. server should pick up defaults
Properties properties = new Properties();
VerifiableProperties verifiableProperties = new VerifiableProperties(properties);
ClusterMap clusterMap = new MockClusterMap();
RestResponseHandler restResponseHandler = new MockRestRequestResponseHandler();
Router router = new InMemoryRouter(verifiableProperties, clusterMap);
// VerifiableProperties null.
try {
new AmbryBlobStorageServiceFactory(null, clusterMap, restResponseHandler, router, new MockNotifier());
fail("Instantiation should have failed because one of the arguments was null");
} catch (IllegalArgumentException e) {
// expected. Nothing to do.
}
// ClusterMap null.
try {
new AmbryBlobStorageServiceFactory(verifiableProperties, null, restResponseHandler, router, new MockNotifier());
fail("Instantiation should have failed because one of the arguments was null");
} catch (IllegalArgumentException e) {
// expected. Nothing to do.
}
// RestResponseHandler null.
try {
new AmbryBlobStorageServiceFactory(verifiableProperties, clusterMap, null, router, new MockNotifier());
fail("Instantiation should have failed because one of the arguments was null");
} catch (IllegalArgumentException e) {
// expected. Nothing to do.
}
// Router null.
try {
new AmbryBlobStorageServiceFactory(verifiableProperties, clusterMap, restResponseHandler, null, new MockNotifier());
fail("Instantiation should have failed because one of the arguments was null");
} catch (IllegalArgumentException e) {
// expected. Nothing to do.
}
}
use of com.github.ambry.clustermap.MockClusterMap in project ambry by linkedin.
the class AmbrySecurityServiceFactoryTest method getAmbrySecurityServiceFactoryTest.
/**
* Tests instantiation of {@link AmbrySecurityServiceFactory}.
* @throws Exception
*/
@Test
public void getAmbrySecurityServiceFactoryTest() throws Exception {
SecurityService securityService = new AmbrySecurityServiceFactory(new VerifiableProperties(new Properties()), new MockClusterMap(), null, null, null, null, QuotaTestUtils.createDummyQuotaManager()).getSecurityService();
Assert.assertNotNull(securityService);
}
use of com.github.ambry.clustermap.MockClusterMap in project ambry by linkedin.
the class FrontendRestRequestServiceFactoryTest method getFrontendRestRequestServiceFactoryWithBadInputTest.
/**
* Tests instantiation of {@link FrontendRestRequestServiceFactory} with bad input.
* @throws Exception
*/
@Test
public void getFrontendRestRequestServiceFactoryWithBadInputTest() throws Exception {
// dud properties. server should pick up defaults
Properties properties = new Properties();
VerifiableProperties verifiableProperties = new VerifiableProperties(properties);
ClusterMap clusterMap = new MockClusterMap();
Router router = new InMemoryRouter(verifiableProperties, clusterMap);
AccountService accountService = new InMemAccountService(false, true);
// VerifiableProperties null.
try {
new FrontendRestRequestServiceFactory(null, clusterMap, router, accountService);
fail("Instantiation should have failed because VerifiableProperties was null");
} catch (NullPointerException e) {
// expected. Nothing to do.
}
// ClusterMap null.
try {
new FrontendRestRequestServiceFactory(verifiableProperties, null, router, accountService);
fail("Instantiation should have failed because ClusterMap was null");
} catch (NullPointerException e) {
// expected. Nothing to do.
}
// Router null.
try {
new FrontendRestRequestServiceFactory(verifiableProperties, clusterMap, null, accountService);
fail("Instantiation should have failed because Router was null");
} catch (NullPointerException e) {
// expected. Nothing to do.
}
// AccountService null.
try {
new FrontendRestRequestServiceFactory(verifiableProperties, clusterMap, router, null);
fail("Instantiation should have failed because AccountService was null");
} catch (NullPointerException e) {
// expected. Nothing to do.
}
}
use of com.github.ambry.clustermap.MockClusterMap in project ambry by linkedin.
the class FrontendUtilsTest method testGetBlobIdFromString.
/**
* Tests {@link FrontendUtils#getBlobIdFromString(String, ClusterMap)}
* @throws IOException
* @throws RestServiceException
*/
@Test
public void testGetBlobIdFromString() throws IOException, RestServiceException {
// good path
byte[] bytes = new byte[2];
ClusterMap referenceClusterMap = new MockClusterMap();
TestUtils.RANDOM.nextBytes(bytes);
BlobId.BlobIdType referenceType = TestUtils.RANDOM.nextBoolean() ? BlobId.BlobIdType.NATIVE : BlobId.BlobIdType.CRAFTED;
TestUtils.RANDOM.nextBytes(bytes);
byte referenceDatacenterId = bytes[0];
short referenceAccountId = getRandomShort(TestUtils.RANDOM);
short referenceContainerId = getRandomShort(TestUtils.RANDOM);
PartitionId referencePartitionId = referenceClusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0);
boolean referenceIsEncrypted = TestUtils.RANDOM.nextBoolean();
List<Short> versions = Arrays.stream(BlobId.getAllValidVersions()).filter(version -> version >= BlobId.BLOB_ID_V3).collect(Collectors.toList());
for (short version : versions) {
BlobId blobId = new BlobId(version, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted, BlobId.BlobDataType.DATACHUNK);
BlobId regeneratedBlobId = FrontendUtils.getBlobIdFromString(blobId.getID(), referenceClusterMap);
assertEquals("BlobId mismatch", blobId, regeneratedBlobId);
assertBlobIdFieldValues(regeneratedBlobId, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, version >= BlobId.BLOB_ID_V4 && referenceIsEncrypted);
// bad path
try {
FrontendUtils.getBlobIdFromString(blobId.getID().substring(1), referenceClusterMap);
fail("Should have thrown exception for bad blobId ");
} catch (RestServiceException e) {
assertEquals("RestServiceErrorCode mismatch", RestServiceErrorCode.BadRequest, e.getErrorCode());
}
}
}
Aggregations