Skip to content

Commit a7d6535

Browse files
authored
chore: add tests for setting IP address in r2dbc-postgres and r2dbc-sqlserver connectors (#1131)
1 parent 559952f commit a7d6535

File tree

4 files changed

+246
-1
lines changed

4 files changed

+246
-1
lines changed

r2dbc/postgres/pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,25 @@
243243
<version>1.0.0.RELEASE</version>
244244
<scope>test</scope>
245245
</dependency>
246-
246+
<dependency>
247+
<groupId>org.mockito</groupId>
248+
<artifactId>mockito-core</artifactId>
249+
<version>4.11.0</version>
250+
<scope>test</scope>
251+
</dependency>
252+
<dependency>
253+
<groupId>org.bouncycastle</groupId>
254+
<artifactId>bcpkix-jdk15on</artifactId>
255+
<version>1.70</version>
256+
<scope>test</scope>
257+
</dependency>
258+
<dependency>
259+
<groupId>com.google.cloud.sql</groupId>
260+
<artifactId>cloud-sql-connector-r2dbc-core</artifactId>
261+
<version>${project.parent.version}</version>
262+
<type>test-jar</type>
263+
<scope>test</scope>
264+
</dependency>
247265
</dependencies>
248266

249267
<profiles>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

r2dbc/sqlserver/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,25 @@
237237
<version>1.0.0.RELEASE</version>
238238
<scope>test</scope>
239239
</dependency>
240+
<dependency>
241+
<groupId>org.mockito</groupId>
242+
<artifactId>mockito-core</artifactId>
243+
<version>4.11.0</version>
244+
<scope>test</scope>
245+
</dependency>
246+
<dependency>
247+
<groupId>org.bouncycastle</groupId>
248+
<artifactId>bcpkix-jdk15on</artifactId>
249+
<version>1.70</version>
250+
<scope>test</scope>
251+
</dependency>
252+
<dependency>
253+
<groupId>com.google.cloud.sql</groupId>
254+
<artifactId>cloud-sql-connector-r2dbc-core</artifactId>
255+
<version>${project.parent.version}</version>
256+
<type>test-jar</type>
257+
<scope>test</scope>
258+
</dependency>
240259
</dependencies>
241260

242261
<profiles>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 GcpConnectionFactoryProviderMssqlTest 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, "mssql").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+
GcpConnectionFactoryProviderMssql mysqlProvider = new GcpConnectionFactoryProviderMssql();
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

Comments
 (0)