Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Fix re-arranging issue of Vax certs that have the same vaccinationOn …
Browse files Browse the repository at this point in the history
…(EXPOSUREAPP-8756) (#4136)

* Fix re-arranging issue of Vax certs that have the same vaccinationOn

* Fix test
  • Loading branch information
mtwalli authored Sep 29, 2021
1 parent 292f97c commit 68c4f88
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,31 @@ import timber.log.Timber

/*
The list items shall be sorted descending by the following date attributes depending on the type of the DGC:
for Vaccination Certificates (i.e. DGC with v[0]): the date of the vaccination v[0].dt
for Vaccination Certificates (i.e. DGC with v[0]): the date of the vaccination v[0].dt and issuedAt date
for Test Certificates (i.e. DGC with t[0]): the date of the sample collection t[0].sc
for Recovery Certificates (i.e. DGC with r[0]): the date of begin of the validity r[0].df
*/
fun Collection<CwaCovidCertificate>.toCertificateSortOrder(): List<CwaCovidCertificate> {
return this.sortedByDescending {
when (it) {
is VaccinationCertificate -> it.vaccinatedOn
is TestCertificate -> it.sampleCollectedAt.toLocalDateUserTz()
is RecoveryCertificate -> it.validFrom
else -> throw IllegalStateException("Can't sort $it")
}
}
return this.sortedWith(
compareBy(
{
when (it) {
is VaccinationCertificate -> it.vaccinatedOn
is TestCertificate -> it.sampleCollectedAt.toLocalDateUserTz()
is RecoveryCertificate -> it.validFrom
else -> throw IllegalStateException("Can't sort $it")
}
},
{
when (it) {
is VaccinationCertificate -> it.headerIssuedAt
is TestCertificate -> it.sampleCollectedAt.toLocalDateUserTz()
is RecoveryCertificate -> it.validFrom
else -> throw IllegalStateException("Can't sort $it")
}
}
)
).reversed()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class PersonCertificatesExtensionsTest : BaseTest() {
fun `certificate sort order`() {

val certificateFirst = mockk<VaccinationCertificate>().apply {
every { headerIssuedAt } returns Instant.parse("2021-09-20T10:00:00.000Z")
every { vaccinatedOn } returns time.plus(oneDayDuration).toLocalDateUtc()
}

val certificateFirstIssuedAtAnotherDate = mockk<VaccinationCertificate>().apply {
every { headerIssuedAt } returns Instant.parse("2021-07-20T10:00:00.000Z")
every { vaccinatedOn } returns time.plus(oneDayDuration).toLocalDateUtc()
}

Expand All @@ -36,9 +42,12 @@ class PersonCertificatesExtensionsTest : BaseTest() {
every { validFrom } returns time.minus(oneDayDuration).toLocalDateUtc()
}

val expectedOrder = listOf(certificateFirst, certificateSecond, certificateThird)
val wrongOrder = listOf(certificateSecond, certificateFirst, certificateThird)
val wrongOrder2 = listOf(certificateThird, certificateSecond, certificateFirst)
val expectedOrder =
listOf(certificateFirst, certificateFirstIssuedAtAnotherDate, certificateSecond, certificateThird)
val wrongOrder =
listOf(certificateSecond, certificateFirst, certificateThird, certificateFirstIssuedAtAnotherDate)
val wrongOrder2 =
listOf(certificateThird, certificateSecond, certificateFirstIssuedAtAnotherDate, certificateFirst)

expectedOrder.toCertificateSortOrder() shouldBe expectedOrder
wrongOrder.toCertificateSortOrder() shouldBe expectedOrder
Expand Down

0 comments on commit 68c4f88

Please sign in to comment.