|
| 1 | +package com.clickhouse.client.api; |
| 2 | + |
| 3 | +import org.apache.hc.core5.net.URIBuilder; |
| 4 | +import org.testng.annotations.Test; |
| 5 | + |
| 6 | +import java.net.URI; |
| 7 | +import java.net.URISyntaxException; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import static org.testng.Assert.assertEquals; |
| 12 | +import static org.testng.Assert.assertTrue; |
| 13 | + |
| 14 | +/** |
| 15 | + * Unit tests for custom URL path configuration feature. |
| 16 | + * Tests that custom paths are correctly appended to endpoint URLs. |
| 17 | + */ |
| 18 | +public class CustomURLPathTest { |
| 19 | + |
| 20 | + @Test(groups = {"unit"}) |
| 21 | + public void testCustomURLPathConfiguration() { |
| 22 | + String customPath = "/sales/db"; |
| 23 | + |
| 24 | + // Simulate the URL building logic from HttpAPIClientHelper |
| 25 | + String baseURL = "http://localhost:8123"; |
| 26 | + Map<String, Object> requestConfig = new HashMap<>(); |
| 27 | + requestConfig.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), customPath); |
| 28 | + |
| 29 | + try { |
| 30 | + URIBuilder uriBuilder = new URIBuilder(baseURL); |
| 31 | + |
| 32 | + // Add custom URL path if configured |
| 33 | + String configuredPath = (String) requestConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey()); |
| 34 | + if (configuredPath != null && !configuredPath.isEmpty()) { |
| 35 | + String existingPath = uriBuilder.getPath(); |
| 36 | + if (existingPath == null || existingPath.isEmpty() || existingPath.equals("/")) { |
| 37 | + uriBuilder.setPath(configuredPath); |
| 38 | + } else { |
| 39 | + uriBuilder.setPath(existingPath + configuredPath); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + URI uri = uriBuilder.normalizeSyntax().build(); |
| 44 | + |
| 45 | + assertEquals(uri.toString(), "http://localhost:8123/sales/db"); |
| 46 | + assertEquals(uri.getPath(), "/sales/db"); |
| 47 | + } catch (URISyntaxException e) { |
| 48 | + throw new RuntimeException(e); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Test(groups = {"unit"}) |
| 53 | + public void testCustomURLPathWithExistingPath() { |
| 54 | + String customPath = "/app/db"; |
| 55 | + |
| 56 | + // Test with base URL that already has a path |
| 57 | + String baseURL = "http://localhost:8123/api"; |
| 58 | + Map<String, Object> requestConfig = new HashMap<>(); |
| 59 | + requestConfig.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), customPath); |
| 60 | + |
| 61 | + try { |
| 62 | + URIBuilder uriBuilder = new URIBuilder(baseURL); |
| 63 | + |
| 64 | + // Add custom URL path if configured |
| 65 | + String configuredPath = (String) requestConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey()); |
| 66 | + if (configuredPath != null && !configuredPath.isEmpty()) { |
| 67 | + String existingPath = uriBuilder.getPath(); |
| 68 | + if (existingPath == null || existingPath.isEmpty() || existingPath.equals("/")) { |
| 69 | + uriBuilder.setPath(configuredPath); |
| 70 | + } else { |
| 71 | + uriBuilder.setPath(existingPath + configuredPath); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + URI uri = uriBuilder.normalizeSyntax().build(); |
| 76 | + |
| 77 | + assertEquals(uri.toString(), "http://localhost:8123/api/app/db"); |
| 78 | + assertEquals(uri.getPath(), "/api/app/db"); |
| 79 | + } catch (URISyntaxException e) { |
| 80 | + throw new RuntimeException(e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test(groups = {"unit"}) |
| 85 | + public void testEmptyCustomURLPath() { |
| 86 | + // Test with empty custom path |
| 87 | + String baseURL = "http://localhost:8123"; |
| 88 | + Map<String, Object> requestConfig = new HashMap<>(); |
| 89 | + requestConfig.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), ""); |
| 90 | + |
| 91 | + try { |
| 92 | + URIBuilder uriBuilder = new URIBuilder(baseURL); |
| 93 | + |
| 94 | + // Add custom URL path if configured |
| 95 | + String configuredPath = (String) requestConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey()); |
| 96 | + if (configuredPath != null && !configuredPath.isEmpty()) { |
| 97 | + String existingPath = uriBuilder.getPath(); |
| 98 | + if (existingPath == null || existingPath.isEmpty() || existingPath.equals("/")) { |
| 99 | + uriBuilder.setPath(configuredPath); |
| 100 | + } else { |
| 101 | + uriBuilder.setPath(existingPath + configuredPath); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + URI uri = uriBuilder.normalizeSyntax().build(); |
| 106 | + |
| 107 | + // Empty path should not modify the URL |
| 108 | + assertEquals(uri.toString(), "http://localhost:8123"); |
| 109 | + } catch (URISyntaxException e) { |
| 110 | + throw new RuntimeException(e); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Test(groups = {"unit"}) |
| 115 | + public void testNoCustomURLPath() { |
| 116 | + // Test without custom path configured |
| 117 | + String baseURL = "http://localhost:8123"; |
| 118 | + Map<String, Object> requestConfig = new HashMap<>(); |
| 119 | + |
| 120 | + try { |
| 121 | + URIBuilder uriBuilder = new URIBuilder(baseURL); |
| 122 | + |
| 123 | + // Add custom URL path if configured |
| 124 | + String configuredPath = (String) requestConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey()); |
| 125 | + if (configuredPath != null && !configuredPath.isEmpty()) { |
| 126 | + String existingPath = uriBuilder.getPath(); |
| 127 | + if (existingPath == null || existingPath.isEmpty() || existingPath.equals("/")) { |
| 128 | + uriBuilder.setPath(configuredPath); |
| 129 | + } else { |
| 130 | + uriBuilder.setPath(existingPath + configuredPath); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + URI uri = uriBuilder.normalizeSyntax().build(); |
| 135 | + |
| 136 | + // No custom path should keep URL unchanged |
| 137 | + assertEquals(uri.toString(), "http://localhost:8123"); |
| 138 | + } catch (URISyntaxException e) { |
| 139 | + throw new RuntimeException(e); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Test(groups = {"unit"}) |
| 144 | + public void testClientBuilderCustomURLPath() { |
| 145 | + // Test that the builder method sets the configuration correctly |
| 146 | + Map<String, String> config = new HashMap<>(); |
| 147 | + config.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), "/sales/db"); |
| 148 | + |
| 149 | + Map<String, Object> parsedConfig = ClientConfigProperties.parseConfigMap(config); |
| 150 | + |
| 151 | + String customPath = (String) parsedConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey()); |
| 152 | + assertEquals(customPath, "/sales/db"); |
| 153 | + } |
| 154 | +} |
0 commit comments