|
| 1 | +package io.tarantool.driver.integration; |
| 2 | + |
| 3 | +import io.tarantool.driver.api.TarantoolClientFactory; |
| 4 | +import io.tarantool.driver.api.TarantoolServerAddress; |
| 5 | +import io.tarantool.driver.api.TarantoolClient; |
| 6 | +import io.tarantool.driver.api.TarantoolClientConfig; |
| 7 | +import io.tarantool.driver.api.TarantoolClusterAddressProvider; |
| 8 | +import io.tarantool.driver.api.TarantoolResult; |
| 9 | +import io.tarantool.driver.api.space.TarantoolSpaceOperations; |
| 10 | +import io.tarantool.driver.api.tuple.DefaultTarantoolTupleFactory; |
| 11 | +import io.tarantool.driver.api.tuple.TarantoolTuple; |
| 12 | +import io.tarantool.driver.api.tuple.TarantoolTupleFactory; |
| 13 | +import io.tarantool.driver.auth.SimpleTarantoolCredentials; |
| 14 | +import io.tarantool.driver.auth.TarantoolCredentials; |
| 15 | +import io.tarantool.driver.cluster.BinaryClusterDiscoveryEndpoint; |
| 16 | +import io.tarantool.driver.cluster.BinaryDiscoveryClusterAddressProvider; |
| 17 | +import io.tarantool.driver.cluster.TarantoolClusterDiscoveryConfig; |
| 18 | +import io.tarantool.driver.cluster.TestWrappedClusterAddressProvider; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.concurrent.ExecutionException; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 27 | + |
| 28 | +public class ProxyTarantoolClientWithAddressProviderExampleIT extends SharedCartridgeContainer { |
| 29 | + |
| 30 | + private static TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> client; |
| 31 | + |
| 32 | + public static TarantoolCredentials credentials; |
| 33 | + |
| 34 | + private static final String SPACE_NAME = "test__profile"; |
| 35 | + |
| 36 | + @BeforeAll |
| 37 | + public static void setUp() throws Exception { |
| 38 | + startCluster(); |
| 39 | + initClient(); |
| 40 | + truncateSpace(SPACE_NAME); |
| 41 | + } |
| 42 | + |
| 43 | + private static TarantoolClusterAddressProvider getClusterAddressProvider() { |
| 44 | + TarantoolClientConfig config = TarantoolClientConfig.builder() |
| 45 | + .withCredentials(credentials) |
| 46 | + .build(); |
| 47 | + |
| 48 | + BinaryClusterDiscoveryEndpoint endpoint = new BinaryClusterDiscoveryEndpoint.Builder() |
| 49 | + // Config for connecting to instance with entry function |
| 50 | + .withClientConfig(config) |
| 51 | + // Name of a function that returns a pool of addresses to connect to |
| 52 | + .withEntryFunction("get_routers") |
| 53 | + // Setting first router URI as entry point |
| 54 | + .withEndpointProvider(() -> Collections.singletonList( |
| 55 | + new TarantoolServerAddress(container.getRouterHost(), container.getRouterPort()))) |
| 56 | + .build(); |
| 57 | + |
| 58 | + TarantoolClusterDiscoveryConfig clusterDiscoveryConfig = new TarantoolClusterDiscoveryConfig.Builder() |
| 59 | + .withEndpoint(endpoint) |
| 60 | + .withDelay(1) |
| 61 | + .build(); |
| 62 | + |
| 63 | + // TestWrappedClusterAddressProvider changes ports provided by address providers to docker mapped ports |
| 64 | + // You need to use TestWrappedClusterAddressProvider only if you run Tarantool in TestContainers |
| 65 | + return new TestWrappedClusterAddressProvider( |
| 66 | + new BinaryDiscoveryClusterAddressProvider(clusterDiscoveryConfig), |
| 67 | + container); |
| 68 | + } |
| 69 | + |
| 70 | + public static void initClient() { |
| 71 | + // For connecting to a Cartridge application, use the value of cluster_cookie parameter in the init.lua file |
| 72 | + credentials = new SimpleTarantoolCredentials(container.getUsername(), |
| 73 | + container.getPassword()); |
| 74 | + |
| 75 | + client = TarantoolClientFactory.createClient() |
| 76 | + // You don't have to set the routers addresses yourself address provider will do it for you |
| 77 | + .withAddressProvider(getClusterAddressProvider()) |
| 78 | + // For connecting to a Cartridge application, use the value of cluster_cookie parameter in the init.lua file |
| 79 | + .withCredentials(credentials) |
| 80 | + // Specify using the default CRUD proxy operations mapping configuration |
| 81 | + .withProxyMethodMapping() |
| 82 | + .build(); |
| 83 | + } |
| 84 | + |
| 85 | + private static void truncateSpace(String spaceName) { |
| 86 | + client.space(spaceName).truncate().join(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void insertOneTuple() throws ExecutionException, InterruptedException { |
| 91 | + TarantoolSpaceOperations<TarantoolTuple, TarantoolResult<TarantoolTuple>> space = client.space(SPACE_NAME); |
| 92 | + |
| 93 | + // Use TarantoolTupleFactory for instantiating new tuples |
| 94 | + TarantoolTupleFactory tupleFactory = new DefaultTarantoolTupleFactory( |
| 95 | + client.getConfig().getMessagePackMapper()); |
| 96 | + |
| 97 | + TarantoolResult<TarantoolTuple> insertTuples = space.insert(tupleFactory.create(1, null, "FIO", 50, 100)).get(); |
| 98 | + |
| 99 | + assertEquals(insertTuples.size(), 1); |
| 100 | + TarantoolTuple tuple = insertTuples.get(0); |
| 101 | + assertEquals(tuple.size(), 5); |
| 102 | + assertEquals(tuple.getInteger(0), 1); |
| 103 | + assertNotNull(tuple.getInteger(1)); //bucket_id |
| 104 | + assertEquals(tuple.getString(2), "FIO"); |
| 105 | + assertEquals(tuple.getInteger(3), 50); |
| 106 | + assertEquals(tuple.getInteger(4), 100); |
| 107 | + } |
| 108 | +} |
0 commit comments