Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move newsletter request outside NewsletterService #148

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package fr.nicopico.n2rss.controller.home

import fr.nicopico.n2rss.config.N2RssProperties
import fr.nicopico.n2rss.monitoring.MonitoringService
import fr.nicopico.n2rss.newsletter.models.GroupedNewsletterInfo
import fr.nicopico.n2rss.newsletter.models.toGroupedNewsletterInfo
import fr.nicopico.n2rss.newsletter.service.NewsletterService
Expand All @@ -44,6 +45,7 @@ import java.net.URL
class HomeController(
private val newsletterService: NewsletterService,
private val reCaptchaService: ReCaptchaService,
private val monitoringService: MonitoringService,
private val feedProperties: N2RssProperties.FeedsProperties,
private val recaptchaProperties: N2RssProperties.ReCaptchaProperties,
) {
Expand Down Expand Up @@ -100,7 +102,7 @@ class HomeController(
val url = with(newsletterUrl) {
if (contains("://")) newsletterUrl else "https://$newsletterUrl"
}.let { URL(it) }
newsletterService.saveNewsletterRequest(url)
monitoringService.notifyNewsletterRequest(url)
ResponseEntity.ok().build()
} else {
ResponseEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import kotlinx.datetime.format.char
import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.net.URL

@Service
Expand Down Expand Up @@ -103,7 +104,15 @@ class GithubMonitoringService(
}

@Async
override fun notifyRequest(uniqueUrl: URL) {
@Transactional
override fun notifyNewsletterRequest(newsletterUrl: URL) {
// Sanitize URL
val uniqueUrl = URL(
/* protocol = */ "https",
/* host = */ newsletterUrl.host,
/* port = */ newsletterUrl.port,
/* file = */ "",
)
try {
val today = clock.now().format(DateTimeComponents.Format {
year(Padding.ZERO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ interface MonitoringService {
fun notifyEmailClientError(error: Exception)
@Async
fun notifyEmailProcessingError(email: Email, error: Exception)

/**
* Saves a given newsletter request by sanitizing the provided URL and notifying the monitoring service.
*
* @param newsletterUrl The URL of the newsletter request to be saved.
*/
@Async
fun notifyRequest(uniqueUrl: URL)
fun notifyNewsletterRequest(newsletterUrl: URL)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class NoOpMonitoringService : MonitoringService {
// No-op
}

override fun notifyRequest(uniqueUrl: URL) {
override fun notifyNewsletterRequest(newsletterUrl: URL) {
// No-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,17 @@
package fr.nicopico.n2rss.newsletter.service

import fr.nicopico.n2rss.mail.models.Email
import fr.nicopico.n2rss.monitoring.MonitoringService
import fr.nicopico.n2rss.newsletter.data.NewsletterRepository
import fr.nicopico.n2rss.newsletter.handlers.NewsletterHandler
import fr.nicopico.n2rss.newsletter.models.Newsletter
import fr.nicopico.n2rss.newsletter.models.NewsletterInfo
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.net.URL

@Service
class NewsletterService(
private val newsletterRepository: NewsletterRepository,
private val publicationService: PublicationService,
private val monitoringService: MonitoringService,
) {
/**
* Retrieves information on all enabled newsletters.
Expand Down Expand Up @@ -85,23 +81,6 @@ class NewsletterService(
}
}

/**
* Saves a given newsletter request by sanitizing the provided URL and notifying the monitoring service.
*
* @param newsletterUrl The URL of the newsletter request to be saved.
*/
@Transactional
fun saveNewsletterRequest(newsletterUrl: URL) {
// Sanitize URL
val uniqueUrl = URL(
/* protocol = */ "https",
/* host = */ newsletterUrl.host,
/* port = */ newsletterUrl.port,
/* file = */ "",
)
monitoringService.notifyRequest(uniqueUrl)
}

companion object {
private val LOG = LoggerFactory.getLogger(NewsletterService::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package fr.nicopico.n2rss.controller.home

import fr.nicopico.n2rss.config.N2RssProperties
import fr.nicopico.n2rss.monitoring.MonitoringService
import fr.nicopico.n2rss.newsletter.models.GroupedNewsletterInfo
import fr.nicopico.n2rss.newsletter.models.NewsletterInfo
import fr.nicopico.n2rss.newsletter.service.NewsletterService
Expand Down Expand Up @@ -47,6 +48,8 @@ class HomeControllerTest {
@MockK
private lateinit var reCaptchaService: ReCaptchaService
@MockK
private lateinit var monitoringService: MonitoringService
@MockK
private lateinit var feedsProperties: N2RssProperties.FeedsProperties
@MockK(relaxed = true)
private lateinit var reCaptchaProperties: N2RssProperties.ReCaptchaProperties
Expand All @@ -62,6 +65,7 @@ class HomeControllerTest {
homeController = HomeController(
newsletterService = newsletterService,
reCaptchaService = reCaptchaService,
monitoringService = monitoringService,
feedProperties = feedsProperties,
recaptchaProperties = reCaptchaProperties
)
Expand Down Expand Up @@ -246,14 +250,14 @@ class HomeControllerTest {
// SETUP
every { reCaptchaProperties.enabled } returns true
every { reCaptchaProperties.secretKey } returns captchaSecretKey
every { newsletterService.saveNewsletterRequest(any()) } just Runs
every { monitoringService.notifyNewsletterRequest(any()) } just Runs
every { reCaptchaService.isCaptchaValid(any(), any()) } returns true

// WHEN
val response = homeController.requestNewsletter(newsletterUrl, captchaResponse, userAgent)

// THEN
verify { newsletterService.saveNewsletterRequest(URL(newsletterUrl)) }
verify { monitoringService.notifyNewsletterRequest(URL(newsletterUrl)) }
verify { reCaptchaService.isCaptchaValid(captchaSecretKey, captchaResponse) }
response.statusCode shouldBe HttpStatus.OK
}
Expand All @@ -274,7 +278,7 @@ class HomeControllerTest {
val response = homeController.requestNewsletter(newsletterUrl, captchaResponse, userAgent)

// THEN
verify(exactly = 0) { newsletterService.saveNewsletterRequest(any()) }
verify(exactly = 0) { monitoringService.notifyNewsletterRequest(any()) }
response.statusCode shouldBe HttpStatus.BAD_REQUEST
}

Expand All @@ -285,13 +289,13 @@ class HomeControllerTest {

// SETUP
every { reCaptchaProperties.enabled } returns false
every { newsletterService.saveNewsletterRequest(any()) } just Runs
every { monitoringService.notifyNewsletterRequest(any()) } just Runs

// WHEN
val response = homeController.requestNewsletter(newsletterUrl, userAgent = userAgent)

// THEN
verify(exactly = 1) { newsletterService.saveNewsletterRequest(any()) }
verify(exactly = 1) { monitoringService.notifyNewsletterRequest(any()) }
verify(exactly = 0) { reCaptchaService.isCaptchaValid(any(), any()) }
response.statusCode shouldBe HttpStatus.OK
}
Expand All @@ -304,14 +308,14 @@ class HomeControllerTest {

// SETUP
every { reCaptchaProperties.enabled } returns false
every { newsletterService.saveNewsletterRequest(any()) } just Runs
every { monitoringService.notifyNewsletterRequest(any()) } just Runs

// WHEN
homeController.requestNewsletter(newsletterUrl, captchaResponse, userAgent)

// THEN
val slotUrl = slot<URL>()
verify { newsletterService.saveNewsletterRequest(capture(slotUrl)) }
verify { monitoringService.notifyNewsletterRequest(capture(slotUrl)) }
slotUrl.captured.toString() shouldBe "https://www.google.com"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ class GithubMonitoringServiceTest {
}
//endregion

//region notifyRequest
//region notifyNewsletterRequest
@Test
fun `notifyRequest should create a new GitHub issue and save it to the repository`() {
fun `notifyNewsletterRequest should create a new GitHub issue and save it to the repository`() {
// GIVEN
val newsletterUrl = URL("https://www.androidweekly.net")
val issueId = IssueId(Random.nextInt())
Expand All @@ -285,7 +285,7 @@ class GithubMonitoringServiceTest {
}

// WHEN
monitoringService.notifyRequest(newsletterUrl)
monitoringService.notifyNewsletterRequest(newsletterUrl)

// THEN
val bodySlot = slot<String>()
Expand Down Expand Up @@ -313,7 +313,7 @@ class GithubMonitoringServiceTest {
}

@Test
fun `notifyRequest should add a comment to an existing GitHub issue`() {
fun `notifyNewsletterRequest should add a comment to an existing GitHub issue`() {
// GIVEN
val newsletterUrl = URL("https://www.androidweekly.net")
val issueId = IssueId(Random.nextInt())
Expand All @@ -327,7 +327,7 @@ class GithubMonitoringServiceTest {
every { client.addCommentToIssue(any(), any()) } just Runs

// WHEN
monitoringService.notifyRequest(newsletterUrl)
monitoringService.notifyNewsletterRequest(newsletterUrl)

// THEN
verifySequence {
Expand All @@ -338,18 +338,47 @@ class GithubMonitoringServiceTest {
}

@Test
fun `notifyRequest should not throw if a GithubException occurs`() {
fun `notifyNewsletterRequest should not throw if a GithubException occurs`() {
// SETUP
every { repository.findNewsletterRequest(any()) } returns null
every { client.createIssue(any(), any(), any()) } throws GithubException("Some GitHub error !")

// WHEN
shouldNotThrowAny {
monitoringService.notifyRequest(URL("https://github.com/test"))
monitoringService.notifyNewsletterRequest(URL("https://github.com/test"))
}

// THEN
verify(exactly = 0) { repository.save(any<GithubIssueData.NewsletterRequest>()) }
}

@Test
fun `notifyNewsletterRequest should unify the newsletter url`() {
// GIVEN
@Suppress("HttpUrlsUsage")
val newsletterUrl = URL("http://www.nicopico.com/test/")
val uniqueUrl = URL("https://www.nicopico.com")
val issueId = IssueId(Random.nextInt())

// SETUP
every { repository.findNewsletterRequest(any()) } returns null
every { client.createIssue(any(), any(), any()) } returns issueId
every { repository.save(any<GithubIssueData.NewsletterRequest>()) } answers {
firstArg<GithubIssueData.NewsletterRequest>()
}

// WHEN
monitoringService.notifyNewsletterRequest(newsletterUrl)

// THEN
verify {
repository.save(
match<GithubIssueData.NewsletterRequest> {
it.issueId == issueId
&& it.newsletterUrl.toURI() == uniqueUrl.toURI()
}
)
}
}
//endregion
}
Loading