Search in sources :

Example 41 with RestRequestBuilder

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

the class TestBatchFinderResponseBuilder method testBuildCriteriaURIDataProvider.

@DataProvider(name = "build_uri_test_cases")
public Object[][] testBuildCriteriaURIDataProvider() throws RestLiSyntaxException {
    String[] requestURIs = { "/greetings?bq=searchGreetings&criteria=List((id:1,tone:SINCERE))&message=hello", "/talent/api/talentHiringProjectCandidates?bq=candidates&candidates=List((candidate:urn%3Ali%3Ats_hire_identity%3A88156577))", "/talent/api/talentHiringProjectCandidates?bq=candidates&candidates=List((candidate:urn%3Ali%3Ats_hire_identity%3A88156577))&fields=candidate", "/talent/api/talentHiringProjectCandidates?bq=candidates&candidates=List((candidate:urn%3Ali%3Ats_hire_identity%3A88156577),(candidate:urn%3Baba%3Ats_hire_identity%3A88156588))", "/talent/api/talentHiringProjectCandidates?bq=candidates&candidates=List((candidate:urn%3Ali%3Ats_hire_identity%3A88156577),(candidate:urn%3Baba%3Ats_hire_identity%3A88156588))&fields=candidate", "/test/api/restli?bq=findSomething&fields=field1&search_criteria=List((field1:val1,field2:val2))", "/test/api/restli?bq=findSomething&fields=field1&search_criteria=List((field1:val1,field2:val1),(field1:val2,field2:val2))", "/test/api/restli?bq=findSomething&fields=person:(firstname,lastname)&search_criteria=List((field1:val1,field2:val2))", "/groups?fields=state,locale&metadataFields=city,age&pagingFields=start,count&q=emailDomain&search=List((field1:value1))" };
    String[] batchCriteriaParameterNames = { "criteria", "candidates", "candidates", "candidates", "candidates", "search_criteria", "search_criteria", "search_criteria", "search" };
    String[] expectedURIs = { "/greetings?bq=searchGreetings&criteria=List((id:1,tone:SINCERE))&message=hello", "/talent/api/talentHiringProjectCandidates?bq=candidates&candidates=List((candidate:urn%3Ali%3Ats_hire_identity%3A88156577))", "/talent/api/talentHiringProjectCandidates?bq=candidates&candidates=List((candidate:urn%3Ali%3Ats_hire_identity%3A88156577))&fields=candidate", "/talent/api/talentHiringProjectCandidates?bq=candidates&candidates=List((candidate:urn%3Ali%3Ats_hire_identity%3A88156577))", "/talent/api/talentHiringProjectCandidates?bq=candidates&candidates=List((candidate:urn;aba%3Ats_hire_identity%3A88156588))", "/talent/api/talentHiringProjectCandidates?bq=candidates&candidates=List((candidate:urn%3Ali%3Ats_hire_identity%3A88156577))&fields=candidate", "/talent/api/talentHiringProjectCandidates?bq=candidates&candidates=List((candidate:urn;aba%3Ats_hire_identity%3A88156588))&fields=candidate", "/test/api/restli?bq=findSomething&fields=field1&search_criteria=List((field1:val1,field2:val2))", "/test/api/restli?bq=findSomething&fields=field1&search_criteria=List((field1:val1,field2:val1))", "/test/api/restli?bq=findSomething&fields=field1&search_criteria=List((field1:val2,field2:val2))", "/test/api/restli?bq=findSomething&fields=person:(firstname,lastname)&search_criteria=List((field1:val1,field2:val2))", "/groups?fields=state,locale&metadataFields=city,age&pagingFields=start,count&q=emailDomain&search=List((field1:value1))" };
    int totalCriteriaCases = expectedURIs.length;
    Object[][] testData = new Object[totalCriteriaCases][5];
    int cases = 0;
    int uriIndex = 0;
    while (cases < totalCriteriaCases) {
        Object[] singleCase = new Object[5];
        URI uri = URI.create(requestURIs[uriIndex]);
        RequestContext requestContext = new RequestContext();
        requestContext.putLocalAttr("timingsDisabled", true);
        ResourceContextImpl resourceContext = new ResourceContextImpl(new PathKeysImpl(), new RestRequestBuilder(uri).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion().toString()).build(), requestContext);
        DataList criteriaParameters = (DataList) resourceContext.getStructuredParameter(batchCriteriaParameterNames[uriIndex]);
        for (int i = 0; i < criteriaParameters.size(); i++) {
            RecordTemplate criteria = new AnyRecord((DataMap) criteriaParameters.get(i));
            singleCase[0] = resourceContext;
            singleCase[1] = criteria;
            singleCase[2] = batchCriteriaParameterNames[uriIndex];
            singleCase[3] = uri;
            singleCase[4] = URI.create(expectedURIs[cases]);
            testData[cases] = singleCase;
            cases++;
        }
        uriIndex++;
    }
    return testData;
}
Also used : AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) URI(java.net.URI) DataList(com.linkedin.data.DataList) RecordTemplate(com.linkedin.data.template.RecordTemplate) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) DataProvider(org.testng.annotations.DataProvider)

