Skip to content

Commit

Permalink
Fix regex for finding CPA ID + sync should not abort when failing to …
Browse files Browse the repository at this point in the history
…find CPA ID
  • Loading branch information
GardOS committed Sep 11, 2024
1 parent c467e44 commit c0b0e36
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CpaSyncService(private val cpaRepoClient: HttpClient, private val nfsConne
connector.folder().asSequence()
.filter { entry -> isXmlFileEntry(entry) }
.fold(mutableMapOf()) { accumulator, nfsCpaFile ->
val nfsCpa = getNfsCpa(connector, nfsCpaFile)
val nfsCpa = getNfsCpa(connector, nfsCpaFile) ?: return accumulator

val existingEntry = accumulator.put(nfsCpa.id, nfsCpa)
require(existingEntry == null) { "NFS contains duplicate CPA IDs. Aborting sync." }
Expand All @@ -55,14 +55,18 @@ class CpaSyncService(private val cpaRepoClient: HttpClient, private val nfsConne
return false
}

internal fun getNfsCpa(connector: NFSConnector, nfsCpaFile: ChannelSftp.LsEntry): NfsCpa {
internal fun getNfsCpa(connector: NFSConnector, nfsCpaFile: ChannelSftp.LsEntry): NfsCpa? {
val timestamp = getLastModified(nfsCpaFile.attrs.mTime.toLong())
val cpaContent = fetchNfsCpaContent(connector, nfsCpaFile)
val cpaId = getCpaIdFromCpaContent(cpaContent)
require(cpaId != null) {
"Regex to find CPA ID in file ${nfsCpaFile.filename} did not find any match. " +
"File corrupted or wrongful regex. Aborting sync."

if (cpaId == null) {
log.warn("Regex to find CPA ID in file ${nfsCpaFile.filename} did not find any match. " +
"File corrupted or wrongful regex. Aborting sync."
)
return null
}

val zippedCpaContent = zipCpaContent(cpaContent)

return NfsCpa(cpaId, timestamp, zippedCpaContent)
Expand All @@ -75,7 +79,7 @@ class CpaSyncService(private val cpaRepoClient: HttpClient, private val nfsConne
}

private fun getCpaIdFromCpaContent(cpaContent: String): String? {
return Regex("cppa:cpaid=\"(?<cpaId>.+?)\"")
return Regex("cpaid=\"(?<cpaId>.+?)\"")
.find(cpaContent)?.groups?.get("cpaId")?.value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class CpaSyncServiceTest {
val cpaSyncService = CpaSyncService(mockCpaRepoClient, mockNfs)
val nfsCpa = cpaSyncService.getNfsCpa(mockNfs, lsEntry)

assertEquals("nav:qass:12345", nfsCpa.id)
assertEquals("nav:qass:12345", nfsCpa?.id)
}

@Test
Expand All @@ -98,21 +98,18 @@ class CpaSyncServiceTest {
val cpaSyncService = CpaSyncService(mockCpaRepoClient, mockNfs)
val nfsCpa = cpaSyncService.getNfsCpa(mockNfs, lsEntry)

assertEquals("nav:qass:12345", nfsCpa.id)
assertEquals("nav:qass:12345", nfsCpa?.id)
}

@Test
fun `should throw exception if CPA ID is missing in content`() = runBlocking {
fun `should return null if cpa ID is not found`() = runBlocking {
val lsEntry = mockLsEntry("nav.qass.missing.txt", "2025-01-01T00:00:00Z")
val mockNfs = mockNfsFromEntries(listOf(lsEntry), listOf(""))

val cpaSyncService = CpaSyncService(mockCpaRepoClient, mockNfs)
val cpa = cpaSyncService.getNfsCpa(mockNfs, lsEntry)

val exception = assertThrows<IllegalArgumentException> {
cpaSyncService.getNfsCpa(mockNfs, lsEntry)
}

assertTrue(exception.message!!.contains("Regex to find CPA ID in file nav.qass.missing.txt did not find any match."))
assertTrue(cpa == null)
}

@Test
Expand Down Expand Up @@ -157,6 +154,21 @@ class CpaSyncServiceTest {
assertTrue(exception.message!!.contains("NFS contains duplicate CPA IDs. Aborting sync."))
}

@Test
fun `should skip processing if cpa ID is not found in CPA`() = runBlocking {
val lsEntry = mockLsEntry("nav.qass.missing.txt", "2025-01-01T00:00:00Z")
val mockNfs = mockNfsFromEntries(listOf(lsEntry), listOf(""))

val dbCpa = emptyMap<String, String>()
mockCpaRepoFromMap(dbCpa)

val cpaSyncService = CpaSyncService(mockCpaRepoClient, mockNfs)
cpaSyncService.sync()

coVerify(exactly = 0) { mockCpaRepoClient.putCPAinCPARepo(any(), any()) }
coVerify(exactly = 0) { mockCpaRepoClient.deleteCPAinCPARepo(any()) }
}

@Test
fun `sync should do nothing when all entries match database`() = runBlocking {
val nfsCpa = mapOf("nav:qass:12345" to "2024-01-01T00:00:00Z")
Expand Down

0 comments on commit c0b0e36

Please sign in to comment.