Search in sources :

Example 16 with RestResponseChannel

use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.

the class AmbrySecurityServiceTest method testGetSubResource.

/**
 * Tests GET of sub-resources.
 * @param subResource the {@link RestUtils.SubResource}  to test.
 * @throws Exception
 */
private void testGetSubResource(BlobInfo blobInfo, RestUtils.SubResource subResource) throws Exception {
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    RestRequest restRequest = createRestRequest(RestMethod.GET, "/sampleId/" + subResource, null);
    Pair<Account, Container> accountAndContainer = getAccountAndContainer(blobInfo.getBlobProperties());
    insertAccountAndContainer(restRequest, accountAndContainer.getFirst(), accountAndContainer.getSecond());
    securityService.processResponse(restRequest, restResponseChannel, blobInfo).get();
    Assert.assertEquals("ProcessResponse status should have been set ", ResponseStatus.Ok, restResponseChannel.getStatus());
    Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
    Assert.assertEquals("Last Modified does not match creation time", RestUtils.toSecondsPrecisionInMs(blobInfo.getBlobProperties().getCreationTimeInMs()), RestUtils.getTimeFromDateString(restResponseChannel.getHeader(RestUtils.Headers.LAST_MODIFIED)).longValue());
    if (subResource.equals(RestUtils.SubResource.BlobInfo)) {
        verifyBlobPropertiesHeaders(blobInfo.getBlobProperties(), restResponseChannel);
        verifyAccountAndContainerHeaders(restResponseChannel, accountAndContainer.getFirst(), accountAndContainer.getSecond());
        Assert.assertEquals("LifeVersion mismatch", Short.toString(blobInfo.getLifeVersion()), restResponseChannel.getHeader(RestUtils.Headers.LIFE_VERSION));
    } else {
        verifyAbsenceOfHeaders(restResponseChannel, RestUtils.Headers.PRIVATE, RestUtils.Headers.TTL, RestUtils.Headers.SERVICE_ID, RestUtils.Headers.OWNER_ID, RestUtils.Headers.AMBRY_CONTENT_TYPE, RestUtils.Headers.CREATION_TIME, RestUtils.Headers.BLOB_SIZE, RestUtils.Headers.ACCEPT_RANGES, RestUtils.Headers.CONTENT_RANGE, RestUtils.Headers.LIFE_VERSION);
    }
    Map<String, String> userMetadata = blobInfo.getUserMetadata() != null ? RestUtils.buildUserMetadata(blobInfo.getUserMetadata()) : null;
    if (userMetadata == null && !(blobInfo.getUserMetadata().length == 0)) {
        Assert.assertTrue("Internal key " + RestUtils.InternalKeys.SEND_USER_METADATA_AS_RESPONSE_BODY + " should be set", (Boolean) restRequest.getArgs().get(RestUtils.InternalKeys.SEND_USER_METADATA_AS_RESPONSE_BODY));
    } else if (!(blobInfo.getUserMetadata().length == 0)) {
        USER_METADATA.forEach((key, value) -> Assert.assertEquals("Value of " + key + " not as expected", value, restResponseChannel.getHeader(key)));
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) FrontendConfig(com.github.ambry.config.FrontendConfig) BlobProperties(com.github.ambry.messageformat.BlobProperties) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) ResponseStatus(com.github.ambry.rest.ResponseStatus) ByteBuffer(java.nio.ByteBuffer) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) Future(java.util.concurrent.Future) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) HostThrottleConfig(com.github.ambry.config.HostThrottleConfig) TestUtils(com.github.ambry.utils.TestUtils) Locale(java.util.Locale) Map(java.util.Map) Container(com.github.ambry.account.Container) TimeZone(java.util.TimeZone) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Utils(com.github.ambry.utils.Utils) BlobInfo(com.github.ambry.messageformat.BlobInfo) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) QuotaTestUtils(com.github.ambry.quota.QuotaTestUtils) Account(com.github.ambry.account.Account) Callback(com.github.ambry.commons.Callback) RestUtils(com.github.ambry.rest.RestUtils) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InMemAccountService(com.github.ambry.account.InMemAccountService) ByteRanges(com.github.ambry.router.ByteRanges) SimpleDateFormat(java.text.SimpleDateFormat) AccountService(com.github.ambry.account.AccountService) HashMap(java.util.HashMap) RestTestUtils(com.github.ambry.rest.RestTestUtils) QuotaManager(com.github.ambry.quota.QuotaManager) QuotaConfig(com.github.ambry.config.QuotaConfig) RequestPath(com.github.ambry.rest.RequestPath) QuotaMode(com.github.ambry.quota.QuotaMode) HostLevelThrottler(com.github.ambry.commons.HostLevelThrottler) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) Pair(com.github.ambry.utils.Pair) RestMethod(com.github.ambry.rest.RestMethod) VerifiableProperties(com.github.ambry.config.VerifiableProperties) ByteRange(com.github.ambry.router.ByteRange) IOException(java.io.IOException) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) InMemAccountServiceFactory(com.github.ambry.account.InMemAccountServiceFactory) AmbryQuotaManager(com.github.ambry.quota.AmbryQuotaManager) Mockito(org.mockito.Mockito) QuotaMetrics(com.github.ambry.quota.QuotaMetrics) SimpleQuotaRecommendationMergePolicy(com.github.ambry.quota.SimpleQuotaRecommendationMergePolicy) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 17 with RestResponseChannel

