Skip to content

Commit

Permalink
feat: detect empty newsletter as parsing error (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicopico-dev authored Oct 6, 2024
1 parent 61d15bb commit 9ac6bd8
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/main/kotlin/fr/nicopico/n2rss/mail/EmailChecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -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")
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions src/test/kotlin/fr/nicopico/n2rss/mail/EmailCheckerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
) {
Expand All @@ -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))
}
}
}

0 comments on commit 9ac6bd8

Please sign in to comment.