diff --git a/src/main/java/app/coronawarn/quicktest/config/ArchiveProperties.java b/src/main/java/app/coronawarn/quicktest/config/ArchiveProperties.java index 9ad7df46..be923716 100644 --- a/src/main/java/app/coronawarn/quicktest/config/ArchiveProperties.java +++ b/src/main/java/app/coronawarn/quicktest/config/ArchiveProperties.java @@ -34,6 +34,11 @@ public class ArchiveProperties { private Crypt crypt = new Crypt(); private VaultTransit vaultTransit = new VaultTransit(); + /** + * Comma seperated list of PartnerID of Partners who should be ignored for archiving. + */ + private String excludedPartners = ""; + @Data public static final class VaultTransit { private String folder = "cwa-quick-test-archive"; diff --git a/src/main/java/app/coronawarn/quicktest/service/ArchiveService.java b/src/main/java/app/coronawarn/quicktest/service/ArchiveService.java index aa29ac50..be61af0f 100644 --- a/src/main/java/app/coronawarn/quicktest/service/ArchiveService.java +++ b/src/main/java/app/coronawarn/quicktest/service/ArchiveService.java @@ -39,6 +39,8 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -82,6 +84,7 @@ public void moveToArchive() { final LocalDateTime beforeDateTime = LocalDateTime.now().minusSeconds(olderThanInSeconds); quickTestArchiveRepository.findAllByUpdatedAtBefore(beforeDateTime, PageRequest.of(0, chunkSize)) .filter(quickTestArchive -> StringUtils.isNotBlank(quickTestArchive.getPocId())) + .filter(this::checkIsNotOnIgnoreListOrDelete) .map(this::convertQuickTest) .map(this::buildArchive) .map(repository::save) @@ -134,6 +137,26 @@ public List getQuicktestsFromLongtermByTenantId(final String return dtos; } + /** + * Checks whether the PartnerId of given Test is on ignore-list for longterm archive and directly deletes entry + * from archive. + * + * @param archiveData entity to check + * @return true if entity's partner ID is NOT on ignore list. + */ + private boolean checkIsNotOnIgnoreListOrDelete(QuickTestArchiveDataView archiveData) { + List excludedPartners = Stream.of(properties.getExcludedPartners().split(",")) + .map(String::trim) + .collect(Collectors.toList()); + + if (excludedPartners.stream().anyMatch(partnerId -> partnerId.equals(archiveData.getTenantId()))) { + quickTestArchiveRepository.deleteById(archiveData.getHashedGuid()); + return false; + } else { + return true; + } + } + private ArchiveCipherDtoV1 convertQuickTest(final QuickTestArchiveDataView quickTestArchive) { final ArchiveCipherDtoV1 archive = new ArchiveCipherDtoV1(); diff --git a/src/main/resources/application-cloud.yml b/src/main/resources/application-cloud.yml index 03720629..c9dd740c 100644 --- a/src/main/resources/application-cloud.yml +++ b/src/main/resources/application-cloud.yml @@ -139,6 +139,7 @@ quicktest: environment-name: ${QUICK_TEST_ENVIRONMENT_NAME:unknown} archive: + excluded-partners: ${QT_ARCHIVE_EXCLUDED_PARTNERS:} moveToArchiveJob: cron: 0 0/5 * * * * locklimit: 1800000 diff --git a/src/test/java/app/coronawarn/quicktest/service/ArchiveServiceTest.java b/src/test/java/app/coronawarn/quicktest/service/ArchiveServiceTest.java index 6de35c62..e2674ce5 100644 --- a/src/test/java/app/coronawarn/quicktest/service/ArchiveServiceTest.java +++ b/src/test/java/app/coronawarn/quicktest/service/ArchiveServiceTest.java @@ -21,6 +21,7 @@ package app.coronawarn.quicktest.service; import static org.assertj.core.api.Assertions.assertThat; + import app.coronawarn.quicktest.archive.domain.Archive; import app.coronawarn.quicktest.archive.domain.ArchiveCipherDtoV1; import app.coronawarn.quicktest.archive.repository.ArchiveRepository; @@ -31,16 +32,18 @@ import app.coronawarn.quicktest.service.cryption.CryptionService; import com.fasterxml.jackson.databind.ObjectMapper; import java.time.LocalDateTime; -import java.util.Base64; import java.util.List; -import java.util.Optional; - +import org.apache.commons.lang3.RandomUtils; +import org.apache.tomcat.util.buf.HexUtils; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -@SpringBootTest +@SpringBootTest( + properties = "archive.excludedPartners=excluded_partner , excluded_partner2" +) class ArchiveServiceTest { @Autowired @@ -70,8 +73,12 @@ void setUp() { void moveToArchive() throws Exception { // GIVEN final int initialArchive = this.archiveRepository.findAll().size(); - final QuickTestArchive test = this.buildQuickTestArchive(); + final QuickTestArchive test = this.buildQuickTestArchive("tenant_id"); + final QuickTestArchive excludedTest = this.buildQuickTestArchive("excluded_partner"); + final QuickTestArchive excludedTest2 = this.buildQuickTestArchive("excluded_partner2"); this.quickTestArchiveRepository.saveAndFlush(test); + this.quickTestArchiveRepository.saveAndFlush(excludedTest); + this.quickTestArchiveRepository.saveAndFlush(excludedTest2); // WHEN this.archiveService.moveToArchive(); // THEN @@ -129,15 +136,16 @@ void moveToArchive() throws Exception { assertThat(dto.getAdditionalInfo()).isEqualTo(test.getAdditionalInfo()); assertThat(dto.getGroupName()).isEqualTo(test.getGroupName()); // AND deleted QuickTestArchive - final Optional deletedTest = this.quickTestArchiveRepository.findById(test.getHashedGuid()); - assertThat(deletedTest).isEmpty(); + Assertions.assertTrue(this.quickTestArchiveRepository.findById(test.getHashedGuid()).isEmpty(), "Archived tests are not properly deleted from archive table."); + Assertions.assertTrue(this.quickTestArchiveRepository.findById(excludedTest.getHashedGuid()).isEmpty(), "Excluded tests are not properly deleted from archive table."); + Assertions.assertTrue(this.quickTestArchiveRepository.findById(excludedTest2.getHashedGuid()).isEmpty(), "Excluded tests are not properly deleted from archive table."); } - private QuickTestArchive buildQuickTestArchive() { + private QuickTestArchive buildQuickTestArchive(String tenantId) { QuickTestArchive qta = new QuickTestArchive(); - qta.setShortHashedGuid("27a9ac47"); - qta.setHashedGuid("27a9ac470b7832857ad03b25dc96032e0a1056ff4f5afb538de4994e0a63d227"); - qta.setTenantId("tenant_id"); + qta.setShortHashedGuid(HexUtils.toHexString(RandomUtils.nextBytes(4))); + qta.setHashedGuid(HexUtils.toHexString(RandomUtils.nextBytes(32))); + qta.setTenantId(tenantId); qta.setPocId("poc_id"); qta.setCreatedAt(LocalDateTime.now().minusMonths(3)); qta.setUpdatedAt(LocalDateTime.now().minusMonths(2));