use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class UndeleteHandlerTest method verifyUndelete.
/**
* Verifies that the blob is undeleted
* @param restRequest the {@link RestRequest} to get a signed URL.
* @param expectedAccount the {@link Account} that should be populated in {@link RestRequest}.
* @param expectedContainer the {@link Container} that should be populated in {@link RestRequest}.
* @throws Exception
*/
private void verifyUndelete(RestRequest restRequest, Account expectedAccount, Container expectedContainer) throws Exception {
try {
router.getBlob(blobId, new GetBlobOptionsBuilder().build()).get(1, TimeUnit.SECONDS);
fail("Get blob should fail on a deleted blob");
} catch (ExecutionException e) {
RouterException routerException = (RouterException) e.getCause();
assertEquals(RouterErrorCode.BlobDeleted, routerException.getErrorCode());
}
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
sendRequestGetResponse(restRequest, restResponseChannel);
assertEquals("ResponseStatus not as expected", ResponseStatus.Ok, restResponseChannel.getStatus());
assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
assertEquals("Content-length is not as expected", 0, Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
assertEquals("Account not as expected", expectedAccount, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY));
assertEquals("Container not as expected", expectedContainer, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY));
router.getBlob(blobId, new GetBlobOptionsBuilder().build()).get(1, TimeUnit.SECONDS);
}
use of com.github.ambry.rest.MockRestResponseChannel 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.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method checkRouterExceptionPipeline.
/**
* Checks that the exception received by submitting {@code restRequest} to {@link FrontendRestRequestService} matches
* what was expected.
* @param expectedExceptionMsg the expected exception message.
* @param restRequest the {@link RestRequest} to submit to {@link FrontendRestRequestService}.
* @throws Exception
*/
private void checkRouterExceptionPipeline(String expectedExceptionMsg, RestRequest restRequest) throws Exception {
try {
doOperation(restRequest, new MockRestResponseChannel());
fail("Operation " + restRequest.getRestMethod() + " should have failed because an external service would have thrown an exception");
} catch (RestServiceException | RuntimeException e) {
// catching RestServiceException because RouterException should have been converted.
// RuntimeException might get bubbled up as is.
assertEquals("Unexpected exception message", expectedExceptionMsg, Utils.getRootCause(e).getMessage());
// Nothing should be closed.
assertTrue("RestRequest channel is not open", restRequest.isOpen());
restRequest.close();
}
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method verifyDeleteAccepted.
/**
* Verifies that a request returns the right response code once the blob has been deleted.
* @param restRequest the {@link RestRequest} to send to {@link FrontendRestRequestService}.
* @throws Exception
*/
private void verifyDeleteAccepted(RestRequest restRequest) throws Exception {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
assertEquals("Unexpected response status", ResponseStatus.Accepted, restResponseChannel.getStatus());
assertTrue("No Date header", restResponseChannel.getHeader(RestUtils.Headers.DATE) != null);
assertEquals("Content-Length is not 0", "0", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH));
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method deleteServiceIdTest.
/**
* Test that the correct service ID is sent to the router on deletes.
* @throws Exception
*/
@Test
public void deleteServiceIdTest() throws Exception {
FrontendTestRouter testRouter = new FrontendTestRouter();
frontendRestRequestService = new FrontendRestRequestService(frontendConfig, frontendMetrics, testRouter, clusterMap, idConverterFactory, securityServiceFactory, urlSigningService, idSigningService, null, accountService, accountAndContainerInjector, datacenterName, hostname, clusterName, accountStatsStore, QUOTA_MANAGER);
frontendRestRequestService.setupResponseHandler(responseHandler);
frontendRestRequestService.start();
JSONObject headers = new JSONObject();
String serviceId = "service-id";
headers.put(RestUtils.Headers.SERVICE_ID, serviceId);
doOperation(createRestRequest(RestMethod.DELETE, referenceBlobIdStr, headers, null), new MockRestResponseChannel());
assertEquals(serviceId, testRouter.deleteServiceId);
doOperation(createRestRequest(RestMethod.DELETE, referenceBlobIdStr, null, null), new MockRestResponseChannel());
assertNull("Service ID should not have been set for this delete", testRouter.deleteServiceId);
}
Aggregations