Search in sources :

Example 21 with MockRestResponseChannel

use of com.github.ambry.rest.MockRestResponseChannel 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 22 with MockRestResponseChannel

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

the class TailoredPeersClusterMap method verifyFailureWithMsg.

/**
 * Verifies that attempting to get peers of any datanode fails with the provided {@code msg}.
 * @param msg the message in the {@link Exception} that will be thrown.
 * @throws Exception
 */
private void verifyFailureWithMsg(String msg) throws Exception {
    RestRequest restRequest = getRestRequest(TailoredPeersClusterMap.DATANODE_NAMES[0]);
    try {
        sendRequestGetResponse(restRequest, new MockRestResponseChannel());
        fail("Request should have failed");
    } catch (Exception e) {
        assertEquals("Unexpected Exception", msg, e.getMessage());
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) IOException(java.io.IOException) RestServiceException(com.github.ambry.rest.RestServiceException)

Example 23 with MockRestResponseChannel

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

the class GetSignedUrlHandlerTest method verifySigningUrl.

// handleGoodCaseTest() helpers
/**
 * Verifies that a singed URL is returned and it matches what is expected
 * @param restRequest the {@link RestRequest} to get a signed URL.
 * @param urlExpected the URL that should be returned.
 * @param expectedAccount the {@link Account} that should be populated in {@link RestRequest}.
 * @param expectedContainer the {@link Container} that should be populated in {@link RestRequest}.
 * @throws Exception
 */
private void verifySigningUrl(RestRequest restRequest, String urlExpected, Account expectedAccount, Container expectedContainer) throws Exception {
    RestResponseChannel restResponseChannel = new MockRestResponseChannel();
    sendRequestGetResponse(restRequest, restResponseChannel);
    Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
    assertEquals("Content-length is not as expected", 0, Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
    assertEquals("Signed URL is not as expected", urlExpected, restResponseChannel.getHeader(RestUtils.Headers.SIGNED_URL));
    assertEquals("Account not as expected", expectedAccount, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY));
    assertEquals("Container not as expected", expectedContainer, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY));
}
Also used : RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 24 with MockRestResponseChannel

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

the class GetClusterMapSnapshotHandlerTest method handleGoodCaseTest.

/**
 * Handles the case where everything works as expected
 * @throws Exception
 */
@Test
public void handleGoodCaseTest() throws Exception {
    RestRequest restRequest = createRestRequest();
    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)));
    JSONObject expected = clusterMap.getSnapshot();
    JSONObject actual = RestTestUtils.getJsonizedResponseBody(channel);
    // remove timestamps because they may differ
    expected.remove(ClusterMapSnapshotConstants.TIMESTAMP_MS);
    actual.remove(ClusterMapSnapshotConstants.TIMESTAMP_MS);
    assertEquals("Snapshot does not match expected", expected.toString(), actual.toString());
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) 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 25 with MockRestResponseChannel

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

the class GetClusterMapSnapshotHandlerTest method verifyFailureWithMsg.

/**
 * Verifies that attempting to get the snapshot of the cluster map fails with {@code msg}.
 * @param msg the message in the {@link Exception} that will be thrown.
 * @throws Exception
 */
private void verifyFailureWithMsg(String msg) throws Exception {
    RestRequest restRequest = createRestRequest();
    try {
        sendRequestGetResponse(restRequest, new MockRestResponseChannel());
        fail("Request should have failed");
    } catch (Exception e) {
        assertEquals("Unexpected Exception", msg, e.getMessage());
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) IOException(java.io.IOException)

Aggregations

MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)72 RestRequest (com.github.ambry.rest.RestRequest)57 MockRestRequest (com.github.ambry.rest.MockRestRequest)51 JSONObject (org.json.JSONObject)39 RestServiceException (com.github.ambry.rest.RestServiceException)36 Test (org.junit.Test)34 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)27 RestMethod (com.github.ambry.rest.RestMethod)19 Account (com.github.ambry.account.Account)18 FutureResult (com.github.ambry.router.FutureResult)17 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)17 ExecutionException (java.util.concurrent.ExecutionException)17 StorageStatsUtilTest (com.github.ambry.server.StorageStatsUtilTest)16 RestUtils (com.github.ambry.rest.RestUtils)15 ByteBuffer (java.nio.ByteBuffer)15 MetricRegistry (com.codahale.metrics.MetricRegistry)14 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)14 RestUtilsTest (com.github.ambry.rest.RestUtilsTest)14 TestUtils (com.github.ambry.utils.TestUtils)14 TimeUnit (java.util.concurrent.TimeUnit)14