Search in sources :

Example 21 with RestServiceException

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

the class FrontendTestUrlSigningServiceFactory method getPeersTest.

/**
 * Tests the handling of {@link Operations#GET_PEERS} requests.
 * @throws Exception
 */
@Test
public void getPeersTest() throws Exception {
    ambryBlobStorageService.shutdown();
    TailoredPeersClusterMap clusterMap = new TailoredPeersClusterMap();
    ambryBlobStorageService = new AmbryBlobStorageService(frontendConfig, frontendMetrics, responseHandler, router, clusterMap, idConverterFactory, securityServiceFactory, accountService, urlSigningService, accountAndContainerInjector);
    ambryBlobStorageService.start();
    // test good requests
    for (String datanode : TailoredPeersClusterMap.DATANODE_NAMES) {
        String[] parts = datanode.split(":");
        String baseUri = Operations.GET_PEERS + "?" + GetPeersHandler.NAME_QUERY_PARAM + "=" + parts[0] + "&" + GetPeersHandler.PORT_QUERY_PARAM + "=" + parts[1];
        String[] uris = { baseUri, "/" + baseUri };
        for (String uri : uris) {
            MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
            doOperation(createRestRequest(RestMethod.GET, uri, null, null), restResponseChannel);
            byte[] peerStrBytes = restResponseChannel.getResponseBody();
            Set<String> peersFromResponse = GetPeersHandlerTest.getPeersFromResponse(new JSONObject(new String(peerStrBytes)));
            Set<String> expectedPeers = clusterMap.getPeers(datanode);
            assertEquals("Peer list returned does not match expected for " + datanode, expectedPeers, peersFromResponse);
        }
    }
    // test one bad request
    RestRequest restRequest = createRestRequest(RestMethod.GET, Operations.GET_PEERS, null, null);
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    try {
        doOperation(restRequest, restResponseChannel);
        fail("Request should have failed");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.MissingArgs, e.getErrorCode());
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) 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 22 with RestServiceException

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

the class FrontendTestUrlSigningServiceFactory method getReplicasWithBadInputTest.

/**
 * Tests reactions of the {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)} operation to bad input -
 * specifically if we do not include required parameters.
 * @throws Exception
 */
@Test
public void getReplicasWithBadInputTest() throws Exception {
    // bad input - invalid blob id.
    RestRequest restRequest = createRestRequest(RestMethod.GET, "12345/" + RestUtils.SubResource.Replicas, null, null);
    try {
        doOperation(restRequest, new MockRestResponseChannel());
        fail("Exception should have been thrown because the blobid is invalid");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest, e.getErrorCode());
    }
    // bad input - invalid blob id for this cluster map.
    String blobId = "AAEAAQAAAAAAAADFAAAAJDMyYWZiOTJmLTBkNDYtNDQyNS1iYzU0LWEwMWQ1Yzg3OTJkZQ.gif";
    restRequest = createRestRequest(RestMethod.GET, blobId + "/" + RestUtils.SubResource.Replicas, null, null);
    try {
        doOperation(restRequest, new MockRestResponseChannel());
        fail("Exception should have been thrown because the blobid is invalid");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest, e.getErrorCode());
    }
}
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) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 23 with RestServiceException

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

the class AmbryIdConverterFactoryTest method ambryIdConverterTest.

/**
 * Tests the instantiation and use of the {@link IdConverter} instance returned through the
 * {@link AmbryIdConverterFactory}.
 * @throws Exception
 */
@Test
public void ambryIdConverterTest() throws Exception {
    // dud properties. server should pick up defaults
    Properties properties = new Properties();
    VerifiableProperties verifiableProperties = new VerifiableProperties(properties);
    AmbryIdConverterFactory ambryIdConverterFactory = new AmbryIdConverterFactory(verifiableProperties, new MetricRegistry());
    IdConverter idConverter = ambryIdConverterFactory.getIdConverter();
    assertNotNull("No IdConverter returned", idConverter);
    String input = UtilsTest.getRandomString(10);
    String inputWithLeadingSlash = "/" + input;
    // GET
    // without leading slash
    testConversion(idConverter, RestMethod.GET, input, input);
    // with leading slash
    testConversion(idConverter, RestMethod.GET, inputWithLeadingSlash, input);
    // POST
    // without leading slash (there will be no leading slashes returned from the Router)
    testConversion(idConverter, RestMethod.POST, input, inputWithLeadingSlash);
    idConverter.close();
    IdConversionCallback callback = new IdConversionCallback();
    try {
        idConverter.convert(new MockRestRequest(MockRestRequest.DUMMY_DATA, null), input, callback).get();
        fail("ID conversion should have failed because IdConverter is closed");
    } catch (ExecutionException e) {
        RestServiceException re = (RestServiceException) e.getCause();
        assertEquals("Unexpected RestServerErrorCode (Future)", RestServiceErrorCode.ServiceUnavailable, re.getErrorCode());
        re = (RestServiceException) callback.exception;
        assertEquals("Unexpected RestServerErrorCode (Callback)", RestServiceErrorCode.ServiceUnavailable, re.getErrorCode());
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) VerifiableProperties(com.github.ambry.config.VerifiableProperties) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) MockRestRequest(com.github.ambry.rest.MockRestRequest) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) UtilsTest(com.github.ambry.utils.UtilsTest)

