|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package com.hedera.hashgraph.sdk; |
| 3 | + |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 6 | + |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.ExecutorService; |
| 10 | +import java.util.concurrent.Executors; |
| 11 | +import org.junit.jupiter.api.AfterEach; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +/** |
| 16 | + * Replacement for MirrorNodeUrlBuilderTest validating base URL generation via Client/MirrorNode. |
| 17 | + */ |
| 18 | +public class ClientMirrorBaseUrlTest { |
| 19 | + |
| 20 | + private ExecutorService executor; |
| 21 | + |
| 22 | + @BeforeEach |
| 23 | + void setUp() { |
| 24 | + executor = Executors.newSingleThreadExecutor(); |
| 25 | + } |
| 26 | + |
| 27 | + @AfterEach |
| 28 | + void tearDown() { |
| 29 | + if (executor != null) { |
| 30 | + executor.shutdown(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void hostPort_customPort_preserved_https_whenLedgerSet() { |
| 36 | + var network = Network.forNetwork(executor, new HashMap<>()); |
| 37 | + var mirrorNetwork = MirrorNetwork.forNetwork(executor, List.of("mirror.example.com:8080")); |
| 38 | + var client = new Client(executor, network, mirrorNetwork, null, true, null, 0, 0); |
| 39 | + client.setLedgerId(LedgerId.TESTNET); |
| 40 | + |
| 41 | + String base = client.getMirrorRestBaseUrl(); |
| 42 | + assertThat(base).isEqualTo("https://mirror.example.com:8080/api/v1"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void hostPort_defaultHttpsPort_omitted() { |
| 47 | + var network = Network.forNetwork(executor, new HashMap<>()); |
| 48 | + var mirrorNetwork = MirrorNetwork.forNetwork(executor, List.of("mirror.example.com:443")); |
| 49 | + var client = new Client(executor, network, mirrorNetwork, null, true, null, 0, 0); |
| 50 | + client.setLedgerId(LedgerId.TESTNET); |
| 51 | + |
| 52 | + String base = client.getMirrorRestBaseUrl(); |
| 53 | + assertThat(base).isEqualTo("https://mirror.example.com/api/v1"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void localNetwork_regularQuery_uses5551_http() { |
| 58 | + var network = Network.forNetwork(executor, new HashMap<>()); |
| 59 | + var mirrorNetwork = MirrorNetwork.forNetwork(executor, List.of("localhost:8080")); |
| 60 | + var client = new Client(executor, network, mirrorNetwork, null, true, null, 0, 0); |
| 61 | + // No ledger id -> local |
| 62 | + |
| 63 | + String base = client.getMirrorRestBaseUrl(); |
| 64 | + assertThat(base).isEqualTo("http://localhost:5551/api/v1"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void localNetwork_contractCall_uses5551_http() { |
| 69 | + var network = Network.forNetwork(executor, new HashMap<>()); |
| 70 | + var mirrorNetwork = MirrorNetwork.forNetwork(executor, List.of("127.0.0.1:8080")); |
| 71 | + var client = new Client(executor, network, mirrorNetwork, null, true, null, 0, 0); |
| 72 | + // No ledger id -> local |
| 73 | + |
| 74 | + String base = client.getMirrorRestBaseUrl(); |
| 75 | + assertThat(base).isEqualTo("http://127.0.0.1:5551/api/v1"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void emptyMirrorNetwork_throws_whenAccessingBase() { |
| 80 | + var network = Network.forNetwork(executor, new HashMap<>()); |
| 81 | + var mirrorNetwork = MirrorNetwork.forNetwork(executor, List.of()); |
| 82 | + var client = new Client(executor, network, mirrorNetwork, null, true, null, 0, 0); |
| 83 | + |
| 84 | + assertThatThrownBy(() -> client.getMirrorRestBaseUrl()).isInstanceOf(RuntimeException.class); |
| 85 | + } |
| 86 | +} |
0 commit comments