-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
217 additions
and
68 deletions.
There are no files selected for viewing
12 changes: 0 additions & 12 deletions
12
buildSrc/src/main/java/io/micronaut/guides/feature/OracleTestContainer.java
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
...ut-oracle-autonomous-db/groovy/src/test/groovy/example/micronaut/repository/Oracle.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
73 changes: 46 additions & 27 deletions
73
...onomous-db/groovy/src/test/groovy/example/micronaut/repository/ThingRepositorySpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...icronaut-oracle-autonomous-db/java/src/test/java/example/micronaut/repository/Oracle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...ronaut-oracle-autonomous-db/kotlin/src/test/kotlin/example/micronaut/repository/Oracle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.