use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class NamedBlobPutHandlerTest method putBlobAndVerify.
/**
* Make a stitch blob call using {@link PostBlobHandler} and verify the result of the operation.
* @param errorChecker if non-null, expect an exception to be thrown by the post flow and verify it using this
* {@link ThrowingConsumer}.
* @param ttl the ttl used for the blob
* @throws Exception
*/
private void putBlobAndVerify(ThrowingConsumer<ExecutionException> errorChecker, long ttl) throws Exception {
JSONObject headers = new JSONObject();
FrontendRestRequestServiceTest.setAmbryHeadersForPut(headers, ttl, !REF_CONTAINER.isCacheable(), SERVICE_ID, CONTENT_TYPE, OWNER_ID, null, null, null);
byte[] content = TestUtils.getRandomBytes(1024);
RestRequest request = getRestRequest(headers, request_path, content);
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
FutureResult<Void> future = new FutureResult<>();
idConverterFactory.lastInput = null;
idConverterFactory.lastBlobInfo = null;
idConverterFactory.lastConvertedId = null;
namedBlobPutHandler.handle(request, restResponseChannel, future::done);
if (errorChecker == null) {
future.get(TIMEOUT_SECS, TimeUnit.SECONDS);
assertEquals("Unexpected location header", idConverterFactory.lastConvertedId, restResponseChannel.getHeader(RestUtils.Headers.LOCATION));
InMemoryRouter.InMemoryBlob blob = router.getActiveBlobs().get(idConverterFactory.lastInput);
assertEquals("Unexpected blob content stored", ByteBuffer.wrap(content), blob.getBlob());
assertEquals("Unexpected TTL in blob", ttl, blob.getBlobProperties().getTimeToLiveInSeconds());
assertEquals("Unexpected TTL in named blob DB", ttl, idConverterFactory.lastBlobInfo.getBlobProperties().getTimeToLiveInSeconds());
assertEquals("Unexpected response status", restResponseChannel.getStatus(), ResponseStatus.Ok);
} else {
TestUtils.assertException(ExecutionException.class, () -> future.get(TIMEOUT_SECS, TimeUnit.SECONDS), errorChecker);
}
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class AmbryUrlSigningServiceTest method notSignedRequestFailuresTest.
/**
* Tests for some failure scenarios in verification.
* @throws Exception
*/
@Test
public void notSignedRequestFailuresTest() throws Exception {
// positive test done in signAndVerifyTest()
AmbryUrlSigningService signer = getUrlSignerWithDefaults(new MockTime());
RestRequest request = getRequestFromUrl(RestMethod.GET, "/");
assertFalse("Request should not be declared signed", signer.isRequestSigned(request));
ensureVerificationFailure(signer, request, RestServiceErrorCode.InternalServerError);
request.setArg(RestUtils.Headers.URL_TYPE, RestMethod.POST.name());
assertFalse("Request should not be declared signed", signer.isRequestSigned(request));
ensureVerificationFailure(signer, request, RestServiceErrorCode.InternalServerError);
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class AmbryUrlSigningServiceTest method doSignAndVerifyTest.
// signAndVerifyTest() tests helpers
/**
* Generates and verifies signed URLs. Also tests some failure scenarios.
* @param signer the {@link AmbryUrlSigningService} to use.
* @param restMethod the {@link RestMethod} to verify.
* @param time the {@link Time} instance to use.
* @throws Exception
*/
private void doSignAndVerifyTest(AmbryUrlSigningService signer, RestMethod restMethod, Time time) throws Exception {
long urlTtl = Math.min(Utils.getRandomLong(TestUtils.RANDOM, 2000) + 2000, MAX_URL_TTL_SECS);
String randomHeaderVal = TestUtils.getRandomString(10);
long maxUploadSize = Utils.getRandomLong(TestUtils.RANDOM, 4001) + 2000;
// all defaults overridden
RestRequest request = getUrlSignRequest(restMethod, urlTtl, randomHeaderVal, maxUploadSize, false);
String url = signer.getSignedUrl(request);
verifySignedUrl(signer, url, restMethod, randomHeaderVal, maxUploadSize, Utils.Infinite_Time, false);
time.sleep(TimeUnit.SECONDS.toMillis(urlTtl + 1));
ensureVerificationFailure(signer, getRequestFromUrl(restMethod, url), RestServiceErrorCode.Unauthorized);
// chunk upload case, should use special stitched chunk upload settings.
request = getUrlSignRequest(restMethod, urlTtl, randomHeaderVal, maxUploadSize, true);
url = signer.getSignedUrl(request);
verifySignedUrl(signer, url, restMethod, randomHeaderVal, CHUNK_UPLOAD_MAX_CHUNK_SIZE, CHUNK_UPLOAD_INITIAL_CHUNK_TTL_SECS, true);
time.sleep(TimeUnit.SECONDS.toMillis(urlTtl + 1));
ensureVerificationFailure(signer, getRequestFromUrl(restMethod, url), RestServiceErrorCode.Unauthorized);
// no defaults overridden
request = getUrlSignRequest(restMethod, Utils.Infinite_Time, randomHeaderVal, null, false);
url = signer.getSignedUrl(request);
verifySignedUrl(signer, url, restMethod, randomHeaderVal, DEFAULT_MAX_UPLOAD_SIZE, Utils.Infinite_Time, false);
// change RestMethod and ensure verification failure
ensureVerificationFailure(signer, getRequestFromUrl(RestMethod.UNKNOWN, url), RestServiceErrorCode.Unauthorized);
time.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_URL_TTL_SECS + 1));
ensureVerificationFailure(signer, getRequestFromUrl(restMethod, url), RestServiceErrorCode.Unauthorized);
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method releaseResourcesTest.
/**
* Tests releasing of resources if response submission fails.
* @throws JSONException
* @throws UnsupportedEncodingException
* @throws URISyntaxException
*/
@Test
public void releaseResourcesTest() throws JSONException, UnsupportedEncodingException, URISyntaxException {
responseHandler.shutdown();
// handleResponse of FrontendTestResponseHandler throws exception because it has been shutdown.
try {
RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
assertTrue("RestRequest channel not open", restRequest.isOpen());
assertTrue("ReadableStreamChannel not open", channel.isOpen());
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, channel, null);
assertFalse("ReadableStreamChannel is still open", channel.isOpen());
// null ReadableStreamChannel
restRequest = createRestRequest(RestMethod.GET, "/", null, null);
restResponseChannel = new MockRestResponseChannel();
assertTrue("RestRequest channel not open", restRequest.isOpen());
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
// bad RestRequest (close() throws IOException)
channel = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
restResponseChannel = new MockRestResponseChannel();
assertTrue("ReadableStreamChannel not open", channel.isOpen());
frontendRestRequestService.submitResponse(new BadRestRequest(), restResponseChannel, channel, null);
// bad ReadableStreamChannel (close() throws IOException)
restRequest = createRestRequest(RestMethod.GET, "/", null, null);
restResponseChannel = new MockRestResponseChannel();
assertTrue("RestRequest channel not open", restRequest.isOpen());
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, new BadRSC(), null);
} finally {
frontendRestRequestService.setupResponseHandler(responseHandler);
responseHandler.start();
}
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method getStatsReportTest.
/**
* Tests the handling of {@link Operations#STATS_REPORT} get requests.
* @throws Exception
*/
@Test
public void getStatsReportTest() throws Exception {
AggregatedAccountStorageStats aggregatedAccountStorageStats = new AggregatedAccountStorageStats(StorageStatsUtilTest.generateRandomAggregatedAccountStorageStats((short) 1, 10, 10, 1000L, 2, 100));
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 aggregatedAccountStorageStats;
} else {
return null;
}
}).when(accountStatsStore).queryAggregatedAccountStorageStatsByClusterName(anyString());
doAnswer(invocation -> {
String clusterName = invocation.getArgument(0);
if (clusterName.equals(CLUSTER_NAME)) {
return aggregatedPartitionClassStorageStats;
} else {
return null;
}
}).when(accountStatsStore).queryAggregatedPartitionClassStorageStatsByClusterName(anyString());
ObjectMapper mapper = new ObjectMapper();
// construct a request to get account stats
JSONObject headers = new JSONObject();
headers.put(RestUtils.Headers.CLUSTER_NAME, CLUSTER_NAME);
headers.put(RestUtils.Headers.GET_STATS_REPORT_TYPE, StatsReportType.ACCOUNT_REPORT.name());
RestRequest request = createRestRequest(RestMethod.GET, Operations.STATS_REPORT, headers, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(request, restResponseChannel);
assertEquals("Storage stats mismatch", aggregatedAccountStorageStats.getStorageStats(), mapper.readValue(restResponseChannel.getResponseBody(), AggregatedAccountStorageStats.class).getStorageStats());
// construct a request to get partition class stats
headers = new JSONObject();
headers.put(RestUtils.Headers.CLUSTER_NAME, CLUSTER_NAME);
headers.put(RestUtils.Headers.GET_STATS_REPORT_TYPE, StatsReportType.PARTITION_CLASS_REPORT.name());
request = createRestRequest(RestMethod.GET, Operations.STATS_REPORT, headers, null);
restResponseChannel = new MockRestResponseChannel();
doOperation(request, restResponseChannel);
assertEquals("Storage stats mismatch", aggregatedPartitionClassStorageStats.getStorageStats(), mapper.readValue(restResponseChannel.getResponseBody(), AggregatedPartitionClassStorageStats.class).getStorageStats());
// test clustername not found case to ensure that it goes through the exception path
headers = new JSONObject();
headers.put(RestUtils.Headers.CLUSTER_NAME, "WRONG_CLUSTER");
headers.put(RestUtils.Headers.GET_STATS_REPORT_TYPE, StatsReportType.ACCOUNT_REPORT.name());
request = createRestRequest(RestMethod.GET, Operations.STATS_REPORT, headers, null);
try {
doOperation(request, new MockRestResponseChannel());
fail("Operation should have failed");
} catch (RestServiceException e) {
assertEquals("ErrorCode not as expected", RestServiceErrorCode.NotFound, e.getErrorCode());
}
}
Aggregations