Search in sources :

Example 1 with MockRestRequest

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

the class NoSizeRSC method readByteByByteTest.

// helpers
// commonCaseTest() helpers
/**
 * Tests reading {@link ReadableStreamChannelInputStream} byte by byte.
 * @param in the data that the {@link ReadableStreamChannelInputStream} should contain.
 * @throws Exception
 */
private void readByteByByteTest(byte[] in) throws Exception {
    // channel with size and one piece of content.
    ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(in));
    InputStream stream = new ReadableStreamChannelInputStream(channel);
    doReadByteByByteTest(stream, in);
    stream.close();
    // channel with no size but one piece of content.
    channel = new NoSizeRSC(ByteBuffer.wrap(in));
    stream = new ReadableStreamChannelInputStream(channel);
    doReadByteByByteTest(stream, in);
    stream.close();
    // channel with no size and multiple pieces of content.
    List<ByteBuffer> contents = splitContent(in, CONTENT_SPLIT_PART_COUNT);
    contents.add(null);
    channel = new MockRestRequest(MockRestRequest.DUMMY_DATA, contents);
    stream = new ReadableStreamChannelInputStream(channel);
    doReadByteByByteTest(stream, in);
    stream.close();
}
Also used : ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) InputStream(java.io.InputStream) MockRestRequest(com.github.ambry.rest.MockRestRequest) ByteBuffer(java.nio.ByteBuffer)

Example 2 with MockRestRequest

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

the class NoSizeRSC method availableTest.

/**
 * Tests correctness of {@link ReadableStreamChannelInputStream#available()}.
 * @throws Exception
 */
@Test
public void availableTest() throws Exception {
    int[] sizes = { 0, 1024 * CONTENT_SPLIT_PART_COUNT };
    for (int size : sizes) {
        byte[] in = new byte[size];
        new Random().nextBytes(in);
        // channel with size and one piece of content.
        ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(in));
        InputStream stream = new ReadableStreamChannelInputStream(channel);
        doAvailableTest(stream, in, in.length);
        stream.close();
        // channel with no size and multiple pieces of content.
        channel = new NoSizeRSC(ByteBuffer.wrap(in));
        stream = new ReadableStreamChannelInputStream(channel);
        doAvailableTest(stream, in, in.length);
        stream.close();
        // channel with no size and multiple pieces of content.
        List<ByteBuffer> contents = splitContent(in, CONTENT_SPLIT_PART_COUNT);
        contents.add(null);
        // assuming all parts are the same length.
        int partLength = contents.get(0).remaining();
        channel = new MockRestRequest(MockRestRequest.DUMMY_DATA, contents);
        stream = new ReadableStreamChannelInputStream(channel);
        doAvailableTest(stream, in, partLength);
        stream.close();
    }
}
Also used : Random(java.util.Random) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) InputStream(java.io.InputStream) MockRestRequest(com.github.ambry.rest.MockRestRequest) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with MockRestRequest

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

the class AmbryIdConverterFactoryTest method testConversion.

/**
 * 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 testConversion(IdConverter idConverter, RestMethod restMethod, Map<String, String> signedIdMetadata, String expectedOutput, String input) throws Exception {
    JSONObject requestData = new JSONObject();
    requestData.put(MockRestRequest.REST_METHOD_KEY, restMethod.name());
    requestData.put(MockRestRequest.URI_KEY, "/");
    RestRequest restRequest = new MockRestRequest(requestData, null);
    if (signedIdMetadata != null) {
        restRequest.setArg(RestUtils.InternalKeys.SIGNED_ID_METADATA_KEY, signedIdMetadata);
    }
    IdConversionCallback callback = new IdConversionCallback();
    assertEquals("Converted ID does not match expected (Future)", expectedOutput, idConverter.convert(restRequest, input, callback).get());
    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) MockRestRequest(com.github.ambry.rest.MockRestRequest)

Example 4 with MockRestRequest

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

the class HostLevelThrottlerTest method createRestRequest.

// A copy of FrontendRestRequestServiceTest.createRestRequest
static RestRequest createRestRequest(RestMethod restMethod, String uri) throws UnsupportedEncodingException, URISyntaxException {
    JSONObject request = new JSONObject();
    request.put(MockRestRequest.REST_METHOD_KEY, restMethod.name());
    request.put(MockRestRequest.URI_KEY, uri);
    return new MockRestRequest(request, null);
}
Also used : JSONObject(org.json.JSONObject) MockRestRequest(com.github.ambry.rest.MockRestRequest)

Example 5 with MockRestRequest

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

the class NoSizeRSC method readAllAtOnceTest.

/**
 * Tests reading {@link ReadableStreamChannelInputStream} all at once.
 * @param in the data that the {@link ReadableStreamChannelInputStream} should contain.
 * @throws Exception
 */
private void readAllAtOnceTest(byte[] in) throws Exception {
    // channel with size and one piece of content.
    ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(in));
    InputStream stream = new ReadableStreamChannelInputStream(channel);
    doReadAllAtOnceTest(stream, in);
    stream.close();
    // channel with no size but one piece of content.
    channel = new NoSizeRSC(ByteBuffer.wrap(in));
    stream = new ReadableStreamChannelInputStream(channel);
    doReadAllAtOnceTest(stream, in);
    stream.close();
    // channel with no size and multiple pieces of content.
    List<ByteBuffer> contents = splitContent(in, CONTENT_SPLIT_PART_COUNT);
    contents.add(null);
    channel = new MockRestRequest(MockRestRequest.DUMMY_DATA, contents);
    stream = new ReadableStreamChannelInputStream(channel);
    doReadAllAtOnceTest(stream, in);
    stream.close();
}
Also used : ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) InputStream(java.io.InputStream) MockRestRequest(com.github.ambry.rest.MockRestRequest) ByteBuffer(java.nio.ByteBuffer)

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