use of com.github.ambry.utils.ThrowingConsumer 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.utils.ThrowingConsumer in project ambry by linkedin.
the class AmbryServerSecurityServiceTest method validateConnectionTest.
/**
* Tests for {@link AmbryServerSecurityService#validateConnection(SSLSession, Callback)}
* @throws Exception
*/
@Test
public void validateConnectionTest() throws Exception {
// sslSession is null
TestUtils.assertException(IllegalArgumentException.class, () -> serverSecurityService.validateConnection(null).get(), null);
// success case
SSLSession sslSession = Mockito.mock(SSLSession.class);
serverSecurityService.validateConnection(sslSession, (r, e) -> {
Assert.assertNull("result not null", r);
Assert.assertNull("exception not null", e);
});
// service is closed
serverSecurityService.close();
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)", RestServiceErrorCode.ServiceUnavailable, re.getErrorCode());
};
TestUtils.assertException(ExecutionException.class, () -> serverSecurityService.validateConnection(sslSession).get(), errorAction);
}
use of com.github.ambry.utils.ThrowingConsumer 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 {
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);
}
use of com.github.ambry.utils.ThrowingConsumer in project ambry by linkedin.
the class PostAccountsHandlerTest method validRequestsTest.
/**
* Test valid request cases.
* @throws Exception
*/
@Test
public void validRequestsTest() throws Exception {
ThrowingConsumer<Collection<Account>> testAction = accountsToUpdate -> {
String requestBody = new String(AccountCollectionSerde.serializeAccountsInJson(accountsToUpdate));
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
sendRequestGetResponse(requestBody, restResponseChannel);
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)));
for (Account account : accountsToUpdate) {
assertEquals("Account in account service not as expected", account, accountService.getAccountById(account.getId()));
}
};
testAction.accept(Collections.emptyList());
// add new account
testAction.accept(Collections.singleton(accountService.generateRandomAccount()));
// update multiple accounts
testAction.accept(IntStream.range(0, 3).mapToObj(i -> {
Account account = accountService.createAndAddRandomAccount();
return new AccountBuilder(account).name(account.getName() + i).build();
}).collect(Collectors.toList()));
}
use of com.github.ambry.utils.ThrowingConsumer in project ambry by linkedin.
the class FrontendRestRequestService method handleGet.
@Override
public void handleGet(final RestRequest restRequest, final RestResponseChannel restResponseChannel) {
ThrowingConsumer<RequestPath> routingAction = requestPath -> {
if (requestPath.matchesOperation(Operations.GET_PEERS)) {
getPeersHandler.handle(restRequest, restResponseChannel, (result, exception) -> submitResponse(restRequest, restResponseChannel, result, exception));
} else if (requestPath.matchesOperation(Operations.GET_CLUSTER_MAP_SNAPSHOT)) {
getClusterMapSnapshotHandler.handle(restRequest, restResponseChannel, (result, exception) -> submitResponse(restRequest, restResponseChannel, result, exception));
} else if (requestPath.matchesOperation(Operations.GET_SIGNED_URL)) {
getSignedUrlHandler.handle(restRequest, restResponseChannel, (result, exception) -> submitResponse(restRequest, restResponseChannel, result, exception));
} else if (requestPath.matchesOperation(Operations.ACCOUNTS)) {
getAccountsHandler.handle(restRequest, restResponseChannel, (result, exception) -> submitResponse(restRequest, restResponseChannel, result, exception));
} else if (requestPath.matchesOperation(Operations.STATS_REPORT)) {
getStatsReportHandler.handle(restRequest, restResponseChannel, (result, exception) -> submitResponse(restRequest, restResponseChannel, result, exception));
} else if (requestPath.matchesOperation(Operations.NAMED_BLOB) && NamedBlobPath.parse(requestPath, restRequest.getArgs()).getBlobName() == null) {
listNamedBlobsHandler.handle(restRequest, restResponseChannel, ((result, exception) -> submitResponse(restRequest, restResponseChannel, result, exception)));
} else {
SubResource subResource = requestPath.getSubResource();
GetBlobOptions options = buildGetBlobOptions(restRequest.getArgs(), subResource, getGetOption(restRequest, frontendConfig.defaultRouterGetOption), restRequest, requestPath.getBlobSegmentIdx());
GetCallback routerCallback = new GetCallback(restRequest, restResponseChannel, subResource, options);
SecurityProcessRequestCallback securityCallback = new SecurityProcessRequestCallback(restRequest, restResponseChannel, routerCallback);
if (subResource == SubResource.Replicas) {
securityCallback = new SecurityProcessRequestCallback(restRequest, restResponseChannel);
}
RestRequestMetricsGroup metricsGroup = getMetricsGroupForGet(frontendMetrics, subResource);
RestRequestMetrics restRequestMetrics = metricsGroup.getRestRequestMetrics(restRequest.isSslUsed(), false);
restRequest.getMetricsTracker().injectMetrics(restRequestMetrics);
// named blob requests have their account/container in the URI, so checks can be done prior to ID conversion.
if (requestPath.matchesOperation(Operations.NAMED_BLOB)) {
accountAndContainerInjector.injectAccountAndContainerForNamedBlob(restRequest, metricsGroup);
}
securityService.processRequest(restRequest, securityCallback);
}
};
preProcessAndRouteRequest(restRequest, restResponseChannel, frontendMetrics.getPreProcessingMetrics, routingAction);
}
Aggregations