diff --git a/src/main/kotlin/fr/nicopico/n2rss/mail/EmailChecker.kt b/src/main/kotlin/fr/nicopico/n2rss/mail/EmailChecker.kt index 62b9b26..e7dc3b7 100644 --- a/src/main/kotlin/fr/nicopico/n2rss/mail/EmailChecker.kt +++ b/src/main/kotlin/fr/nicopico/n2rss/mail/EmailChecker.kt @@ -23,6 +23,7 @@ import fr.nicopico.n2rss.monitoring.MonitoringService import fr.nicopico.n2rss.newsletter.NewsletterConfiguration import fr.nicopico.n2rss.newsletter.data.PublicationRepository import fr.nicopico.n2rss.newsletter.handlers.NewsletterHandler +import fr.nicopico.n2rss.newsletter.handlers.exception.NoPublicationFoundException import fr.nicopico.n2rss.newsletter.handlers.process import jakarta.annotation.PostConstruct import org.slf4j.LoggerFactory @@ -65,7 +66,11 @@ class EmailChecker( try { getNewsletterHandler(email) ?.process(email) - ?.also { + ?.also { publications -> + // At least one of the publications must have articles + if (publications.all { it.articles.isEmpty() }) { + throw NoPublicationFoundException() + } emailClient.markAsRead(email) } } catch (e: Exception) { diff --git a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/AndroidWeeklyNewsletterHandler.kt b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/AndroidWeeklyNewsletterHandler.kt index c227b49..b5f3676 100644 --- a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/AndroidWeeklyNewsletterHandler.kt +++ b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/AndroidWeeklyNewsletterHandler.kt @@ -17,11 +17,12 @@ */ package fr.nicopico.n2rss.newsletter.handlers +import fr.nicopico.n2rss.mail.models.Email +import fr.nicopico.n2rss.newsletter.handlers.exception.NewsletterParsingException import fr.nicopico.n2rss.newsletter.handlers.jsoup.Section import fr.nicopico.n2rss.newsletter.handlers.jsoup.extractSections import fr.nicopico.n2rss.newsletter.handlers.jsoup.process import fr.nicopico.n2rss.newsletter.models.Article -import fr.nicopico.n2rss.mail.models.Email import fr.nicopico.n2rss.newsletter.models.Newsletter import fr.nicopico.n2rss.utils.url.toUrlOrNull import org.jsoup.Jsoup diff --git a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/BuiltForMarsNewsletterHandler.kt b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/BuiltForMarsNewsletterHandler.kt index 0eb1db0..309adbd 100644 --- a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/BuiltForMarsNewsletterHandler.kt +++ b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/BuiltForMarsNewsletterHandler.kt @@ -17,10 +17,11 @@ */ package fr.nicopico.n2rss.newsletter.handlers +import fr.nicopico.n2rss.mail.models.Email +import fr.nicopico.n2rss.newsletter.handlers.exception.NewsletterParsingException import fr.nicopico.n2rss.newsletter.handlers.jsoup.indexOf import fr.nicopico.n2rss.newsletter.handlers.jsoup.select import fr.nicopico.n2rss.newsletter.models.Article -import fr.nicopico.n2rss.mail.models.Email import fr.nicopico.n2rss.newsletter.models.Newsletter import org.jsoup.Jsoup import org.jsoup.nodes.Document diff --git a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/KotlinWeeklyNewsletterHandler.kt b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/KotlinWeeklyNewsletterHandler.kt index 70721ef..33511e9 100644 --- a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/KotlinWeeklyNewsletterHandler.kt +++ b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/KotlinWeeklyNewsletterHandler.kt @@ -17,11 +17,12 @@ */ package fr.nicopico.n2rss.newsletter.handlers +import fr.nicopico.n2rss.mail.models.Email +import fr.nicopico.n2rss.newsletter.handlers.exception.NewsletterParsingException import fr.nicopico.n2rss.newsletter.handlers.jsoup.Section import fr.nicopico.n2rss.newsletter.handlers.jsoup.extractSections import fr.nicopico.n2rss.newsletter.handlers.jsoup.process import fr.nicopico.n2rss.newsletter.models.Article -import fr.nicopico.n2rss.mail.models.Email import fr.nicopico.n2rss.newsletter.models.Newsletter import fr.nicopico.n2rss.utils.url.toUrlOrNull import org.jsoup.Jsoup diff --git a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/QuickBirdNewsletterHandler.kt b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/QuickBirdNewsletterHandler.kt index 0bc9ff1..8e3d9d7 100644 --- a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/QuickBirdNewsletterHandler.kt +++ b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/QuickBirdNewsletterHandler.kt @@ -17,8 +17,9 @@ */ package fr.nicopico.n2rss.newsletter.handlers -import fr.nicopico.n2rss.newsletter.models.Article import fr.nicopico.n2rss.mail.models.Email +import fr.nicopico.n2rss.newsletter.handlers.exception.NewsletterParsingException +import fr.nicopico.n2rss.newsletter.models.Article import fr.nicopico.n2rss.newsletter.models.Newsletter import org.jsoup.Jsoup import org.jsoup.nodes.TextNode diff --git a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/NewsletterParsingException.kt b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/exception/NewsletterParsingException.kt similarity index 89% rename from src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/NewsletterParsingException.kt rename to src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/exception/NewsletterParsingException.kt index 1c65b3b..5521e38 100644 --- a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/NewsletterParsingException.kt +++ b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/exception/NewsletterParsingException.kt @@ -15,6 +15,7 @@ * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ -package fr.nicopico.n2rss.newsletter.handlers -class NewsletterParsingException(message: String) : Exception(message) +package fr.nicopico.n2rss.newsletter.handlers.exception + +open class NewsletterParsingException(message: String) : Exception(message) diff --git a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/exception/NoPublicationFoundException.kt b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/exception/NoPublicationFoundException.kt new file mode 100644 index 0000000..5863819 --- /dev/null +++ b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/exception/NoPublicationFoundException.kt @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 Nicolas PICON + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED + * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +package fr.nicopico.n2rss.newsletter.handlers.exception + +class NoPublicationFoundException : NewsletterParsingException("No publication found in the newsletter") diff --git a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/legacy/LegacyPointer540NewsletterHandler.kt b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/legacy/LegacyPointer540NewsletterHandler.kt index 31815d6..065f624 100644 --- a/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/legacy/LegacyPointer540NewsletterHandler.kt +++ b/src/main/kotlin/fr/nicopico/n2rss/newsletter/handlers/legacy/LegacyPointer540NewsletterHandler.kt @@ -19,7 +19,7 @@ package fr.nicopico.n2rss.newsletter.handlers.legacy import fr.nicopico.n2rss.mail.models.Email import fr.nicopico.n2rss.newsletter.handlers.NewsletterHandlerSingleFeed -import fr.nicopico.n2rss.newsletter.handlers.NewsletterParsingException +import fr.nicopico.n2rss.newsletter.handlers.exception.NewsletterParsingException import fr.nicopico.n2rss.newsletter.models.Article import fr.nicopico.n2rss.newsletter.models.Newsletter import fr.nicopico.n2rss.utils.url.toUrlOrNull diff --git a/src/test/kotlin/fr/nicopico/n2rss/mail/EmailCheckerTest.kt b/src/test/kotlin/fr/nicopico/n2rss/mail/EmailCheckerTest.kt index c098184..0d4ee4c 100644 --- a/src/test/kotlin/fr/nicopico/n2rss/mail/EmailCheckerTest.kt +++ b/src/test/kotlin/fr/nicopico/n2rss/mail/EmailCheckerTest.kt @@ -23,6 +23,7 @@ import fr.nicopico.n2rss.mail.models.Email import fr.nicopico.n2rss.monitoring.MonitoringService import fr.nicopico.n2rss.newsletter.data.PublicationRepository import fr.nicopico.n2rss.newsletter.handlers.NewsletterHandler +import fr.nicopico.n2rss.newsletter.handlers.exception.NoPublicationFoundException import fr.nicopico.n2rss.newsletter.handlers.process import fr.nicopico.n2rss.newsletter.models.Publication import io.mockk.MockKAnnotations @@ -199,7 +200,7 @@ class EmailCheckerTest { } @Test - fun `emailChecker should not save empty publications`( + fun `emailChecker should notify an error on empty publications`( @MockK(relaxed = true) email: Email, @MockK publication: Publication, ) { @@ -208,12 +209,18 @@ class EmailCheckerTest { every { newsletterHandlerA.canHandle(email) } returns true every { newsletterHandlerA.process(email) } returns listOf(publication) every { publication.articles } returns emptyList() + every { monitoringService.notifyEmailProcessingError(any(), any()) } just Runs // When we check the email emailChecker.savePublicationsFromEmails() // Then the email should be marked as read without creating a publication - verify(exactly = 0) { publicationRepository.saveAll(eq(listOf(publication))) } - verify { emailClient.markAsRead(email) } + verify(exactly = 0) { + publicationRepository.saveAll(eq(listOf(publication))) + emailClient.markAsRead(email) + } + verify { + monitoringService.notifyEmailProcessingError(email, any(NoPublicationFoundException::class)) + } } }