Search in sources :

Example 21 with MockRestRequest

use of com.github.ambry.rest.MockRestRequest 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());
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestRequest(com.github.ambry.rest.MockRestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) IOException(java.io.IOException) RestServiceException(com.github.ambry.rest.RestServiceException)

Example 22 with MockRestRequest

use of com.github.ambry.rest.MockRestRequest 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());
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) FutureResult(com.github.ambry.router.FutureResult) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MockRestRequest(com.github.ambry.rest.MockRestRequest) ExecutionException(java.util.concurrent.ExecutionException) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList) ExecutionException(java.util.concurrent.ExecutionException) RestServiceException(com.github.ambry.rest.RestServiceException)

Example 23 with MockRestRequest

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

the class AmbryIdConverterFactoryTest method testConversionForNamedBlob.

/**
 * Tests the conversion by the {@code idConverter}.
 * @param idConverter the {@link IdConverter} instance to use.
 * @param restMethod the {@link RestMethod} of the {@link RestRequest} that will be created.
 * @param signedIdMetadata the headers of the {@link RestRequest}.
 * @param expectedOutput the expected output from the {@code idConverter}.
 * @param input the input string
 * @throws Exception
 */
private void testConversionForNamedBlob(IdConverter idConverter, RestMethod restMethod, Map<String, String> signedIdMetadata, String expectedOutput, String input) throws Exception {
    JSONObject requestData = new JSONObject();
    JSONObject headers = new JSONObject();
    String contentType = "application/octet-stream";
    headers.put(RestUtils.Headers.AMBRY_CONTENT_TYPE, contentType);
    requestData.put(MockRestRequest.REST_METHOD_KEY, restMethod.name());
    requestData.put(MockRestRequest.URI_KEY, NAMED_BLOB_PATH);
    requestData.put(MockRestRequest.HEADERS_KEY, headers);
    RestRequest restRequest = new MockRestRequest(requestData, null);
    if (signedIdMetadata != null) {
        restRequest.setArg(RestUtils.InternalKeys.SIGNED_ID_METADATA_KEY, signedIdMetadata);
    }
    BlobInfo blobInfo = null;
    if (restMethod.equals(RestMethod.PUT)) {
        restRequest.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse(NAMED_BLOB_PATH, Collections.emptyMap(), Collections.emptyList(), "Ambry-test"));
        blobInfo = new BlobInfo(new BlobProperties(-1, "service", accountId, containerId, false), new byte[0]);
    }
    IdConversionCallback callback = new IdConversionCallback();
    assertEquals("Converted ID does not match expected (Future)", expectedOutput, idConverter.convert(restRequest, input, blobInfo, callback).get(5, TimeUnit.SECONDS));
    assertEquals("Converted ID does not match expected (Callback)", expectedOutput, callback.result);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) BlobProperties(com.github.ambry.messageformat.BlobProperties) BlobInfo(com.github.ambry.messageformat.BlobInfo) MockRestRequest(com.github.ambry.rest.MockRestRequest)

Example 24 with MockRestRequest

