Search in sources :

Example 1 with ThrowingConsumer

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);
        }
    }
}
Also used : Arrays(java.util.Arrays) FrontendConfig(com.github.ambry.config.FrontendConfig) BlobProperties(com.github.ambry.messageformat.BlobProperties) URISyntaxException(java.net.URISyntaxException) ResponseStatus(com.github.ambry.rest.ResponseStatus) FutureResult(com.github.ambry.router.FutureResult) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ContainerBuilder(com.github.ambry.account.ContainerBuilder) ByteBuffer(java.nio.ByteBuffer) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) Map(java.util.Map) ChunkInfo(com.github.ambry.router.ChunkInfo) Container(com.github.ambry.account.Container) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Utils(com.github.ambry.utils.Utils) UUID(java.util.UUID) AccountBuilder(com.github.ambry.account.AccountBuilder) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) RestServiceException(com.github.ambry.rest.RestServiceException) Stream(java.util.stream.Stream) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) MockTime(com.github.ambry.utils.MockTime) QuotaTestUtils(com.github.ambry.quota.QuotaTestUtils) Account(com.github.ambry.account.Account) RestUtils(com.github.ambry.rest.RestUtils) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InMemAccountService(com.github.ambry.account.InMemAccountService) InMemoryRouter(com.github.ambry.router.InMemoryRouter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RequestPath(com.github.ambry.rest.RequestPath) LinkedList(java.util.LinkedList) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) RestMethod(com.github.ambry.rest.RestMethod) VerifiableProperties(com.github.ambry.config.VerifiableProperties) ClusterMap(com.github.ambry.clustermap.ClusterMap) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PutBlobOptionsBuilder(com.github.ambry.router.PutBlobOptionsBuilder) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) MockClusterMap(com.github.ambry.clustermap.MockClusterMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) InMemoryRouter(com.github.ambry.router.InMemoryRouter) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) FutureResult(com.github.ambry.router.FutureResult) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 2 with ThrowingConsumer

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);
}
Also used : IntStream(java.util.stream.IntStream) MockRestRequest(com.github.ambry.rest.MockRestRequest) FrontendConfig(com.github.ambry.config.FrontendConfig) FutureResult(com.github.ambry.router.FutureResult) ByteBuffer(java.nio.ByteBuffer) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) AccountCollectionSerde(com.github.ambry.account.AccountCollectionSerde) RequestPath(com.github.ambry.rest.RequestPath) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) LinkedList(java.util.LinkedList) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) RestMethod(com.github.ambry.rest.RestMethod) VerifiableProperties(com.github.ambry.config.VerifiableProperties) Collection(java.util.Collection) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test) AccountBuilder(com.github.ambry.account.AccountBuilder) ThrowingBiConsumer(com.github.ambry.utils.ThrowingBiConsumer) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Account(com.github.ambry.account.Account) RestUtils(com.github.ambry.rest.RestUtils) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) TestUtils(com.github.ambry.utils.TestUtils) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test)

Example 3 with ThrowingConsumer

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);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) FutureResult(com.github.ambry.router.FutureResult) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) AccountCollectionSerde(com.github.ambry.account.AccountCollectionSerde) RequestPath(com.github.ambry.rest.RequestPath) HashSet(java.util.HashSet) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) Container(com.github.ambry.account.Container) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) RestMethod(com.github.ambry.rest.RestMethod) Collection(java.util.Collection) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test) ThrowingBiConsumer(com.github.ambry.utils.ThrowingBiConsumer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Account(com.github.ambry.account.Account) RestUtils(com.github.ambry.rest.RestUtils) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) TestUtils(com.github.ambry.utils.TestUtils) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test)

Example 4 with ThrowingConsumer

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);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) ServerSecurityService(com.github.ambry.rest.ServerSecurityService) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) ServerConfig(com.github.ambry.config.ServerConfig) Test(org.junit.Test) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) ExecutionException(java.util.concurrent.ExecutionException) AmbryRequests(com.github.ambry.protocol.AmbryRequests) Mockito(org.mockito.Mockito) RestServiceException(com.github.ambry.rest.RestServiceException) SSLSession(javax.net.ssl.SSLSession) TestUtils(com.github.ambry.utils.TestUtils) ServerMetrics(com.github.ambry.commons.ServerMetrics) Callback(com.github.ambry.commons.Callback) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) RestServiceException(com.github.ambry.rest.RestServiceException) SSLSession(javax.net.ssl.SSLSession) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 5 with ThrowingConsumer

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
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) FrontendConfig(com.github.ambry.config.FrontendConfig) FutureResult(com.github.ambry.router.FutureResult) ContainerBuilder(com.github.ambry.account.ContainerBuilder) ByteBuffer(java.nio.ByteBuffer) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) ArrayList(java.util.ArrayList) AccountCollectionSerde(com.github.ambry.account.AccountCollectionSerde) RequestPath(com.github.ambry.rest.RequestPath) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) LinkedList(java.util.LinkedList) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) Container(com.github.ambry.account.Container) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) RestMethod(com.github.ambry.rest.RestMethod) VerifiableProperties(com.github.ambry.config.VerifiableProperties) Collection(java.util.Collection) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test) ThrowingBiConsumer(com.github.ambry.utils.ThrowingBiConsumer) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Account(com.github.ambry.account.Account) RestUtils(com.github.ambry.rest.RestUtils) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) ArrayList(java.util.ArrayList) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Container(com.github.ambry.account.Container) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ContainerBuilder(com.github.ambry.account.ContainerBuilder) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Collection(java.util.Collection) Test(org.junit.Test)

Aggregations

RestRequest (com.github.ambry.rest.RestRequest)11 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)11 RestServiceException (com.github.ambry.rest.RestServiceException)11 ThrowingConsumer (com.github.ambry.utils.ThrowingConsumer)11 MetricRegistry (com.codahale.metrics.MetricRegistry)9 RequestPath (com.github.ambry.rest.RequestPath)9 RestMethod (com.github.ambry.rest.RestMethod)9 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)9 RestUtils (com.github.ambry.rest.RestUtils)9 TestUtils (com.github.ambry.utils.TestUtils)9 ExecutionException (java.util.concurrent.ExecutionException)9 Assert (org.junit.Assert)9 Test (org.junit.Test)9 FrontendConfig (com.github.ambry.config.FrontendConfig)8 VerifiableProperties (com.github.ambry.config.VerifiableProperties)8 ByteBuffer (java.nio.ByteBuffer)8 Properties (java.util.Properties)8 Account (com.github.ambry.account.Account)7 InMemAccountService (com.github.ambry.account.InMemAccountService)7 MockRestRequest (com.github.ambry.rest.MockRestRequest)7