Search in sources :

Example 56 with RestResponse

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

the class TestComplexKeysResource method testBatchGetEntityEmpty.

@SuppressWarnings("deprecation")
public void testBatchGetEntityEmpty(BatchGetEntityRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> builder) throws Exception {
    final Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> request = builder.build();
    final FutureCallback<RestResponse> callback = new FutureCallback<RestResponse>();
    getClient().sendRestRequest(request, new RequestContext(), callback);
    final RestResponse result = callback.get();
    final DataMap responseMap = DataMapUtils.readMap(result);
    final DataMap resultsMap = responseMap.getDataMap(BatchKVResponse.RESULTS);
    Assert.assertNotNull(resultsMap, "Response does not contain results map");
    Assert.assertTrue(resultsMap.isEmpty());
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) Message(com.linkedin.restli.examples.greetings.api.Message) RestResponse(com.linkedin.r2.message.rest.RestResponse) RequestContext(com.linkedin.r2.message.RequestContext) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) FutureCallback(com.linkedin.common.callback.FutureCallback) DataMap(com.linkedin.data.DataMap)

Example 57 with RestResponse

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

the class TestDebugRequestHandlers method sendRequestAndVerifyParseqTracevisResponse.

private void sendRequestAndVerifyParseqTracevisResponse(RestRequest request) throws InterruptedException, ExecutionException {
    Future<RestResponse> restResponseFuture = getDefaultTransportClient().restRequest(request);
    RestResponse restResponse = restResponseFuture.get();
    Assert.assertEquals(restResponse.getStatus(), 200);
    List<String> contentTypeValues = restResponse.getHeaderValues(RestConstants.HEADER_CONTENT_TYPE);
    Assert.assertTrue(contentTypeValues.size() == 1);
    Assert.assertEquals(contentTypeValues.get(0), HEADER_VALUE_TEXT_HTML);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse)

Example 58 with RestResponse

use of com.linkedin.r2.message.rest.RestResponse 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 59 with RestResponse

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

the class TestParseqTraceDebugRequestHandler method testStaticContent.

/**
   * Tests the static content retrieval from the parseq trace debug request handler. It enumerates through all
   * files imported into the JAR containing the parseq trace debug request handler, skips the ones that should
   * not be served and verifies the rest can be retrieved. This test makes sure all files we import are actually
   * servicable by the parseq trace debug request handler.
   * @throws IOException
   */
@Test
public void testStaticContent() throws IOException {
    ClassLoader classLoader = ParseqTraceDebugRequestHandler.class.getClassLoader();
    //Collect all files under tracevis folder in the jar containing the parseq trace debug request handler.
    Enumeration<URL> resources = classLoader.getResources(ParseqTraceDebugRequestHandler.class.getName().replace('.', '/') + ".class");
    List<String> files = new ArrayList<String>();
    while (resources.hasMoreElements()) {
        URL url = resources.nextElement();
        URLConnection urlConnection = url.openConnection();
        if (urlConnection instanceof JarURLConnection) {
            JarURLConnection jarURLConnection = (JarURLConnection) urlConnection;
            JarFile jar = jarURLConnection.getJarFile();
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry currentEntry = entries.nextElement();
                if (!currentEntry.isDirectory()) {
                    String entry = currentEntry.getName();
                    if (entry.startsWith("tracevis/")) {
                        files.add(entry);
                    }
                }
            }
        }
    }
    Assert.assertTrue(files.size() > 0);
    // All other files should be retrievable from the parseq trace debug request handler.
    for (String file : files) {
        final String mimeType = determineMediaType(file);
        final URI uri = URI.create("http://host/abc/12/__debug/parseqtrace/" + file.substring(file.indexOf('/') + 1));
        executeRequestThroughParseqDebugHandler(uri, new RequestExecutionCallback<RestResponse>() {

            @Override
            public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
                Assert.fail("Static content cannot be retrieved for " + uri.toString());
            }

            @Override
            public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
                Assert.assertEquals(result.getHeader(RestConstants.HEADER_CONTENT_TYPE), mimeType);
            }
        });
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) RestResponse(com.linkedin.r2.message.rest.RestResponse) ArrayList(java.util.ArrayList) ByteString(com.linkedin.data.ByteString) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URI(java.net.URI) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) Test(org.testng.annotations.Test)

Example 60 with RestResponse

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

the class TestParseqTraceDebugRequestHandler method executeRequestThroughParseqDebugHandler.

private void executeRequestThroughParseqDebugHandler(URI uri, RequestExecutionCallback<RestResponse> callback) {
    ParseqTraceDebugRequestHandler requestHandler = new ParseqTraceDebugRequestHandler();
    RestRequestBuilder requestBuilder = new RestRequestBuilder(uri);
    RestRequest request = requestBuilder.build();
    RequestContext requestContext = new RequestContext();
    requestHandler.handleRequest(request, requestContext, new RestLiDebugRequestHandler.ResourceDebugRequestHandler() {

        @Override
        public void handleRequest(RestRequest request, RequestContext requestContext, RequestExecutionCallback<RestResponse> callback) {
            RestResponse response = EasyMock.createMock(RestResponse.class);
            RequestExecutionReportBuilder executionReportBuilder = new RequestExecutionReportBuilder();
            JsonTraceCodec jsonTraceCodec = new JsonTraceCodec();
            Trace t = null;
            try {
                t = jsonTraceCodec.decode(TEST_TRACE);
                executionReportBuilder.setParseqTrace(t);
            } catch (IOException exc) {
            //test will fail later
            }
            callback.onSuccess(response, executionReportBuilder.build(), null);
        }
    }, null, callback);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) IOException(java.io.IOException) Trace(com.linkedin.parseq.trace.Trace) RestRequest(com.linkedin.r2.message.rest.RestRequest) JsonTraceCodec(com.linkedin.parseq.trace.codec.json.JsonTraceCodec) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext)

Aggregations

RestResponse (com.linkedin.r2.message.rest.RestResponse)231 Test (org.testng.annotations.Test)174 RestRequest (com.linkedin.r2.message.rest.RestRequest)147 RequestContext (com.linkedin.r2.message.RequestContext)108 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)100 URI (java.net.URI)74 RestException (com.linkedin.r2.message.rest.RestException)68 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)55 ByteString (com.linkedin.data.ByteString)50 FutureCallback (com.linkedin.common.callback.FutureCallback)45 Callback (com.linkedin.common.callback.Callback)43 HashMap (java.util.HashMap)41 ExecutionException (java.util.concurrent.ExecutionException)40 Map (java.util.Map)38 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)35 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)29 URISyntaxException (java.net.URISyntaxException)27 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)24 FilterChain (com.linkedin.r2.filter.FilterChain)23 BeforeTest (org.testng.annotations.BeforeTest)23