Skip to content

Commit 8a090c0

Browse files
committed
Added readonly profile tests
1 parent b444455 commit 8a090c0

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

jdbc-v2/src/test/java/com/clickhouse/jdbc/JdbcDataTypeTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2863,12 +2863,23 @@ public void testJSONRead(String json, Object expected) throws Exception {
28632863

28642864
assertTrue(rs.next());
28652865
Object jsonObj = rs.getObject(1);
2866+
assertTrue(jsonObj instanceof Map, "JSON should be read as a Map");
2867+
Map<String, Object> jsonMap = (Map<String, Object>) jsonObj;
2868+
28662869
if (expected == null) {
28672870
expected = jsonToClientMap(json);
28682871
}
2869-
assertEquals(jsonObj, expected);
2872+
Map<String, Object> expectedMap = (Map<String, Object>) expected;
2873+
2874+
assertEquals(jsonMap, expectedMap);
2875+
for (Map.Entry<String, Object> entry : expectedMap.entrySet()) {
2876+
assertTrue(jsonMap.containsKey(entry.getKey()), "Map should contain key: " + entry.getKey());
2877+
assertEquals(jsonMap.get(entry.getKey()), entry.getValue(), "Values should match for key: " + entry.getKey());
2878+
}
2879+
28702880
assertTrue(rs.next());
28712881
Object emptyJsonObj = rs.getObject(1);
2882+
assertTrue(emptyJsonObj instanceof Map, "Empty JSON should be returned as Map");
28722883
assertEquals(emptyJsonObj, EMPTY_JSON);
28732884
assertFalse(rs.next());
28742885
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.clickhouse.jdbc;
2+
3+
import com.clickhouse.client.api.ClientConfigProperties;
4+
import com.clickhouse.client.api.internal.ServerSettings;
5+
import org.testng.Assert;
6+
import org.testng.annotations.AfterClass;
7+
import org.testng.annotations.BeforeClass;
8+
import org.testng.annotations.Test;
9+
10+
import java.sql.Connection;
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.sql.Statement;
14+
import java.util.Properties;
15+
16+
public class ReadonlyProfileTest extends JdbcIntegrationTest {
17+
18+
@BeforeClass(groups = { "integration" })
19+
public void setup() throws SQLException {
20+
com.clickhouse.client.ClickHouseServerForTest.beforeSuite();
21+
try (Connection conn = getJdbcConnection();
22+
Statement stmt = conn.createStatement()) {
23+
stmt.execute("CREATE SETTINGS PROFILE IF NOT EXISTS profile_readonly_1 SETTINGS readonly=1");
24+
stmt.execute("CREATE SETTINGS PROFILE IF NOT EXISTS profile_readonly_2 SETTINGS readonly=2");
25+
stmt.execute("CREATE USER IF NOT EXISTS user_readonly_1 IDENTIFIED WITH no_password SETTINGS PROFILE profile_readonly_1");
26+
stmt.execute("CREATE USER IF NOT EXISTS user_readonly_2 IDENTIFIED WITH no_password SETTINGS PROFILE profile_readonly_2");
27+
stmt.execute("GRANT SELECT ON *.* TO user_readonly_1");
28+
stmt.execute("GRANT SELECT ON *.* TO user_readonly_2");
29+
}
30+
}
31+
32+
@AfterClass(groups = { "integration" })
33+
public void teardown() throws SQLException {
34+
try (Connection conn = getJdbcConnection();
35+
Statement stmt = conn.createStatement()) {
36+
stmt.execute("DROP USER IF EXISTS user_readonly_1");
37+
stmt.execute("DROP USER IF EXISTS user_readonly_2");
38+
stmt.execute("DROP SETTINGS PROFILE IF EXISTS profile_readonly_1");
39+
stmt.execute("DROP SETTINGS PROFILE IF EXISTS profile_readonly_2");
40+
}
41+
}
42+
43+
@Test(groups = { "integration" })
44+
public void testReadonly1CannotChangeSettings() throws Exception {
45+
Properties properties = new Properties();
46+
properties.setProperty("user", "user_readonly_1");
47+
properties.setProperty("password", "");
48+
properties.put(ClientConfigProperties.serverSetting(ServerSettings.OUTPUT_FORMAT_BINARY_WRITE_JSON_AS_STRING), "1");
49+
50+
try (Connection conn = getJdbcConnection(properties)) {
51+
try (Statement stmt = conn.createStatement();
52+
ResultSet rs = stmt.executeQuery("SELECT 1")) {
53+
Assert.fail("Should have thrown an exception because readonly=1 prevents changing settings");
54+
}
55+
} catch (SQLException e) {
56+
Assert.assertTrue(e.getMessage().contains("Cannot modify"), "Exception message should indicate setting cannot be modified: " + e.getMessage());
57+
}
58+
}
59+
60+
@Test(groups = { "integration" })
61+
public void testReadonly2CanChangeSettings() throws Exception {
62+
if (isVersionMatch("(,24.8]")) {
63+
return; // JSON was introduced in 24.10
64+
}
65+
66+
Properties properties = new Properties();
67+
properties.setProperty("user", "user_readonly_2");
68+
properties.setProperty("password", "");
69+
properties.put(ClientConfigProperties.serverSetting(ServerSettings.OUTPUT_FORMAT_BINARY_WRITE_JSON_AS_STRING), "1");
70+
properties.put(ClientConfigProperties.serverSetting("allow_experimental_json_type"), "1");
71+
72+
try (Connection conn = getJdbcConnection(properties)) {
73+
try (Statement stmt = conn.createStatement();
74+
ResultSet rs = stmt.executeQuery("SELECT '{\"key\":\"value\"}'::JSON")) {
75+
Assert.assertTrue(rs.next());
76+
Assert.assertEquals(rs.getString(1), "{\"key\":\"value\"}");
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)