|
1 | 1 | package com.clickhouse.client.api; |
2 | 2 |
|
3 | | -import org.apache.hc.core5.net.URIBuilder; |
4 | 3 | import org.testng.annotations.Test; |
5 | 4 |
|
6 | | -import java.net.URI; |
7 | | -import java.net.URISyntaxException; |
8 | 5 | import java.util.HashMap; |
9 | 6 | import java.util.Map; |
10 | 7 |
|
11 | 8 | import static org.testng.Assert.assertEquals; |
12 | | -import static org.testng.Assert.assertTrue; |
| 9 | +import static org.testng.Assert.assertNotNull; |
13 | 10 |
|
14 | 11 | /** |
15 | 12 | * Unit tests for custom URL path configuration feature. |
16 | | - * Tests that custom paths are correctly appended to endpoint URLs. |
| 13 | + * Tests that the configuration property and builder method work correctly. |
17 | 14 | */ |
18 | 15 | public class CustomURLPathTest { |
19 | 16 |
|
20 | | - /** |
21 | | - * Helper method to build URI with custom path, simulating the logic from HttpAPIClientHelper. |
22 | | - */ |
23 | | - private URI buildURIWithCustomPath(String baseURL, String customPath) throws URISyntaxException { |
24 | | - Map<String, Object> requestConfig = new HashMap<>(); |
25 | | - if (customPath != null) { |
26 | | - requestConfig.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), customPath); |
27 | | - } |
28 | | - |
29 | | - URIBuilder uriBuilder = new URIBuilder(baseURL); |
30 | | - |
31 | | - // Add custom URL path if configured |
32 | | - String configuredPath = (String) requestConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey()); |
33 | | - if (configuredPath != null && !configuredPath.isEmpty()) { |
34 | | - String existingPath = uriBuilder.getPath(); |
35 | | - if (existingPath == null || existingPath.isEmpty() || existingPath.equals("/")) { |
36 | | - uriBuilder.setPath(configuredPath); |
37 | | - } else { |
38 | | - uriBuilder.setPath(existingPath + configuredPath); |
39 | | - } |
40 | | - } |
41 | | - |
42 | | - return uriBuilder.normalizeSyntax().build(); |
| 17 | + @Test(groups = {"unit"}) |
| 18 | + public void testClientConfigPropertiesHasCustomURLPath() { |
| 19 | + // Test that CUSTOM_URL_PATH property exists and has correct key |
| 20 | + assertEquals(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), "custom_url_path"); |
| 21 | + assertEquals(ClientConfigProperties.CUSTOM_URL_PATH.getDefaultValue(), ""); |
43 | 22 | } |
44 | 23 |
|
45 | 24 | @Test(groups = {"unit"}) |
46 | | - public void testCustomURLPathConfiguration() { |
| 25 | + public void testClientBuilderCustomURLPathMethod() { |
| 26 | + // Test that the builder method exists and sets configuration correctly |
| 27 | + // We create a minimal client configuration to test the builder method |
47 | 28 | try { |
48 | | - URI uri = buildURIWithCustomPath("http://localhost:8123", "/sales/db"); |
| 29 | + Client.Builder builder = new Client.Builder() |
| 30 | + .addEndpoint("http://localhost:8123") |
| 31 | + .setUsername("default") |
| 32 | + .setPassword("") |
| 33 | + .customURLPath("/sales/db"); |
49 | 34 |
|
50 | | - assertEquals(uri.toString(), "http://localhost:8123/sales/db"); |
51 | | - assertEquals(uri.getPath(), "/sales/db"); |
52 | | - } catch (URISyntaxException e) { |
53 | | - throw new RuntimeException(e); |
| 35 | + // Build client to verify configuration is set |
| 36 | + Client client = builder.build(); |
| 37 | + try { |
| 38 | + // Verify configuration was set correctly |
| 39 | + Map<String, String> config = client.getConfiguration(); |
| 40 | + assertNotNull(config); |
| 41 | + assertEquals(config.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey()), "/sales/db"); |
| 42 | + } finally { |
| 43 | + client.close(); |
| 44 | + } |
| 45 | + } catch (Exception e) { |
| 46 | + throw new RuntimeException("Failed to test customURLPath builder method", e); |
54 | 47 | } |
55 | 48 | } |
56 | 49 |
|
57 | 50 | @Test(groups = {"unit"}) |
58 | | - public void testCustomURLPathWithExistingPath() { |
59 | | - try { |
60 | | - URI uri = buildURIWithCustomPath("http://localhost:8123/api", "/app/db"); |
61 | | - |
62 | | - assertEquals(uri.toString(), "http://localhost:8123/api/app/db"); |
63 | | - assertEquals(uri.getPath(), "/api/app/db"); |
64 | | - } catch (URISyntaxException e) { |
65 | | - throw new RuntimeException(e); |
66 | | - } |
| 51 | + public void testClientConfigPropertyParsing() { |
| 52 | + // Test that the configuration property can be parsed correctly |
| 53 | + Map<String, String> config = new HashMap<>(); |
| 54 | + config.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), "/sales/db"); |
| 55 | + |
| 56 | + Map<String, Object> parsedConfig = ClientConfigProperties.parseConfigMap(config); |
| 57 | + |
| 58 | + String customPath = (String) parsedConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey()); |
| 59 | + assertEquals(customPath, "/sales/db"); |
67 | 60 | } |
68 | 61 |
|
69 | 62 | @Test(groups = {"unit"}) |
70 | 63 | public void testEmptyCustomURLPath() { |
71 | | - try { |
72 | | - URI uri = buildURIWithCustomPath("http://localhost:8123", ""); |
73 | | - |
74 | | - // Empty path should not modify the URL |
75 | | - assertEquals(uri.toString(), "http://localhost:8123"); |
76 | | - } catch (URISyntaxException e) { |
77 | | - throw new RuntimeException(e); |
78 | | - } |
| 64 | + // Test with empty custom path |
| 65 | + Map<String, String> config = new HashMap<>(); |
| 66 | + config.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), ""); |
| 67 | + |
| 68 | + Map<String, Object> parsedConfig = ClientConfigProperties.parseConfigMap(config); |
| 69 | + |
| 70 | + String customPath = (String) parsedConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey()); |
| 71 | + assertEquals(customPath, ""); |
79 | 72 | } |
80 | 73 |
|
81 | 74 | @Test(groups = {"unit"}) |
82 | | - public void testNoCustomURLPath() { |
83 | | - try { |
84 | | - URI uri = buildURIWithCustomPath("http://localhost:8123", null); |
85 | | - |
86 | | - // No custom path should keep URL unchanged |
87 | | - assertEquals(uri.toString(), "http://localhost:8123"); |
88 | | - } catch (URISyntaxException e) { |
89 | | - throw new RuntimeException(e); |
| 75 | + public void testNoCustomURLPathConfiguration() { |
| 76 | + // Test without custom path configured - should use default |
| 77 | + Map<String, String> config = new HashMap<>(); |
| 78 | + // Don't set CUSTOM_URL_PATH |
| 79 | + |
| 80 | + Map<String, Object> parsedConfig = ClientConfigProperties.parseConfigMap(config); |
| 81 | + |
| 82 | + // Should not be in parsed config if not provided |
| 83 | + Object customPath = parsedConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey()); |
| 84 | + // Either null or empty string is acceptable for unset value |
| 85 | + if (customPath != null) { |
| 86 | + assertEquals(customPath, ""); |
90 | 87 | } |
91 | 88 | } |
92 | 89 |
|
93 | 90 | @Test(groups = {"unit"}) |
94 | | - public void testClientBuilderCustomURLPath() { |
95 | | - // Test that the builder method sets the configuration correctly |
96 | | - Map<String, String> config = new HashMap<>(); |
97 | | - config.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), "/sales/db"); |
98 | | - |
99 | | - Map<String, Object> parsedConfig = ClientConfigProperties.parseConfigMap(config); |
| 91 | + public void testCustomURLPathWithDifferentPaths() { |
| 92 | + // Test various path formats |
| 93 | + String[] testPaths = { |
| 94 | + "/sales/db", |
| 95 | + "/app/db", |
| 96 | + "/custom", |
| 97 | + "/a/b/c/d", |
| 98 | + "/123/456" |
| 99 | + }; |
100 | 100 |
|
101 | | - String customPath = (String) parsedConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey()); |
102 | | - assertEquals(customPath, "/sales/db"); |
| 101 | + for (String testPath : testPaths) { |
| 102 | + Map<String, String> config = new HashMap<>(); |
| 103 | + config.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), testPath); |
| 104 | + |
| 105 | + Map<String, Object> parsedConfig = ClientConfigProperties.parseConfigMap(config); |
| 106 | + |
| 107 | + String customPath = (String) parsedConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey()); |
| 108 | + assertEquals(customPath, testPath, "Failed for path: " + testPath); |
| 109 | + } |
103 | 110 | } |
104 | 111 | } |
0 commit comments