Example 42 with RestRequestBuilder

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

the class TestAsyncMethodInvocationPlanClass method testAsyncGet.

@Test(dataProvider = "requestData")
public void testAsyncGet(String uri, String method, String expectedPlanClass) throws URISyntaxException, IOException, InterruptedException {
    RestLiConfig config = new RestLiConfig();
    config.addResourcePackageNames("com.linkedin.restli.server.multiplexer.resources");
    SettablePromise<Trace> traceHolder = Promises.settable();
    Engine engine = engine(traceHolder);
    RestLiServer server = new RestLiServer(config, resourceFactory(), engine);
    RestRequest request = new RestRequestBuilder(new URI(uri)).setMethod(method).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
    CountDownLatch latch = new CountDownLatch(1);
    server.handleRequest(request, new RequestContext(), callback(latch));
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertTrue(traceHolder.await(5, TimeUnit.SECONDS));
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertTrue(traceHolder.await(5, TimeUnit.SECONDS));
    assertEquals(traceHolder.get().getPlanClass(), expectedPlanClass);
}
Also used : Trace(com.linkedin.parseq.trace.Trace) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServer(com.linkedin.restli.server.RestLiServer) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) RestLiConfig(com.linkedin.restli.server.RestLiConfig) Engine(com.linkedin.parseq.Engine) Test(org.testng.annotations.Test)

Example 43 with RestRequestBuilder

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

the class TestRestLiSymbolTableRequestHandler method testReturnOtherSymbolTable.

@Test
public void testReturnOtherSymbolTable() throws Exception {
    SymbolTable symbolTable = new InMemorySymbolTable("TestName", ImmutableList.of("Haha", "Hehe", "Hoho"));
    URI uri = URI.create("/symbolTable/OtherTable");
    RestRequest request = new RestRequestBuilder(uri).build();
    when(_symbolTableProvider.getSymbolTable(eq("OtherTable"))).thenReturn(symbolTable);
    CompletableFuture<RestResponse> future = new CompletableFuture<>();
    _requestHandler.handleRequest(request, mock(RequestContext.class), new Callback<RestResponse>() {

        @Override
        public void onError(Throwable e) {
            future.completeExceptionally(e);
        }

        @Override
        public void onSuccess(RestResponse result) {
            future.complete(result);
        }
    });
    Assert.assertFalse(future.isCompletedExceptionally());
    Assert.assertTrue(future.isDone());
    RestResponse response = future.get();
    Assert.assertEquals(response.getStatus(), HttpStatus.S_200_OK.getCode());
    Assert.assertEquals(response.getHeader(RestConstants.HEADER_CONTENT_TYPE), ContentType.PROTOBUF2.getHeaderKey());
    Assert.assertEquals(symbolTable, SymbolTableSerializer.fromByteString(response.getEntity(), ContentType.PROTOBUF2.getCodec()));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) Test(org.testng.annotations.Test)

