|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC. All Rights Reserved. |
| 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 | + * http://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 | +package com.google.cloud.sql.core; |
| 17 | + |
| 18 | +import static com.google.cloud.sql.core.GcpConnectionFactoryProvider.IP_TYPES; |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static io.r2dbc.spi.ConnectionFactoryOptions.DATABASE; |
| 21 | +import static io.r2dbc.spi.ConnectionFactoryOptions.DRIVER; |
| 22 | +import static io.r2dbc.spi.ConnectionFactoryOptions.HOST; |
| 23 | +import static io.r2dbc.spi.ConnectionFactoryOptions.PORT; |
| 24 | +import static io.r2dbc.spi.ConnectionFactoryOptions.PROTOCOL; |
| 25 | +import static io.r2dbc.spi.ConnectionFactoryOptions.USER; |
| 26 | + |
| 27 | +import io.r2dbc.spi.ConnectionFactoryOptions; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.Map; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.junit.runners.JUnit4; |
| 36 | +import org.mockito.MockedStatic; |
| 37 | +import org.mockito.Mockito; |
| 38 | + |
| 39 | + |
| 40 | +@RunWith(JUnit4.class) |
| 41 | +public class GcpConnectionFactoryProviderPostgresTest extends GcpConnectionFactoryProviderTest { |
| 42 | + |
| 43 | + private static Map<String, String> IP_LABEL = new HashMap<String,String>() {{ |
| 44 | + put("PUBLIC", "PRIMARY"); |
| 45 | + put("PRIVATE", "PRIVATE"); |
| 46 | + }}; |
| 47 | + |
| 48 | + private ConnectionFactoryOptions privateIpOptions; |
| 49 | + private ConnectionFactoryOptions publicIpOptions; |
| 50 | + |
| 51 | + @Before |
| 52 | + public void setupOptions() { |
| 53 | + |
| 54 | + // Set up ConnectionFactoryOptions |
| 55 | + privateIpOptions = ConnectionFactoryOptions.builder().option(DRIVER, "gcp") |
| 56 | + .option(PROTOCOL, "postgres").option(USER, "fake_user").option(DATABASE, "fake_db") |
| 57 | + .option(HOST, fakeInstanceName).option(IP_TYPES, "PRIVATE").build(); |
| 58 | + |
| 59 | + publicIpOptions = privateIpOptions.mutate().option(IP_TYPES, "PUBLIC").build(); |
| 60 | + } |
| 61 | + |
| 62 | + public void setsCorrectOptionsForDriverHostAndPort(String ipType, |
| 63 | + ConnectionFactoryOptions options, String expectedIp) { |
| 64 | + try (MockedStatic<CoreSocketFactory> mockSocketFactory = Mockito.mockStatic( |
| 65 | + CoreSocketFactory.class)) { |
| 66 | + |
| 67 | + mockSocketFactory.when(CoreSocketFactory::getDefaultServerProxyPort).thenReturn(3307); |
| 68 | + mockSocketFactory.when(() -> CoreSocketFactory.getSslData(fakeInstanceName)) |
| 69 | + .thenReturn(coreSocketFactoryStub.getCloudSqlInstance(fakeInstanceName).getSslData()); |
| 70 | + |
| 71 | + mockSocketFactory.when(() -> CoreSocketFactory.getHostIp(fakeInstanceName, ipType)) |
| 72 | + .thenReturn(coreSocketFactoryStub.getCloudSqlInstance(fakeInstanceName) |
| 73 | + .getPreferredIp(Arrays.asList(IP_LABEL.get(ipType)))); |
| 74 | + |
| 75 | + GcpConnectionFactoryProviderPostgres mysqlProvider = new GcpConnectionFactoryProviderPostgres(); |
| 76 | + |
| 77 | + // Use the PrivateIP options to make a Cloud SQL Connection Factory |
| 78 | + CloudSqlConnectionFactory csqlConnFactoryPrivate = (CloudSqlConnectionFactory) mysqlProvider.create( |
| 79 | + options); |
| 80 | + csqlConnFactoryPrivate.setBuilderHostAndPort(); |
| 81 | + |
| 82 | + // Check that Driver, Host, and Port are set properly |
| 83 | + ConnectionFactoryOptions mysqlOptions = csqlConnFactoryPrivate.getBuilder().build(); |
| 84 | + assertThat(mysqlProvider.supportedProtocol((String) mysqlOptions.getValue(DRIVER))).isTrue(); |
| 85 | + assertThat((String) mysqlOptions.getValue(HOST)).isEqualTo(expectedIp); |
| 86 | + assertThat((int) mysqlOptions.getValue(PORT)).isEqualTo( |
| 87 | + CoreSocketFactory.getDefaultServerProxyPort()); |
| 88 | + |
| 89 | + |
| 90 | + } catch (IOException e) { |
| 91 | + throw new RuntimeException(e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void setsCorrectOptionsForDriverHostAndPortPrivate() { |
| 97 | + setsCorrectOptionsForDriverHostAndPort("PRIVATE", privateIpOptions, PRIVATE_IP); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void setsCorrectOptionsForDriverHostAndPortPublic() { |
| 102 | + setsCorrectOptionsForDriverHostAndPort("PUBLIC", publicIpOptions, PUBLIC_IP); |
| 103 | + } |
| 104 | +} |
0 commit comments