diff --git a/smtp-listeners/src/main/kotlin/no/nav/emottak/smtp/cpasync/CpaSyncService.kt b/smtp-listeners/src/main/kotlin/no/nav/emottak/smtp/cpasync/CpaSyncService.kt index 54c672b4..d9e8ea5b 100644 --- a/smtp-listeners/src/main/kotlin/no/nav/emottak/smtp/cpasync/CpaSyncService.kt +++ b/smtp-listeners/src/main/kotlin/no/nav/emottak/smtp/cpasync/CpaSyncService.kt @@ -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." } @@ -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) @@ -75,7 +79,7 @@ class CpaSyncService(private val cpaRepoClient: HttpClient, private val nfsConne } private fun getCpaIdFromCpaContent(cpaContent: String): String? { - return Regex("cppa:cpaid=\"(?.+?)\"") + return Regex("cpaid=\"(?.+?)\"") .find(cpaContent)?.groups?.get("cpaId")?.value } diff --git a/smtp-listeners/src/test/kotlin/no/nav/emottak/smtp/cpasync/CpaSyncServiceTest.kt b/smtp-listeners/src/test/kotlin/no/nav/emottak/smtp/cpasync/CpaSyncServiceTest.kt index f88a3797..b2d89309 100644 --- a/smtp-listeners/src/test/kotlin/no/nav/emottak/smtp/cpasync/CpaSyncServiceTest.kt +++ b/smtp-listeners/src/test/kotlin/no/nav/emottak/smtp/cpasync/CpaSyncServiceTest.kt @@ -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 @@ -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 { - 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 @@ -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() + 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")