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

EIP1-11114: Review 5 - add pending register check archiving queue #121

Open
wants to merge 3 commits into
base: EIP1-11114-review-4-merge-ems-api-tests
Choose a base branch
from
Open
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 @@ -9,13 +9,14 @@ import org.springframework.context.annotation.Configuration
import software.amazon.awssdk.services.sqs.SqsAsyncClient
import uk.gov.dluhc.messagingsupport.MessageQueue
import uk.gov.dluhc.messagingsupport.MessagingConfigurationHelper
import uk.gov.dluhc.registercheckerapi.messaging.models.PendingRegisterCheckArchiveMessage
import uk.gov.dluhc.registercheckerapi.messaging.models.RegisterCheckResultMessage

@Configuration
class MessagingConfiguration {

@Value("\${sqs.confirm-applicant-register-check-result-queue-name}")
private lateinit var confirmRegisterCheckResultQueueName: String
private lateinit var pendingRegisterCheckResultQueueName: String

@Value("\${sqs.postal-vote-confirm-applicant-register-check-result-queue-name}")
private lateinit var postalVoteConfirmRegisterCheckResultQueueName: String
Expand All @@ -29,9 +30,12 @@ class MessagingConfiguration {
@Value("\${sqs.register-check-result-response-queue-name}")
private lateinit var registerCheckResultResponseQueueName: String

@Value("\${sqs.pending-register-check-archive-queue-name}")
private lateinit var pendingRegisterCheckArchiveQueueName: String

@Bean(name = ["confirmRegisterCheckResultQueue"])
fun confirmRegisterCheckResultQueue(sqsTemplate: SqsTemplate) =
MessageQueue<RegisterCheckResultMessage>(confirmRegisterCheckResultQueueName, sqsTemplate)
MessageQueue<RegisterCheckResultMessage>(pendingRegisterCheckResultQueueName, sqsTemplate)

@Bean(name = ["postalVoteConfirmRegisterCheckResultQueue"])
fun postalVoteConfirmRegisterCheckResultQueue(sqsTemplate: SqsTemplate) =
Expand All @@ -49,6 +53,10 @@ class MessagingConfiguration {
fun registerCheckResultResponseQueue(sqsTemplate: SqsTemplate) =
MessageQueue<RegisterCheckResultMessage>(registerCheckResultResponseQueueName, sqsTemplate)

@Bean(name = ["pendingRegisterCheckArchiveQueue"])
fun pendingRegisterCheckArchiveQueue(sqsTemplate: SqsTemplate) =
MessageQueue<PendingRegisterCheckArchiveMessage>(pendingRegisterCheckResultQueueName, sqsTemplate)

@Bean
fun sqsMessagingMessageConverter(
objectMapper: ObjectMapper,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uk.gov.dluhc.emsintegrationapi.exception

import uk.gov.dluhc.emsintegrationapi.database.entity.CheckStatus

class PendingRegisterCheckArchiveInvalidStatusException(
status: CheckStatus,
) : IllegalStateException("Register Check is at status $status so cannot be archived (must be at status PENDING")
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import java.util.UUID
/**
* Thrown if a pending register check for a given correlationId does not exist.
*/
class PendingRegisterCheckNotFoundException(correlationId: UUID) :
RuntimeException("Pending register check for requestid:[$correlationId] not found")
class PendingRegisterCheckNotFoundException(
correlationId: UUID,
) : RuntimeException("Pending register check for requestid:[$correlationId] not found")
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uk.gov.dluhc.emsintegrationapi.messaging
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Component
import uk.gov.dluhc.messagingsupport.MessageQueue
import uk.gov.dluhc.registercheckerapi.messaging.models.PendingRegisterCheckArchiveMessage
import uk.gov.dluhc.registercheckerapi.messaging.models.RegisterCheckResultMessage
import uk.gov.dluhc.registercheckerapi.messaging.models.SourceType
import uk.gov.dluhc.registercheckerapi.messaging.models.SourceType.APPLICATIONS_MINUS_API
Expand All @@ -18,6 +19,7 @@ class MessageQueueResolver(
@Qualifier("proxyVoteConfirmRegisterCheckResultQueue") private val proxyVoteConfirmRegisterCheckResultQueue: MessageQueue<RegisterCheckResultMessage>,
@Qualifier("overseasVoteConfirmRegisterCheckResultQueue") private val overseasVoteConfirmRegisterCheckResultQueue: MessageQueue<RegisterCheckResultMessage>,
@Qualifier("registerCheckResultResponseQueue") private val registerCheckResultResponseQueue: MessageQueue<RegisterCheckResultMessage>,
@Qualifier("pendingRegisterCheckArchiveQueue") private val pendingRegisterCheckArchiveQueue: MessageQueue<PendingRegisterCheckArchiveMessage>,
) {

fun getTargetQueueForSourceType(sourceType: SourceType) =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package uk.gov.dluhc.emsintegrationapi.messaging

import io.awspring.cloud.sqs.annotation.SqsListener
import jakarta.validation.Valid
import mu.KotlinLogging
import org.springframework.messaging.handler.annotation.Payload
import org.springframework.stereotype.Component
import uk.gov.dluhc.emsintegrationapi.service.PendingRegisterCheckArchiveService
import uk.gov.dluhc.messagingsupport.MessageListener
import uk.gov.dluhc.registercheckerapi.messaging.models.PendingRegisterCheckArchiveMessage

private val logger = KotlinLogging.logger { }

/**
* Implementation of [MessageListener] to handle [PendingRegisterCheckArchiveMessage] messages
*/
@Component
class PendingRegisterCheckArchiveDataMessageListener(
private val pendingRegisterCheckArchiveService: PendingRegisterCheckArchiveService,
) : MessageListener<PendingRegisterCheckArchiveMessage> {
@SqsListener("\${sqs.pending-register-check-archive-queue-name}")
override fun handleMessage(
@Valid @Payload payload: PendingRegisterCheckArchiveMessage,
) {
with(payload) {
logger.info {
"New PendingRegisterCheckArchiveMessage received with " +
"correlationId: $correlationId"
}
pendingRegisterCheckArchiveService.archiveIfStatusIsPending(correlationId)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package uk.gov.dluhc.emsintegrationapi.service

import mu.KotlinLogging
import org.springframework.stereotype.Service
import uk.gov.dluhc.emsintegrationapi.database.entity.CheckStatus
import uk.gov.dluhc.emsintegrationapi.database.repository.RegisterCheckRepository
import uk.gov.dluhc.emsintegrationapi.exception.PendingRegisterCheckArchiveInvalidStatusException
import uk.gov.dluhc.emsintegrationapi.exception.PendingRegisterCheckNotFoundException
import java.time.Instant
import java.util.UUID

private val logger = KotlinLogging.logger { }

@Service
class PendingRegisterCheckArchiveService(
private val registerCheckRepository: RegisterCheckRepository,
) {
fun archiveIfStatusIsPending(correlationId: UUID?) {
val corrid = correlationId ?: throw IllegalArgumentException("Correlation ID is null")
val registerCheck = registerCheckRepository.findByCorrelationId(corrid)
if (registerCheck == null) {
logger.warn {
"Pending register check for requestid:[$correlationId] not found"
}
throw PendingRegisterCheckNotFoundException(corrid)
}
if (registerCheck.status == CheckStatus.PENDING) {
registerCheck.status = CheckStatus.ARCHIVED
registerCheck.matchResultSentAt = Instant.now()
registerCheckRepository.save(registerCheck)
} else {
logger.warn {
"Register Check with correlationId $correlationId has status ${registerCheck.status} so cannot be archived (must be at status PENDING)"
}
throw PendingRegisterCheckArchiveInvalidStatusException(registerCheck.status)
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ sqs:
overseas-vote-confirm-applicant-register-check-result-queue-name: ${SQS_OVERSEAS_VOTE_CONFIRM_APPLICANT_REGISTER_CHECK_RESULT_QUEUE_NAME}
register-check-result-response-queue-name: ${SQS_REGISTER_CHECK_RESULT_RESPONSE_QUEUE_NAME}
remove-applicant-register-check-data-queue-name: ${SQS_REMOVE_APPLICANT_REGISTER_CHECK_DATA_QUEUE_NAME}
pending-register-check-archive-queue-name: ${SQS_PENDING_REGISTER_CHECK_AERCHIVE_QUEUE_NAME}

api:
ero-management:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ components:
- registerCheckResult
- matches

PendingRegisterCheckArchiveMessage:
title: PendingRegisterCheckArchiveMessage
type: object
description: SQS message containing a request to archive all data related to a register check
properties:
correlationId:
type: string
format: uuid
description: The id to allow the response from rca to be associated with the correct register status e.g. `VoterCardApplicationRegisterStatus.id`
example: c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd

required:
- correlationId

RegisterCheckResult:
title: RegisterCheckResult
description: Enum containing the possible values for a register check match outcome.
Expand Down Expand Up @@ -350,3 +364,8 @@ components:
application/json:
schema:
$ref: '#/components/schemas/RegisterCheckResultMessage'
PendingRegisterCheckArchiveMessage:
content:
application/json:
schema:
$ref: '#/components/schemas/PendingRegisterCheckArchiveMessage'
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ internal abstract class IntegrationTest {
@Value("\${sqs.remove-applicant-register-check-data-queue-name}")
protected lateinit var removeApplicantRegisterCheckDataQueueName: String

@Value("\${sqs.pending-register-check-archive-queue-name}")
protected lateinit var pendingRegisterCheckArchiveQueueName: String

@Value("\${caching.time-to-live}")
protected lateinit var timeToLive: Duration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class LocalStackContainerConfiguration {
@Value("\${sqs.overseas-vote-confirm-applicant-register-check-result-queue-name}") overseasVoteConfirmRegisterCheckResultMessageQueueName: String,
@Value("\${sqs.register-check-result-response-queue-name}") registerCheckResultResponseQueueName: String,
@Value("\${sqs.remove-applicant-register-check-data-queue-name}") removeRegisterCheckDataMessageQueueName: String,
@Value("\${sqs.pending-register-check-archive-queue-name}") pendingRegisterCheckArchiveQueueName: String,
objectMapper: ObjectMapper,
): LocalStackContainerSettings {
val queueUrlInitiateApplicantRegisterCheck = localStackContainer.createSqsQueue(initiateApplicantRegisterCheckQueueName, objectMapper)
Expand All @@ -108,6 +109,7 @@ class LocalStackContainerConfiguration {
val queueUrlOverseasVoteConfirmRegisterCheckResult = localStackContainer.createSqsQueue(overseasVoteConfirmRegisterCheckResultMessageQueueName, objectMapper)
val queueUrlRegisterCheckResultResponse = localStackContainer.createSqsQueue(registerCheckResultResponseQueueName, objectMapper)
val queueUrlRemoveRegisterCheckData = localStackContainer.createSqsQueue(removeRegisterCheckDataMessageQueueName, objectMapper)
val queueUrlPendingRegisterCheckArchive = localStackContainer.createSqsQueue(pendingRegisterCheckArchiveQueueName, objectMapper)

val apiUrl = "http://${localStackContainer.host}:${localStackContainer.getMappedPort(DEFAULT_PORT)}"

Expand All @@ -121,7 +123,8 @@ class LocalStackContainerConfiguration {
queueUrlProxyVoteConfirmRegisterCheckResult = queueUrlProxyVoteConfirmRegisterCheckResult,
queueUrlOverseasVoteConfirmRegisterCheckResult = queueUrlOverseasVoteConfirmRegisterCheckResult,
queueUrlRemoveRegisterCheckData = queueUrlRemoveRegisterCheckData,
queueUrlRegisterCheckResultResponse = queueUrlRegisterCheckResultResponse
queueUrlRegisterCheckResultResponse = queueUrlRegisterCheckResultResponse,
queueUrlPendingRegisterCheckArchive = queueUrlPendingRegisterCheckArchive,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ data class LocalStackContainerSettings(
val queueUrlProxyVoteConfirmRegisterCheckResult: String,
val queueUrlOverseasVoteConfirmRegisterCheckResult: String,
val queueUrlRemoveRegisterCheckData: String,
val queueUrlRegisterCheckResultResponse: String
val queueUrlRegisterCheckResultResponse: String,
val queueUrlPendingRegisterCheckArchive: String,
) {
val mappedQueueUrlInitiateApplicantRegisterCheck: String = toMappedUrl(queueUrlInitiateApplicantRegisterCheck, apiUrl)
val mappedQueueUrlConfirmRegisterCheckResult: String = toMappedUrl(queueUrlConfirmRegisterCheckResult, apiUrl)
Expand All @@ -19,6 +20,7 @@ data class LocalStackContainerSettings(
val mappedQueueUrlOverseasVoteConfirmRegisterCheckResult: String = toMappedUrl(queueUrlOverseasVoteConfirmRegisterCheckResult, apiUrl)
val mappedQueueUrlRegisterCheckResultResponse: String = toMappedUrl(queueUrlRegisterCheckResultResponse, apiUrl)
val mappedQueueUrlRemoveRegisterCheckData: String = toMappedUrl(queueUrlRemoveRegisterCheckData, apiUrl)
val mappedQueueUrlPendingRegisterCheckArchive: String = toMappedUrl(queueUrlPendingRegisterCheckArchive, apiUrl)
val sesMessagesUrl = "$apiUrl/_aws/ses"

private fun toMappedUrl(rawUrlString: String, apiUrlString: String): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ internal class CorrelationIdMdcIntegrationTest : IntegrationTest() {
fun deletePostalRecordsBefore() {
ClearDownUtils.clearDownRecords(
postalRepository = postalVoteApplicationRepository,
registerCheckRepository = registerCheckRepository,
sqsAsyncClient = sqsAsyncClient,
queueName = postalApplicationQueueName
)
Expand All @@ -199,6 +200,7 @@ internal class CorrelationIdMdcIntegrationTest : IntegrationTest() {
fun deletePostalRecordsAfter() {
ClearDownUtils.clearDownRecords(
postalRepository = postalVoteApplicationRepository,
registerCheckRepository = registerCheckRepository,
sqsAsyncClient = sqsAsyncClient,
queueName = postalApplicationQueueName
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private class IntegrationDataRemovalIntegrationTest : IntegrationTest() {
fun deletePostalRecordsBefore() {
ClearDownUtils.clearDownRecords(
postalRepository = postalVoteApplicationRepository,
registerCheckRepository = registerCheckRepository,
queueName = removeApplicationEmsDataQueueName
)
}
Expand All @@ -58,6 +59,7 @@ private class IntegrationDataRemovalIntegrationTest : IntegrationTest() {
fun deletePostalRecordsAfter() {
ClearDownUtils.clearDownRecords(
postalRepository = postalVoteApplicationRepository,
registerCheckRepository = registerCheckRepository,
queueName = removeApplicationEmsDataQueueName
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import uk.gov.dluhc.messagingsupport.MessageQueue
import uk.gov.dluhc.registercheckerapi.messaging.models.PendingRegisterCheckArchiveMessage
import uk.gov.dluhc.registercheckerapi.messaging.models.RegisterCheckResultMessage
import uk.gov.dluhc.registercheckerapi.messaging.models.SourceType

Expand All @@ -29,6 +30,9 @@ internal class MessageQueueResolverTest {
@Mock
private lateinit var registerCheckResultResponseQueue: MessageQueue<RegisterCheckResultMessage>

@Mock
private lateinit var pendingRegisterCheckArchiveQueue: MessageQueue<PendingRegisterCheckArchiveMessage>

private lateinit var messageQueueResolver: MessageQueueResolver

@BeforeEach
Expand All @@ -39,8 +43,8 @@ internal class MessageQueueResolverTest {
proxyVoteConfirmRegisterCheckResultQueue = proxyVoteConfirmRegisterCheckResultMockQueue,
overseasVoteConfirmRegisterCheckResultQueue = overseasVoteConfirmRegisterCheckResultMockQueue,
confirmRegisterCheckResultQueue = confirmRegisterCheckResultMockQueue,
registerCheckResultResponseQueue = registerCheckResultResponseQueue

registerCheckResultResponseQueue = registerCheckResultResponseQueue,
pendingRegisterCheckArchiveQueue = pendingRegisterCheckArchiveQueue,
)
}

Expand Down
Loading
Loading