Example 24 with RestServiceException

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

the class AmbrySecurityServiceTest method processRequestTest.

/**
 * Tests {@link AmbrySecurityService#processRequest(RestRequest, Callback)} for common as well as uncommon cases
 * @throws Exception
 */
@Test
public void processRequestTest() throws Exception {
    // rest request being null
    TestUtils.assertException(IllegalArgumentException.class, () -> securityService.preProcessRequest(null).get(), null);
    TestUtils.assertException(IllegalArgumentException.class, () -> securityService.processRequest(null).get(), null);
    TestUtils.assertException(IllegalArgumentException.class, () -> securityService.postProcessRequest(null).get(), null);
    // without callbacks
    RestMethod[] methods = new RestMethod[] { RestMethod.POST, RestMethod.GET, RestMethod.DELETE, RestMethod.HEAD, RestMethod.OPTIONS };
    for (RestMethod restMethod : methods) {
        RestRequest restRequest = createRestRequest(restMethod, "/", null);
        securityService.preProcessRequest(restRequest).get();
        securityService.processRequest(restRequest).get();
        securityService.postProcessRequest(restRequest).get();
    }
    // with GET sub resources
    for (RestUtils.SubResource subResource : RestUtils.SubResource.values()) {
        RestRequest restRequest = createRestRequest(RestMethod.GET, "/sampleId/" + subResource, null);
        securityService.preProcessRequest(restRequest).get();
        securityService.processRequest(restRequest).get();
        securityService.postProcessRequest(restRequest).get();
    }
    // with UrlSigningService denying the request
    URL_SIGNING_SERVICE_FACTORY.isRequestSigned = true;
    URL_SIGNING_SERVICE_FACTORY.verifySignedRequestException = new RestServiceException("Msg", RestServiceErrorCode.Unauthorized);
    testExceptionCasesProcessRequest(createRestRequest(RestMethod.GET, "/", null), RestServiceErrorCode.Unauthorized, false);
    URL_SIGNING_SERVICE_FACTORY.isRequestSigned = false;
    // security service closed
    securityService.close();
    for (RestMethod restMethod : methods) {
        testExceptionCasesProcessRequest(createRestRequest(restMethod, "/", null), RestServiceErrorCode.ServiceUnavailable, true);
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) RestUtils(com.github.ambry.rest.RestUtils) RestMethod(com.github.ambry.rest.RestMethod) Test(org.junit.Test)

Example 25 with RestServiceException

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

the class AmbrySecurityServiceTest method testExceptionCasesProcessResponse.

/**
 * Tests exception cases for
 * {@link SecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)}
 * @param restMethod the {@link RestMethod} of the request to be made
 * @param restResponseChannel the {@link RestResponseChannel} to write responses over.
 * @param blobInfo the {@link BlobInfo} to be used for the {@link RestRequest}
 * @param expectedErrorCode the {@link RestServiceErrorCode} expected in the exception returned.
 * @throws Exception
 */
private void testExceptionCasesProcessResponse(RestMethod restMethod, RestResponseChannel restResponseChannel, BlobInfo blobInfo, RestServiceErrorCode expectedErrorCode) throws Exception {
    TestUtils.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)", expectedErrorCode, re.getErrorCode());
    };
    RestRequest restRequest = createRestRequest(restMethod, "/", null);
    TestUtils.assertException(ExecutionException.class, () -> securityService.processResponse(restRequest, restResponseChannel, blobInfo).get(), errorAction);
}
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) SimpleDateFormat(java.text.SimpleDateFormat) RestTestUtils(com.github.ambry.rest.RestTestUtils) ByteBuffer(java.nio.ByteBuffer) Future(java.util.concurrent.Future) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) Locale(java.util.Locale) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Container(com.github.ambry.account.Container) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) Pair(com.github.ambry.utils.Pair) RestMethod(com.github.ambry.rest.RestMethod) TimeZone(java.util.TimeZone) VerifiableProperties(com.github.ambry.config.VerifiableProperties) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) ByteRange(com.github.ambry.router.ByteRange) Utils(com.github.ambry.utils.Utils) IOException(java.io.IOException) Test(org.junit.Test) BlobInfo(com.github.ambry.messageformat.BlobInfo) ExecutionException(java.util.concurrent.ExecutionException) InMemAccountServiceFactory(com.github.ambry.account.InMemAccountServiceFactory) 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) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) Callback(com.github.ambry.router.Callback) RestServiceException(com.github.ambry.rest.RestServiceException) RestTestUtils(com.github.ambry.rest.RestTestUtils) TestUtils(com.github.ambry.utils.TestUtils) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ExecutionException(java.util.concurrent.ExecutionException)

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