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);
}
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());
}
}
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));
}
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());
}
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());
}
}
Aggregations