Skip to content

Commit

Permalink
✅ test: Replace embedded database with MongoDB test container for onF…
Browse files Browse the repository at this point in the history
…hir unit tests
  • Loading branch information
dogukan10 authored and sinaci committed May 22, 2024
1 parent 6afbfd5 commit 2b28d43
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
6 changes: 6 additions & 0 deletions tofhir-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@
<artifactId>kafka</artifactId>
<scope>test</scope>
</dependency>
<!-- For MongoDB test container -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<scope>test</scope>
</dependency>
<!-- onFhir test container-->
<dependency>
<groupId>org.testcontainers</groupId>
Expand Down
15 changes: 13 additions & 2 deletions tofhir-engine/src/test/scala/io/tofhir/OnFhirTestContainer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.tofhir

import io.onfhir.client.OnFhirNetworkClient
import io.tofhir.engine.Execution.actorSystem
import org.testcontainers.containers.GenericContainer
import org.testcontainers.containers.{GenericContainer, MongoDBContainer, Network}
import org.testcontainers.containers.wait.strategy.Wait
import org.testcontainers.utility.DockerImageName

Expand All @@ -18,8 +18,19 @@ object OnFhirTestContainer {
private val TIMEOUT_SECONDS = 180 // Timeout in seconds for container startup

private lazy val container: GenericContainer[_] = {
// initialize a Testcontainers network
val network = Network.newNetwork()
// Start a MongoDB container and attach it to the created network
new MongoDBContainer("mongo:7.0")
.withNetwork(network)
.withNetworkAliases("mongoDB")
.start()
// Create a generic container for onFHIR
val container: GenericContainer[Nothing] = new GenericContainer(DockerImageName.parse("srdc/onfhir:r5")).withExposedPorts(ONFHIR_TESTCONTAINER_PORT)
container.addEnv("DB_EMBEDDED", "true")
container.withNetwork(network) // attach the onFHIR container to the same network
container.withNetworkAliases("onFhir")
container.addEnv("DB_EMBEDDED", "false") // disable embedded database
container.addEnv("DB_HOST", s"mongoDB:27017") // connect to MongoDB
container.addEnv("SERVER_PORT", ONFHIR_TESTCONTAINER_PORT.toString)
container.addEnv("SERVER_BASE_URI", "fhir")
container.addEnv("FHIR_ROOT_URL", s"http://${container.getHost}:$ONFHIR_TESTCONTAINER_PORT/fhir")
Expand Down
6 changes: 6 additions & 0 deletions tofhir-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<!-- For MongoDB test container -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import org.json4s.jackson.Serialization.writePretty
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import org.testcontainers.containers.GenericContainer
import org.testcontainers.containers.{GenericContainer, MongoDBContainer}
import org.testcontainers.containers.wait.strategy.Wait
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.containers.Network
import org.testcontainers.utility.DockerImageName

import java.io.File
Expand All @@ -36,6 +37,8 @@ trait BaseEndpointTest extends AnyWordSpec with Matchers with ScalatestRouteTest
// route endpoint
var route: Route = _

private val ONFHIR_TESTCONTAINER_PORT = 8081 // Port number of the OnFhir instance to be created for testing.

// Instance of OnFhirNetworkClient initialized with onFhir container
lazy val onFhirClient: OnFhirNetworkClient = initializeOnFhirClient()

Expand All @@ -44,11 +47,22 @@ trait BaseEndpointTest extends AnyWordSpec with Matchers with ScalatestRouteTest
* */
def initializeOnFhirClient(): OnFhirNetworkClient = {
@Container
val container: GenericContainer[Nothing] = new GenericContainer(DockerImageName.parse("srdc/onfhir:r5")).withExposedPorts(8081);
container.addEnv("DB_EMBEDDED", "true");
container.addEnv("SERVER_PORT", "8081");
container.addEnv("SERVER_BASE_URI", "fhir");
container.addEnv("FHIR_ROOT_URL", s"http://${container.getHost}:8081/fhir")
// initialize a Testcontainers network
val network = Network.newNetwork()
// Start a MongoDB container and attach it to the created network
new MongoDBContainer("mongo:7.0")
.withNetwork(network)
.withNetworkAliases("mongoDB")
.start()
// Create a generic container for onFHIR
val container: GenericContainer[Nothing] = new GenericContainer(DockerImageName.parse("srdc/onfhir:r5")).withExposedPorts(ONFHIR_TESTCONTAINER_PORT)
container.withNetwork(network) // attach the onFHIR container to the same network
container.withNetworkAliases("onFhir")
container.addEnv("DB_EMBEDDED", "false") // disable embedded database
container.addEnv("DB_HOST", s"mongoDB:27017") // connect to MongoDB
container.addEnv("SERVER_PORT", ONFHIR_TESTCONTAINER_PORT.toString)
container.addEnv("SERVER_BASE_URI", "fhir")
container.addEnv("FHIR_ROOT_URL", s"http://${container.getHost}:$ONFHIR_TESTCONTAINER_PORT/fhir")
// The default startup timeout of 60 seconds is not sufficient for OnFhir, as it utilizes an embedded
// MongoDB, which requires additional initialization time. Therefore, we increase the timeout to 3 minutes (180 seconds).
container.waitingFor(Wait.forHttp("/fhir/metadata").forStatusCode(200).withStartupTimeout(Duration.ofSeconds(180)))
Expand Down

0 comments on commit 2b28d43

Please sign in to comment.