use of com.github.ambry.rest.MockRestRequest 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);
    IdSigningService idSigningService = mock(IdSigningService.class);
    AmbryIdConverterFactory ambryIdConverterFactory = new AmbryIdConverterFactory(verifiableProperties, new MetricRegistry(), idSigningService, null);
    IdConverter idConverter = ambryIdConverterFactory.getIdConverter();
    assertNotNull("No IdConverter returned", idConverter);
    String input = TestUtils.getRandomString(10);
    String inputWithLeadingSlash = "/" + input;
    // GET
    // without leading slash
    reset(idSigningService);
    testConversion(idConverter, RestMethod.GET, null, input, input);
    verify(idSigningService, never()).parseSignedId(any());
    // with leading slash
    reset(idSigningService);
    testConversion(idConverter, RestMethod.GET, null, input, inputWithLeadingSlash);
    verify(idSigningService, never()).parseSignedId(any());
    // with signed ID input.
    String idFromParsedSignedId = "parsedId" + input;
    reset(idSigningService);
    when(idSigningService.isIdSigned(any())).thenReturn(true);
    when(idSigningService.parseSignedId(any())).thenReturn(new Pair<>(idFromParsedSignedId, Collections.emptyMap()));
    testConversion(idConverter, RestMethod.GET, null, idFromParsedSignedId, inputWithLeadingSlash);
    verify(idSigningService).parseSignedId(input);
    // test signed id parsing exception
    reset(idSigningService);
    when(idSigningService.isIdSigned(any())).thenReturn(true);
    when(idSigningService.parseSignedId(any())).thenThrow(new RestServiceException("expected", RestServiceErrorCode.InternalServerError));
    testConversionFailure(idConverter, new MockRestRequest(MockRestRequest.DUMMY_DATA, null), input, RestServiceErrorCode.InternalServerError);
    verify(idSigningService).parseSignedId(input);
    // with named blob
    NamedBlobDb namedBlobDb = mock(NamedBlobDb.class);
    ambryIdConverterFactory = new AmbryIdConverterFactory(verifiableProperties, new MetricRegistry(), idSigningService, namedBlobDb);
    idConverter = ambryIdConverterFactory.getIdConverter();
    assertNotNull("No IdConverter returned", idConverter);
    String outputId = "dummy-id";
    reset(idSigningService);
    reset(namedBlobDb);
    when(namedBlobDb.get(any(), any(), any())).thenReturn(CompletableFuture.completedFuture(new NamedBlobRecord("", "", "", outputId, Utils.Infinite_Time)));
    testConversion(idConverter, RestMethod.GET, null, outputId, NAMED_BLOB_PATH);
    verify(namedBlobDb).get(ACCOUNT_NAME, CONTAINER_NAME, BLOB_NAME);
    reset(idSigningService);
    reset(namedBlobDb);
    when(namedBlobDb.delete(any(), any(), any())).thenReturn(CompletableFuture.completedFuture(new DeleteResult(outputId, false)));
    testConversion(idConverter, RestMethod.DELETE, null, outputId, NAMED_BLOB_PATH);
    verify(namedBlobDb).delete(ACCOUNT_NAME, CONTAINER_NAME, BLOB_NAME);
    // POST
    // without leading slash (there will be no leading slashes returned from the Router)
    reset(idSigningService);
    testConversion(idConverter, RestMethod.POST, null, inputWithLeadingSlash, input);
    verify(idSigningService, never()).getSignedId(any(), any());
    // with signed id metadata set, requires signed ID.
    String signedId = "signedId/" + input;
    reset(idSigningService);
    when(idSigningService.getSignedId(any(), any())).thenReturn(signedId);
    Map<String, String> signedIdMetadata = Collections.singletonMap("test-key", "test-value");
    testConversion(idConverter, RestMethod.POST, signedIdMetadata, "/" + signedId, input);
    verify(idSigningService).getSignedId(input, signedIdMetadata);
    idConverter.close();
    testConversionFailure(idConverter, new MockRestRequest(MockRestRequest.DUMMY_DATA, null), input, RestServiceErrorCode.ServiceUnavailable);
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) NamedBlobDb(com.github.ambry.named.NamedBlobDb) MetricRegistry(com.codahale.metrics.MetricRegistry) BlobProperties(com.github.ambry.messageformat.BlobProperties) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestServiceException(com.github.ambry.rest.RestServiceException) DeleteResult(com.github.ambry.named.DeleteResult) NamedBlobRecord(com.github.ambry.named.NamedBlobRecord) Test(org.junit.Test)

Example 25 with MockRestRequest

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

the class AmbryUrlSigningServiceTest method getRequestFromUrl.

/**
 * Gets a {@link RestRequest} from {@code url}.
 * @param restMethod the {@link RestMethod} of the request.
 * @param url the url of the request.
 * @return a {@link RestRequest} from {@code url}.
 * @throws Exception
 */
private RestRequest getRequestFromUrl(RestMethod restMethod, String url) throws Exception {
    JSONObject request = new JSONObject();
    request.put(MockRestRequest.REST_METHOD_KEY, restMethod.name());
    request.put(MockRestRequest.URI_KEY, url);
    return new MockRestRequest(request, null);
}
Also used : JSONObject(org.json.JSONObject) MockRestRequest(com.github.ambry.rest.MockRestRequest)

Aggregations

MockRestRequest (com.github.ambry.rest.MockRestRequest)34 JSONObject (org.json.JSONObject)20 RestRequest (com.github.ambry.rest.RestRequest)18 RestServiceException (com.github.ambry.rest.RestServiceException)7 Test (org.junit.Test)7 ByteBuffer (java.nio.ByteBuffer)6 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)5 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)4 InputStream (java.io.InputStream)4 IOException (java.io.IOException)3 ExecutionException (java.util.concurrent.ExecutionException)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 VerifiableProperties (com.github.ambry.config.VerifiableProperties)2 BlobProperties (com.github.ambry.messageformat.BlobProperties)2 LinkedList (java.util.LinkedList)2 Properties (java.util.Properties)2 Callback (com.github.ambry.commons.Callback)1 ServerMetrics (com.github.ambry.commons.ServerMetrics)1 ServerConfig (com.github.ambry.config.ServerConfig)1 BlobInfo (com.github.ambry.messageformat.BlobInfo)1