Skip to content

Commit

Permalink
[16144] Added code for tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
wcutshall committed Nov 14, 2024
1 parent 1102247 commit d0517ec
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class FHIRTranslator(
val bodyBytes = BlobAccess.downloadBlobAsByteArray(originalReport.bodyUrl)
val localDigest = BlobUtils.digestToString(sha256Digest(bodyBytes))
check(message.digest == localDigest) {
"Downloaded file does not match expected file\n$message.digest | $localDigest"
"Downloaded file does not match expected file\n${message.digest} | $localDigest"
}

// get a Report from the message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import gov.cdc.prime.router.azure.DatabaseAccess
import gov.cdc.prime.router.azure.db.enums.TaskAction
import gov.cdc.prime.router.azure.db.tables.pojos.Action
import gov.cdc.prime.router.azure.db.tables.pojos.ReportFile
import gov.cdc.prime.router.azure.observability.event.AzureEventService
import gov.cdc.prime.router.fhirengine.utils.FhirTranscoder
import gov.cdc.prime.router.report.ReportService
import gov.cdc.prime.router.unittest.UnitTestUtils
Expand Down Expand Up @@ -78,6 +79,7 @@ class FhirTranslatorTests {
)
)
val reportServiceMock = mockk<ReportService>()
val azureEventService = mockk<AzureEventService>()

private fun makeFhirEngine(
metadata: Metadata = Metadata(
Expand All @@ -90,7 +92,8 @@ class FhirTranslatorTests {
settings: SettingsProvider = FileSettings().loadOrganizations(oneOrganization),
): FHIRTranslator {
return FHIREngine.Builder().metadata(metadata).settingsProvider(settings).databaseAccess(accessSpy)
.blobAccess(blobMock).reportService(reportServiceMock).build(TaskAction.translate) as FHIRTranslator
.blobAccess(blobMock).reportService(reportServiceMock).azureEventService(azureEventService)
.build(TaskAction.translate) as FHIRTranslator
}

@BeforeEach
Expand Down Expand Up @@ -165,6 +168,8 @@ class FhirTranslatorTests {
every { reportServiceMock.getRootReport(any()) } returns rootReport
every { reportServiceMock.getRootReports(any()) } returns listOf(rootReport)
every { reportServiceMock.getRootItemIndex(any(), any()) } returns 1
every { BlobAccess.downloadBlobAsByteArray(any()) } returns "1".toByteArray(Charsets.UTF_8)
every { azureEventService.trackEvent(any()) } returns Unit

// act
accessSpy.transact { txn ->
Expand All @@ -178,6 +183,88 @@ class FhirTranslatorTests {
BlobAccess.Companion.uploadBlob(any(), any(), any())
accessSpy.insertTask(any(), any(), any(), any(), any())
actionHistory.trackActionReceiverInfo(any(), any())
azureEventService.trackEvent(any())
}
}

@Test
fun `test translation happy path with file digest exception`() {
mockkObject(BlobAccess)
mockkObject(BlobAccess.BlobContainerMetadata)

// set up
val actionHistory = mockk<ActionHistory>()
val actionLogger = mockk<ActionLogger>()
val engine = makeFhirEngine()

val reportId = UUID.randomUUID()
val message =
spyk(
FhirTranslateQueueMessage(
reportId,
BLOB_URL,
"test",
BLOB_SUB_FOLDER,
topic = Topic.ELR_ELIMS,
oneOrganization.receivers[0].fullName
)
)

val bodyFormat = MimeFormat.FHIR
val bodyUrl = BODY_URL
val rootReport = mockk<ReportFile>()

every { actionLogger.hasErrors() } returns false
every { actionLogger.error(any<ActionLogDetail>()) } returns Unit
every { BlobAccess.downloadBlob(any(), any()) }
.returns(File(VALID_DATA_URL).readText())
every { BlobAccess.Companion.uploadBlob(any(), any()) } returns "test"
every {
BlobAccess.BlobContainerMetadata.build(
"metadata",
any()
)
} returns mockk<BlobAccess.BlobContainerMetadata>()
every { accessSpy.insertTask(any(), bodyFormat.toString(), bodyUrl, any()) }.returns(Unit)
every { actionHistory.trackCreatedReport(any(), any(), blobInfo = any()) }.returns(Unit)
every { actionHistory.trackExistingInputReport(any()) }.returns(Unit)
every { actionHistory.trackActionReceiverInfo(any(), any()) }.returns(Unit)
every { actionHistory.action }.returns(
Action(
1,
TaskAction.receive,
"",
"",
OffsetDateTime.now(),
JSONB.valueOf(""),
1,
1,
"",
"",
"",
"",
"",
"",
""
)
)
every { rootReport.reportId } returns reportId
every { rootReport.sendingOrg } returns oneOrganization.name
every { rootReport.sendingOrgClient } returns oneOrganization.receivers[0].fullName
every { rootReport.bodyUrl } returns BLOB_URL
every { reportServiceMock.getRootReport(any()) } returns rootReport
every { reportServiceMock.getRootReports(any()) } returns listOf(rootReport)
every { reportServiceMock.getRootItemIndex(any(), any()) } returns 1
every { BlobAccess.downloadBlobAsByteArray(any()) } returns "1".toByteArray(Charsets.UTF_8)

// act
@Suppress("ktlint:standard:max-line-length")
accessSpy.transact { txn ->
assertFailsWith<IllegalStateException>(
message = "Downloaded file does not match expected file\n" +
"test | 6bffffff86ffffffb273ffffffff34fffffffcffffffe1ffffff9d6bffffff804effffffff5a3f5747ffffffadffffffa4ffffffeaffffffa22f1d49ffffffc01e52ffffffddffffffb7ffffff875b4b",
block = { engine.run(message, actionLogger, actionHistory, txn) }
)
}
}

Expand Down Expand Up @@ -248,6 +335,7 @@ class FhirTranslatorTests {
every { reportServiceMock.getRootReport(any()) } returns rootReport
every { reportServiceMock.getRootReports(any()) } returns listOf(rootReport)
every { reportServiceMock.getRootItemIndex(any(), any()) } returns 1
every { azureEventService.trackEvent(any()) } returns Unit

// act
accessSpy.transact { txn ->
Expand Down

0 comments on commit d0517ec

Please sign in to comment.