|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + */ |
| 22 | +package com.influxdb.v3.client.internal; |
| 23 | + |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import io.grpc.internal.GrpcUtil; |
| 27 | +import org.apache.arrow.flight.CallHeaders; |
| 28 | +import org.apache.arrow.flight.CallInfo; |
| 29 | +import org.apache.arrow.flight.CallStatus; |
| 30 | +import org.apache.arrow.flight.FlightClient; |
| 31 | +import org.apache.arrow.flight.FlightClientMiddleware; |
| 32 | +import org.apache.arrow.flight.FlightServer; |
| 33 | +import org.apache.arrow.flight.Location; |
| 34 | +import org.apache.arrow.flight.NoOpFlightProducer; |
| 35 | +import org.apache.arrow.memory.RootAllocator; |
| 36 | +import org.assertj.core.api.Assertions; |
| 37 | +import org.junit.jupiter.api.AfterEach; |
| 38 | +import org.junit.jupiter.api.BeforeEach; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | + |
| 41 | +import com.influxdb.v3.client.config.ClientConfig; |
| 42 | +import com.influxdb.v3.client.query.QueryType; |
| 43 | + |
| 44 | +public class FlightSqlClientTest { |
| 45 | + |
| 46 | + private static final String LOCALHOST = "localhost"; |
| 47 | + private final Location grpcLocation = Location.forGrpcInsecure(LOCALHOST, 0); |
| 48 | + private final String serverLocation = String.format("http://%s:%d", LOCALHOST, grpcLocation.getUri().getPort()); |
| 49 | + |
| 50 | + private final CallHeadersMiddleware callHeadersMiddleware = new CallHeadersMiddleware(); |
| 51 | + |
| 52 | + private RootAllocator allocator; |
| 53 | + private FlightServer server; |
| 54 | + private FlightClient client; |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + void reset() { |
| 58 | + callHeadersMiddleware.headers = null; |
| 59 | + } |
| 60 | + |
| 61 | + @BeforeEach |
| 62 | + void setUp() throws Exception { |
| 63 | + allocator = new RootAllocator(Long.MAX_VALUE); |
| 64 | + server = FlightServer.builder(allocator, grpcLocation, new NoOpFlightProducer()).build().start(); |
| 65 | + client = FlightClient.builder(allocator, server.getLocation()).intercept(callHeadersMiddleware).build(); |
| 66 | + callHeadersMiddleware.headers = null; |
| 67 | + } |
| 68 | + |
| 69 | + @AfterEach |
| 70 | + void tearDown() throws Exception { |
| 71 | + if (client != null) { |
| 72 | + client.close(); |
| 73 | + } |
| 74 | + if (server != null) { |
| 75 | + server.shutdown(); |
| 76 | + server.awaitTermination(); |
| 77 | + } |
| 78 | + if (allocator != null) { |
| 79 | + allocator.close(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void callHeaders() throws Exception { |
| 85 | + ClientConfig clientConfig = new ClientConfig.Builder() |
| 86 | + .host(serverLocation) |
| 87 | + .token("my-token".toCharArray()) |
| 88 | + .build(); |
| 89 | + |
| 90 | + try (FlightSqlClient flightSqlClient = new FlightSqlClient(clientConfig, client)) { |
| 91 | + |
| 92 | + flightSqlClient.execute("select * from cpu", "mydb", QueryType.SQL, Map.of()); |
| 93 | + |
| 94 | + final CallHeaders incomingHeaders = callHeadersMiddleware.headers; |
| 95 | + |
| 96 | + Assertions.assertThat(incomingHeaders.keys()).containsOnly( |
| 97 | + "authorization", |
| 98 | + GrpcUtil.MESSAGE_ACCEPT_ENCODING |
| 99 | + ); |
| 100 | + Assertions.assertThat(incomingHeaders.get("authorization")).isEqualTo("Bearer my-token"); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void callHeadersCustomHeader() throws Exception { |
| 106 | + ClientConfig clientConfig = new ClientConfig.Builder() |
| 107 | + .host(serverLocation) |
| 108 | + .token("my-token".toCharArray()) |
| 109 | + .headers(Map.of("X-Tracing-Id", "123")) |
| 110 | + .build(); |
| 111 | + |
| 112 | + try (FlightSqlClient flightSqlClient = new FlightSqlClient(clientConfig, client)) { |
| 113 | + |
| 114 | + flightSqlClient.execute("select * from cpu", "mydb", QueryType.SQL, Map.of()); |
| 115 | + |
| 116 | + final CallHeaders incomingHeaders = callHeadersMiddleware.headers; |
| 117 | + |
| 118 | + Assertions.assertThat(incomingHeaders.keys()).containsOnly( |
| 119 | + "authorization", |
| 120 | + "x-tracing-id", |
| 121 | + GrpcUtil.MESSAGE_ACCEPT_ENCODING |
| 122 | + ); |
| 123 | + Assertions.assertThat(incomingHeaders.get("X-Tracing-Id")).isEqualTo("123"); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + static class CallHeadersMiddleware implements FlightClientMiddleware.Factory { |
| 128 | + CallHeaders headers; |
| 129 | + |
| 130 | + @Override |
| 131 | + public FlightClientMiddleware onCallStarted(final CallInfo info) { |
| 132 | + return new FlightClientMiddleware() { |
| 133 | + @Override |
| 134 | + public void onBeforeSendingHeaders(final CallHeaders outgoingHeaders) { |
| 135 | + headers = outgoingHeaders; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void onHeadersReceived(final CallHeaders incomingHeaders) { |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void onCallCompleted(final CallStatus status) { |
| 144 | + } |
| 145 | + }; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments