1+ /*-
2+ * #%L
3+ * athena-postgresql
4+ * %%
5+ * Copyright (C) 2019 - 2025 Amazon Web Services
6+ * %%
7+ * Licensed under the Apache License, Version 2.0 (the "License");
8+ * you may not use this file except in compliance with the License.
9+ * You may obtain a copy of the License at
10+ *
11+ * http://www.apache.org/licenses/LICENSE-2.0
12+ *
13+ * Unless required by applicable law or agreed to in writing, software
14+ * distributed under the License is distributed on an "AS IS" BASIS,
15+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+ * See the License for the specific language governing permissions and
17+ * limitations under the License.
18+ * #L%
19+ */
20+ package com .amazonaws .athena .connectors .postgresql ;
21+
22+ import org .junit .Before ;
23+ import org .junit .Test ;
24+
25+ import java .util .HashMap ;
26+ import java .util .Map ;
27+
28+ import static com .amazonaws .athena .connector .lambda .connection .EnvironmentConstants .DATABASE ;
29+ import static com .amazonaws .athena .connector .lambda .connection .EnvironmentConstants .DEFAULT ;
30+ import static com .amazonaws .athena .connector .lambda .connection .EnvironmentConstants .HOST ;
31+ import static com .amazonaws .athena .connector .lambda .connection .EnvironmentConstants .JDBC_PARAMS ;
32+ import static com .amazonaws .athena .connector .lambda .connection .EnvironmentConstants .PORT ;
33+ import static com .amazonaws .athena .connector .lambda .connection .EnvironmentConstants .SECRET_NAME ;
34+ import static org .junit .Assert .assertEquals ;
35+
36+ public class PostGreSqlEnvironmentPropertiesTest
37+ {
38+ private static final String DEFAULT_HOST = "postgres-endpoint" ;
39+ private static final String DEFAULT_PORT = "5000" ;
40+ private static final String DEFAULT_DATABASE = "testdb" ;
41+ private static final String SSL_PARAMS = "ssl=true" ;
42+
43+ Map <String , String > connectionProperties ;
44+ PostGreSqlEnvironmentProperties postGreSqlEnvironmentProperties ;
45+
46+ @ Before
47+ public void setUp () {
48+ connectionProperties = new HashMap <>();
49+ connectionProperties .put (HOST , DEFAULT_HOST );
50+ connectionProperties .put (DATABASE , DEFAULT_DATABASE );
51+ connectionProperties .put (SECRET_NAME , "postgres-secret" );
52+ connectionProperties .put (PORT , DEFAULT_PORT );
53+ postGreSqlEnvironmentProperties = new PostGreSqlEnvironmentProperties ();
54+ }
55+
56+ @ Test
57+ public void testBasicConnectionString () {
58+ Map <String , String > postGreSqlConnectionProperties = postGreSqlEnvironmentProperties .connectionPropertiesToEnvironment (connectionProperties );
59+
60+ assertEquals ("postgres://jdbc:postgresql://postgres-endpoint:5000/testdb?${postgres-secret}" , postGreSqlConnectionProperties .get (DEFAULT ));
61+ }
62+
63+ @ Test
64+ public void testConnectionStringWithJdbcParams () {
65+ connectionProperties .put (JDBC_PARAMS , SSL_PARAMS );
66+ connectionProperties .remove (SECRET_NAME );
67+
68+ Map <String , String > postGreSqlConnectionProperties = postGreSqlEnvironmentProperties .connectionPropertiesToEnvironment (connectionProperties );
69+
70+ assertEquals ("postgres://jdbc:postgresql://postgres-endpoint:5000/testdb?ssl=true" , postGreSqlConnectionProperties .get (DEFAULT ));
71+ }
72+
73+ @ Test
74+ public void testConnectionStringWithJdbcParamsAndSecret () {
75+ connectionProperties .put (JDBC_PARAMS , SSL_PARAMS );
76+
77+ Map <String , String > postGreSqlConnectionProperties = postGreSqlEnvironmentProperties .connectionPropertiesToEnvironment (connectionProperties );
78+
79+ assertEquals ("postgres://jdbc:postgresql://postgres-endpoint:5000/testdb?ssl=true&${postgres-secret}" , postGreSqlConnectionProperties .get (DEFAULT ));
80+ }
81+
82+ @ Test
83+ public void testConnectionStringWithoutOptionalParams () {
84+ connectionProperties .remove (SECRET_NAME );
85+
86+ Map <String , String > postGreSqlConnectionProperties = postGreSqlEnvironmentProperties .connectionPropertiesToEnvironment (connectionProperties );
87+
88+ assertEquals ("postgres://jdbc:postgresql://postgres-endpoint:5000/testdb?" , postGreSqlConnectionProperties .get (DEFAULT ));
89+ }
90+
91+ @ Test
92+ public void testConnectionStringWithEmptyValues () {
93+ connectionProperties .put (JDBC_PARAMS , "" );
94+ connectionProperties .put (SECRET_NAME , "" );
95+
96+ Map <String , String > postGreSqlConnectionProperties = postGreSqlEnvironmentProperties .connectionPropertiesToEnvironment (connectionProperties );
97+
98+ assertEquals ("postgres://jdbc:postgresql://postgres-endpoint:5000/testdb?&${}" , postGreSqlConnectionProperties .get (DEFAULT ));
99+ }
100+
101+ @ Test
102+ public void testEmptyJdbcParamsWithSecret () {
103+ connectionProperties .put (JDBC_PARAMS , "" );
104+
105+ Map <String , String > postGreSqlConnectionProperties = postGreSqlEnvironmentProperties .connectionPropertiesToEnvironment (connectionProperties );
106+
107+ assertEquals ("postgres://jdbc:postgresql://postgres-endpoint:5000/testdb?&${postgres-secret}" , postGreSqlConnectionProperties .get (DEFAULT ));
108+ }
109+
110+ @ Test
111+ public void testSpecialCharactersInParameters () {
112+ connectionProperties .put (HOST , "postgres.example.com" );
113+ connectionProperties .put (DATABASE , "test/db" );
114+ connectionProperties .put (JDBC_PARAMS , "currentSchema=public&search_path=public%2Cshared" );
115+
116+ Map <String , String > postGreSqlConnectionProperties = postGreSqlEnvironmentProperties .connectionPropertiesToEnvironment (connectionProperties );
117+
118+ assertEquals ("postgres://jdbc:postgresql://postgres.example.com:5000/test/db?currentSchema=public&search_path=public%2Cshared&${postgres-secret}" , postGreSqlConnectionProperties .get (DEFAULT ));
119+ }
120+
121+ @ Test
122+ public void testMultipleJdbcParameters () {
123+ connectionProperties .put (JDBC_PARAMS , "ssl=true&sslmode=verify-full&connectTimeout=10&socketTimeout=20&tcpKeepAlive=true" );
124+
125+ Map <String , String > postGreSqlConnectionProperties = postGreSqlEnvironmentProperties .connectionPropertiesToEnvironment (connectionProperties );
126+
127+ assertEquals ("postgres://jdbc:postgresql://postgres-endpoint:5000/testdb?ssl=true&sslmode=verify-full&connectTimeout=10&socketTimeout=20&tcpKeepAlive=true&${postgres-secret}" , postGreSqlConnectionProperties .get (DEFAULT ));
128+ }
129+ }
0 commit comments