use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method putFailureTest.
/**
* Checks reactions of PUT methods in {@link FrontendRestRequestService} when there are bad request parameters
* @throws Exception
*/
@Test
public void putFailureTest() throws Exception {
// unrecognized operation
RestRequest restRequest = createRestRequest(RestMethod.PUT, "/non-existent-op", null, null);
verifyOperationFailure(restRequest, RestServiceErrorCode.BadRequest);
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method undeleteBlobAndVerify.
/**
* Undeletes the blob with blob ID {@code blobId} and verifies the response returned. Also checks responses from
* GET, HEAD.
* @param blobId the blob ID of the blob to undelete.
* @param expectedHeaders the expected headers in the GET response triggered for verification (if right options are
* provided).
* @param expectedContent the expected account in the GET response triggered for verification (if right options are
* provided). Also used to attach preconditions if required.
* @param expectedAccount the expected container in the GET response triggered for verification (if right options are
* provided). Also used to attach preconditions if required.
* @param expectedContainer the {@link Container} details that are eventually expected to be populated.
* @param attachPreconditions if {@code true}, attaches preconditions to the request
* @param expectedErrorCode the expected {@link RestServiceErrorCode}. Null when the request should have a 200 response.
* @throws Exception
*/
private void undeleteBlobAndVerify(String blobId, JSONObject expectedHeaders, ByteBuffer expectedContent, Account expectedAccount, Container expectedContainer, boolean attachPreconditions, RestServiceErrorCode expectedErrorCode) throws Exception {
JSONObject headers = new JSONObject();
if (attachPreconditions) {
setAccountAndContainerHeaders(headers, expectedAccount.getName(), expectedContainer.getName());
}
headers.put(RestUtils.Headers.BLOB_ID, blobId);
headers.put(RestUtils.Headers.SERVICE_ID, "undeleteBlobAndVerify");
RestRequest restRequest = createRestRequest(RestMethod.PUT, "/" + Operations.UNDELETE, headers, null);
if (expectedErrorCode != null) {
verifyOperationFailure(restRequest, expectedErrorCode);
} else {
verifyUndeleteOK(restRequest);
verifyOperationsAfterUndelete(blobId, expectedHeaders, expectedContent, expectedAccount, expectedContainer);
}
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method optionsTest.
/**
* Tests for handling of {@link RestMethod#OPTIONS}.
* @throws Exception
*/
@Test
public void optionsTest() throws Exception {
RestRequest restRequest = createRestRequest(RestMethod.OPTIONS, "/", null, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
assertEquals("Unexpected response status", ResponseStatus.Ok, restResponseChannel.getStatus());
assertTrue("No Date header", restResponseChannel.getHeader(RestUtils.Headers.DATE) != null);
assertEquals("Unexpected content length", 0, Long.parseLong(restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
assertEquals("Unexpected value for " + RestUtils.Headers.ACCESS_CONTROL_ALLOW_METHODS, frontendConfig.optionsAllowMethods, restResponseChannel.getHeader(RestUtils.Headers.ACCESS_CONTROL_ALLOW_METHODS));
assertEquals("Unexpected value for " + RestUtils.Headers.ACCESS_CONTROL_MAX_AGE, frontendConfig.optionsValiditySeconds, Long.parseLong(restResponseChannel.getHeader(RestUtils.Headers.ACCESS_CONTROL_MAX_AGE)));
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method deleteBlobAndVerify.
/**
* Deletes the blob with blob ID {@code blobId} and verifies the response returned. Also checks responses from
* GET, HEAD, TTL Update and DELETE
* @param blobId the blob ID of the blob to DELETE.
* @param expectedHeaders the expected headers in the GET response triggered for verification (if right options are
* provided).
* @param expectedContent the expected account in the GET response triggered for verification (if right options are
* provided). Also used to attach preconditions if required.
* @param expectedAccount the expected container in the GET response triggered for verification (if right options are
* provided). Also used to attach preconditions if required.
* @param expectedContainer the {@link Container} details that are eventually expected to be populated.
* @param attachPreconditions if {@code true}, attaches preconditions to the request
* @throws Exception
*/
private void deleteBlobAndVerify(String blobId, JSONObject expectedHeaders, ByteBuffer expectedContent, Account expectedAccount, Container expectedContainer, boolean attachPreconditions) throws Exception {
JSONObject headers = new JSONObject();
if (attachPreconditions) {
setAccountAndContainerHeaders(headers, expectedAccount.getName(), expectedContainer.getName());
}
RestRequest restRequest = createRestRequest(RestMethod.DELETE, blobId, headers, null);
verifyDeleteAccepted(restRequest);
verifyOperationsAfterDelete(blobId, expectedHeaders, expectedContent, expectedAccount, expectedContainer);
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class NamedBlobPutHandlerTest method stitchBlobAndVerify.
/**
* Make a stitch blob call using {@link PostBlobHandler} and verify the result of the operation.
* @param requestBody the body of the stitch request to supply.
* @param expectedStitchedChunks the expected chunks stitched together.
* @param errorChecker if non-null, expect an exception to be thrown by the post flow and verify it using this
* {@link ThrowingConsumer}.
* @throws Exception
*/
private void stitchBlobAndVerify(byte[] requestBody, List<ChunkInfo> expectedStitchedChunks, ThrowingConsumer<ExecutionException> errorChecker) throws Exception {
// call
for (long ttl : new long[] { TestUtils.TTL_SECS, Utils.Infinite_Time }) {
JSONObject headers = new JSONObject();
FrontendRestRequestServiceTest.setAmbryHeadersForPut(headers, ttl, !REF_CONTAINER.isCacheable(), SERVICE_ID, CONTENT_TYPE, OWNER_ID, null, null, "STITCH");
RestRequest request = getRestRequest(headers, request_path, requestBody);
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("List of chunks stitched does not match expected", expectedStitchedChunks, blob.getStitchedChunks());
ByteArrayOutputStream expectedContent = new ByteArrayOutputStream();
expectedStitchedChunks.stream().map(chunkInfo -> router.getActiveBlobs().get(chunkInfo.getBlobId()).getBlob().array()).forEach(buf -> expectedContent.write(buf, 0, buf.length));
assertEquals("Unexpected blob content stored", ByteBuffer.wrap(expectedContent.toByteArray()), blob.getBlob());
// check actual size of stitched blob
assertEquals("Unexpected blob size", Long.toString(getStitchedBlobSize(expectedStitchedChunks)), restResponseChannel.getHeader(RestUtils.Headers.BLOB_SIZE));
assertEquals("Unexpected TTL in named blob DB", ttl, idConverterFactory.lastBlobInfo.getBlobProperties().getTimeToLiveInSeconds());
assertEquals("Unexpected TTL in blob", ttl, blob.getBlobProperties().getTimeToLiveInSeconds());
} else {
TestUtils.assertException(ExecutionException.class, () -> future.get(TIMEOUT_SECS, TimeUnit.SECONDS), errorChecker);
}
}
}
Aggregations