Skip to content

Commit

Permalink
✨ test(ExecutionServiceTest): Add a checkpoint clearing test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Okanmercan99 committed Oct 9, 2023
1 parent b477e00 commit 1a8552f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tofhir-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@
<artifactId>scalatest_${scala.binary.version}</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-scala_2.13</artifactId>
<scope>test</scope>
</dependency>

<!-- Testkit for Akka Stream-->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.tofhir.server.service

import akka.actor.ActorSystem
import io.tofhir.engine.config.ErrorHandlingType
import io.tofhir.engine.model._
import io.tofhir.server.model.ExecuteJobTask
import io.tofhir.server.service.job.JobFolderRepository
import io.tofhir.server.service.mapping.ProjectMappingFolderRepository
import io.tofhir.server.service.schema.SchemaFolderRepository
import org.apache.commons.io
import org.mockito.MockitoSugar._
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import java.io.{File, FileOutputStream}
import scala.collection.mutable
import scala.concurrent.ExecutionContext

class ExecutionServiceTest extends AnyWordSpec with Matchers {

implicit val actorSystem: ActorSystem = ActorSystem("toFhirEngineTest")
implicit val executionContext: ExecutionContext = actorSystem.getDispatcher

// FhirMappingJob for test
val testJob: FhirMappingJob = FhirMappingJob(name = Some("testJob"), sourceSettings = Map.empty, sinkSettings = sinkSettings, mappings = Seq.apply(patientMappingTask),
dataProcessingSettings = DataProcessingSettings(mappingErrorHandling = ErrorHandlingType.CONTINUE, archiveMode = ArchiveModes.OFF))
val sinkSettings: FhirSinkSettings = FileSystemSinkSettings(path = "http://example.com/fhir")
val patientMappingTask: FhirMappingTask = FhirMappingTask(
mappingRef = "https://aiccelerate.eu/fhir/mappings/patient-mapping",
sourceContext = Map("source" -> KafkaSource(topicName = "patients", groupId = "tofhir", startingOffsets = "earliest"))
)
val testExecuteJobTask: ExecuteJobTask = ExecuteJobTask(clearCheckpoints = true, Option.empty, Option.empty)

val mappingRepository: ProjectMappingFolderRepository = mock[ProjectMappingFolderRepository]
val schemaRepository: SchemaFolderRepository = mock[SchemaFolderRepository]
val mappingJobRepository: JobFolderRepository = getMockMappingJobRepository

// the execution service instance for the test
val executionService: ExecutionService = new ExecutionService(mappingJobRepository, mappingRepository, schemaRepository)

"The Execution Service" should {
"should clear checkpoint directory" in {

val path: String = s"./checkpoint/${testJob.id}/${patientMappingTask.mappingRef.hashCode}"

// create an example file for test
val testFile: File = new File(s"${path}/test.txt")
io.FileUtils.createParentDirectories(testFile)
val fileOutputStream = new FileOutputStream(testFile)
fileOutputStream.write("test".getBytes("UTF-8"))
fileOutputStream.close()

// check whether file is written to the directory
val testDirectory: File = new File(path)
io.FileUtils.sizeOfDirectory(testDirectory) shouldBe > (0L)

// run job and expect to clear the created directory
executionService.runJob("testProject", "testJob", Option.empty, Some(testExecuteJobTask)).map(_ =>
io.FileUtils.sizeOfDirectory(testDirectory) shouldBe 0L
)
}
}

private def getMockMappingJobRepository: JobFolderRepository = {
val mockMappingJobRepository: JobFolderRepository = mock[JobFolderRepository]
when(mockMappingJobRepository.getCachedMappingsJobs).thenReturn(mutable.Map("testProject" -> mutable.Map("testJob" -> testJob)))
}

}

0 comments on commit 1a8552f

Please sign in to comment.