use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class TailoredPeersClusterMap method doBadArgsTest.
// badArgsTest() helpers.
/**
* Does the test where bad args are provided in the request to {@link GetPeersHandler}.
* @param name the name of the host whose peers are required. Can be {@code null} if this param should be omitted.
* @param port the port of the host whose peers are required. Can be {@code null} if this param should be omitted.
* @param expectedErrorCode the {@link RestServiceErrorCode} expected in response.
* @throws Exception
*/
private void doBadArgsTest(String name, String port, RestServiceErrorCode expectedErrorCode) throws Exception {
StringBuilder uri = new StringBuilder(Operations.GET_PEERS + "?");
if (name != null) {
uri.append(GetPeersHandler.NAME_QUERY_PARAM).append("=").append(name);
}
if (port != null) {
if (name != null) {
uri.append("&");
}
uri.append(GetPeersHandler.PORT_QUERY_PARAM).append("=").append(port);
}
JSONObject data = new JSONObject();
data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.GET.name());
data.put(MockRestRequest.URI_KEY, uri.toString());
RestRequest restRequest = new MockRestRequest(data, null);
try {
sendRequestGetResponse(restRequest, new MockRestResponseChannel());
fail("Request should have failed");
} catch (RestServiceException e) {
assertEquals("Unexpected RestServiceErrorCode", expectedErrorCode, e.getErrorCode());
}
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class GetSignedUrlHandlerTest method verifyFailureWithMsg.
/**
* Verifies that attempting to get signed urls 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 = new MockRestRequest(MockRestRequest.DUMMY_DATA, null);
restRequest.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse("/signedUrl", Collections.emptyMap(), Collections.emptyList(), "Ambry-test"));
restRequest.setArg(RestUtils.Headers.URL_TYPE, RestMethod.POST.name());
restRequest.setArg(RestUtils.Headers.TARGET_ACCOUNT_NAME, REF_ACCOUNT.getName());
restRequest.setArg(RestUtils.Headers.TARGET_CONTAINER_NAME, REF_CONTAINER.getName());
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.RestRequest in project ambry by linkedin.
the class NamedBlobPutHandlerTest method getRestRequest.
/**
* Method to easily create a PUT {@link RestRequest}. This will set {@link RestUtils.InternalKeys#REQUEST_PATH} to a
* valid {@link RequestPath} object.
* @param headers any associated headers as a {@link JSONObject}.
* @param path the path for the request.
* @param requestBody the body of the request.
* @return A {@link RestRequest} object that defines the request required by the input.
*/
private RestRequest getRestRequest(JSONObject headers, String path, byte[] requestBody) throws UnsupportedEncodingException, URISyntaxException, RestServiceException {
RestRequest request = FrontendRestRequestServiceTest.createRestRequest(RestMethod.PUT, path, headers, new LinkedList<>(Arrays.asList(ByteBuffer.wrap(requestBody), null)));
request.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse(request, frontendConfig.pathPrefixesToRemove, CLUSTER_NAME));
return request;
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class NamedBlobPutHandlerTest method doTtlRequiredEnforcementTest.
/**
* Does the TTL required enforcement test by selecting the right verification methods based on container and frontend
* config
* @param container the {@link Container} to upload to
* @param blobTtlSecs the TTL to set for the blob
* @throws Exception
*/
private void doTtlRequiredEnforcementTest(Container container, long blobTtlSecs) throws Exception {
JSONObject headers = new JSONObject();
FrontendRestRequestServiceTest.setAmbryHeadersForPut(headers, blobTtlSecs, !container.isCacheable(), SERVICE_ID, CONTENT_TYPE, OWNER_ID, null, null, null);
byte[] content = TestUtils.getRandomBytes(1024);
RestRequest request = getRestRequest(headers, request_path, content);
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
FutureResult<Void> future = new FutureResult<>();
namedBlobPutHandler.handle(request, restResponseChannel, future::done);
if (container.isTtlRequired() && (blobTtlSecs == Utils.Infinite_Time || blobTtlSecs > frontendConfig.maxAcceptableTtlSecsIfTtlRequired)) {
if (frontendConfig.failIfTtlRequiredButNotProvided) {
try {
future.get(TIMEOUT_SECS, TimeUnit.SECONDS);
fail("Post should have failed");
} catch (ExecutionException e) {
RestServiceException rootCause = (RestServiceException) Utils.getRootCause(e);
assertNotNull("Root cause should be a RestServiceException", rootCause);
assertEquals("Incorrect RestServiceErrorCode", RestServiceErrorCode.InvalidArgs, rootCause.getErrorCode());
}
} else {
verifySuccessResponseOnTtlEnforcement(future, content, blobTtlSecs, restResponseChannel, true);
}
} else {
verifySuccessResponseOnTtlEnforcement(future, content, blobTtlSecs, restResponseChannel, false);
}
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class PostAccountsHandlerTest method sendRequestGetResponse.
// helpers
// general
/**
* Sends a request to the {@link PostAccountsHandler} and waits for the response.
* @param requestBody body of the request in string form.
* @param restResponseChannel the {@link RestResponseChannel} where headers will be set.
* @throws Exception
*/
private void sendRequestGetResponse(String requestBody, RestResponseChannel restResponseChannel) throws Exception {
JSONObject data = new JSONObject();
data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.POST.name());
data.put(MockRestRequest.URI_KEY, Operations.ACCOUNTS);
List<ByteBuffer> body = new LinkedList<>();
body.add(ByteBuffer.wrap(requestBody.getBytes(StandardCharsets.UTF_8)));
body.add(null);
RestRequest restRequest = new MockRestRequest(data, body);
restRequest.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse(restRequest, null, null));
FutureResult<ReadableStreamChannel> future = new FutureResult<>();
handler.handle(restRequest, restResponseChannel, future::done);
try {
future.get(1, TimeUnit.SECONDS);
} catch (ExecutionException e) {
throw e.getCause() instanceof Exception ? (Exception) e.getCause() : new Exception(e.getCause());
}
}
Aggregations