Repository navigation
Expand file tree
/
Copy pathDataSourceTest.java
More file actions
222 lines (193 loc) · 9.39 KB
/
Copy pathDataSourceTest.java
File metadata and controls
222 lines (193 loc) · 9.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigquery.jdbc;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.mockStatic;
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException;
import com.google.cloud.bigquery.jdbc.utils.URIBuilder;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.MockedStatic;
public class DataSourceTest {
private static URIBuilder baseUrl() {
return BigQueryJdbcBaseTest.getUriOAuthToken().append("ProjectId", "test-project");
}
@Test
public void testWrapperMethods() throws SQLException {
DataSource dataSource = new DataSource();
assertTrue(dataSource.isWrapperFor(javax.sql.DataSource.class));
assertTrue(dataSource.isWrapperFor(DataSource.class));
assertFalse(dataSource.isWrapperFor(java.sql.Connection.class));
assertFalse(dataSource.isWrapperFor(null));
Object unwrappedDs = dataSource.unwrap(javax.sql.DataSource.class);
assertSame(unwrappedDs, dataSource);
Object unwrappedImpl = dataSource.unwrap(DataSource.class);
assertSame(unwrappedImpl, dataSource);
BigQueryJdbcException e =
assertThrows(
BigQueryJdbcException.class, () -> dataSource.unwrap(java.sql.Connection.class));
assertTrue(e.getMessage().contains("Cannot unwrap to java.sql.Connection"));
}
@Test
public void testMetadataFetchThreadCountValidation() {
DataSource dataSource = new DataSource();
// Setting positive values should succeed
dataSource.setMetadataFetchThreadCount(1);
assertEquals(1, dataSource.getMetadataFetchThreadCount());
dataSource.setMetadataFetchThreadCount(5);
assertEquals(5, dataSource.getMetadataFetchThreadCount());
dataSource.setMetadataFetchThreadCount(null); // Should fallback to default
assertEquals(
BigQueryJdbcUrlUtility.DEFAULT_METADATA_FETCH_THREAD_COUNT_VALUE,
dataSource.getMetadataFetchThreadCount());
// Setting 0 or negative should throw BigQueryJdbcRuntimeException
BigQueryJdbcRuntimeException ex0 =
assertThrows(
BigQueryJdbcRuntimeException.class, () -> dataSource.setMetadataFetchThreadCount(0));
assertTrue(
ex0.getMessage()
.contains(
"Invalid value for MetaDataFetchThreadCount. It must be greater than or equal to"
+ " 1"));
BigQueryJdbcRuntimeException exNeg =
assertThrows(
BigQueryJdbcRuntimeException.class, () -> dataSource.setMetadataFetchThreadCount(-5));
assertTrue(
exNeg
.getMessage()
.contains(
"Invalid value for MetaDataFetchThreadCount. It must be greater than or equal to"
+ " 1"));
}
@Test
public void testUniverseDomainAndByoidTokenUriResolution() {
try (MockedStatic<BigQueryJdbcUrlUtility> urlUtil =
mockStatic(BigQueryJdbcUrlUtility.class, CALLS_REAL_METHODS)) {
// 1. Neither URL/setter nor GOOGLE_CLOUD_UNIVERSE_DOMAIN is set -> null universeDomain
urlUtil.when(BigQueryJdbcUrlUtility::getEnvUniverseDomain).thenReturn(null);
DataSource ds = new DataSource();
assertNull(ds.getUniverseDomain());
assertNull(ds.resolveUniverseDomain());
assertEquals("https://sts.googleapis.com/v1/token", ds.getByoidTokenUri());
// 2. GOOGLE_CLOUD_UNIVERSE_DOMAIN is set, explicit universeDomain is omitted:
// getUniverseDomain() stays null (preserving setter symmetry), resolveUniverseDomain() uses
// env
urlUtil
.when(BigQueryJdbcUrlUtility::getEnvUniverseDomain)
.thenReturn("custom-universe.example.com");
assertNull(ds.getUniverseDomain());
assertEquals("custom-universe.example.com", ds.resolveUniverseDomain());
assertEquals("https://sts.googleapis.com/v1/token", ds.getByoidTokenUri());
// 3. Explicit universeDomain takes precedence over GOOGLE_CLOUD_UNIVERSE_DOMAIN
ds.setUniverseDomain("explicit-universe.example.com");
assertEquals("explicit-universe.example.com", ds.getUniverseDomain());
assertEquals("explicit-universe.example.com", ds.resolveUniverseDomain());
assertEquals("https://sts.explicit-universe.example.com/v1/token", ds.getByoidTokenUri());
// 4. Explicit BYOID_TokenUri takes precedence over universeDomain derivation
ds.setByoidTokenUri("https://custom-sts.internal/v1/token");
assertEquals("https://custom-sts.internal/v1/token", ds.getByoidTokenUri());
}
}
@Test
public void testNormalizeUniverseDomain() {
assertNull(BigQueryJdbcUrlUtility.normalizeUniverseDomain(null));
assertNull(BigQueryJdbcUrlUtility.normalizeUniverseDomain(""));
assertNull(BigQueryJdbcUrlUtility.normalizeUniverseDomain(" "));
assertEquals(
"custom-universe.example.com",
BigQueryJdbcUrlUtility.normalizeUniverseDomain(" custom-universe.example.com "));
assertNull(BigQueryJdbcUrlUtility.resolveEnvUniverseDomain(null));
assertEquals("googleapis.com", BigQueryJdbcUrlUtility.resolveEnvUniverseDomain(""));
assertEquals("googleapis.com", BigQueryJdbcUrlUtility.resolveEnvUniverseDomain(" "));
assertEquals(
"custom-universe.example.com",
BigQueryJdbcUrlUtility.resolveEnvUniverseDomain(" custom-universe.example.com "));
}
@ParameterizedTest
@ValueSource(longs = {-1L, -3_600_000L})
public void testDestinationDatasetExpirationTimeRejectsNegativeValues(long invalidValue) {
DataSource ds = new DataSource();
BigQueryJdbcRuntimeException setterEx =
assertThrows(
BigQueryJdbcRuntimeException.class,
() -> ds.setDestinationDatasetExpirationTime(invalidValue));
assertTrue(
setterEx
.getMessage()
.contains(
"Invalid value for LargeResultsDatasetExpirationTime. It must be greater than or"
+ " equal to 0."));
String invalidUrl =
baseUrl().append("LargeResultsDatasetExpirationTime", invalidValue).toString();
BigQueryJdbcRuntimeException urlEx =
assertThrows(BigQueryJdbcRuntimeException.class, () -> DataSource.fromUrl(invalidUrl));
assertTrue(
urlEx
.getMessage()
.contains(
"Invalid value for LargeResultsDatasetExpirationTime. It must be greater than or"
+ " equal to 0."));
BigQueryJdbcException driverEx =
assertThrows(BigQueryJdbcException.class, () -> DriverManager.getConnection(invalidUrl));
assertTrue(
driverEx
.getMessage()
.contains(
"Invalid value for LargeResultsDatasetExpirationTime. It must be greater than or"
+ " equal to 0."));
}
@Test
public void testDestinationDatasetExpirationTimeAllowsZeroForCompatibility() {
DataSource ds = new DataSource();
ds.setDestinationDatasetExpirationTime(0L);
assertEquals(0L, ds.getDestinationDatasetExpirationTime());
String zeroUrl = baseUrl().append("LargeResultsDatasetExpirationTime", 0L).toString();
DataSource parsed = DataSource.fromUrl(zeroUrl);
assertEquals(0L, parsed.getDestinationDatasetExpirationTime());
}
@ParameterizedTest
@ValueSource(longs = {1L, 600_000L, 3_599_999L})
public void testDestinationDatasetExpirationTimeClampsSubHourPositiveValuesToMinimum(
long subHourValue) {
long expectedMin = BigQueryJdbcUrlUtility.MIN_DESTINATION_DATASET_EXPIRATION_TIME_VALUE;
DataSource ds = new DataSource();
ds.setDestinationDatasetExpirationTime(subHourValue);
assertEquals(expectedMin, ds.getDestinationDatasetExpirationTime());
String url = baseUrl().append("LargeResultsDatasetExpirationTime", subHourValue).toString();
DataSource parsed = DataSource.fromUrl(url);
assertEquals(expectedMin, parsed.getDestinationDatasetExpirationTime());
}
@ParameterizedTest
@ValueSource(longs = {3_600_000L, 7_200_000L})
public void testDestinationDatasetExpirationTimePreservesAtLeastMinimumValues(long validValue) {
DataSource ds = new DataSource();
ds.setDestinationDatasetExpirationTime(validValue);
assertEquals(validValue, ds.getDestinationDatasetExpirationTime());
String url = baseUrl().append("LargeResultsDatasetExpirationTime", validValue).toString();
DataSource parsed = DataSource.fromUrl(url);
assertEquals(validValue, parsed.getDestinationDatasetExpirationTime());
}
}