Add Docker Compose support for Oracle Database using R2DBC

Closes gh-35143
This commit is contained in:
Andy Wilkinson
2023-04-25 13:06:40 +01:00
parent 8fd36d04e1
commit 1e785e81f8
9 changed files with 343 additions and 1 deletions

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.oracle;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Tests for {@link OracleEnvironment}.
*
* @author Andy Wilkinson
*/
class OracleEnvironmentTests {
@Test
void getUsernameWhenHasAppUser() {
OracleEnvironment environment = new OracleEnvironment(Map.of("APP_USER", "alice", "APP_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("alice");
}
@Test
void getUsernameWhenHasNoAppUser() {
OracleEnvironment environment = new OracleEnvironment(Map.of("ORACLE_PASSWORD", "secret"));
assertThat(environment.getUsername()).isEqualTo("system");
}
@Test
void getPasswordWhenHasAppPassword() {
OracleEnvironment environment = new OracleEnvironment(Map.of("APP_USER", "alice", "APP_PASSWORD", "secret"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getPasswordWhenHasOraclePassword() {
OracleEnvironment environment = new OracleEnvironment(Map.of("ORACLE_PASSWORD", "secret"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void createWhenRandomPasswordAndAppPasswordDoesNotThrow() {
assertThatNoException().isThrownBy(() -> new OracleEnvironment(
Map.of("APP_USER", "alice", "APP_PASSWORD", "secret", "ORACLE_RANDOM_PASSWORD", "true")));
}
@Test
void createWhenRandomPasswordThrowsException() {
assertThatIllegalStateException()
.isThrownBy(() -> new OracleEnvironment(Map.of("ORACLE_RANDOM_PASSWORD", "true")))
.withMessage("ORACLE_RANDOM_PASSWORD is not supported without APP_USER and APP_PASSWORD");
}
@Test
void createWhenAppUserAndNoAppPasswordThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new OracleEnvironment(Map.of("APP_USER", "alice")))
.withMessage("No Oracle app password found");
}
@Test
void createWhenAppUserAndEmptyAppPasswordThrowsException() {
assertThatIllegalStateException()
.isThrownBy(() -> new OracleEnvironment(Map.of("APP_USER", "alice", "APP_PASSWORD", "")))
.withMessage("No Oracle app password found");
}
@Test
void createWhenHasNoPasswordThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new OracleEnvironment(Collections.emptyMap()))
.withMessage("No Oracle password found");
}
@Test
void createWhenHasEmptyPasswordThrowsException() {
assertThatIllegalStateException().isThrownBy(() -> new OracleEnvironment(Map.of("ORACLE_PASSWORD", "")))
.withMessage("No Oracle password found");
}
@Test
void getDatabaseWhenHasNoOracleDatabase() {
OracleEnvironment environment = new OracleEnvironment(Map.of("ORACLE_PASSWORD", "secret"));
assertThat(environment.getDatabase()).isEqualTo("xepdb1");
}
@Test
void getDatabaseWhenHasOracleDatabase() {
OracleEnvironment environment = new OracleEnvironment(
Map.of("ORACLE_PASSWORD", "secret", "ORACLE_DATABASE", "db"));
assertThat(environment.getDatabase()).isEqualTo("db");
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.oracle;
import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactoryOptions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.test.AbstractDockerComposeIntegrationTests;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.r2dbc.core.DatabaseClient;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link OracleR2dbcDockerComposeConnectionDetailsFactory}
*
* @author Andy Wilkinson
*/
class OracleR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests extends AbstractDockerComposeIntegrationTests {
OracleR2dbcDockerComposeConnectionDetailsFactoryIntegrationTests() {
super("oracle-compose.yaml");
}
@Test
void runCreatesConnectionDetailsThatCanBeUsedToAccessDatabase() {
R2dbcConnectionDetails connectionDetails = run(R2dbcConnectionDetails.class);
ConnectionFactoryOptions connectionFactoryOptions = connectionDetails.getConnectionFactoryOptions();
assertThat(connectionFactoryOptions.toString()).contains("database=xepdb1", "driver=oracle",
"password=REDACTED", "user=system");
assertThat(connectionFactoryOptions.getRequiredValue(ConnectionFactoryOptions.PASSWORD)).isEqualTo("secret");
Object result = DatabaseClient.create(ConnectionFactories.get(connectionFactoryOptions))
.sql(DatabaseDriver.ORACLE.getValidationQuery())
.map((row, metadata) -> row.get(0))
.first()
.block();
assertThat(result).isEqualTo("Hello");
}
}

View File

@@ -0,0 +1,13 @@
services:
database:
image: 'gvenzl/oracle-xe:18.4.0-slim'
ports:
- '1521'
environment:
- 'ORACLE_PASSWORD=secret'
healthcheck:
test: ["CMD-SHELL", "healthcheck.sh"]
interval: 10s
timeout: 5s
retries: 10