|
| 1 | +/* |
| 2 | + ** Copyright (c) 2024 Oracle and/or its affiliates. |
| 3 | + ** |
| 4 | + ** The Universal Permissive License (UPL), Version 1.0 |
| 5 | + ** |
| 6 | + ** Subject to the condition set forth below, permission is hereby granted to any |
| 7 | + ** person obtaining a copy of this software, associated documentation and/or data |
| 8 | + ** (collectively the "Software"), free of charge and under any and all copyright |
| 9 | + ** rights in the Software, and any and all patent rights owned or freely |
| 10 | + ** licensable by each licensor hereunder covering either (i) the unmodified |
| 11 | + ** Software as contributed to or provided by such licensor, or (ii) the Larger |
| 12 | + ** Works (as defined below), to deal in both |
| 13 | + ** |
| 14 | + ** (a) the Software, and |
| 15 | + ** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 16 | + ** one is included with the Software (each a "Larger Work" to which the Software |
| 17 | + ** is contributed by such licensors), |
| 18 | + ** |
| 19 | + ** without restriction, including without limitation the rights to copy, create |
| 20 | + ** derivative works of, display, perform, and distribute the Software and make, |
| 21 | + ** use, sell, offer for sale, import, export, have made, and have sold the |
| 22 | + ** Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 23 | + ** either these or other terms. |
| 24 | + ** |
| 25 | + ** This license is subject to the following condition: |
| 26 | + ** The above copyright notice and either this complete permission notice or at |
| 27 | + ** a minimum a reference to the UPL must be included in all copies or |
| 28 | + ** substantial portions of the Software. |
| 29 | + ** |
| 30 | + ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 31 | + ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 32 | + ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 33 | + ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 34 | + ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 35 | + ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 36 | + ** SOFTWARE. |
| 37 | + */ |
| 38 | + |
| 39 | +package oracle.jdbc.provider.oci.resource; |
| 40 | + |
| 41 | +import oracle.jdbc.provider.TestProperties; |
| 42 | +import oracle.jdbc.provider.oci.OciTestProperty; |
| 43 | +import oracle.jdbc.spi.ConnectionStringProvider; |
| 44 | +import oracle.jdbc.spi.OracleResourceProvider.Parameter; |
| 45 | +import org.junit.jupiter.api.Test; |
| 46 | + |
| 47 | +import java.util.HashMap; |
| 48 | +import java.util.Map; |
| 49 | + |
| 50 | +import static oracle.jdbc.provider.resource.ResourceProviderTestUtil.createParameterValues; |
| 51 | +import static oracle.jdbc.provider.resource.ResourceProviderTestUtil.findProvider; |
| 52 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 53 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 54 | + |
| 55 | +/** Verifies {@link DatabaseConnectionStringProvider} */ |
| 56 | +public class VaultConnectionStringProviderTest { |
| 57 | + |
| 58 | + private static final ConnectionStringProvider PROVIDER = |
| 59 | + findProvider( |
| 60 | + ConnectionStringProvider.class, |
| 61 | + "ojdbc-provider-oci-vault-tnsnames"); |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testValidAlias() { |
| 65 | + Map<String, String> testParameters = new HashMap<>(); |
| 66 | + testParameters.put("authenticationMethod", "config-file"); |
| 67 | + testParameters.put("configFile", |
| 68 | + TestProperties.getOrAbort(OciTestProperty.OCI_CONFIG_FILE)); |
| 69 | + testParameters.put("profile", |
| 70 | + TestProperties.getOrAbort(OciTestProperty.OCI_CONFIG_PROFILE)); |
| 71 | + testParameters.put("ocid", |
| 72 | + TestProperties.getOrAbort(OciTestProperty.OCI_TNS_NAMES_OCID)); |
| 73 | + testParameters.put("tnsAlias", |
| 74 | + TestProperties.getOrAbort(OciTestProperty.OCI_TNS_NAMES_ALIAS)); |
| 75 | + |
| 76 | + Map<Parameter, CharSequence> parameterValues = |
| 77 | + createParameterValues(PROVIDER, testParameters); |
| 78 | + |
| 79 | + String connectionString = PROVIDER.getConnectionString(parameterValues); |
| 80 | + assertNotNull(connectionString); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testInvalidOrNonExistentAlias() { |
| 85 | + Map<String, String> testParameters = new HashMap<>(); |
| 86 | + testParameters.put("authenticationMethod", "config-file"); |
| 87 | + testParameters.put("configFile", |
| 88 | + TestProperties.getOrAbort(OciTestProperty.OCI_CONFIG_FILE)); |
| 89 | + testParameters.put("profile", |
| 90 | + TestProperties.getOrAbort(OciTestProperty.OCI_CONFIG_PROFILE)); |
| 91 | + testParameters.put("ocid", |
| 92 | + TestProperties.getOrAbort(OciTestProperty.OCI_TNS_NAMES_OCID)); |
| 93 | + testParameters.put("tnsAlias", "INVALID_ALIAS"); |
| 94 | + |
| 95 | + Map<Parameter, CharSequence> parameterValues = |
| 96 | + createParameterValues(PROVIDER, testParameters); |
| 97 | + |
| 98 | + assertThrows(IllegalArgumentException.class, |
| 99 | + () -> PROVIDER.getConnectionString(parameterValues), |
| 100 | + "Expected IllegalArgumentException for invalid alias" |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testMissingAliasParameter() { |
| 106 | + Map<String, String> testParameters = new HashMap<>(); |
| 107 | + testParameters.put("authenticationMethod", "config-file"); |
| 108 | + testParameters.put("configFile", |
| 109 | + TestProperties.getOrAbort(OciTestProperty.OCI_CONFIG_FILE)); |
| 110 | + testParameters.put("profile", |
| 111 | + TestProperties.getOrAbort(OciTestProperty.OCI_CONFIG_PROFILE)); |
| 112 | + testParameters.put("ocid", |
| 113 | + TestProperties.getOrAbort(OciTestProperty.OCI_TNS_NAMES_OCID)); |
| 114 | + |
| 115 | + Map<Parameter, CharSequence> parameterValues = |
| 116 | + createParameterValues(PROVIDER, testParameters); |
| 117 | + |
| 118 | + assertThrows(IllegalArgumentException.class, |
| 119 | + () -> PROVIDER.getConnectionString(parameterValues), |
| 120 | + "Expected IllegalArgumentException when tnsAlias parameter is missing" |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testPlainTextEncodedTnsnamesContent() { |
| 126 | + Map<String, String> testParameters = new HashMap<>(); |
| 127 | + testParameters.put("authenticationMethod", "config-file"); |
| 128 | + testParameters.put("configFile", |
| 129 | + TestProperties.getOrAbort(OciTestProperty.OCI_CONFIG_FILE)); |
| 130 | + testParameters.put("profile", |
| 131 | + TestProperties.getOrAbort(OciTestProperty.OCI_CONFIG_PROFILE)); |
| 132 | + testParameters.put("ocid", |
| 133 | + TestProperties.getOrAbort(OciTestProperty.OCI_NON_BASE64_TNS_NAMES_OCID)); |
| 134 | + testParameters.put("tnsAlias", |
| 135 | + TestProperties.getOrAbort(OciTestProperty.OCI_TNS_NAMES_ALIAS)); |
| 136 | + |
| 137 | + Map<Parameter, CharSequence> parameterValues = |
| 138 | + createParameterValues(PROVIDER, testParameters); |
| 139 | + |
| 140 | + String connectionString = PROVIDER.getConnectionString(parameterValues); |
| 141 | + assertNotNull(connectionString); |
| 142 | + } |
| 143 | +} |
0 commit comments