From 8a4dcc1315e9a86d6dd6f5418841a84db7f080c8 Mon Sep 17 00:00:00 2001 From: Agit Rubar Demir <61833677+agitrubard@users.noreply.github.com> Date: Tue, 5 Mar 2024 15:58:55 +0300 Subject: [PATCH] AYS-28 | `ReservedAssignmentsRegulationScheduler` Method Has Been Fixed for Set User ID of Assignment (#291) --- ...eservedAssignmentsRegulationScheduler.java | 13 ++-- .../InvalidTokenDeletionScheduler.java | 13 +++- .../ays/common/model/entity/BaseEntity.java | 6 +- .../org/ays/user/model/entity/UserEntity.java | 2 +- ...ignmentsRegulationSchedulerSystemTest.java | 23 +++--- ...vedAssignmentsRegulationSchedulerTest.java | 10 +-- .../impl/AssignmentSearchServiceImplTest.java | 74 +++++++++++++------ ...validTokenDeletionSchedulerSystemTest.java | 11 ++- .../InvalidTokenDeletionSchedulerTest.java | 51 +++++++++++++ src/test/resources/application.yml | 5 ++ 10 files changed, 158 insertions(+), 50 deletions(-) rename src/main/java/org/ays/{common => assignment}/scheduler/ReservedAssignmentsRegulationScheduler.java (87%) rename src/main/java/org/ays/{common => auth}/scheduler/InvalidTokenDeletionScheduler.java (87%) rename src/test/java/org/ays/{common => assignment}/scheduler/ReservedAssignmentsRegulationSchedulerSystemTest.java (84%) rename src/test/java/org/ays/{common => assignment}/scheduler/ReservedAssignmentsRegulationSchedulerTest.java (86%) rename src/test/java/org/ays/{common => auth}/scheduler/InvalidTokenDeletionSchedulerSystemTest.java (93%) create mode 100644 src/test/java/org/ays/auth/scheduler/InvalidTokenDeletionSchedulerTest.java diff --git a/src/main/java/org/ays/common/scheduler/ReservedAssignmentsRegulationScheduler.java b/src/main/java/org/ays/assignment/scheduler/ReservedAssignmentsRegulationScheduler.java similarity index 87% rename from src/main/java/org/ays/common/scheduler/ReservedAssignmentsRegulationScheduler.java rename to src/main/java/org/ays/assignment/scheduler/ReservedAssignmentsRegulationScheduler.java index d83a509bb..d3946a46a 100644 --- a/src/main/java/org/ays/common/scheduler/ReservedAssignmentsRegulationScheduler.java +++ b/src/main/java/org/ays/assignment/scheduler/ReservedAssignmentsRegulationScheduler.java @@ -1,6 +1,5 @@ -package org.ays.common.scheduler; +package org.ays.assignment.scheduler; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.ays.assignment.model.entity.AssignmentEntity; import org.ays.assignment.model.enums.AssignmentStatus; @@ -25,12 +24,17 @@ */ @Slf4j @Component -@RequiredArgsConstructor @ConditionalOnProperty(name = "ays.scheduler.reserved-assignments-regulation.enable", havingValue = "true") class ReservedAssignmentsRegulationScheduler { private final AssignmentRepository assignmentRepository; + public ReservedAssignmentsRegulationScheduler(AssignmentRepository assignmentRepository) { + this.assignmentRepository = assignmentRepository; + + log.info("ReservedAssignmentsRegulationScheduler is enabled."); + } + /** * Scheduled task method to update reserved assignments to available status. * This method is triggered based on the cron expression specified in the configuration property @@ -53,8 +57,7 @@ public void updateReservedAssignmentsToAvailable() { LocalDateTime.now().minusSeconds(20) ); - assignmentEntities.forEach(assignment -> assignment.setStatus(AssignmentStatus.AVAILABLE)); - + assignmentEntities.forEach(AssignmentEntity::available); assignmentRepository.saveAll(assignmentEntities); log.trace("All reserved assignments are updated to available."); diff --git a/src/main/java/org/ays/common/scheduler/InvalidTokenDeletionScheduler.java b/src/main/java/org/ays/auth/scheduler/InvalidTokenDeletionScheduler.java similarity index 87% rename from src/main/java/org/ays/common/scheduler/InvalidTokenDeletionScheduler.java rename to src/main/java/org/ays/auth/scheduler/InvalidTokenDeletionScheduler.java index f11c021ef..f48bdaab6 100644 --- a/src/main/java/org/ays/common/scheduler/InvalidTokenDeletionScheduler.java +++ b/src/main/java/org/ays/auth/scheduler/InvalidTokenDeletionScheduler.java @@ -1,6 +1,5 @@ -package org.ays.common.scheduler; +package org.ays.auth.scheduler; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.ays.auth.model.enums.AysConfigurationParameter; import org.ays.auth.repository.AysInvalidTokenRepository; @@ -27,13 +26,21 @@ */ @Slf4j @Component -@RequiredArgsConstructor @ConditionalOnProperty(name = "ays.scheduler.invalid-tokens-deletion.enable", havingValue = "true") class InvalidTokenDeletionScheduler { private final AysInvalidTokenRepository invalidTokenRepository; private final AysParameterService parameterService; + public InvalidTokenDeletionScheduler(AysInvalidTokenRepository invalidTokenRepository, + AysParameterService parameterService) { + + this.invalidTokenRepository = invalidTokenRepository; + this.parameterService = parameterService; + + log.info("InvalidTokenDeletionScheduler is enabled."); + } + /** * Scheduled task method to delete invalid tokens that are no longer in use. * This method is triggered based on the cron expression specified in the configuration property diff --git a/src/main/java/org/ays/common/model/entity/BaseEntity.java b/src/main/java/org/ays/common/model/entity/BaseEntity.java index ff6a24a1f..6cb83a718 100644 --- a/src/main/java/org/ays/common/model/entity/BaseEntity.java +++ b/src/main/java/org/ays/common/model/entity/BaseEntity.java @@ -42,7 +42,8 @@ public void prePersist() { .map(Jwt.class::cast) .map(jwt -> jwt.getClaim(AysTokenClaims.USERNAME.getValue()).toString()) .orElse("AYS"); - this.createdAt = LocalDateTime.now(); + this.createdAt = Optional.ofNullable(this.createdAt) + .orElse(LocalDateTime.now()); } @@ -60,6 +61,7 @@ public void preUpdate() { .map(Jwt.class::cast) .map(jwt -> jwt.getClaim(AysTokenClaims.USERNAME.getValue()).toString()) .orElse("AYS"); - this.updatedAt = LocalDateTime.now(); + this.updatedAt = Optional.ofNullable(this.updatedAt) + .orElse(LocalDateTime.now()); } } diff --git a/src/main/java/org/ays/user/model/entity/UserEntity.java b/src/main/java/org/ays/user/model/entity/UserEntity.java index b8ca28daf..62f8da2b8 100644 --- a/src/main/java/org/ays/user/model/entity/UserEntity.java +++ b/src/main/java/org/ays/user/model/entity/UserEntity.java @@ -94,7 +94,7 @@ public boolean isDeleted() { } public boolean isReady() { - return this.isActive() && UserSupportStatus.READY.equals(this.supportStatus); + return UserSupportStatus.READY.equals(this.supportStatus); } public void delete() { diff --git a/src/test/java/org/ays/common/scheduler/ReservedAssignmentsRegulationSchedulerSystemTest.java b/src/test/java/org/ays/assignment/scheduler/ReservedAssignmentsRegulationSchedulerSystemTest.java similarity index 84% rename from src/test/java/org/ays/common/scheduler/ReservedAssignmentsRegulationSchedulerSystemTest.java rename to src/test/java/org/ays/assignment/scheduler/ReservedAssignmentsRegulationSchedulerSystemTest.java index 8e1c07478..dd3fa45be 100644 --- a/src/test/java/org/ays/common/scheduler/ReservedAssignmentsRegulationSchedulerSystemTest.java +++ b/src/test/java/org/ays/assignment/scheduler/ReservedAssignmentsRegulationSchedulerSystemTest.java @@ -1,4 +1,4 @@ -package org.ays.common.scheduler; +package org.ays.assignment.scheduler; import org.awaitility.Awaitility; import org.ays.AbstractSystemTest; @@ -9,6 +9,7 @@ import org.ays.institution.model.entity.InstitutionEntityBuilder; import org.ays.user.model.entity.UserEntity; import org.ays.user.model.entity.UserEntityBuilder; +import org.ays.user.model.enums.UserSupportStatus; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -25,7 +26,6 @@ class ReservedAssignmentsRegulationSchedulerSystemTest extends AbstractSystemTest { - @Value("${ays.scheduler.reserved-assignments-regulation.cron}") private String expectedCronExpression; @@ -35,12 +35,12 @@ class ReservedAssignmentsRegulationSchedulerSystemTest extends AbstractSystemTes @SpyBean private ReservedAssignmentsRegulationScheduler reservedAssignmentsRegulationScheduler; - private void initialize(InstitutionEntity institutionEntity, - UserEntity userEntity, + private void initialize(InstitutionEntity mockInstitutionEntity, + UserEntity mockUserEntity, List mockAssignmentEntities) { - institutionRepository.save(institutionEntity); - userRepository.save(userEntity); + institutionRepository.save(mockInstitutionEntity); + userRepository.save(mockUserEntity); assignmentRepository.saveAll(mockAssignmentEntities); } @@ -52,7 +52,7 @@ void whenCronReservedAssignmentsRegulationSchedulerTaskCreated_thenCronTaskSched .map(ScheduledTask::getTask) .filter(CronTask.class::isInstance) .map(CronTask.class::cast) - .filter(task -> task.toString().contains("common.scheduler.ReservedAssignmentsRegulationScheduler")) + .filter(task -> task.toString().contains(ReservedAssignmentsRegulationScheduler.class.getSimpleName())) .findFirst() .orElseThrow(() -> new IllegalStateException("No scheduled tasks")); @@ -60,7 +60,7 @@ void whenCronReservedAssignmentsRegulationSchedulerTaskCreated_thenCronTaskSched } @Test - void whenWait10Seconds_thenClearInvalidToken() { + void whenWait2Seconds_thenUpdateReservedAssignmentsToAvailableAndUserToBusy() { // Initialize InstitutionEntity mockInstitutionEntity = new InstitutionEntityBuilder() @@ -68,12 +68,14 @@ void whenWait10Seconds_thenClearInvalidToken() { .build(); UserEntity mockUserEntity = new UserEntityBuilder() .withValidFields() + .withSupportStatus(UserSupportStatus.READY) .withInstitutionId(mockInstitutionEntity.getId()) .withInstitution(null) .build(); List mockAssignmentEntities = List.of( new AssignmentEntityBuilder() .withValidFields() + .withStatus(AssignmentStatus.RESERVED) .withCreatedAt(LocalDateTime.now().minusSeconds(20)) .withInstitutionId(mockInstitutionEntity.getId()) .withInstitution(null) @@ -82,6 +84,7 @@ void whenWait10Seconds_thenClearInvalidToken() { .build(), new AssignmentEntityBuilder() .withValidFields() + .withStatus(AssignmentStatus.RESERVED) .withCreatedAt(LocalDateTime.now().minusSeconds(20)) .withInstitutionId(mockInstitutionEntity.getId()) .withInstitution(null) @@ -96,18 +99,20 @@ void whenWait10Seconds_thenClearInvalidToken() { .await() .atMost(2, java.util.concurrent.TimeUnit.SECONDS) .untilAsserted(() -> { + Mockito.verify(reservedAssignmentsRegulationScheduler, Mockito.atLeast(1)) .updateReservedAssignmentsToAvailable(); mockAssignmentEntities.forEach(mockAssignmentEntity -> { + Optional assignmentEntity = assignmentRepository .findById(mockAssignmentEntity.getId()); Assertions.assertTrue(assignmentEntity.isPresent()); Assertions.assertEquals(AssignmentStatus.AVAILABLE, assignmentEntity.get().getStatus()); + Assertions.assertNull(assignmentEntity.get().getUserId()); }); }); - } } diff --git a/src/test/java/org/ays/common/scheduler/ReservedAssignmentsRegulationSchedulerTest.java b/src/test/java/org/ays/assignment/scheduler/ReservedAssignmentsRegulationSchedulerTest.java similarity index 86% rename from src/test/java/org/ays/common/scheduler/ReservedAssignmentsRegulationSchedulerTest.java rename to src/test/java/org/ays/assignment/scheduler/ReservedAssignmentsRegulationSchedulerTest.java index 147c490e7..75f504707 100644 --- a/src/test/java/org/ays/common/scheduler/ReservedAssignmentsRegulationSchedulerTest.java +++ b/src/test/java/org/ays/assignment/scheduler/ReservedAssignmentsRegulationSchedulerTest.java @@ -1,4 +1,4 @@ -package org.ays.common.scheduler; +package org.ays.assignment.scheduler; import org.ays.AbstractUnitTest; import org.ays.assignment.model.entity.AssignmentEntity; @@ -16,7 +16,7 @@ class ReservedAssignmentsRegulationSchedulerTest extends AbstractUnitTest { @InjectMocks - private ReservedAssignmentsRegulationScheduler reservedAssignmentsRegulationSchedulerMock; + private ReservedAssignmentsRegulationScheduler reservedAssignmentsRegulationScheduler; @Mock private AssignmentRepository assignmentRepository; @@ -41,15 +41,15 @@ void whenReservedAssignmentsUpdateToAvailableByBefore20Seconds_thenSaveAll() { )).thenReturn(mockAssignmentEntities); // Then - reservedAssignmentsRegulationSchedulerMock.updateReservedAssignmentsToAvailable(); + reservedAssignmentsRegulationScheduler.updateReservedAssignmentsToAvailable(); // Verify - Mockito.verify(assignmentRepository, Mockito.atLeast(1)) + Mockito.verify(assignmentRepository, Mockito.times(1)) .findAllByStatusAndCreatedAtBefore( Mockito.any(AssignmentStatus.class), Mockito.any(LocalDateTime.class) ); - Mockito.verify(assignmentRepository, Mockito.atLeast(1)) + Mockito.verify(assignmentRepository, Mockito.times(1)) .saveAll(Mockito.anyList()); } diff --git a/src/test/java/org/ays/assignment/service/impl/AssignmentSearchServiceImplTest.java b/src/test/java/org/ays/assignment/service/impl/AssignmentSearchServiceImplTest.java index 9b248a6ef..fc001aec5 100644 --- a/src/test/java/org/ays/assignment/service/impl/AssignmentSearchServiceImplTest.java +++ b/src/test/java/org/ays/assignment/service/impl/AssignmentSearchServiceImplTest.java @@ -61,8 +61,11 @@ void givenValidAssignmentSearchRequest_whenAssignmentSearched_thenReturnAssignme .build(); // When - Mockito.when(identity.getUserId()).thenReturn(mockUserEntity.getId()); - Mockito.when(identity.getInstitutionId()).thenReturn(mockUserEntity.getInstitutionId()); + Mockito.when(identity.getUserId()) + .thenReturn(mockUserEntity.getId()); + + Mockito.when(identity.getInstitutionId()) + .thenReturn(mockUserEntity.getInstitutionId()); Mockito.when(userRepository.findByIdAndInstitutionId( mockUserEntity.getId(), @@ -81,15 +84,19 @@ void givenValidAssignmentSearchRequest_whenAssignmentSearched_thenReturnAssignme // Then assignmentSearchService.searchAssignment(mockAssignmentSearchRequest); - Mockito.verify(identity, Mockito.times(1)).getUserId(); - Mockito.verify(identity, Mockito.times(1)).getInstitutionId(); + // Verify + Mockito.verify(identity, Mockito.times(1)) + .getUserId(); + Mockito.verify(identity, Mockito.times(1)) + .getInstitutionId(); Mockito.verify(userRepository, Mockito.times(1)) .findByIdAndInstitutionId(mockUserEntity.getId(), mockUserEntity.getInstitutionId()); Mockito.verify(assignmentRepository, Mockito.times(1)) .findByUserIdAndStatusNot(mockUserEntity.getId(), AssignmentStatus.DONE); Mockito.verify(assignmentRepository, Mockito.times(1)) .findNearestAvailableAssignment(mockAssignmentPoint, mockUserEntity.getInstitutionId()); - Mockito.verify(assignmentRepository, Mockito.times(1)).save(mockAssignmentEntity); + Mockito.verify(assignmentRepository, Mockito.times(1)) + .save(mockAssignmentEntity); } @Test @@ -103,41 +110,52 @@ void givenValidAssignmentSearchRequest_whenUserIsNotReady_thenThrowAysAssignment UserEntity mockUserEntity = new UserEntityBuilder() .withValidFields() .build(); + Point mockAssignmentPoint = AysLocationUtil.generatePoint( mockAssignmentSearchRequest.getLongitude(), mockAssignmentSearchRequest.getLatitude() ); + AssignmentEntity mockAssignmentEntity = new AssignmentEntityBuilder() .withValidFields() .withPoint(mockAssignmentPoint) .build(); // When - Mockito.when(identity.getUserId()).thenReturn(mockUserEntity.getId()); - Mockito.when(identity.getInstitutionId()).thenReturn(mockUserEntity.getInstitutionId()); + Mockito.when(identity.getUserId()) + .thenReturn(mockUserEntity.getId()); + + Mockito.when(identity.getInstitutionId()) + .thenReturn(mockUserEntity.getInstitutionId()); + Mockito.when(assignmentRepository.findByUserIdAndStatusNot(mockUserEntity.getId(), AssignmentStatus.DONE)) .thenReturn(Optional.empty()); + Mockito.when(userRepository.findByIdAndInstitutionId( mockUserEntity.getId(), mockUserEntity.getInstitutionId() )) .thenReturn(Optional.of(mockUserEntity)); - // Then Assertions.assertThrows( AysAssignmentUserNotReadyException.class, () -> assignmentSearchService.searchAssignment(mockAssignmentSearchRequest) ); - Mockito.verify(identity, Mockito.times(1)).getUserId(); - Mockito.verify(identity, Mockito.times(1)).getInstitutionId(); + + // Verify + Mockito.verify(identity, Mockito.times(1)) + .getUserId(); + Mockito.verify(identity, Mockito.times(1)) + .getInstitutionId(); Mockito.verify(userRepository, Mockito.times(1)) .findByIdAndInstitutionId(mockUserEntity.getId(), mockUserEntity.getInstitutionId()); Mockito.verify(assignmentRepository, Mockito.times(1)) .findByUserIdAndStatusNot(mockUserEntity.getId(), AssignmentStatus.DONE); Mockito.verify(assignmentRepository, Mockito.times(0)) .findNearestAvailableAssignment(mockAssignmentPoint, mockUserEntity.getInstitutionId()); - Mockito.verify(assignmentRepository, Mockito.times(0)).save(mockAssignmentEntity); + Mockito.verify(assignmentRepository, Mockito.times(0)) + .save(mockAssignmentEntity); } @Test @@ -161,8 +179,11 @@ void givenInValidAssignmentSearchRequest_whenAssignmentNotExistForSearching_then .build(); // When - Mockito.when(identity.getUserId()).thenReturn(mockUserEntity.getId()); - Mockito.when(identity.getInstitutionId()).thenReturn(mockUserEntity.getInstitutionId()); + Mockito.when(identity.getUserId()) + .thenReturn(mockUserEntity.getId()); + + Mockito.when(identity.getInstitutionId()) + .thenReturn(mockUserEntity.getInstitutionId()); Mockito.when(userRepository.findByIdAndInstitutionId( mockUserEntity.getId(), @@ -182,15 +203,19 @@ void givenInValidAssignmentSearchRequest_whenAssignmentNotExistForSearching_then () -> assignmentSearchService.searchAssignment(mockAssignmentSearchRequest) ); - Mockito.verify(identity, Mockito.times(1)).getUserId(); - Mockito.verify(identity, Mockito.times(1)).getInstitutionId(); + // Verify + Mockito.verify(identity, Mockito.times(1)) + .getUserId(); + Mockito.verify(identity, Mockito.times(1)) + .getInstitutionId(); Mockito.verify(userRepository, Mockito.times(1)) .findByIdAndInstitutionId(mockUserEntity.getId(), mockUserEntity.getInstitutionId()); Mockito.verify(assignmentRepository, Mockito.times(1)) .findByUserIdAndStatusNot(mockUserEntity.getId(), AssignmentStatus.DONE); Mockito.verify(assignmentRepository, Mockito.times(1)) .findNearestAvailableAssignment(mockAssignmentPoint, mockUserEntity.getInstitutionId()); - Mockito.verify(assignmentRepository, Mockito.times(0)).save(mockAssignmentEntity); + Mockito.verify(assignmentRepository, Mockito.times(0)) + .save(mockAssignmentEntity); } @Test @@ -209,8 +234,11 @@ void givenValidAssignmentSearchRequest_whenUserAlreadyHasAssignment_thenThrowAys .build(); // When - Mockito.when(identity.getUserId()).thenReturn(mockUserEntity.getId()); - Mockito.when(identity.getInstitutionId()).thenReturn(mockUserEntity.getInstitutionId()); + Mockito.when(identity.getUserId()) + .thenReturn(mockUserEntity.getId()); + + Mockito.when(identity.getInstitutionId()) + .thenReturn(mockUserEntity.getInstitutionId()); Mockito.when(userRepository.findByIdAndInstitutionId( mockUserEntity.getId(), @@ -230,15 +258,19 @@ void givenValidAssignmentSearchRequest_whenUserAlreadyHasAssignment_thenThrowAys () -> assignmentSearchService.searchAssignment(mockAssignmentSearchRequest) ); - Mockito.verify(identity, Mockito.times(1)).getUserId(); - Mockito.verify(identity, Mockito.times(1)).getInstitutionId(); + // Verify + Mockito.verify(identity, Mockito.times(1)) + .getUserId(); + Mockito.verify(identity, Mockito.times(1)) + .getInstitutionId(); Mockito.verify(userRepository, Mockito.times(1)) .findByIdAndInstitutionId(mockUserEntity.getId(), mockUserEntity.getInstitutionId()); Mockito.verify(assignmentRepository, Mockito.times(1)) .findByUserIdAndStatusNot(mockUserEntity.getId(), AssignmentStatus.DONE); Mockito.verify(assignmentRepository, Mockito.never()) .findNearestAvailableAssignment(Mockito.any(Point.class), Mockito.anyString()); - Mockito.verify(assignmentRepository, Mockito.never()).save(Mockito.any(AssignmentEntity.class)); + Mockito.verify(assignmentRepository, Mockito.never()) + .save(Mockito.any(AssignmentEntity.class)); } } diff --git a/src/test/java/org/ays/common/scheduler/InvalidTokenDeletionSchedulerSystemTest.java b/src/test/java/org/ays/auth/scheduler/InvalidTokenDeletionSchedulerSystemTest.java similarity index 93% rename from src/test/java/org/ays/common/scheduler/InvalidTokenDeletionSchedulerSystemTest.java rename to src/test/java/org/ays/auth/scheduler/InvalidTokenDeletionSchedulerSystemTest.java index 2de04fcea..685ac85b9 100644 --- a/src/test/java/org/ays/common/scheduler/InvalidTokenDeletionSchedulerSystemTest.java +++ b/src/test/java/org/ays/auth/scheduler/InvalidTokenDeletionSchedulerSystemTest.java @@ -1,4 +1,4 @@ -package org.ays.common.scheduler; +package org.ays.auth.scheduler; import org.awaitility.Awaitility; import org.ays.AbstractSystemTest; @@ -42,7 +42,7 @@ void whenCronTaskUnusedInvalidTokenDeletionSchedulerCreated_thenCronTaskSchedule .map(ScheduledTask::getTask) .filter(CronTask.class::isInstance) .map(CronTask.class::cast) - .filter(task -> task.toString().contains("common.scheduler.InvalidTokenDeletionScheduler")) + .filter(task -> task.toString().contains(InvalidTokenDeletionScheduler.class.getSimpleName())) .findFirst() .orElseThrow(() -> new IllegalStateException("No scheduled tasks")); @@ -50,9 +50,9 @@ void whenCronTaskUnusedInvalidTokenDeletionSchedulerCreated_thenCronTaskSchedule } @Test - void whenWait10Seconds_thenClearInvalidToken() { + void whenWait2Seconds_thenClearInvalidToken() { - // When + // Initialize int refreshTokenExpireDay = Integer .parseInt(AysConfigurationParameter.AUTH_REFRESH_TOKEN_EXPIRE_DAY.getDefaultValue()); List mockInvalidTokenEntities = List.of( @@ -72,12 +72,15 @@ void whenWait10Seconds_thenClearInvalidToken() { .await() .atMost(2, java.util.concurrent.TimeUnit.SECONDS) .untilAsserted(() -> { + Mockito.verify(invalidTokenDeletionScheduler, Mockito.atLeast(1)) .deleteInvalidTokens(); mockInvalidTokenEntities.forEach(mockInvalidTokenEntity -> { + Optional invalidTokenEntity = invalidTokenRepository .findById(mockInvalidTokenEntity.getId()); + Assertions.assertTrue(invalidTokenEntity.isEmpty()); }); }); diff --git a/src/test/java/org/ays/auth/scheduler/InvalidTokenDeletionSchedulerTest.java b/src/test/java/org/ays/auth/scheduler/InvalidTokenDeletionSchedulerTest.java new file mode 100644 index 000000000..2e4444bf6 --- /dev/null +++ b/src/test/java/org/ays/auth/scheduler/InvalidTokenDeletionSchedulerTest.java @@ -0,0 +1,51 @@ +package org.ays.auth.scheduler; + +import org.ays.AbstractUnitTest; +import org.ays.auth.model.enums.AysConfigurationParameter; +import org.ays.auth.repository.AysInvalidTokenRepository; +import org.ays.parameter.model.AysParameter; +import org.ays.parameter.service.AysParameterService; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; + +import java.time.LocalDateTime; + +class InvalidTokenDeletionSchedulerTest extends AbstractUnitTest { + + @InjectMocks + private InvalidTokenDeletionScheduler invalidTokenDeletionScheduler; + + @Mock + private AysParameterService parameterService; + + @Mock + private AysInvalidTokenRepository invalidTokenRepository; + + + @Test + void whenReservedAssignmentsUpdateToAvailableByBefore20Seconds_thenSaveAll() { + + // When + AysParameter mockParameter = AysParameter + .from(AysConfigurationParameter.AUTH_REFRESH_TOKEN_EXPIRE_DAY); + Mockito.when(parameterService.getParameter(Mockito.anyString())) + .thenReturn(mockParameter); + + Mockito.doNothing() + .when(invalidTokenRepository) + .deleteAllByCreatedAtBefore(Mockito.any(LocalDateTime.class)); + + // Then + invalidTokenDeletionScheduler.deleteInvalidTokens(); + + // Verify + Mockito.verify(parameterService, Mockito.times(1)) + .getParameter(Mockito.anyString()); + + Mockito.verify(invalidTokenRepository, Mockito.times(1)) + .deleteAllByCreatedAtBefore(Mockito.any(LocalDateTime.class)); + } + +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 82d1db4d7..5222efc1f 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -1,3 +1,8 @@ +logging: + level: + web: trace + sql: info + spring: datasource: username: ays