use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method doNullInputsForFunctionsTest.

// nullInputsForFunctionsTest() helpers
/**
 * Checks for reaction to null input in {@code methodName} in {@link FrontendRestRequestService}.
 * @param methodName the name of the method to invoke.
 * @throws Exception
 */
private void doNullInputsForFunctionsTest(String methodName) throws Exception {
    Method method = FrontendRestRequestService.class.getDeclaredMethod(methodName, RestRequest.class, RestResponseChannel.class);
    RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
    RestResponseChannel restResponseChannel = new MockRestResponseChannel();
    responseHandler.reset();
    try {
        method.invoke(frontendRestRequestService, null, restResponseChannel);
        fail("Method [" + methodName + "] should have failed because RestRequest is null");
    } catch (InvocationTargetException e) {
        assertEquals("Unexpected exception class", IllegalArgumentException.class, e.getTargetException().getClass());
    }
    responseHandler.reset();
    try {
        method.invoke(frontendRestRequestService, restRequest, null);
        fail("Method [" + methodName + "] should have failed because RestResponseChannel is null");
    } catch (InvocationTargetException e) {
        assertEquals("Unexpected exception class", IllegalArgumentException.class, e.getTargetException().getClass());
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) RestMethod(com.github.ambry.rest.RestMethod) Method(java.lang.reflect.Method) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 18 with RestResponseChannel

use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.

the class GetAccountsHandlerTest method validRequestsTest.

/**
 * Test valid request cases.
 * @throws Exception
 */
@Test
public void validRequestsTest() throws Exception {
    Account account = accountService.createAndAddRandomAccount();
    ThrowingBiConsumer<RestRequest, Collection<Account>> testAction = (request, expectedAccounts) -> {
        RestResponseChannel restResponseChannel = new MockRestResponseChannel();
        ReadableStreamChannel channel = sendRequestGetResponse(request, restResponseChannel);
        assertNotNull("There should be a response", channel);
        Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
        assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
        assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
        RetainingAsyncWritableChannel asyncWritableChannel = new RetainingAsyncWritableChannel((int) channel.getSize());
        channel.readInto(asyncWritableChannel, null).get();
        assertEquals("Accounts do not match", new HashSet<>(expectedAccounts), new HashSet<>(AccountCollectionSerde.accountsFromInputStreamInJson(asyncWritableChannel.consumeContentAsInputStream())));
    };
    testAction.accept(createRestRequest(null, null, null, Operations.ACCOUNTS), accountService.getAllAccounts());
    testAction.accept(createRestRequest(account.getName(), null, null, Operations.ACCOUNTS), Collections.singleton(account));
    testAction.accept(createRestRequest(null, Short.toString(account.getId()), null, Operations.ACCOUNTS), Collections.singleton(account));
}
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) Account(com.github.ambry.account.Account) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Collection(java.util.Collection) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with RestResponseChannel

