Skip to content

Commit

Permalink
EROPSPT-396: Reset initial removal date when reissuing AED (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirstenland authored Nov 1, 2024
1 parent fe52514 commit 17ecea2
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import uk.gov.dluhc.printapi.database.entity.AnonymousElectorDocumentStatus
import uk.gov.dluhc.printapi.dto.aed.ReIssueAnonymousElectorDocumentDto
import uk.gov.dluhc.printapi.mapper.DeliveryAddressTypeMapper
import uk.gov.dluhc.printapi.models.ReIssueAnonymousElectorDocumentRequest
import uk.gov.dluhc.printapi.service.ElectorDocumentRemovalDateResolver
import uk.gov.dluhc.printapi.service.IdFactory

@Mapper(
Expand All @@ -31,6 +32,9 @@ abstract class ReIssueAnonymousElectorDocumentMapper {
@Autowired
protected lateinit var deliveryAddressTypeMapper: DeliveryAddressTypeMapper

@Autowired
protected lateinit var removalDateResolver: ElectorDocumentRemovalDateResolver

abstract fun toReIssueAnonymousElectorDocumentDto(
apiRequest: ReIssueAnonymousElectorDocumentRequest,
userId: String
Expand Down Expand Up @@ -78,4 +82,14 @@ abstract class ReIssueAnonymousElectorDocumentMapper {
) {
aed.delivery?.deliveryAddressType = deliveryAddressTypeMapper.mapDtoToEntity(dto.deliveryAddressType)
}

@AfterMapping
protected fun setDataRetentionDates(
@MappingTarget aed: AnonymousElectorDocument,
previousAed: AnonymousElectorDocument
) {
if (previousAed.initialRetentionRemovalDate != null) {
aed.initialRetentionRemovalDate = removalDateResolver.getAedInitialRetentionPeriodRemovalDate(aed.issueDate)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.any
import org.mockito.kotlin.given
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoInteractions
import uk.gov.dluhc.printapi.database.entity.AnonymousElectorDocumentStatus
import uk.gov.dluhc.printapi.database.entity.DeliveryAddressType
import uk.gov.dluhc.printapi.dto.DeliveryAddressType.ERO_COLLECTION
import uk.gov.dluhc.printapi.dto.DeliveryAddressType.REGISTERED
import uk.gov.dluhc.printapi.dto.aed.ReIssueAnonymousElectorDocumentDto
import uk.gov.dluhc.printapi.mapper.DeliveryAddressTypeMapper
import uk.gov.dluhc.printapi.models.DeliveryAddressType.ERO_MINUS_COLLECTION
import uk.gov.dluhc.printapi.service.ElectorDocumentRemovalDateResolver
import uk.gov.dluhc.printapi.service.IdFactory
import uk.gov.dluhc.printapi.testsupport.deepCopy
import uk.gov.dluhc.printapi.testsupport.testdata.aValidElectoralRollNumber
Expand Down Expand Up @@ -55,6 +57,9 @@ class ReIssueAnonymousElectorDocumentMapperTest {
@Mock
private lateinit var aedMappingHelper: AedMappingHelper

@Mock
private lateinit var electorDocumentRemovalDateResolver: ElectorDocumentRemovalDateResolver

@Test
fun `should map to ReIssueAnonymousElectorDocumentDto DTO given API Request`() {
// Given
Expand Down Expand Up @@ -179,4 +184,61 @@ class ReIssueAnonymousElectorDocumentMapperTest {
verify(aedMappingHelper).statusHistory(AnonymousElectorDocumentStatus.Status.PRINTED)
verify(deliveryAddressTypeMapper).mapDtoToEntity(REGISTERED)
}

@Test
fun `should set initialRetentionRemovalDate to be null if not set for previous AED`() {
// Given
val sourceReference = aValidSourceReference()
val previousAed = buildAnonymousElectorDocument(
sourceReference = sourceReference,
initialRetentionRemovalDate = null,
)
val templateFilename = aTemplateFilename()
val newCertificateNumber = aValidVacNumber()
given(idFactory.vacNumber()).willReturn(newCertificateNumber)
given(aedMappingHelper.requestDateTime()).willReturn(FIXED_TIME)
given(aedMappingHelper.issueDate()).willReturn(FIXED_DATE)

val dto = buildReIssueAnonymousElectorDocumentDto(sourceReference = sourceReference)

given(deliveryAddressTypeMapper.mapDtoToEntity(any())).willReturn(DeliveryAddressType.REGISTERED)

// When
val actual = mapper.toNewAnonymousElectorDocument(previousAed, dto, templateFilename)

// Then
assertThat(actual.initialRetentionRemovalDate).isNull()
verifyNoInteractions(electorDocumentRemovalDateResolver)
}

@Test
fun `should set initialRetentionRemovalDate based on new issue date if already set for previous AED`() {
// Given
val sourceReference = aValidSourceReference()

val previousRemovalTime = LocalDate.parse("2023-01-01")
val previousAed = buildAnonymousElectorDocument(
sourceReference = sourceReference,
initialRetentionRemovalDate = previousRemovalTime,
)
val templateFilename = aTemplateFilename()
val newCertificateNumber = aValidVacNumber()
given(idFactory.vacNumber()).willReturn(newCertificateNumber)
given(aedMappingHelper.requestDateTime()).willReturn(FIXED_TIME)
given(aedMappingHelper.issueDate()).willReturn(FIXED_DATE)

val dto = buildReIssueAnonymousElectorDocumentDto(sourceReference = sourceReference)

given(deliveryAddressTypeMapper.mapDtoToEntity(any())).willReturn(DeliveryAddressType.REGISTERED)

val newRemovalTime = FIXED_DATE.plusMonths(15)
given(electorDocumentRemovalDateResolver.getAedInitialRetentionPeriodRemovalDate(FIXED_DATE)).willReturn(newRemovalTime)

// When
val actual = mapper.toNewAnonymousElectorDocument(previousAed, dto, templateFilename)

// Then
assertThat(actual.initialRetentionRemovalDate).isEqualTo(newRemovalTime)
verify(electorDocumentRemovalDateResolver).getAedInitialRetentionPeriodRemovalDate(FIXED_DATE)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import uk.gov.dluhc.printapi.testsupport.testdata.model.buildLocalAuthorityRespo
import uk.gov.dluhc.printapi.testsupport.testdata.model.buildReIssueAnonymousElectorDocumentRequest
import uk.gov.dluhc.printapi.testsupport.withBody
import java.io.ByteArrayInputStream
import java.time.LocalDate
import java.util.UUID
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -199,6 +200,131 @@ internal class ReIssueAnonymousElectorDocumentIntegrationTest : IntegrationTest(
}
}

@Test
fun `should set initial data retention date for new AED given previously issued AED has initial data retention date set`() {
// Given
val eroResponse = buildElectoralRegistrationOfficeResponse(
id = ERO_ID,
localAuthorities = listOf(buildLocalAuthorityResponse(gssCode = GSS_CODE), buildLocalAuthorityResponse())
)
wireMockService.stubCognitoJwtIssuerResponse()
wireMockService.stubEroManagementGetEroByEroId(eroResponse, ERO_ID)

val sourceReference = aValidSourceReference()
val photoLocationArn = addPhotoToS3()
val originalRetentionRemovalDate = LocalDate.now().minusMonths(4)

val originalElectoralRollNumber = "ORIGINAL ELECTORAL ROLL #"
val previousAed = buildAnonymousElectorDocument(
gssCode = GSS_CODE,
sourceReference = sourceReference,
photoLocationArn = photoLocationArn,
electoralRollNumber = originalElectoralRollNumber,
initialRetentionRemovalDate = originalRetentionRemovalDate,
)
anonymousElectorDocumentRepository.save(previousAed)

val newElectoralRollNumber = aValidElectoralRollNumber()
val requestBody = buildReIssueAnonymousElectorDocumentRequest(
sourceReference = sourceReference,
electoralRollNumber = newElectoralRollNumber,
)

// When
val response = webTestClient.mutate()
.codecs { it.defaultCodecs().maxInMemorySize(MAX_SIZE_2_MB) }
.build()
.post()
.uri(URI_TEMPLATE, ERO_ID)
.bearerToken(getVCAnonymousAdminBearerToken(eroId = ERO_ID))
.contentType(APPLICATION_JSON)
.withBody(requestBody)
.exchange()
.expectStatus().isCreated
.expectHeader().contentType(MediaType.APPLICATION_PDF)
.expectBody(ByteArray::class.java)
.returnResult()

// Then
val contentDisposition = response.responseHeaders.contentDisposition

val electorDocuments = anonymousElectorDocumentRepository.findByGssCodeAndSourceTypeAndSourceReference(
GSS_CODE,
ANONYMOUS_ELECTOR_DOCUMENT,
sourceReference
)

// Get the new AED that this test would have created
val newlyCreatedAed = electorDocuments.first { aed ->
contentDisposition.filename == "anonymous-elector-document-${aed.certificateNumber}.pdf"
}

val expectedNewRetentionDate = LocalDate.now().plusMonths(15)
assertThat(newlyCreatedAed)
.hasInitialRetentionRemovalDate(expectedNewRetentionDate)
}

@Test
fun `should not set initial data retention date for new AED given previously issued AED does not have the initial data retention date set`() {
// Given
val eroResponse = buildElectoralRegistrationOfficeResponse(
id = ERO_ID,
localAuthorities = listOf(buildLocalAuthorityResponse(gssCode = GSS_CODE), buildLocalAuthorityResponse())
)
wireMockService.stubCognitoJwtIssuerResponse()
wireMockService.stubEroManagementGetEroByEroId(eroResponse, ERO_ID)

val sourceReference = aValidSourceReference()
val photoLocationArn = addPhotoToS3()

val originalElectoralRollNumber = "ORIGINAL ELECTORAL ROLL #"
val previousAed = buildAnonymousElectorDocument(
gssCode = GSS_CODE,
sourceReference = sourceReference,
photoLocationArn = photoLocationArn,
electoralRollNumber = originalElectoralRollNumber,
initialRetentionRemovalDate = null,
)
anonymousElectorDocumentRepository.save(previousAed)

val newElectoralRollNumber = aValidElectoralRollNumber()
val requestBody = buildReIssueAnonymousElectorDocumentRequest(
sourceReference = sourceReference,
electoralRollNumber = newElectoralRollNumber,
)

// When
val response = webTestClient.mutate()
.codecs { it.defaultCodecs().maxInMemorySize(MAX_SIZE_2_MB) }
.build()
.post()
.uri(URI_TEMPLATE, ERO_ID)
.bearerToken(getVCAnonymousAdminBearerToken(eroId = ERO_ID))
.contentType(APPLICATION_JSON)
.withBody(requestBody)
.exchange()
.expectStatus().isCreated
.expectHeader().contentType(MediaType.APPLICATION_PDF)
.expectBody(ByteArray::class.java)
.returnResult()

// Then
val contentDisposition = response.responseHeaders.contentDisposition

val electorDocuments = anonymousElectorDocumentRepository.findByGssCodeAndSourceTypeAndSourceReference(
GSS_CODE,
ANONYMOUS_ELECTOR_DOCUMENT,
sourceReference
)

// Get the new AED that this test would have created
val newlyCreatedAed = electorDocuments.first { aed ->
contentDisposition.filename == "anonymous-elector-document-${aed.certificateNumber}.pdf"
}

assertThat(newlyCreatedAed).hasInitialRetentionRemovalDate(null)
}

private fun addPhotoToS3(): String {
val s3Resource = ResourceUtils.getFile(AED_SAMPLE_PHOTO).readBytes()
val s3Bucket = LocalStackContainerConfiguration.VCA_TARGET_BUCKET
Expand Down

0 comments on commit 17ecea2

Please sign in to comment.