use of com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource in project rest.li by linkedin.
the class TestStreamingGreetings method fullStreamTest.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void fullStreamTest(final RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
//Perform a create to the server to store some bytes via an attachment.
final byte[] clientSuppliedBytes = "ClientSupplied".getBytes();
final RestLiTestAttachmentDataSource greetingAttachment = new RestLiTestAttachmentDataSource("1", ByteString.copy(clientSuppliedBytes));
final RootBuilderWrapper.MethodBuilderWrapper<Long, Greeting, EmptyRecord> methodBuilderWrapper = builders.create();
methodBuilderWrapper.appendSingleAttachment(greetingAttachment);
//Provide a header to verify the server's ability to transform the first part into the RestRequest.
methodBuilderWrapper.setHeader("createHeader", "createHeaderValue");
final Greeting greeting = new Greeting().setMessage("A greeting with an attachment");
final Request<EmptyRecord> createRequest = methodBuilderWrapper.input(greeting).build();
try {
final Response<EmptyRecord> createResponse = getClient().sendRequest(createRequest).getResponse();
Assert.assertEquals(createResponse.getStatus(), 201);
//Verify that headers propagate properly.
Assert.assertEquals(createResponse.getHeader("createHeader"), "createHeaderValue");
} catch (final RestLiResponseException responseException) {
Assert.fail("We should not reach here!", responseException);
}
//Then perform a GET and verify the bytes are present
try {
final Request<Greeting> getRequest = builders.get().id(1l).setHeader("getHeader", "getHeaderValue").build();
final Response<Greeting> getResponse = getClient().sendRequest(getRequest).getResponse();
Assert.assertEquals(getResponse.getStatus(), 200);
//Verify that headers propagate properly.
Assert.assertEquals(getResponse.getHeader("getHeader"), "getHeaderValue");
Assert.assertEquals(getResponse.getHeader(RestConstants.HEADER_CONTENT_TYPE), RestConstants.HEADER_VALUE_APPLICATION_JSON);
Assert.assertEquals(getResponse.getEntity().getMessage(), "Your greeting has an attachment since you were kind and decided you wanted to read it!");
Assert.assertTrue(getResponse.hasAttachments(), "We must have some response attachments");
RestLiAttachmentReader attachmentReader = getResponse.getAttachmentReader();
final CountDownLatch latch = new CountDownLatch(1);
final GreetingBlobReaderCallback greetingBlobReaderCallback = new GreetingBlobReaderCallback(latch);
attachmentReader.registerAttachmentReaderCallback(greetingBlobReaderCallback);
try {
latch.await(3000, TimeUnit.SECONDS);
Assert.assertEquals(greetingBlobReaderCallback.getAttachmentList().size(), 1);
Assert.assertEquals(greetingBlobReaderCallback.getAttachmentList().get(0), ByteString.copy(clientSuppliedBytes));
} catch (Exception exception) {
Assert.fail();
}
} catch (final RestLiResponseException responseException) {
Assert.fail("We should not reach here!", responseException);
}
}
use of com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource in project rest.li by linkedin.
the class TestParseqTraceDebugRequestHandler method testResponseStreamingAttachmentsForbidden.
@Test
public void testResponseStreamingAttachmentsForbidden() {
//This test verifies that the ParseqTraceDebugRequestHandler aborts any potential outgoing response attachments
//set by a resource method.
final URI uri = URI.create("http://host/abc/12/__debug/parseqtrace/raw");
ParseqTraceDebugRequestHandler requestHandler = new ParseqTraceDebugRequestHandler();
RestRequestBuilder requestBuilder = new RestRequestBuilder(uri);
RestRequest request = requestBuilder.build();
RequestContext requestContext = new RequestContext();
final RequestExecutionCallback<RestResponse> callback = new RequestExecutionCallback<RestResponse>() {
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
Assert.fail("Request execution failed unexpectedly.");
}
@Override
public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
}
};
final RestLiTestAttachmentDataSource testAttachmentDataSource = RestLiTestAttachmentDataSource.createWithRandomPayload("1");
final RestLiResponseAttachments responseAttachments = new RestLiResponseAttachments.Builder().appendSingleAttachment(testAttachmentDataSource).build();
requestHandler.handleRequest(request, requestContext, new RestLiDebugRequestHandler.ResourceDebugRequestHandler() {
@Override
public void handleRequest(RestRequest request, RequestContext requestContext, RequestExecutionCallback<RestResponse> callback) {
RestResponse response = EasyMock.createMock(RestResponse.class);
//Provide some attachments that should be aborted.
callback.onSuccess(response, new RequestExecutionReportBuilder().build(), responseAttachments);
}
}, null, callback);
Assert.assertTrue(testAttachmentDataSource.dataSourceAborted());
}
use of com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource in project rest.li by linkedin.
the class TestClientBuilders method checkBasicRequest.
private void checkBasicRequest(Request<?> request, URIDetails expectedURIDetails, ResourceMethod expectedMethod, RecordTemplate expectedInput, Map<String, String> expectedHeaders, List<Object> expectedStreamingDataSources) {
URIDetails.testUriGeneration(request, expectedURIDetails);
checkRequestIsReadOnly(request);
Assert.assertEquals(request.getMethod(), expectedMethod);
Assert.assertEquals(request.getHeaders(), expectedHeaders);
if (expectedInput != null && (expectedMethod == ResourceMethod.BATCH_UPDATE || expectedMethod == ResourceMethod.BATCH_PARTIAL_UPDATE || expectedMethod == ResourceMethod.BATCH_CREATE)) {
//The list of elements will need to be compared order independently because CollectionRequest has a list
//which is constructed in a non-deterministic order for these 3 method types
final List<Object> expectedElementList = (DataList) expectedInput.data().get(CollectionRequest.ELEMENTS);
final Multiset<Object> expectedElementSet = HashMultiset.create(expectedElementList);
final List<Object> actualElementList = (DataList) request.getInputRecord().data().get(CollectionRequest.ELEMENTS);
final Multiset<Object> actualElementSet = HashMultiset.create(actualElementList);
//TestNG has sporadic issues with comparing Sets for equality
Assert.assertTrue(actualElementSet.equals(expectedElementSet), "The CollectRequest must be correct");
} else {
Assert.assertEquals(request.getInputRecord(), expectedInput);
}
if (expectedStreamingDataSources != null) {
Assert.assertEquals(request.getStreamingAttachments(), expectedStreamingDataSources);
try {
request.getStreamingAttachments().add(new RestLiTestAttachmentDataSource("1", ByteString.empty()));
Assert.fail("Should not be able to add to an immutable list");
} catch (Exception e) {
Assert.assertTrue(e instanceof UnsupportedOperationException);
}
} else {
Assert.assertNull(request.getStreamingAttachments());
}
}
use of com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource in project rest.li by linkedin.
the class TestClientBuilders method testBatchCreateRequestBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "noEntity")
public void testBatchCreateRequestBuilder(URIDetails expectedURIDetails) {
BatchCreateRequestBuilder<Long, TestRecord> builder = new BatchCreateRequestBuilder<Long, TestRecord>(TEST_URI, TestRecord.class, _COLL_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
List<TestRecord> newRecords = Arrays.asList(new TestRecord(), new TestRecord(), new TestRecord());
BatchCreateRequest<TestRecord> request = builder.inputs(newRecords).appendSingleAttachment(_dataSourceWriterA).appendMultipleAttachments(_dataSourceIterator).appendSingleAttachment(_dataSourceWriterB).build();
testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
Assert.assertEquals(request.isSafe(), false);
Assert.assertEquals(request.isIdempotent(), false);
Assert.assertEquals(request.getStreamingAttachments(), _streamingDataSources);
try {
request.getStreamingAttachments().add(new RestLiTestAttachmentDataSource("1", ByteString.empty()));
Assert.fail("Should not be able to add to an immutable list");
} catch (Exception e) {
Assert.assertTrue(e instanceof UnsupportedOperationException);
}
CollectionRequest<TestRecord> expectedRequest = new CollectionRequest<TestRecord>(new DataMap(), TestRecord.class);
expectedRequest.getElements().addAll(newRecords);
checkBasicRequest(request, expectedURIDetails, ResourceMethod.BATCH_CREATE, expectedRequest, Collections.<String, String>emptyMap(), _streamingDataSources);
}
use of com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource in project rest.li by linkedin.
the class TestClientBuilders method testCreateRequestBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "noEntity")
public void testCreateRequestBuilder(URIDetails expectedURIDetails) {
CreateRequestBuilder<Long, TestRecord> builder = new CreateRequestBuilder<Long, TestRecord>(TEST_URI, TestRecord.class, _COLL_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
CreateRequest<TestRecord> request = builder.input(new TestRecord()).appendSingleAttachment(_dataSourceWriterA).appendMultipleAttachments(_dataSourceIterator).appendSingleAttachment(_dataSourceWriterB).build();
Assert.assertEquals(request.isSafe(), false);
Assert.assertEquals(request.isIdempotent(), false);
Assert.assertEquals(request.getStreamingAttachments(), _streamingDataSources);
try {
request.getStreamingAttachments().add(new RestLiTestAttachmentDataSource("1", ByteString.empty()));
Assert.fail("Should not be able to add to an immutable list");
} catch (Exception e) {
Assert.assertTrue(e instanceof UnsupportedOperationException);
}
checkBasicRequest(request, expectedURIDetails, ResourceMethod.CREATE, new TestRecord(), Collections.<String, String>emptyMap(), _streamingDataSources);
}
Aggregations