Skip to content

Commit

Permalink
oracle autonomous db
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo committed Nov 16, 2023
1 parent 0635457 commit 3fb04bb
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 68 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package example.micronaut.repository;

import org.testcontainers.containers.OracleContainer;

import java.util.Map;

public class Oracle {

public static OracleContainer oracle = new OracleContainer("gvenzl/oracle-xe:21-slim-faststart")
.withDatabaseName("testDB")
.withUsername("testUser")
.withPassword("testPassword");

public static Map<String, Object> getConfiguration() {
start();
return Map.of("datasources.default.url", oracle.getJdbcUrl(),
"datasources.default.username", oracle.getUsername(),
"datasources.default.password", oracle.getPassword(),
"datasources.default.driver-class-name", oracle.getDriverClassName());
}
public static void start() {
if (!oracle.isRunning()) {
oracle.start();
}
}

public static void stop() {
if (oracle.isRunning()) {
oracle.stop();
}
}

public static void close() {
oracle.close();
}

}
Original file line number Diff line number Diff line change
@@ -1,61 +1,80 @@
package example.micronaut.repository

import com.github.dockerjava.api.model.Info
import example.micronaut.domain.Thing
import example.micronaut.repository.ThingRepository
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import io.micronaut.context.ApplicationContext
import spock.lang.IgnoreIf
import org.testcontainers.DockerClientFactory
import spock.lang.Specification

import jakarta.inject.Inject
import java.util.stream.Collectors

@MicronautTest
class ThingRepositorySpec extends Specification {

@Inject
ThingRepository thingRepository
/**
* WARN t.gvenzl/oracle-xe:21-slim-faststart - The architecture 'amd64' for image 'gvenzl/oracle-xe:21-slim-faststart'
* (ID sha256:395e7780aaba5f8c33082bf533a17a4bffdb7bcdd58034702a1634fcbd3d1137) does not match the Docker server architecture 'arm64'.
* This will cause the container to execute much more slowly due to emulation and may lead to timeout failures.
*/
static boolean dockerArchitecture() {
Info info = DockerClientFactory.instance().getInfo()
String architecture = info.getArchitecture()
if (!architecture) {
return true
}
architecture == "x86_64"
}

void 'test findAll'() {
@IgnoreIf({ !dockerArchitecture() })
void testFindAll() {
given:
ApplicationContext applicationContext = ApplicationContext.run(Oracle.getConfiguration())
ThingRepository thingRepository = applicationContext.getBean(ThingRepository.class)

when:
// clear out existing data; safe because each
// test runs in a transaction that's rolled back
when:
thingRepository.deleteAll()

then:
!thingRepository.count()
0 == thingRepository.count()

when:
thingRepository.saveAll(Arrays.asList(
new Thing('t1'),
new Thing('t2'),
new Thing('t3')))

new Thing("t1"),
new Thing("t2"),
new Thing("t3")))
List<Thing> things = thingRepository.findAll()

then:
things.size() == 3
['t1', 't2', 't3'] == things.stream()
.map(Thing::getName)
.sorted()
.collect(Collectors.toList())
3 == things.size()
Arrays.asList("t1", "t2", "t3") ==
things.stream()
.map(Thing::getName)
.sorted()
.toList()
cleanup:
applicationContext.close()
}

