Search in sources :

Example 1 with InMemorySymbolTable

use of com.linkedin.data.codec.symbol.InMemorySymbolTable 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 2 with InMemorySymbolTable

use of com.linkedin.data.codec.symbol.InMemorySymbolTable 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 3 with InMemorySymbolTable

use of com.linkedin.data.codec.symbol.InMemorySymbolTable in project rest.li by linkedin.

the class TestRestLiSymbolTableRequestHandler method testReturnOtherSymbolTableNonDefaultAcceptType.

@Test
public void testReturnOtherSymbolTableNonDefaultAcceptType() throws Exception {
    SymbolTable symbolTable = new InMemorySymbolTable("TestName", ImmutableList.of("Haha", "Hehe", "Hoho"));
    URI uri = URI.create("/symbolTable/OtherTable");
    RestRequest request = new RestRequestBuilder(uri).setHeader(RestConstants.HEADER_ACCEPT, ContentType.JSON.getHeaderKey()).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.JSON.getHeaderKey());
    Assert.assertEquals(symbolTable, SymbolTableSerializer.fromByteString(response.getEntity(), ContentType.JSON.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 4 with InMemorySymbolTable

use of com.linkedin.data.codec.symbol.InMemorySymbolTable in project rest.li by linkedin.

the class TestStreamCodec method getCodecs.

private List<StreamDataCodec> getCodecs(int bufferSize, DataComplex data) {
    List<StreamDataCodec> codecs = new ArrayList<>();
    codecs.add(new JacksonSmileStreamDataCodec(bufferSize));
    codecs.add(new JacksonStreamDataCodec(bufferSize));
    codecs.add(new ProtobufStreamDataCodec(bufferSize));
    Set<String> symbols = new HashSet<>();
    if (data instanceof DataMap) {
        collectSymbols(symbols, (DataMap) data);
    } else {
        collectSymbols(symbols, (DataList) data);
    }
    final String sharedSymbolTableName = "SHARED";
    SymbolTable symbolTable = new InMemorySymbolTable(sharedSymbolTableName, new ArrayList<>(symbols));
    codecs.add(new JacksonLICORStreamDataCodec(bufferSize, true, symbolTable));
    codecs.add(new ProtobufStreamDataCodec(bufferSize, new ProtobufCodecOptions.Builder().setSymbolTable(symbolTable).setEnableASCIIOnlyStrings(true).build()));
    return codecs;
}
Also used : ArrayList(java.util.ArrayList) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) ProtobufCodecOptions(com.linkedin.data.codec.ProtobufCodecOptions) HashSet(java.util.HashSet) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable)

Example 5 with InMemorySymbolTable

use of com.linkedin.data.codec.symbol.InMemorySymbolTable in project rest.li by linkedin.

the class TestRestLiSymbolTableRequestHandler method testReturnSelfSymbolTable.

@Test
public void testReturnSelfSymbolTable() throws Exception {
    SymbolTable symbolTable = new InMemorySymbolTable("TestName", ImmutableList.of("Haha", "Hehe", "Hoho"));
    URI uri = URI.create("/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)

Aggregations

InMemorySymbolTable (com.linkedin.data.codec.symbol.InMemorySymbolTable)9 SymbolTable (com.linkedin.data.codec.symbol.SymbolTable)8 Test (org.testng.annotations.Test)7 RestRequest (com.linkedin.r2.message.rest.RestRequest)6 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)6 RequestContext (com.linkedin.r2.message.RequestContext)4 RestResponse (com.linkedin.r2.message.rest.RestResponse)4 URI (java.net.URI)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 ByteString (com.linkedin.data.ByteString)2 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 DataMap (com.linkedin.data.DataMap)1 ProtobufCodecOptions (com.linkedin.data.codec.ProtobufCodecOptions)1 SymbolTableProvider (com.linkedin.data.codec.symbol.SymbolTableProvider)1 DataSchema (com.linkedin.data.schema.DataSchema)1