|
| 1 | +package org.utplsql.maven.plugin; |
| 2 | + |
| 3 | +import oracle.security.pki.OracleSecretStore; |
| 4 | +import oracle.security.pki.OracleWallet; |
| 5 | +import org.apache.maven.api.plugin.testing.Basedir; |
| 6 | +import org.apache.maven.api.plugin.testing.InjectMojo; |
| 7 | +import org.apache.maven.api.plugin.testing.MojoParameter; |
| 8 | +import org.apache.maven.api.plugin.testing.MojoTest; |
| 9 | +import org.junit.jupiter.api.AfterEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.io.TempDir; |
| 12 | + |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
| 15 | + |
| 16 | +import static org.utplsql.maven.plugin.UtPlsqlMojoTest.UNIT_TESTS; |
| 17 | + |
| 18 | +/** |
| 19 | + * Tests of the database connection configuration. |
| 20 | + * <p> |
| 21 | + * Unlike {@link UtPlsqlMojoTest}, no connection is given as mojo parameters, |
| 22 | + * so the mojo falls back to the dbUrl, dbUser and dbPass system properties. |
| 23 | + */ |
| 24 | +@MojoTest |
| 25 | +@MojoParameter(name = "targetDir", value = "target/unit-tests") |
| 26 | +class UtPlsqlMojoConnectionTest { |
| 27 | + |
| 28 | + private static final String WALLET_TNS_ALIAS = "UTPLSQL_MAVEN_WALLET"; |
| 29 | + |
| 30 | + @TempDir |
| 31 | + Path tempDir; |
| 32 | + |
| 33 | + @AfterEach |
| 34 | + void clearSystemProperties() { |
| 35 | + System.clearProperty("dbUrl"); |
| 36 | + System.clearProperty("dbUser"); |
| 37 | + System.clearProperty("dbPass"); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * DB configuration from System Properties |
| 42 | + * <p> |
| 43 | + * Given : a pom.xml without dbUrl, dbUser and dbPass configured |
| 44 | + * When : pom is read |
| 45 | + * Then : System Properties must be used to configure database |
| 46 | + */ |
| 47 | + @Test |
| 48 | + @Basedir(UNIT_TESTS + "db_config_using_system_properties") |
| 49 | + void db_config_using_system_properties(@InjectMojo(goal = "test", pom = "pom.xml") UtPlsqlMojo utPlsqlMojo) throws Exception { |
| 50 | + System.setProperty("dbUrl", "jdbc:oracle:thin:@//localhost:1521/FREEPDB1"); |
| 51 | + System.setProperty("dbUser", "UT3"); |
| 52 | + System.setProperty("dbPass", "ut3"); |
| 53 | + |
| 54 | + utPlsqlMojo.execute(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Connection using Oracle Wallet (Secure External Password Store) |
| 59 | + * <p> |
| 60 | + * Given : a pom.xml without dbUser and dbPass and a dbUrl pointing to a TNS alias stored in an Oracle Wallet |
| 61 | + * When : tests are run |
| 62 | + * Then : credentials are taken from the wallet |
| 63 | + */ |
| 64 | + @Test |
| 65 | + @Basedir(UNIT_TESTS + "wallet_connection") |
| 66 | + void wallet_connection_without_credentials(@InjectMojo(goal = "test", pom = "pom.xml") UtPlsqlMojo utPlsqlMojo) throws Exception { |
| 67 | + System.setProperty("dbUrl", walletDbUrl()); |
| 68 | + |
| 69 | + utPlsqlMojo.execute(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Connection using Oracle Wallet with empty credentials |
| 74 | + * <p> |
| 75 | + * Given : a dbUrl pointing to a TNS alias stored in an Oracle Wallet and empty dbUser and dbPass (e.g. -DdbUser= -DdbPass=) |
| 76 | + * When : tests are run |
| 77 | + * Then : empty credentials are ignored and credentials are taken from the wallet |
| 78 | + */ |
| 79 | + @Test |
| 80 | + @Basedir(UNIT_TESTS + "wallet_connection") |
| 81 | + void wallet_connection_with_empty_credentials(@InjectMojo(goal = "test", pom = "pom.xml") UtPlsqlMojo utPlsqlMojo) throws Exception { |
| 82 | + System.setProperty("dbUrl", walletDbUrl()); |
| 83 | + System.setProperty("dbUser", ""); |
| 84 | + System.setProperty("dbPass", ""); |
| 85 | + |
| 86 | + utPlsqlMojo.execute(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Creates an auto-login Oracle Wallet holding the UT3 credentials for {@link #WALLET_TNS_ALIAS}, |
| 91 | + * with tnsnames.ora and ojdbc.properties next to it, so no Oracle client tooling (mkstore/orapki) is needed. |
| 92 | + * |
| 93 | + * @return JDBC URL of the TNS alias, with the generated directory as TNS_ADMIN |
| 94 | + */ |
| 95 | + private String walletDbUrl() throws Exception { |
| 96 | + Path tnsAdmin = Files.createDirectories(tempDir.resolve("tns_admin")); |
| 97 | + |
| 98 | + OracleWallet wallet = new OracleWallet(); |
| 99 | + wallet.create("Wallet_Pwd_123".toCharArray()); |
| 100 | + OracleSecretStore secretStore = wallet.getSecretStore(); |
| 101 | + secretStore.createCredential(WALLET_TNS_ALIAS.toCharArray(), "UT3".toCharArray(), "ut3".toCharArray()); |
| 102 | + wallet.setSecretStore(secretStore); |
| 103 | + wallet.saveAs(tnsAdmin.toString()); |
| 104 | + wallet.createSSO(); |
| 105 | + wallet.saveSSO(); |
| 106 | + |
| 107 | + Files.writeString(tnsAdmin.resolve("tnsnames.ora"), |
| 108 | + WALLET_TNS_ALIAS + " = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))" |
| 109 | + + "(CONNECT_DATA = (SERVICE_NAME = FREEPDB1)))\n"); |
| 110 | + // ${TNS_ADMIN} is resolved by the JDBC driver, the same way as in wallets downloaded for Oracle Autonomous Database |
| 111 | + Files.writeString(tnsAdmin.resolve("ojdbc.properties"), |
| 112 | + "oracle.net.wallet_location=(SOURCE=(METHOD=FILE)(METHOD_DATA=(DIRECTORY=${TNS_ADMIN})))\n"); |
| 113 | + |
| 114 | + return "jdbc:oracle:thin:@" + WALLET_TNS_ALIAS + "?TNS_ADMIN=" + tnsAdmin.toAbsolutePath().toString().replace('\\', '/'); |
| 115 | + } |
| 116 | +} |
0 commit comments