Search in sources :

Example 36 with Response

use of com.linkedin.r2.message.Response in project rest.li by linkedin.

the class TestMessages method testToStreamTransportCallbackSuccess.

@Test
public void testToStreamTransportCallbackSuccess() {
    TransportCallback<RestResponse> restCallback = response -> {
        Assert.assertFalse(response.hasError());
        Assert.assertNotNull(response.getResponse());
        Assert.assertSame(response.getResponse().getEntity(), DATA);
        Assert.assertNotNull(response.getWireAttributes());
        Assert.assertEquals(response.getWireAttributes(), WIRE_ATTR);
    };
    TransportCallback<StreamResponse> streamCallback = Messages.toStreamTransportCallback(restCallback);
    StreamResponseBuilder builder = new StreamResponseBuilder();
    StreamResponse streamResponse = builder.build(EntityStreams.newEntityStream(new ByteStringWriter(DATA)));
    streamCallback.onResponse(TransportResponseImpl.success(streamResponse, WIRE_ATTR));
}
Also used : ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Callback(com.linkedin.common.callback.Callback) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) Messages(com.linkedin.r2.message.Messages) RestResponse(com.linkedin.r2.message.rest.RestResponse) EntityStreams(com.linkedin.r2.message.stream.entitystream.EntityStreams) TransportResponseImpl(com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl) Assert(org.testng.Assert) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Map(java.util.Map) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) StreamException(com.linkedin.r2.message.stream.StreamException) RestException(com.linkedin.r2.message.rest.RestException) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test)

Example 37 with Response

use of com.linkedin.r2.message.Response in project rest.li by linkedin.

the class TestClientRequestFinalizerFilter method testExistingRequestFinalizerManager.

@Test
public void testExistingRequestFinalizerManager() {
    final RequestFinalizerManagerImpl manager = new RequestFinalizerManagerImpl(null, null);
    final AtomicBoolean atomicBoolean = new AtomicBoolean(false);
    manager.registerRequestFinalizer((request, response, requestContext1, throwable) -> atomicBoolean.set(true));
    _requestContext.putLocalAttr(R2Constants.CLIENT_REQUEST_FINALIZER_MANAGER_REQUEST_CONTEXT_KEY, manager);
    _filterChain.onRestRequest(null, _requestContext, null);
    registerRequestFinalizer();
    _filterChain.onRestResponse(null, _requestContext, null);
    assertExecutionOrders();
    Assert.assertTrue(atomicBoolean.get(), "Expected the request finalizer registered before reaching the" + "ClientRequestFinalizerFilter to still be invoked.");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RequestFinalizerManagerImpl(com.linkedin.r2.util.finalizer.RequestFinalizerManagerImpl) Test(org.testng.annotations.Test)

Example 38 with Response

use of com.linkedin.r2.message.Response in project rest.li by linkedin.

the class TestRequestFinalizerDispatcher method testExistingRequestFinalizerManager.

@Test
public void testExistingRequestFinalizerManager() {
    when(_restTransportResponse.getResponse()).thenReturn(_restResponse);
    final RequestContext requestContext = new RequestContext();
    final RequestFinalizerManagerImpl manager = new RequestFinalizerManagerImpl(null, requestContext);
    final AtomicBoolean atomicBoolean = new AtomicBoolean(false);
    manager.registerRequestFinalizer((request, response, requestContext1, throwable) -> atomicBoolean.set(true));
    requestContext.putLocalAttr(R2Constants.SERVER_REQUEST_FINALIZER_MANAGER_REQUEST_CONTEXT_KEY, manager);
    final TestTransportCallback<RestResponse> transportCallback = new TestTransportCallback<>(false);
    _outerDispatcher.handleRestRequest(null, null, requestContext, transportCallback);
    Assert.assertEquals(_outerDispatcher._executionOrder, 1);
    Assert.assertEquals(_innerDispatcher._executionOrder, 2);
    Assert.assertEquals(_innerDispatcher._transportCallback._executionOrder, 3);
    Assert.assertEquals(_outerDispatcher._transportCallback._executionOrder, 4);
    Assert.assertEquals(transportCallback._executionOrder, 5);
    Assert.assertEquals(_requestFinalizer._executionOrder, 6, "Expected request to be finalized after the callback.");
    Assert.assertTrue(atomicBoolean.get(), "Expected the request finalizer registered before reaching the" + "RequestFinalizerDispatcher to still be invoked.");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RestResponse(com.linkedin.r2.message.rest.RestResponse) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 39 with Response

use of com.linkedin.r2.message.Response in project rest.li by linkedin.

the class AbstractCaptureFilterTest method testTwoDifferentRequests.

@Test
public void testTwoDifferentRequests() {
    final RestRequest req1 = request();
    final RestRequest req2 = req1.builder().setEntity("This is a different request".getBytes()).build();
    final RestResponse res1 = response();
    final RestResponse res2 = res1.builder().setEntity("This is a different response".getBytes()).build();
    FilterUtil.fireUntypedRequestResponse(getFilterChain(), req1, res1);
    FilterUtil.fireUntypedRequestResponse(getFilterChain(), req2, res2);
    // Should have created two separate entries
    Assert.assertEquals(res1, getDb().<Response>replay(req1));
    Assert.assertEquals(res2, getDb().<Response>replay(req2));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) Test(org.testng.annotations.Test)

Example 40 with Response

use of com.linkedin.r2.message.Response in project rest.li by linkedin.

the class AbstractCaptureFilterTest method testInitialCapture.

@Test
public void testInitialCapture() {
    final RestRequest req = request();
    final RestResponse res = response();
    Assert.assertNull(getDb().replay(req));
    FilterUtil.fireUntypedRequestResponse(getFilterChain(), req, res);
    Assert.assertEquals(res, getDb().<Response>replay(req));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)155 RestResponse (com.linkedin.r2.message.rest.RestResponse)142 RestRequest (com.linkedin.r2.message.rest.RestRequest)101 RequestContext (com.linkedin.r2.message.RequestContext)95 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)87 URI (java.net.URI)77 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)66 ByteString (com.linkedin.data.ByteString)58 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)41 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)40 HashMap (java.util.HashMap)39 FutureCallback (com.linkedin.common.callback.FutureCallback)36 RestException (com.linkedin.r2.message.rest.RestException)36 CountDownLatch (java.util.concurrent.CountDownLatch)35 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)34 ExecutionException (java.util.concurrent.ExecutionException)32 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)27 Callback (com.linkedin.common.callback.Callback)25 IOException (java.io.IOException)24 Map (java.util.Map)24