use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.

the class TailoredPeersClusterMap method handleGoodCaseTest.

/**
 * Tests for the good cases where the datanodes are correct. The peers obtained from the response is compared
 * against the ground truth.
 * @throws Exception
 */
@Test
public void handleGoodCaseTest() throws Exception {
    for (String datanode : TailoredPeersClusterMap.DATANODE_NAMES) {
        RestRequest restRequest = getRestRequest(datanode);
        RestResponseChannel restResponseChannel = new MockRestResponseChannel();
        ReadableStreamChannel channel = sendRequestGetResponse(restRequest, restResponseChannel);
        assertNotNull("There should be a response", channel);
        Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
        assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
        assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
        Set<String> expectedPeers = clusterMap.getPeers(datanode);
        Set<String> peersFromResponse = getPeersFromResponse(RestTestUtils.getJsonizedResponseBody(channel));
        assertEquals("Peer list returned does not match expected for " + datanode, expectedPeers, peersFromResponse);
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test)

Example 20 with RestResponseChannel

use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.

the class NamedBlobPutHandlerTest method doTtlRequiredEnforcementTest.

/**
 * Does the TTL required enforcement test by selecting the right verification methods based on container and frontend
 * config
 * @param container the {@link Container} to upload to
 * @param blobTtlSecs the TTL to set for the blob
 * @throws Exception
 */
private void doTtlRequiredEnforcementTest(Container container, long blobTtlSecs) throws Exception {
    JSONObject headers = new JSONObject();
    FrontendRestRequestServiceTest.setAmbryHeadersForPut(headers, blobTtlSecs, !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<>();
    namedBlobPutHandler.handle(request, restResponseChannel, future::done);
    if (container.isTtlRequired() && (blobTtlSecs == Utils.Infinite_Time || blobTtlSecs > frontendConfig.maxAcceptableTtlSecsIfTtlRequired)) {
        if (frontendConfig.failIfTtlRequiredButNotProvided) {
            try {
                future.get(TIMEOUT_SECS, TimeUnit.SECONDS);
                fail("Post should have failed");
            } catch (ExecutionException e) {
                RestServiceException rootCause = (RestServiceException) Utils.getRootCause(e);
                assertNotNull("Root cause should be a RestServiceException", rootCause);
                assertEquals("Incorrect RestServiceErrorCode", RestServiceErrorCode.InvalidArgs, rootCause.getErrorCode());
            }
        } else {
            verifySuccessResponseOnTtlEnforcement(future, content, blobTtlSecs, restResponseChannel, true);
        }
    } else {
        verifySuccessResponseOnTtlEnforcement(future, content, blobTtlSecs, restResponseChannel, false);
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) 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) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

RestResponseChannel (com.github.ambry.rest.RestResponseChannel)26 RestRequest (com.github.ambry.rest.RestRequest)23 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)21 JSONObject (org.json.JSONObject)15 RestServiceException (com.github.ambry.rest.RestServiceException)14 Test (org.junit.Test)13 MockRestRequest (com.github.ambry.rest.MockRestRequest)12 RestMethod (com.github.ambry.rest.RestMethod)12 RequestPath (com.github.ambry.rest.RequestPath)11 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)11 RestUtils (com.github.ambry.rest.RestUtils)11 ThrowingConsumer (com.github.ambry.utils.ThrowingConsumer)11 ExecutionException (java.util.concurrent.ExecutionException)11 FutureResult (com.github.ambry.router.FutureResult)10 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)10 ByteBuffer (java.nio.ByteBuffer)10 FrontendConfig (com.github.ambry.config.FrontendConfig)9 MetricRegistry (com.codahale.metrics.MetricRegistry)8 Account (com.github.ambry.account.Account)8 InMemAccountService (com.github.ambry.account.InMemAccountService)8