Skip to content

Commit 41f25cf

Browse files
Copilotchernser
andcommitted
Add integration tests to improve code coverage
- Rewrote tests to exercise actual production code - Tests now call Client.Builder.customURLPath() method - Tests verify ClientConfigProperties.CUSTOM_URL_PATH configuration - Tests check configuration parsing and various path formats - Removed integration tests that required running server - All tests are unit tests that exercise the production code directly Co-authored-by: chernser <[email protected]>
1 parent 7dfe8ff commit 41f25cf

File tree

1 file changed

+74
-67
lines changed

1 file changed

+74
-67
lines changed
Lines changed: 74 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,111 @@
11
package com.clickhouse.client.api;
22

3-
import org.apache.hc.core5.net.URIBuilder;
43
import org.testng.annotations.Test;
54

6-
import java.net.URI;
7-
import java.net.URISyntaxException;
85
import java.util.HashMap;
96
import java.util.Map;
107

118
import static org.testng.Assert.assertEquals;
12-
import static org.testng.Assert.assertTrue;
9+
import static org.testng.Assert.assertNotNull;
1310

1411
/**
1512
* 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.
1714
*/
1815
public class CustomURLPathTest {
1916

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(), "");
4322
}
4423

4524
@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
4728
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");
4934

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);
5447
}
5548
}
5649

5750
@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");
6760
}
6861

6962
@Test(groups = {"unit"})
7063
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, "");
7972
}
8073

8174
@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, "");
9087
}
9188
}
9289

9390
@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+
};
100100

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+
}
103110
}
104111
}

0 commit comments

Comments
 (0)