void 'test findByName'() {
@IgnoreIf({ !dockerArchitecture() })
void testFindByName() {
given:
String name = UUID.randomUUID()

ApplicationContext applicationContext = ApplicationContext.run(Oracle.getConfiguration())
ThingRepository thingRepository = applicationContext.getBean(ThingRepository.class)
String name = UUID.randomUUID().toString()
when:
Thing thing = thingRepository.findByName(name).orElse(null)

Thing thing = thingRepository.findByName(name).orElse(null);
then:
!thing

when:
thingRepository.save(new Thing(name))
thing = thingRepository.findByName(name).orElse(null)
thingRepository.save(new Thing(name));
thing = thingRepository.findByName(name).orElse(null);

then:
thing
name == thing.name

cleanup:
applicationContext.close()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package example.micronaut.repository;

import org.testcontainers.containers.OracleContainer;

import java.util.Map;

public class Oracle {

public static OracleContainer oracle = new OracleContainer("gvenzl/oracle-xe:21-slim-faststart")
.withDatabaseName("testDB")
.withUsername("testUser")
.withPassword("testPassword");

public static Map<String, Object> getConfiguration() {
start();
return Map.of("datasources.default.url", oracle.getJdbcUrl(),
"datasources.default.username", oracle.getUsername(),
"datasources.default.password", oracle.getPassword(),
"datasources.default.driver-class-name", oracle.getDriverClassName());
}
public static void start() {
if (!oracle.isRunning()) {
oracle.start();
}
}

public static void stop() {
if (oracle.isRunning()) {
oracle.stop();
}
}

public static void close() {
oracle.close();
}

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
package example.micronaut.repository;

import com.github.dockerjava.api.model.Info;
import example.micronaut.domain.Thing;
import example.micronaut.repository.ThingRepository;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import io.micronaut.context.ApplicationContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import org.testcontainers.DockerClientFactory;

import jakarta.inject.Inject;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;

@MicronautTest
class ThingRepositoryTest {

@Inject
ThingRepository thingRepository;
/**
* WARN t.gvenzl/oracle-xe:21-slim-faststart - The architecture 'amd64' for image 'gvenzl/oracle-xe:21-slim-faststart'
* (ID sha256:395e7780aaba5f8c33082bf533a17a4bffdb7bcdd58034702a1634fcbd3d1137) does not match the Docker server architecture 'arm64'.
* This will cause the container to execute much more slowly due to emulation and may lead to timeout failures.
*/
boolean dockerArchitecture() {
Info info = DockerClientFactory.instance().getInfo();
String architecture = info.getArchitecture();
if (architecture == null) {
return true;
}
return architecture.equals("x86_64");
}

@EnabledIf("dockerArchitecture")
@Test
void testFindAll() {
ApplicationContext applicationContext = ApplicationContext.run(Oracle.getConfiguration());
ThingRepository thingRepository = applicationContext.getBean(ThingRepository.class);

// clear out existing data; safe because each
// test runs in a transaction that's rolled back
Expand All @@ -42,10 +54,15 @@ void testFindAll() {
.map(Thing::getName)
.sorted()
.collect(Collectors.toList()));
applicationContext.close();
}

@EnabledIf("dockerArchitecture")
@Test
void testFindByName() {
ApplicationContext applicationContext = ApplicationContext.run(Oracle.getConfiguration());
ThingRepository thingRepository = applicationContext.getBean(ThingRepository.class);

String name = UUID.randomUUID().toString();

Thing thing = thingRepository.findByName(name).orElse(null);
Expand All @@ -55,5 +72,6 @@ void testFindByName() {
thing = thingRepository.findByName(name).orElse(null);
assertNotNull(thing);
assertEquals(name, thing.getName());
applicationContext.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package example.micronaut.repository

import org.testcontainers.containers.OracleContainer

object Oracle {
var oracle = OracleContainer("gvenzl/oracle-xe:21-slim-faststart")
.withDatabaseName("testDB")
.withUsername("testUser")
.withPassword("testPassword")

val configuration: Map<String, Any>
get() {
start()
return java.util.Map.of<String, Any>(
"datasources.default.url", oracle.jdbcUrl,
"datasources.default.username", oracle.username,
"datasources.default.password", oracle.password,
"datasources.default.driver-class-name", oracle.driverClassName
)
}

fun start() {
if (!oracle.isRunning()) {
oracle.start()
}
}

fun stop() {
if (oracle.isRunning()) {
oracle.stop()
}
}

fun close() {
oracle.close()
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package example.micronaut.repository

import example.micronaut.domain.Thing
import example.micronaut.repository.ThingRepository
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull
import example.micronaut.repository.Oracle.configuration
import io.micronaut.context.ApplicationContext
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import java.util.UUID
import org.junit.jupiter.api.condition.EnabledIf
import org.testcontainers.DockerClientFactory
import java.util.*
import java.util.stream.Collectors

@MicronautTest
class ThingRepositoryTest(private val thingRepository: ThingRepository) {
class ThingRepositoryTest {

/**
* WARN t.gvenzl/oracle-xe:21-slim-faststart - The architecture 'amd64' for image 'gvenzl/oracle-xe:21-slim-faststart'
* (ID sha256:395e7780aaba5f8c33082bf533a17a4bffdb7bcdd58034702a1634fcbd3d1137) does not match the Docker server architecture 'arm64'.
* This will cause the container to execute much more slowly due to emulation and may lead to timeout failures.
*/
fun dockerArchitecture(): Boolean {
val info = DockerClientFactory.instance().info
val architecture = info.architecture ?: return true
return architecture == "x86_64"
}

@EnabledIf("dockerArchitecture")
@Test
fun testFindAll() {
val applicationContext = ApplicationContext.run(configuration)
val thingRepository = applicationContext.getBean(ThingRepository::class.java)

// clear out existing data; safe because each
// test runs in a transaction that's rolled back
Expand All @@ -34,10 +47,16 @@ class ThingRepositoryTest(private val thingRepository: ThingRepository) {
.map(Thing::name)
.sorted()
.collect(Collectors.toList()))

applicationContext.close()
}

@EnabledIf("dockerArchitecture")
@Test
fun testFindByName() {
val applicationContext = ApplicationContext.run(configuration)
val thingRepository = applicationContext.getBean(ThingRepository::class.java)

val name = UUID.randomUUID().toString()

var thing = thingRepository.findByName(name).orElse(null)
Expand All @@ -47,5 +66,7 @@ class ThingRepositoryTest(private val thingRepository: ThingRepository) {
thing = thingRepository.findByName(name).orElse(null)
assertNotNull(thing)
assertEquals(name, thing.name)

applicationContext.close()
}
}
8 changes: 4 additions & 4 deletions guides/micronaut-oracle-autonomous-db/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"tags": ["oracle", "cloud", "database"],
"categories": ["Oracle Cloud"],
"publicationDate": "2021-05-17",
"skipGradleTests": true,
"skipMavenTests": true,
"apps": [{
"apps": [
{
"excludeTest": ["DefaultTest"],
"name": "default",
"features": ["yaml","data-jdbc", "flyway", "graalvm", "oracle-cloud-atp", "testcontainers", "testcontainers-oracle"]
"features": ["yaml","data-jdbc", "flyway", "graalvm", "oracle-cloud-atp", "testcontainers", "testcontainers-oracle-xe"]
}]
}
Loading

0 comments on commit 3fb04bb

Please sign in to comment.