|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 VK DIGITAL TECHNOLOGIES LIMITED LIABILITY COMPANY |
| 3 | + * All Rights Reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +package client; |
| 7 | + |
| 8 | +// --8<-- [start:all] |
| 9 | + |
| 10 | +import java.net.InetSocketAddress; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 17 | +import org.junit.jupiter.api.Assertions; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import testcontainers.utils.TarantoolSingleNodeConfigUtils; |
| 20 | + |
| 21 | +import io.tarantool.client.TarantoolClient; |
| 22 | +import io.tarantool.client.factory.TarantoolFactory; |
| 23 | +import io.tarantool.pool.InstanceConnectionGroup; |
| 24 | + |
| 25 | +public class TarantoolCallTJSDKExample extends TarantoolCallEvalAbstractExample { |
| 26 | + |
| 27 | + @Test |
| 28 | + void simpleCall() { |
| 29 | + try (TarantoolClient client = setupClient()) { |
| 30 | + loadFunctions(); |
| 31 | + |
| 32 | + final List<String> helloWorldReturns = |
| 33 | + client.call(HELLO_WORLD_FUNCTION, String.class).join().get(); |
| 34 | + Assertions.assertEquals(1, helloWorldReturns.size()); |
| 35 | + Assertions.assertEquals(HELLO_WORLD_FUNCTION_RETURNS, helloWorldReturns.get(0)); |
| 36 | + |
| 37 | + // convert returning lua map into Java pojo representing as map |
| 38 | + final String name = "Petya"; |
| 39 | + final int age = 25; |
| 40 | + |
| 41 | + final List<TestUser> resultAsPojo = |
| 42 | + client.call(SOME_MAP_FUNCTION, Arrays.asList(name, age), TestUser.class).join().get(); |
| 43 | + |
| 44 | + Assertions.assertEquals(1, resultAsPojo.size()); |
| 45 | + Assertions.assertEquals(name, resultAsPojo.get(0).name()); |
| 46 | + Assertions.assertEquals(age, resultAsPojo.get(0).age()); |
| 47 | + |
| 48 | + // convert returning lua map into java list of map via type reference |
| 49 | + // java map key type is string because lua map key type is string! |
| 50 | + final List<Map<String, Object>> objectObjectMap = |
| 51 | + client |
| 52 | + .call( |
| 53 | + SOME_MAP_FUNCTION, |
| 54 | + Arrays.asList(name, age), |
| 55 | + new TypeReference<List<Map<String, Object>>>() {}) |
| 56 | + .join() |
| 57 | + .get(); |
| 58 | + |
| 59 | + } catch (Exception e) { |
| 60 | + throw new RuntimeException(e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static TarantoolClient setupClient() throws Exception { |
| 65 | + final InetSocketAddress address = CONTAINER.mappedAddress(); |
| 66 | + |
| 67 | + final InstanceConnectionGroup connectionGroup = |
| 68 | + InstanceConnectionGroup.builder() |
| 69 | + .withHost(address.getHostName()) |
| 70 | + .withPort(address.getPort()) |
| 71 | + .withUser(TarantoolSingleNodeConfigUtils.LOGIN) |
| 72 | + .withPassword(TarantoolSingleNodeConfigUtils.PWD.toString()) |
| 73 | + .withSize(3) |
| 74 | + .build(); |
| 75 | + |
| 76 | + return TarantoolFactory.box().withGroups(Collections.singletonList(connectionGroup)).build(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// --8<-- [end:all] |
0 commit comments