Search in sources :

Example 6 with RestLiTestAttachmentDataSource

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);
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) EmptyRecord(com.linkedin.restli.common.EmptyRecord) RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) CountDownLatch(java.util.concurrent.CountDownLatch) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) Test(org.testng.annotations.Test)

Example 7 with RestLiTestAttachmentDataSource

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());
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) Test(org.testng.annotations.Test)

Example 8 with RestLiTestAttachmentDataSource

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());
    }
}
Also used : DataList(com.linkedin.data.DataList) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource)

Example 9 with RestLiTestAttachmentDataSource

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);
}
Also used : CollectionRequest(com.linkedin.restli.common.CollectionRequest) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) TestRecord(com.linkedin.restli.client.test.TestRecord) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 10 with RestLiTestAttachmentDataSource

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);
}
Also used : RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Aggregations

RestLiTestAttachmentDataSource (com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource)17 Test (org.testng.annotations.Test)14 MultiPartMIMEWriter (com.linkedin.multipart.MultiPartMIMEWriter)7 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)6 MultiPartMIMEReader (com.linkedin.multipart.MultiPartMIMEReader)5 RestLiAttachmentReader (com.linkedin.restli.common.attachments.RestLiAttachmentReader)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 RequestContext (com.linkedin.r2.message.RequestContext)4 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)4 RestLiTestAttachmentDataSourceIterator (com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSourceIterator)4 URI (java.net.URI)4 ByteString (com.linkedin.data.ByteString)3 MIMETestUtils (com.linkedin.multipart.utils.MIMETestUtils)3 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)3 TestRecord (com.linkedin.restli.client.test.TestRecord)3 ArrayList (java.util.ArrayList)3 Callback (com.linkedin.common.callback.Callback)2 DataMap (com.linkedin.data.DataMap)2 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)2 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)2