Repository navigation
Expand file tree
/
Copy pathBigQueryJdbcBaseTest.java
More file actions
117 lines (103 loc) · 4.25 KB
/
Copy pathBigQueryJdbcBaseTest.java
File metadata and controls
117 lines (103 loc) · 4.25 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
/*
* Copyright 2025 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
*
* http://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 com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.jdbc.utils.TestUtilities;
import com.google.cloud.bigquery.jdbc.utils.URIBuilder;
import com.google.gson.JsonObject;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
public class BigQueryJdbcBaseTest {
// This is a fake pkcs8 key generated specifically for unittests
protected static final String fake_pkcs8_key =
"-----BEGIN PRIVATE KEY-----\n"
+ //
"MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEAnt6w5AMZBvOecsJ9\n"
+ //
"4TeVz+GpAtBnTqkxWfxLJykkvb+V/3IhXr5Zw40y47RdoRly/QDFJz3Ac+nmwCSP\n"
+ //
"8QW3GQIDAQABAkBPmdrd1COFFSnN7F9wKg65QyMQ0uUAR8v/f2cUbwwGuhwdMuGZ\n"
+ //
"DPwgVZySxFKort7TfPru6NzbACL3EFAl9y9RAiEA7XPq5Tu+LOw4/CZFABykguBV\n"
+ //
"8rYC+F72+GqkhvlGhZUCIQCrR2/zGIKqJSTKfQhKOteP7cx5dWrumHYNuC5InOGC\n"
+ //
"dQIgM6bzgcntJHh+LNtmRw/z+UQzbgiJvN1re7426+VtocECIE7ejFxicviqNfDP\n"
+ //
"9ltIES8Dj152hRDtP589qoJhSy5pAiAJot/kBQD8yFYMU1X02oi+6f8QqXxcHwZX\n"
+ //
"2wK1Zawz/A==\n"
+ //
"-----END PRIVATE KEY-----";
protected static String getFakeServiceAccountJson(String clientEmail) {
JsonObject json = new JsonObject();
json.addProperty("type", "service_account");
json.addProperty("project_id", "MyBigQueryProject");
json.addProperty("private_key_id", "fake-key-id");
json.addProperty("private_key", fake_pkcs8_key);
json.addProperty("client_email", clientEmail);
json.addProperty("client_id", "1234567890");
json.addProperty("token_uri", "https://oauth2.googleapis.com/token");
return json.toString();
}
protected static BigQuery getBigQuery(String connectionUrl) {
try {
Class<?> bqConnClass = Class.forName("com.google.cloud.bigquery.jdbc.BigQueryConnection");
Connection conn = DriverManager.getConnection(connectionUrl);
Object unwrapped = conn.unwrap(bqConnClass);
if (unwrapped != null) {
Method method = bqConnClass.getDeclaredMethod("getBigQuery");
method.setAccessible(true);
return (BigQuery) method.invoke(unwrapped);
}
} catch (Throwable e) {
// ignore for some set of tests; Proxy/TPC tests will fail if it doesn't work.
}
return BigQueryOptions.getDefaultInstance().getService();
}
protected static URIBuilder getBaseUri() {
return new URIBuilder(TestUtilities.getBaseConnectionUrl());
}
protected static URIBuilder getBaseUri(int authType) {
return getBaseUri().append("OAuthType", authType);
}
protected static URIBuilder getBaseUri(int authType, String projectId) {
return getBaseUri(authType).append("ProjectId", projectId);
}
protected static URIBuilder getUriOAuthServiceAccount() {
return getBaseUri()
.append("OAuthType", 0)
.append("OAuthServiceAcctEmail", "service@account")
.append("OAuthPvtKey", fake_pkcs8_key);
}
protected static URIBuilder getUriOAuthUserAccount() {
return getBaseUri()
.append("OAuthType", 1)
.append("OAuthClientId", "client@id")
.append("OAuthClientSecret", "client_secret");
}
protected static URIBuilder getUriOAuthToken() {
return getBaseUri().append("OAuthType", 2).append("OAuthAccessToken", "RedactedToken");
}
protected static URIBuilder getUriOAuthApplicationDefault() {
return getBaseUri().append("OAuthType", 3);
}
protected static URIBuilder getUriOAuthExternal() {
return getBaseUri().append("OAuthType", 4).append("OAuthPvtKey", fake_pkcs8_key);
}
}