From 74a805752959814e820b67e78fd4155f35135103 Mon Sep 17 00:00:00 2001 From: Courage Noko Date: Tue, 14 Feb 2023 10:16:44 -0500 Subject: [PATCH] add empty response and GrpcResponseHandler tests --- .../org/apache/druid/grpc/GrpcQueryTest.java | 27 +++++-- .../grpc/client/GrpcResponseHandlerTest.java | 80 +++++++++++++++++++ 2 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 extensions-contrib/grpc-query/src/test/java/org/apache/druid/grpc/client/GrpcResponseHandlerTest.java diff --git a/extensions-contrib/grpc-query/src/test/java/org/apache/druid/grpc/GrpcQueryTest.java b/extensions-contrib/grpc-query/src/test/java/org/apache/druid/grpc/GrpcQueryTest.java index 8febcafcfbb3..bc95821feadb 100644 --- a/extensions-contrib/grpc-query/src/test/java/org/apache/druid/grpc/GrpcQueryTest.java +++ b/extensions-contrib/grpc-query/src/test/java/org/apache/druid/grpc/GrpcQueryTest.java @@ -24,7 +24,7 @@ import org.apache.druid.grpc.proto.QueryOuterClass.QueryResponse; import org.apache.druid.grpc.proto.QueryOuterClass.QueryResultFormat; import org.apache.druid.grpc.proto.QueryOuterClass.QueryStatus; -import org.apache.druid.grpc.proto.TestResults; +import org.apache.druid.grpc.proto.TestResults.QueryResult; import org.apache.druid.grpc.server.QueryDriver; import org.apache.druid.grpc.server.QueryServer; import org.junit.AfterClass; @@ -104,19 +104,34 @@ public void testBasics() * Do a very basic query that output protobuf. */ @Test - public void testGRPCBasics() + public void testGrpcBasics() { QueryRequest request = QueryRequest.newBuilder() .setQuery("SELECT dim1, dim2, dim3, cnt, m1, m2, unique_dim1, __time AS \"date\" FROM foo") - .setProtobufMessageName(TestResults.QueryResult.class.getName()) + .setProtobufMessageName(QueryResult.class.getName()) .setResultFormat(QueryResultFormat.PROTOBUF_INLINE) .build(); QueryResponse response = client.client.submitQuery(request); - GrpcResponseHandler handler = GrpcResponseHandler.of(TestResults.QueryResult.class); - List queryResults = handler.get(response.getData()); - + GrpcResponseHandler handler = GrpcResponseHandler.of(QueryResult.class); + List queryResults = handler.get(response.getData()); assertEquals(6, queryResults.size()); assertEquals(QueryStatus.OK, response.getStatus()); } + + @Test + public void testGrpcEmptyResponse() + { + QueryRequest request = QueryRequest.newBuilder() + .setQuery("SELECT dim1, dim2, dim3, cnt, m1, m2, unique_dim1, __time AS \"date\" FROM foo where cnt = 100000") + .setProtobufMessageName(QueryResult.class.getName()) + .setResultFormat(QueryResultFormat.PROTOBUF_INLINE) + .build(); + + QueryResponse response = client.client.submitQuery(request); + GrpcResponseHandler handler = GrpcResponseHandler.of(QueryResult.class); + List queryResults = handler.get(response.getData()); + assertEquals(0, queryResults.size()); + assertEquals(QueryStatus.OK, response.getStatus()); + } } diff --git a/extensions-contrib/grpc-query/src/test/java/org/apache/druid/grpc/client/GrpcResponseHandlerTest.java b/extensions-contrib/grpc-query/src/test/java/org/apache/druid/grpc/client/GrpcResponseHandlerTest.java new file mode 100644 index 000000000000..af7a33ae2c4d --- /dev/null +++ b/extensions-contrib/grpc-query/src/test/java/org/apache/druid/grpc/client/GrpcResponseHandlerTest.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.grpc.client; + +import com.google.protobuf.ByteString; +import org.apache.druid.grpc.proto.QueryOuterClass.QueryResponse; +import org.apache.druid.grpc.proto.TestResults.QueryResult; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class GrpcResponseHandlerTest +{ + private static List EXPECTED_RESULTS = new ArrayList<>(); + + @BeforeClass + public static void setup() + { + EXPECTED_RESULTS.add(QueryResult.newBuilder().setDim1("test").setCnt(100).build()); + EXPECTED_RESULTS.add(QueryResult.newBuilder().setDim2("test2").setCnt(100).setM2(200.10).build()); + } + + @Test + public void testEmptyResponse() + { + GrpcResponseHandler handler = GrpcResponseHandler.of(QueryResult.class); + List queryResults = handler.get(ByteString.EMPTY); + assertTrue(queryResults.isEmpty()); + } + + @Test + public void testNonEmptysponse() + { + GrpcResponseHandler handler = GrpcResponseHandler.of(QueryResult.class); + QueryResponse queryResponse = getQueryResponse(); + List queryResults = handler.get(queryResponse.getData()); + assertEquals(2, queryResults.size()); + assertEquals(EXPECTED_RESULTS, queryResults); + } + + private static QueryResponse getQueryResponse() + { + try { + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + for (QueryResult queryResult : EXPECTED_RESULTS) { + queryResult.writeDelimitedTo(out); + } + return QueryResponse.newBuilder() + .setData(ByteString.copyFrom(out.toByteArray())) + .build(); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } +}