Search in sources :

Example 16 with RestServiceException

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

the class FrontendTestUrlSigningServiceFactory method badRangeHeaderTest.

/**
 * Test that GET operations fail with the expected error code when a bad range header is provided.
 * @throws Exception
 */
@Test
public void badRangeHeaderTest() throws Exception {
    JSONObject headers = new JSONObject();
    headers.put(RestUtils.Headers.RANGE, "adsfksakdfsdfkdaklf");
    try {
        doOperation(createRestRequest(RestMethod.GET, "/", headers, null), new MockRestResponseChannel());
        fail("GET operation should have failed because of an invalid range header");
    } catch (RestServiceException e) {
        assertEquals("Unexpected error code", RestServiceErrorCode.InvalidArgs, e.getErrorCode());
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 17 with RestServiceException

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

the class FrontendTestUrlSigningServiceFactory method putRequestWithProhibitedHeader.

/**
 * Put with prohibited headers.
 * @param header The header that is prohibited.
 * @throws Exception
 */
private void putRequestWithProhibitedHeader(String header) throws Exception {
    JSONObject headers = new JSONObject();
    setAmbryHeadersForPut(headers, 7200, true, "someServiceId", "application/octet-stream", "someOwnerId", "someAccountName", "someContainerName");
    headers.put(header, "adsfksakdfsdfkdaklf");
    try {
        doOperation(createRestRequest(RestMethod.POST, "/", headers, null), new MockRestResponseChannel());
        fail("Should have thrown");
    } catch (RestServiceException e) {
        assertEquals("Unexpected error code", RestServiceErrorCode.BadRequest, e.getErrorCode());
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 18 with RestServiceException

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

the class FrontendTestUrlSigningServiceFactory method useServiceWithoutStartTest.

/**
 * This tests for exceptions thrown when an {@link AmbryBlobStorageService} instance is used without calling
 * {@link AmbryBlobStorageService#start()} first.
 * @throws Exception
 */
@Test
public void useServiceWithoutStartTest() throws Exception {
    ambryBlobStorageService = getAmbryBlobStorageService();
    // not fine to use without start.
    try {
        doOperation(createRestRequest(RestMethod.GET, "/", null, null), new MockRestResponseChannel());
        fail("Should not have been able to use AmbryBlobStorageService without start");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.ServiceUnavailable, e.getErrorCode());
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 19 with RestServiceException

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

the class FrontendUtilsTest method testGetBlobIdFromString.

/**
 * Tests {@link FrontendUtils#getBlobIdFromString(String, ClusterMap)}
 * @throws IOException
 * @throws RestServiceException
 */
@Test
public void testGetBlobIdFromString() throws IOException, RestServiceException {
    // good path
    byte[] bytes = new byte[2];
    ClusterMap referenceClusterMap = new MockClusterMap();
    TestUtils.RANDOM.nextBytes(bytes);
    BlobId.BlobIdType referenceType = TestUtils.RANDOM.nextBoolean() ? BlobId.BlobIdType.NATIVE : BlobId.BlobIdType.CRAFTED;
    TestUtils.RANDOM.nextBytes(bytes);
    byte referenceDatacenterId = bytes[0];
    short referenceAccountId = getRandomShort(TestUtils.RANDOM);
    short referenceContainerId = getRandomShort(TestUtils.RANDOM);
    PartitionId referencePartitionId = referenceClusterMap.getWritablePartitionIds().get(0);
    boolean referenceIsEncrypted = TestUtils.RANDOM.nextBoolean();
    BlobId blobId = new BlobId(BlobId.BLOB_ID_V3, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted);
    BlobId regeneratedBlobId = FrontendUtils.getBlobIdFromString(blobId.getID(), referenceClusterMap);
    assertEquals("BlobId mismatch", blobId, regeneratedBlobId);
    assertBlobIdFieldValues(regeneratedBlobId, referenceType, referenceDatacenterId, referenceAccountId, referenceContainerId, referencePartitionId, referenceIsEncrypted);
    // bad path
    try {
        FrontendUtils.getBlobIdFromString(blobId.getID().substring(1), referenceClusterMap);
        fail("Should have thrown exception for bad blobId ");
    } catch (RestServiceException e) {
        assertEquals("RestServiceErrorCode mismatch", RestServiceErrorCode.BadRequest, e.getErrorCode());
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) ClusterMap(com.github.ambry.clustermap.ClusterMap) MockClusterMap(com.github.ambry.clustermap.MockClusterMap) PartitionId(com.github.ambry.clustermap.PartitionId) BlobId(com.github.ambry.commons.BlobId) MockClusterMap(com.github.ambry.clustermap.MockClusterMap) Test(org.junit.Test)

Example 20 with RestServiceException

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

the class FrontendTestUrlSigningServiceFactory method verifyAccountAndContainerFromBlobId.

/**
 * Verifies presence of {@link Account} and {@link Container} injected into {@link RestRequest} using a
 * blobId string, for get/head/delete operations.
 * @param blobId The blobId string to get/head/delete.
 * @param expectedAccount The expected {@link Account} to verify its presence in {@link RestRequest}.
 * @param expectedContainer The expected {@link Container} to verify its presence in {@link RestRequest}.
 * @param expectedRestErrorCode The expected {@link RestServiceErrorCode} to verify.
 * @throws Exception
 */
private void verifyAccountAndContainerFromBlobId(String blobId, Account expectedAccount, Container expectedContainer, RestServiceErrorCode expectedRestErrorCode) throws Exception {
    if (blobId.startsWith("/")) {
        blobId = blobId.substring(1);
    }
    for (RestMethod restMethod : Lists.newArrayList(RestMethod.GET, RestMethod.HEAD, RestMethod.DELETE)) {
        RestRequest restRequest = createRestRequest(restMethod, "/" + blobId, null, null);
        MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
        try {
            doOperation(restRequest, restResponseChannel);
            if (expectedRestErrorCode != null) {
                fail("Should have thrown");
            }
        } catch (RestServiceException e) {
            assertEquals("Wrong RestServiceErrorCode", expectedRestErrorCode, e.getErrorCode());
        }
        BlobId deserializedId = new BlobId(blobId, clusterMap);
        // Because BlobInfo is not fetched on deletes, V1 Blob IDs will never be reassigned to a known account/container.
        boolean alwaysExpectUnknown = restMethod == RestMethod.DELETE && deserializedId.getAccountId() == Account.UNKNOWN_ACCOUNT_ID && deserializedId.getContainerId() == Container.UNKNOWN_CONTAINER_ID;
        assertEquals("Wrong account object in RestRequest's args", alwaysExpectUnknown ? InMemAccountService.UNKNOWN_ACCOUNT : expectedAccount, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY));
        assertEquals("Wrong container object in RestRequest's args", alwaysExpectUnknown ? Container.UNKNOWN_CONTAINER : expectedContainer, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY));
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) BlobId(com.github.ambry.commons.BlobId) RestMethod(com.github.ambry.rest.RestMethod)

Aggregations

RestServiceException (com.github.ambry.rest.RestServiceException)35 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)15 Test (org.junit.Test)12 MockRestRequest (com.github.ambry.rest.MockRestRequest)11 RestRequest (com.github.ambry.rest.RestRequest)10 UtilsTest (com.github.ambry.utils.UtilsTest)8 JSONObject (org.json.JSONObject)8 RestMethod (com.github.ambry.rest.RestMethod)7 RestUtilsTest (com.github.ambry.rest.RestUtilsTest)6 Account (com.github.ambry.account.Account)5 Container (com.github.ambry.account.Container)5 IOException (java.io.IOException)4 ByteBuffer (java.nio.ByteBuffer)4 ExecutionException (java.util.concurrent.ExecutionException)4 BlobId (com.github.ambry.commons.BlobId)3 RestUtils (com.github.ambry.rest.RestUtils)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 PartitionId (com.github.ambry.clustermap.PartitionId)2 VerifiableProperties (com.github.ambry.config.VerifiableProperties)2 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)2