use of com.github.ambry.utils.ThrowingConsumer 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);
}
}
}
use of com.github.ambry.utils.ThrowingConsumer in project ambry by linkedin.
the class PostAccountsHandlerTest method securityServiceDenialTest.
/**
* Tests the case where the {@link SecurityService} denies the request.
* @throws Exception
*/
@Test
public void securityServiceDenialTest() throws Exception {
IllegalStateException injectedException = new IllegalStateException("@@expected");
TestUtils.ThrowingRunnable testAction = () -> sendRequestGetResponse(new String(AccountCollectionSerde.serializeAccountsInJson(Collections.emptyList())), new MockRestResponseChannel());
ThrowingConsumer<IllegalStateException> errorChecker = e -> assertEquals("Wrong exception", injectedException, e);
securityServiceFactory.exceptionToReturn = injectedException;
securityServiceFactory.mode = FrontendTestSecurityServiceFactory.Mode.ProcessRequest;
TestUtils.assertException(IllegalStateException.class, testAction, errorChecker);
securityServiceFactory.mode = FrontendTestSecurityServiceFactory.Mode.PostProcessRequest;
TestUtils.assertException(IllegalStateException.class, testAction, errorChecker);
securityServiceFactory.exceptionToThrow = injectedException;
securityServiceFactory.exceptionToReturn = null;
securityServiceFactory.mode = FrontendTestSecurityServiceFactory.Mode.ProcessRequest;
TestUtils.assertException(IllegalStateException.class, testAction, errorChecker);
securityServiceFactory.mode = FrontendTestSecurityServiceFactory.Mode.PostProcessRequest;
TestUtils.assertException(IllegalStateException.class, testAction, errorChecker);
}
use of com.github.ambry.utils.ThrowingConsumer in project ambry by linkedin.
the class GetAccountsHandlerTest method securityServiceDenialTest.
/**
* Tests the case where the {@link SecurityService} denies the request.
* @throws Exception
*/
@Test
public void securityServiceDenialTest() throws Exception {
IllegalStateException injectedException = new IllegalStateException("@@expected");
TestUtils.ThrowingRunnable testAction = () -> sendRequestGetResponse(createRestRequest(null, null, null, Operations.ACCOUNTS), new MockRestResponseChannel());
ThrowingConsumer<IllegalStateException> errorChecker = e -> assertEquals("Wrong exception", injectedException, e);
securityServiceFactory.exceptionToReturn = injectedException;
securityServiceFactory.mode = FrontendTestSecurityServiceFactory.Mode.ProcessRequest;
TestUtils.assertException(IllegalStateException.class, testAction, errorChecker);
securityServiceFactory.mode = FrontendTestSecurityServiceFactory.Mode.PostProcessRequest;
TestUtils.assertException(IllegalStateException.class, testAction, errorChecker);
securityServiceFactory.exceptionToThrow = injectedException;
securityServiceFactory.exceptionToReturn = null;
securityServiceFactory.mode = FrontendTestSecurityServiceFactory.Mode.ProcessRequest;
TestUtils.assertException(IllegalStateException.class, testAction, errorChecker);
securityServiceFactory.mode = FrontendTestSecurityServiceFactory.Mode.PostProcessRequest;
TestUtils.assertException(IllegalStateException.class, testAction, errorChecker);
}
use of com.github.ambry.utils.ThrowingConsumer in project ambry by linkedin.
the class AmbryServerSecurityServiceTest method validateConnectionTest.
/**
* Tests for {@link AmbryServerSecurityService#validateConnection(SSLSession, Callback)}
* @throws Exception
*/
@Test
public void validateConnectionTest() throws Exception {
// sslSession is null
TestUtils.assertException(IllegalArgumentException.class, () -> serverSecurityService.validateConnection(null).get(), null);
// success case
SSLSession sslSession = Mockito.mock(SSLSession.class);
serverSecurityService.validateConnection(sslSession, (r, e) -> {
Assert.assertNull("result not null", r);
Assert.assertNull("exception not null", e);
});
// service is closed
serverSecurityService.close();
ThrowingConsumer<ExecutionException> errorAction = e -> {
Assert.assertTrue("Exception should have been an instance of RestServiceException", e.getCause() instanceof RestServiceException);
RestServiceException re = (RestServiceException) e.getCause();
Assert.assertEquals("Unexpected RestServerErrorCode (Future)", RestServiceErrorCode.ServiceUnavailable, re.getErrorCode());
};
TestUtils.assertException(ExecutionException.class, () -> serverSecurityService.validateConnection(sslSession).get(), errorAction);
}
use of com.github.ambry.utils.ThrowingConsumer in project ambry by linkedin.
the class PostAccountContainersHandlerTest method validRequestsTest.
/**
* Test valid request cases.
* @throws Exception
*/
@Test
public void validRequestsTest() throws Exception {
String accountName = theAccount.getName();
short accountId = theAccount.getId();
ThrowingConsumer<Collection<Container>> testAction = inputContainers -> {
String requestBody = new String(AccountCollectionSerde.serializeContainersInJson(inputContainers));
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
RestRequest request = createRestRequest(requestBody, accountName, null);
ReadableStreamChannel responseChannel = sendRequestGetResponse(request, restResponseChannel);
assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
assertEquals("Content-length is not as expected", responseChannel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
assertEquals("Account id in response header is not as expected", accountId, Short.parseShort((String) restResponseChannel.getHeader(RestUtils.Headers.TARGET_ACCOUNT_ID)));
RetainingAsyncWritableChannel asyncWritableChannel = new RetainingAsyncWritableChannel((int) responseChannel.getSize());
responseChannel.readInto(asyncWritableChannel, null).get();
Collection<Container> outputContainers = AccountCollectionSerde.containersFromInputStreamInJson(asyncWritableChannel.consumeContentAsInputStream(), accountId);
assertEquals("Unexpected count returned", inputContainers.size(), outputContainers.size());
for (Container container : outputContainers) {
assertEquals("Container in account service not as expected", container, accountService.getContainerByName(accountName, container.getName()));
}
};
// add new container
testAction.accept(Collections.singleton(accountService.getRandomContainer(accountId)));
// add multiple containers
List<Container> containerList = new ArrayList<>();
for (int j = 0; j < 10; j++) {
containerList.add(new ContainerBuilder(Container.UNKNOWN_CONTAINER_ID, "Test-" + j, Container.ContainerStatus.ACTIVE, "", accountId).build());
}
testAction.accept(containerList);
// TODO: update existing containers when support is added
}
Aggregations