This repository has been archived by the owner on Apr 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added cron job for deleting expired secrets. fix #2
- Loading branch information
Showing
7 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/click/wheredoi/secretshareapi/ScheduledTasks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2019 Isaak Malik <[email protected]> | ||
* | ||
* Licensed as open source under the EUROPEAN UNION PUBLIC LICENCE v. 1.2 | ||
* See LICENSE, LICENTIE or for other translations: https://joinup.ec.europa.eu/collection/eupl/eupl-text-11-12 | ||
*/ | ||
|
||
package click.wheredoi.secretshareapi; | ||
|
||
import click.wheredoi.secretshareapi.service.SecretService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ScheduledTasks { | ||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
private final SecretService secretService; | ||
|
||
public ScheduledTasks(SecretService secretService) { | ||
this.secretService = secretService; | ||
} | ||
|
||
// Each hour | ||
@Scheduled(cron = "0 0 * * * *") | ||
public void cronDeleteExpiredSecrets() { | ||
secretService.deleteExpiredSecrets(); | ||
logger.info("Cron job cronDeleteExpiredSecrets() has been executed"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/test/java/click/wheredoi/secretshareapi/ScheduledTasksTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2019 Isaak Malik <[email protected]> | ||
* | ||
* Licensed as open source under the EUROPEAN UNION PUBLIC LICENCE v. 1.2 | ||
* See LICENSE, LICENTIE or for other translations: https://joinup.ec.europa.eu/collection/eupl/eupl-text-11-12 | ||
*/ | ||
|
||
package click.wheredoi.secretshareapi; | ||
|
||
import click.wheredoi.secretshareapi.service.SecretService; | ||
import org.awaitility.Duration; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
import static org.mockito.Mockito.atLeast; | ||
import static org.mockito.Mockito.verify; | ||
|
||
// Disabled because not possible to test the one hour cron job immediately | ||
@Disabled | ||
class ScheduledTasksTest { | ||
@Mock | ||
private ScheduledTasks tasks; | ||
|
||
@Mock | ||
private SecretService secretService; | ||
|
||
@BeforeEach | ||
void setup() { | ||
MockitoAnnotations.initMocks(this); | ||
} | ||
|
||
@Test | ||
void testCronDeleteExpiredSecrets() { | ||
Mockito.doNothing().when(secretService).deleteExpiredSecrets(); | ||
|
||
await().atMost(Duration.FIVE_SECONDS).untilAsserted(() -> | ||
verify(tasks, atLeast(1)).cronDeleteExpiredSecrets()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters