use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class AmbrySecurityServiceTest method testPostBlob.
/**
* Tests {@link AmbrySecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)} for
* {@link RestMethod#POST}.
* @throws Exception
*/
private void testPostBlob() throws Exception {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
RestRequest restRequest = createRestRequest(RestMethod.POST, "/", null);
securityService.processResponse(restRequest, restResponseChannel, DEFAULT_INFO).get();
Assert.assertEquals("ProcessResponse status should have been set", ResponseStatus.Created, restResponseChannel.getStatus());
Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
Assert.assertEquals("Creation time should have been set correctly", RestUtils.toSecondsPrecisionInMs(DEFAULT_INFO.getBlobProperties().getCreationTimeInMs()), RestUtils.getTimeFromDateString(restResponseChannel.getHeader(RestUtils.Headers.CREATION_TIME)).longValue());
Assert.assertEquals("Content-Length should have been 0", "0", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH));
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class AmbrySecurityServiceTest method testHeadBlob.
/**
* Tests {@link AmbrySecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)} for
* {@link RestMethod#HEAD}.
* @param blobInfo the {@link BlobInfo} of the blob for which {@link RestMethod#HEAD} is required.
* @param range the {@link ByteRange} used for a range request, or {@code null} for non-ranged requests.
* @throws Exception
*/
private void testHeadBlob(BlobInfo blobInfo, ByteRange range) throws Exception {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
JSONObject headers = range != null ? new JSONObject().put(RestUtils.Headers.RANGE, RestTestUtils.getRangeHeaderString(range)) : null;
RestRequest restRequest = createRestRequest(RestMethod.HEAD, "/", headers);
Pair<Account, Container> accountAndContainer = getAccountAndContainer(blobInfo.getBlobProperties());
insertAccountAndContainer(restRequest, accountAndContainer.getFirst(), accountAndContainer.getSecond());
securityService.processResponse(restRequest, restResponseChannel, blobInfo).get();
Assert.assertEquals("ProcessResponse status should have been set", range == null ? ResponseStatus.Ok : ResponseStatus.PartialContent, restResponseChannel.getStatus());
verifyHeadersForHead(blobInfo.getBlobProperties(), range, restResponseChannel);
verifyAccountAndContainerHeaders(restResponseChannel, accountAndContainer.getFirst(), accountAndContainer.getSecond());
Assert.assertEquals("LifeVersion mismatch", Short.toString(blobInfo.getLifeVersion()), restResponseChannel.getHeader(RestUtils.Headers.LIFE_VERSION));
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class GetReplicasHandlerTest method getReplicasWithBadInputTest.
/**
* Tests reactions of the {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)} operation to bad input -
* specifically if we do not include required parameters.
*/
@Test
public void getReplicasWithBadInputTest() {
// bad input - invalid blob id.
try {
getReplicasHandler.getReplicas("12345", new MockRestResponseChannel());
fail("Exception should have been thrown because the blobid is invalid");
} catch (RestServiceException e) {
assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.NotFound, e.getErrorCode());
}
// bad input - invalid blob id for this cluster map.
String blobId = "AAEAAQAAAAAAAADFAAAAJDMyYWZiOTJmLTBkNDYtNDQyNS1iYzU0LWEwMWQ1Yzg3OTJkZQ.gif";
try {
getReplicasHandler.getReplicas(blobId, new MockRestResponseChannel());
fail("Exception should have been thrown because the blobid is invalid");
} catch (RestServiceException e) {
assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.NotFound, e.getErrorCode());
}
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class GetReplicasHandlerTest method getReplicasTest.
/**
* Tests {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)}
* <p/>
* For the each {@link PartitionId} in the {@link ClusterMap}, a {@link BlobId} is created. The replica list returned
* from {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)}is checked for equality against a locally
* obtained replica list.
* @throws Exception
*/
@Test
public void getReplicasTest() throws Exception {
List<? extends PartitionId> partitionIds = CLUSTER_MAP.getWritablePartitionIds(null);
for (PartitionId partitionId : partitionIds) {
String originalReplicaStr = partitionId.getReplicaIds().toString().replace(", ", ",");
BlobId blobId = new BlobId(CommonTestUtils.getCurrentBlobIdVersion(), BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, partitionId, false, BlobId.BlobDataType.DATACHUNK);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
ReadableStreamChannel channel = getReplicasHandler.getReplicas(blobId.getID(), restResponseChannel);
assertEquals("Unexpected response status", ResponseStatus.Ok, restResponseChannel.getStatus());
assertEquals("Unexpected Content-Type", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
String returnedReplicasStr = RestTestUtils.getJsonizedResponseBody(channel).get(GetReplicasHandler.REPLICAS_KEY).toString().replace("\"", "");
assertEquals("Replica IDs returned for the BlobId do no match with the replicas IDs of partition", originalReplicaStr, returnedReplicasStr);
}
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class GetStatsReportHandlerTest method handleGoodCaseTest.
@Test
public void handleGoodCaseTest() throws Exception {
AggregatedAccountStorageStats aggregatedAccountStorageStats = new AggregatedAccountStorageStats(StorageStatsUtilTest.generateRandomAggregatedAccountStorageStats((short) 1, 10, 10, 1000L, 2, 100));
doAnswer(invocation -> {
String clusterName = invocation.getArgument(0);
if (clusterName.equals(CLUSTER_NAME)) {
return aggregatedAccountStorageStats;
} else {
return null;
}
}).when(accountStatsStore).queryAggregatedAccountStorageStatsByClusterName(anyString());
RestRequest restRequest = createRestRequest(CLUSTER_NAME, StatsReportType.ACCOUNT_REPORT.name());
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
ReadableStreamChannel channel = sendRequestGetResponse(restRequest, restResponseChannel);
assertNotNull("There should be a response", channel);
assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
assertEquals("Storage stats mismatch", aggregatedAccountStorageStats.getStorageStats(), mapper.readValue(RestTestUtils.getResponseBody(channel), AggregatedAccountStorageStats.class).getStorageStats());
AggregatedPartitionClassStorageStats aggregatedPartitionClassStorageStats = new AggregatedPartitionClassStorageStats(StorageStatsUtilTest.generateRandomAggregatedPartitionClassStorageStats(new String[] { "default", "newClass" }, (short) 1, 10, 10, 1000L, 2, 100));
doAnswer(invocation -> {
String clusterName = invocation.getArgument(0);
if (clusterName.equals(CLUSTER_NAME)) {
return aggregatedPartitionClassStorageStats;
} else {
return null;
}
}).when(accountStatsStore).queryAggregatedPartitionClassStorageStatsByClusterName(anyString());
restRequest = createRestRequest(CLUSTER_NAME, StatsReportType.PARTITION_CLASS_REPORT.name());
restResponseChannel = new MockRestResponseChannel();
channel = sendRequestGetResponse(restRequest, restResponseChannel);
assertNotNull("There should be a response", channel);
assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
assertEquals("Storage stats mismatch", aggregatedPartitionClassStorageStats.getStorageStats(), mapper.readValue(RestTestUtils.getResponseBody(channel), AggregatedPartitionClassStorageStats.class).getStorageStats());
}
Aggregations