From 4740ee85d7f303c2f093adcd3174afbc4d5aae80 Mon Sep 17 00:00:00 2001 From: mebo4b Date: Tue, 24 Nov 2020 13:35:18 +0100 Subject: [PATCH] feat: provide cron for clean up of file limits --- .../api/service/UploadTrackingService.java | 10 +++++++ src/main/resources/application.properties | 1 + .../api/service/UploadTrackingServiceIT.java | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/main/java/de/caritas/cob/uploadservice/api/service/UploadTrackingService.java b/src/main/java/de/caritas/cob/uploadservice/api/service/UploadTrackingService.java index 78493ef..79c1274 100644 --- a/src/main/java/de/caritas/cob/uploadservice/api/service/UploadTrackingService.java +++ b/src/main/java/de/caritas/cob/uploadservice/api/service/UploadTrackingService.java @@ -8,6 +8,7 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; /** @@ -48,4 +49,13 @@ public void trackUploadedFileForUser() { this.uploadByUserRepository.save(uploadByUser); } + /** + * Cleans all stored file limits. + */ + @Scheduled(cron = "${upload.file.cleanup.cron}") + public void cleanUpFileLimits() { + this.uploadByUserRepository.deleteAll(); + LogService.logInfo("File restrictions are reset!"); + } + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 90864d1..bb4e0b9 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -56,6 +56,7 @@ spring.servlet.multipart.max-file-size=5MB spring.servlet.multipart.max-request-size=7MB spring.servlet.multipart.location= upload.file.perday.limit=7 +upload.file.cleanup.cron=0 0 0 * * * #Threshold after which files are written to disk.default is 0B spring.servlet.multipart.file-size-threshold=50KB # Task executor configuration diff --git a/src/test/java/de/caritas/cob/uploadservice/api/service/UploadTrackingServiceIT.java b/src/test/java/de/caritas/cob/uploadservice/api/service/UploadTrackingServiceIT.java index 42f2497..ce45d05 100644 --- a/src/test/java/de/caritas/cob/uploadservice/api/service/UploadTrackingServiceIT.java +++ b/src/test/java/de/caritas/cob/uploadservice/api/service/UploadTrackingServiceIT.java @@ -4,7 +4,11 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.powermock.reflect.Whitebox.setInternalState; import de.caritas.cob.uploadservice.UploadServiceApplication; import de.caritas.cob.uploadservice.api.exception.httpresponses.QuotaReachedException; @@ -15,6 +19,8 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; @@ -38,9 +44,13 @@ public class UploadTrackingServiceIT { @MockBean private AuthenticatedUser authenticatedUser; + @Mock + private Logger logger; + @Before public void setup() { when(this.authenticatedUser.getUserId()).thenReturn("userId"); + setInternalState(LogService.class, "LOGGER", logger); } @After @@ -91,4 +101,22 @@ public void validateUploadLimit_Should_throwQuotaReachedException_When_limitIsRe this.uploadTrackingService.validateUploadLimit(); } + @Test + public void cleanUpFileLimits_Should_deleteAllTrackingEntries() { + trackUploadedFile(10); + + assertThat(this.uploadByUserRepository.count(), is(10L)); + + this.uploadTrackingService.cleanUpFileLimits(); + + assertThat(this.uploadByUserRepository.count(), is(0L)); + } + + @Test + public void cleanUpFileLimits_Should_logExpectedInfoMessage() { + this.uploadTrackingService.cleanUpFileLimits(); + + verify(this.logger, times(1)).info(eq("File restrictions are reset!")); + } + }