-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3906 from nationalarchives/TDRD-222-transfer-subm…
…itted-sns [TDRD 222] Send transfer submitted message to notifications sns topic
- Loading branch information
Showing
9 changed files
with
149 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package services | ||
|
||
import configuration.ApplicationConfig | ||
import io.circe.Encoder | ||
import io.circe.generic.semiauto.deriveEncoder | ||
import io.circe.syntax.EncoderOps | ||
import services.MessagingService.TransferCompleteEvent | ||
import software.amazon.awssdk.services.sns.SnsClient | ||
import software.amazon.awssdk.services.sns.model.PublishResponse | ||
import uk.gov.nationalarchives.aws.utils.sns.SNSClients.sns | ||
import uk.gov.nationalarchives.aws.utils.sns.SNSUtils | ||
|
||
import javax.inject.Inject | ||
import scala.concurrent.ExecutionContext | ||
|
||
class MessagingService @Inject() (val applicationConfig: ApplicationConfig)(implicit val ec: ExecutionContext) { | ||
val client: SnsClient = sns(applicationConfig.snsEndpoint) | ||
val utils: SNSUtils = SNSUtils(client) | ||
|
||
implicit val transferCompletedEventEncoder: Encoder[TransferCompleteEvent] = deriveEncoder[TransferCompleteEvent] | ||
def sendTransferCompleteNotification(transferCompletedEvent: TransferCompleteEvent): PublishResponse = { | ||
utils.publish(transferCompletedEvent.asJson.toString, applicationConfig.notificationSnsTopicArn) | ||
} | ||
} | ||
|
||
object MessagingService { | ||
case class TransferCompleteEvent( | ||
transferringBodyName: Option[String], | ||
consignmentReference: String, | ||
consignmentId: String, | ||
seriesName: Option[String], | ||
userId: String, | ||
userEmail: String | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package services | ||
|
||
import configuration.ApplicationConfig | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import uk.gov.nationalarchives.aws.utils.sns.SNSUtils | ||
import org.mockito.Mockito.{verify, when} | ||
import org.scalatestplus.mockito.MockitoSugar.mock | ||
import play.api.Configuration | ||
import software.amazon.awssdk.services.sns.SnsClient | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
class MessagingServiceSpec extends AnyFlatSpec with Matchers { | ||
|
||
implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global | ||
val mockSnsClient: SnsClient = mock[SnsClient] | ||
val mockUtils: SNSUtils = mock[SNSUtils] | ||
val config: Configuration = mock[Configuration] | ||
|
||
"sendTransferCompleteNotification" should "call publish with the correct parameters" in { | ||
val testArn = "arn:test-arn" | ||
when(config.get[String]("sns.endpoint")).thenReturn("http://localhost:9009") | ||
when(config.get[String]("notificationSnsTopicArn")).thenReturn(testArn) | ||
val appConfig = new ApplicationConfig(config) | ||
val service = new MessagingService(appConfig)(ec) { | ||
override val client: SnsClient = mockSnsClient | ||
override val utils: SNSUtils = mockUtils | ||
} | ||
val transferCompleteEvent = MessagingService.TransferCompleteEvent( | ||
transferringBodyName = Some("TransferringBodyName"), | ||
consignmentReference = "Ref123", | ||
consignmentId = "ConsID456", | ||
seriesName = Some("SeriesXYZ"), | ||
userId = "UserID789", | ||
userEmail = "user@example.com" | ||
) | ||
val expectedMessageString = """{ | ||
| "transferringBodyName" : "TransferringBodyName", | ||
| "consignmentReference" : "Ref123", | ||
| "consignmentId" : "ConsID456", | ||
| "seriesName" : "SeriesXYZ", | ||
| "userId" : "UserID789", | ||
| "userEmail" : "user@example.com" | ||
|}""".stripMargin | ||
service.sendTransferCompleteNotification(transferCompleteEvent) | ||
verify(mockUtils).publish(expectedMessageString, testArn) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters