Skip to content

Commit

Permalink
feat: provide cron for clean up of file limits
Browse files Browse the repository at this point in the history
  • Loading branch information
mebo4b committed Nov 24, 2020
1 parent b8f2167 commit 4740ee8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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!");
}

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

}

0 comments on commit 4740ee8

Please sign in to comment.