|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * athena-sqlserver |
| 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.sqlserver; |
| 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.PORT; |
| 32 | +import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.SECRET_NAME; |
| 33 | +import static org.junit.Assert.assertEquals; |
| 34 | + |
| 35 | +public class SqlServerEnvironmentPropertiesTest |
| 36 | +{ |
| 37 | + private Map<String, String> connectionProperties; |
| 38 | + private SqlServerEnvironmentProperties sqlServerEnvironmentProperties; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void setUp() |
| 42 | + { |
| 43 | + connectionProperties = new HashMap<>(); |
| 44 | + connectionProperties.put(HOST, "test.sqlserver.com"); |
| 45 | + connectionProperties.put(PORT, "1433"); |
| 46 | + connectionProperties.put(DATABASE, "testdb"); |
| 47 | + connectionProperties.put(SECRET_NAME, "sqlserver-secret"); |
| 48 | + |
| 49 | + sqlServerEnvironmentProperties = new SqlServerEnvironmentProperties(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void connectionPropertiesToEnvironment_WithValidProperties_ReturnsExpectedConnectionString() |
| 54 | + { |
| 55 | + Map<String, String> sqlServerConnectionProperties = sqlServerEnvironmentProperties.connectionPropertiesToEnvironment(connectionProperties); |
| 56 | + |
| 57 | + String expectedConnectionString = "sqlserver://jdbc:sqlserver://test.sqlserver.com:1433;databaseName=testdb;${sqlserver-secret}"; |
| 58 | + assertEquals(expectedConnectionString, sqlServerConnectionProperties.get(DEFAULT)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void getDelimiter_WithDefaultConfiguration_ReturnsSemicolonDelimiter() |
| 63 | + { |
| 64 | + assertEquals(";", sqlServerEnvironmentProperties.getDelimiter()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void getConnectionStringPrefix_WithValidProperties_ReturnsPrefix() |
| 69 | + { |
| 70 | + assertEquals("sqlserver://jdbc:sqlserver://", sqlServerEnvironmentProperties.getConnectionStringPrefix(connectionProperties)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void getJdbcParametersSeparator_WithDefaultConfiguration_ReturnsSemicolonSeparator() |
| 75 | + { |
| 76 | + assertEquals(";", sqlServerEnvironmentProperties.getJdbcParametersSeparator()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test(expected = NullPointerException.class) |
| 80 | + public void connectionPropertiesToEnvironment_WithNullProperties_ThrowsNullPointerException() |
| 81 | + { |
| 82 | + sqlServerEnvironmentProperties.connectionPropertiesToEnvironment(null); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void connectionPropertiesToEnvironment_WithEmptyProperties_ReturnsConnectionStringWithNulls() |
| 87 | + { |
| 88 | + Map<String, String> sqlServerConnectionProperties = sqlServerEnvironmentProperties.connectionPropertiesToEnvironment(new HashMap<>()); |
| 89 | + |
| 90 | + String expectedConnectionString = "sqlserver://jdbc:sqlserver://null:null;databaseName=null;"; |
| 91 | + assertEquals(expectedConnectionString, sqlServerConnectionProperties.get(DEFAULT)); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void connectionPropertiesToEnvironment_WithMissingHost_ReturnsConnectionStringWithNullHost() |
| 96 | + { |
| 97 | + connectionProperties.remove(HOST); |
| 98 | + Map<String, String> sqlServerConnectionProperties = sqlServerEnvironmentProperties.connectionPropertiesToEnvironment(connectionProperties); |
| 99 | + |
| 100 | + String expectedConnectionString = "sqlserver://jdbc:sqlserver://null:1433;databaseName=testdb;${sqlserver-secret}"; |
| 101 | + assertEquals(expectedConnectionString, sqlServerConnectionProperties.get(DEFAULT)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void connectionPropertiesToEnvironment_WithMissingPort_ReturnsConnectionStringWithNullPort() |
| 106 | + { |
| 107 | + connectionProperties.remove(PORT); |
| 108 | + Map<String, String> sqlServerConnectionProperties = sqlServerEnvironmentProperties.connectionPropertiesToEnvironment(connectionProperties); |
| 109 | + |
| 110 | + String expectedConnectionString = "sqlserver://jdbc:sqlserver://test.sqlserver.com:null;databaseName=testdb;${sqlserver-secret}"; |
| 111 | + assertEquals(expectedConnectionString, sqlServerConnectionProperties.get(DEFAULT)); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void connectionPropertiesToEnvironment_WithMissingDatabase_ReturnsConnectionStringWithNullDatabase() |
| 116 | + { |
| 117 | + connectionProperties.remove(DATABASE); |
| 118 | + Map<String, String> sqlServerConnectionProperties = sqlServerEnvironmentProperties.connectionPropertiesToEnvironment(connectionProperties); |
| 119 | + |
| 120 | + String expectedConnectionString = "sqlserver://jdbc:sqlserver://test.sqlserver.com:1433;databaseName=null;${sqlserver-secret}"; |
| 121 | + assertEquals(expectedConnectionString, sqlServerConnectionProperties.get(DEFAULT)); |
| 122 | + } |
| 123 | +} |
0 commit comments