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