Example 44 with RestRequestBuilder

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

the class TestRestLiSymbolTableRequestHandler method testReturnSelfSymbolTableWhenCalledWithServiceScope.

@Test
public void testReturnSelfSymbolTableWhenCalledWithServiceScope() throws Exception {
    SymbolTable symbolTable = new InMemorySymbolTable("TestName", ImmutableList.of("Haha", "Hehe", "Hoho"));
    URI uri = URI.create("/service/symbolTable");
    RestRequest request = new RestRequestBuilder(uri).build();
    when(_symbolTableProvider.getResponseSymbolTable(eq(uri), eq(Collections.emptyMap()))).thenReturn(symbolTable);
    CompletableFuture<RestResponse> future = new CompletableFuture<>();
    _requestHandler.handleRequest(request, mock(RequestContext.class), new Callback<RestResponse>() {

        @Override
        public void onError(Throwable e) {
            future.completeExceptionally(e);
        }

        @Override
        public void onSuccess(RestResponse result) {
            future.complete(result);
        }
    });
    Assert.assertFalse(future.isCompletedExceptionally());
    Assert.assertTrue(future.isDone());
    RestResponse response = future.get();
    Assert.assertEquals(response.getStatus(), HttpStatus.S_200_OK.getCode());
    Assert.assertEquals(response.getHeader(RestConstants.HEADER_CONTENT_TYPE), ContentType.PROTOBUF2.getHeaderKey());
    Assert.assertEquals(symbolTable, SymbolTableSerializer.fromByteString(response.getEntity(), ContentType.PROTOBUF2.getCodec()));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) Test(org.testng.annotations.Test)

Example 45 with RestRequestBuilder

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

the class TestRestLiServer method testHandleRequestWithRestLiResponseError.

@Test(dataProvider = "restOrStream")
public void testHandleRequestWithRestLiResponseError(final RestOrStream restOrStream) throws Exception {
    final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
    EasyMock.expect(statusResource.get(eq(1L))).andReturn(null).once();
    replay(statusResource);
    Callback<RestLiResponse> restLiResponseCallback = new Callback<RestLiResponse>() {

        @Override
        public void onSuccess(RestLiResponse restLiResponse) {
            fail("We should not get a success here. The server should have returned a 404!");
        }

        @Override
        public void onError(Throwable e) {
            RestLiResponseException restLiResponseException = (RestLiResponseException) e;
            assertEquals(restLiResponseException.getRestLiResponse().getStatus(), HttpStatus.S_404_NOT_FOUND, "We should get a 404 back here!");
            EasyMock.verify(statusResource);
            EasyMock.reset(statusResource);
        }
    };
    if (restOrStream == RestOrStream.REST) {
        RestRequest request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
        _server.handleRequestWithRestLiResponse(request, new RequestContext(), restLiResponseCallback);
    } else {
        StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build(EntityStreams.emptyStream());
        _server.handleRequestWithRestLiResponse(streamRequest, new RequestContext(), restLiResponseCallback);
    }
}
Also used : SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) RestLiResponseException(com.linkedin.restli.internal.server.response.RestLiResponseException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)336 RestRequest (com.linkedin.r2.message.rest.RestRequest)290 Test (org.testng.annotations.Test)267 URI (java.net.URI)220 RestResponse (com.linkedin.r2.message.rest.RestResponse)192 RequestContext (com.linkedin.r2.message.RequestContext)155 ExecutionException (java.util.concurrent.ExecutionException)55 ByteString (com.linkedin.data.ByteString)46 FutureCallback (com.linkedin.common.callback.FutureCallback)43 RestException (com.linkedin.r2.message.rest.RestException)42 HashMap (java.util.HashMap)36 TimeoutException (java.util.concurrent.TimeoutException)29 AfterTest (org.testng.annotations.AfterTest)26 BeforeTest (org.testng.annotations.BeforeTest)26 Callback (com.linkedin.common.callback.Callback)25 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)25 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)21 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)20