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