Skip to content

Commit

Permalink
Add test cases for selected topics in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
waisingyiu committed Apr 11, 2024
1 parent 60c8272 commit 4f5ebdf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AndroidSender(val config: FcmWorkerConfiguration, val firebaseAppName: Opt
override def deliverChunkedTokens(chunkedTokenStream: Stream[IO, (ChunkedTokens, Long, Instant, Int)]): Stream[IO, Unit] = {
chunkedTokenStream.map {
case (chunkedTokens, sentTime, functionStartTime, sqsMessageBatchSize) =>
if (isIndividualSend(chunkedTokens.notification.topic, config.allowedTopicsForIndividualSend))
if (config.isIndividualSend(chunkedTokens.notification.topic.map(_.toString())))
deliverIndividualNotificationStream(Stream.emits(chunkedTokens.toNotificationToSends).covary[IO])
.broadcastTo(
reportSuccesses(chunkedTokens, sentTime, functionStartTime, sqsMessageBatchSize),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ case class FcmWorkerConfiguration(
fcmConfig: FcmConfig,
threadPoolSize: Int,
allowedTopicsForIndividualSend: List[String],
) extends WorkerConfiguration
) extends WorkerConfiguration {
def isIndividualSend(topics: List[String]): Boolean =
topics.forall(topic => allowedTopicsForIndividualSend.exists(topic.startsWith(_)))
}

case class CleanerConfiguration(jdbcConfig: JdbcConfig)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import play.api.libs.json.{Format, Json, JsError, JsValue, JsSuccess}
import com.google.auth.oauth2.GoogleCredentials
import com.google.api.client.json.{JsonFactory, JsonGenerator}
import com.google.firebase.messaging._
import com.google.firebase.ErrorCode
import com.gu.notifications.worker.delivery.FcmPayload
import com.gu.notifications.worker.delivery.fcm.models.payload.{FcmResponse, FcmError, FcmErrorPayload}
import java.net.http.HttpRequest
Expand Down Expand Up @@ -73,7 +74,8 @@ class FcmTransportJdkImpl(credential: GoogleCredentials, url: String, jsonFactor
val invalidTokenErrorCodes = Set(
MessagingErrorCode.INVALID_ARGUMENT,
MessagingErrorCode.UNREGISTERED,
MessagingErrorCode.SENDER_ID_MISMATCH).map(_.name())
MessagingErrorCode.SENDER_ID_MISMATCH,
ErrorCode.PERMISSION_DENIED).map(_.name())

val internalServerErrorCodes = Set(
MessagingErrorCode.UNAVAILABLE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.gu.notifications.worker

import org.specs2.matcher.Matchers
import org.specs2.mutable.Specification
import org.specs2.specification.Scope
import com.gu.notifications.worker.delivery.fcm.models.FcmConfig

class FcmWorkerConfigurationSpec extends Specification with Matchers {

"FcmConfiguration" should {
"use individual send API if the topic is in the selected list" in new TestDataScope {
val config = createConfiguration(List("breaking/uk"))
config.isIndividualSend(List("breaking/uk")) must beTrue
}

"use individual send API if the prefix of topic is in the selected list" in new TestDataScope {
val config = createConfiguration(List("breaking/"))
config.isIndividualSend(List("breaking/uk")) must beTrue
}

"use batch send API if the topic is not in the selected list" in new TestDataScope {
val config = createConfiguration(List("breaking/uk"))
config.isIndividualSend(List("breaking/international")) must beFalse
}

"use batch send API if the prefix of topic is not in the selected list" in new TestDataScope {
val config = createConfiguration(List("breaking/"))
config.isIndividualSend(List("contributor/")) must beFalse
}

"use individual send API if the prefix of topic matches one of the selected topics" in new TestDataScope {
val config = createConfiguration(List("breaking/", "contributor/"))
config.isIndividualSend(List("breaking/uk")) must beTrue
}

"use batch send API if the prefix of topic matches none of the selected topics" in new TestDataScope {
val config = createConfiguration(List("breaking/", "contributor/"))
config.isIndividualSend(List("tag/food")) must beFalse
}

"use individual send API if the prefix of every topic matches one of the selected topics" in new TestDataScope {
val config = createConfiguration(List("breaking/", "contributor/"))
config.isIndividualSend(List("breaking/uk", "breaking/us", "breaking/international")) must beTrue
}

"use batch send API if the prefix of any topic do not match one of the selected topics" in new TestDataScope {
val config = createConfiguration(List("tag/", "contributor/"))
config.isIndividualSend(List("tag/food", "tag/culture", "breaking/international")) must beFalse
}
}

trait TestDataScope extends Scope {
def createConfiguration(selectedTopics: List[String]): FcmWorkerConfiguration =
FcmWorkerConfiguration(
cleaningSqsUrl = "cleaning-sqs-url",
fcmConfig = FcmConfig(serviceAccountKey = "key", debug = false, dryRun = false),
threadPoolSize = 50,
allowedTopicsForIndividualSend = selectedTopics)
}
}

0 comments on commit 4f5ebdf

Please sign in to comment.