|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.sql.postgres; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 21 | + |
| 22 | +import com.google.common.collect.ImmutableList; |
| 23 | +import com.zaxxer.hikari.HikariConfig; |
| 24 | +import com.zaxxer.hikari.HikariDataSource; |
| 25 | +import java.sql.*; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Properties; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.Rule; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.rules.Timeout; |
| 35 | +import org.junit.runner.RunWith; |
| 36 | +import org.junit.runners.JUnit4; |
| 37 | + |
| 38 | +@RunWith(JUnit4.class) |
| 39 | +public class JdbcPostgresCustomerCasIntegrationTests { |
| 40 | + |
| 41 | + private static final String CONNECTION_NAME = |
| 42 | + System.getenv("POSTGRES_CUSTOMER_CAS_CONNECTION_NAME"); |
| 43 | + private static final String DB_NAME = System.getenv("POSTGRES_DB"); |
| 44 | + private static final String DB_USER = System.getenv("POSTGRES_USER"); |
| 45 | + private static final String DB_PASSWORD = System.getenv("POSTGRES_CUSTOMER_CAS_PASS"); |
| 46 | + private static final ImmutableList<String> requiredEnvVars = |
| 47 | + ImmutableList.of( |
| 48 | + "POSTGRES_USER", |
| 49 | + "POSTGRES_CUSTOMER_CAS_PASS", |
| 50 | + "POSTGRES_DB", |
| 51 | + "POSTGRES_CUSTOMER_CAS_CONNECTION_NAME"); |
| 52 | + @Rule public Timeout globalTimeout = new Timeout(80, TimeUnit.SECONDS); |
| 53 | + |
| 54 | + private HikariDataSource connectionPool; |
| 55 | + |
| 56 | + @BeforeClass |
| 57 | + public static void checkEnvVars() { |
| 58 | + // Check that required env vars are set |
| 59 | + requiredEnvVars.forEach( |
| 60 | + (varName) -> |
| 61 | + assertWithMessage( |
| 62 | + String.format( |
| 63 | + "Environment variable '%s' must be set to perform these tests.", varName)) |
| 64 | + .that(System.getenv(varName)) |
| 65 | + .isNotEmpty()); |
| 66 | + } |
| 67 | + |
| 68 | + @Before |
| 69 | + public void setUpPool() throws SQLException { |
| 70 | + // Set up URL parameters |
| 71 | + String jdbcURL = String.format("jdbc:postgresql:///%s", DB_NAME); |
| 72 | + Properties connProps = new Properties(); |
| 73 | + connProps.setProperty("user", DB_USER); |
| 74 | + connProps.setProperty("password", DB_PASSWORD); |
| 75 | + connProps.setProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory"); |
| 76 | + connProps.setProperty("cloudSqlInstance", CONNECTION_NAME); |
| 77 | + |
| 78 | + // Initialize connection pool |
| 79 | + HikariConfig config = new HikariConfig(); |
| 80 | + config.setJdbcUrl(jdbcURL); |
| 81 | + config.setDataSourceProperties(connProps); |
| 82 | + config.setConnectionTimeout(10000); // 10s |
| 83 | + |
| 84 | + this.connectionPool = new HikariDataSource(config); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void pooledConnectionTest() throws SQLException { |
| 89 | + |
| 90 | + List<Timestamp> rows = new ArrayList<>(); |
| 91 | + try (Connection conn = connectionPool.getConnection()) { |
| 92 | + try (PreparedStatement selectStmt = conn.prepareStatement("SELECT NOW() as TS")) { |
| 93 | + ResultSet rs = selectStmt.executeQuery(); |
| 94 | + while (rs.next()) { |
| 95 | + rows.add(rs.getTimestamp("TS")); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + assertThat(rows.size()).isEqualTo(1); |
| 100 | + } |
| 101 | +} |
0 commit comments