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

Feat: Exclude Partner from Longterm Archive #253

Merged
merged 1 commit into from
Aug 11, 2022
Merged
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 @@ -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";
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/app/coronawarn/quicktest/service/ArchiveService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -134,6 +137,26 @@ public List<ArchiveCipherDtoV1> 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<String> 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();

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-cloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<QuickTestArchive> 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));
Expand Down