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;
}
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);
}
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()));
}
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()));
}
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);
}
}
Aggregations