Skip to content

Commit

Permalink
AYS-28 | ReservedAssignmentsRegulationScheduler Method Has Been Fix…
Browse files Browse the repository at this point in the history
…ed for Set User ID of Assignment (#291)
  • Loading branch information
agitrubard authored Mar 5, 2024
1 parent b57f77b commit 8a4dcc1
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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.");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/ays/common/model/entity/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}


Expand All @@ -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());
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/ays/user/model/entity/UserEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.ays.common.scheduler;
package org.ays.assignment.scheduler;

import org.awaitility.Awaitility;
import org.ays.AbstractSystemTest;
Expand All @@ -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;
Expand All @@ -25,7 +26,6 @@

class ReservedAssignmentsRegulationSchedulerSystemTest extends AbstractSystemTest {


@Value("${ays.scheduler.reserved-assignments-regulation.cron}")
private String expectedCronExpression;

Expand All @@ -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<AssignmentEntity> mockAssignmentEntities) {

institutionRepository.save(institutionEntity);
userRepository.save(userEntity);
institutionRepository.save(mockInstitutionEntity);
userRepository.save(mockUserEntity);
assignmentRepository.saveAll(mockAssignmentEntities);
}

Expand All @@ -52,28 +52,30 @@ 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"));

Assertions.assertEquals(expectedCronExpression, cronTask.getExpression());
}

@Test
void whenWait10Seconds_thenClearInvalidToken() {
void whenWait2Seconds_thenUpdateReservedAssignmentsToAvailableAndUserToBusy() {

// Initialize
InstitutionEntity mockInstitutionEntity = new InstitutionEntityBuilder()
.withValidFields()
.build();
UserEntity mockUserEntity = new UserEntityBuilder()
.withValidFields()
.withSupportStatus(UserSupportStatus.READY)
.withInstitutionId(mockInstitutionEntity.getId())
.withInstitution(null)
.build();
List<AssignmentEntity> mockAssignmentEntities = List.of(
new AssignmentEntityBuilder()
.withValidFields()
.withStatus(AssignmentStatus.RESERVED)
.withCreatedAt(LocalDateTime.now().minusSeconds(20))
.withInstitutionId(mockInstitutionEntity.getId())
.withInstitution(null)
Expand All @@ -82,6 +84,7 @@ void whenWait10Seconds_thenClearInvalidToken() {
.build(),
new AssignmentEntityBuilder()
.withValidFields()
.withStatus(AssignmentStatus.RESERVED)
.withCreatedAt(LocalDateTime.now().minusSeconds(20))
.withInstitutionId(mockInstitutionEntity.getId())
.withInstitution(null)
Expand All @@ -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> assignmentEntity = assignmentRepository
.findById(mockAssignmentEntity.getId());

Assertions.assertTrue(assignmentEntity.isPresent());
Assertions.assertEquals(AssignmentStatus.AVAILABLE, assignmentEntity.get().getStatus());
Assertions.assertNull(assignmentEntity.get().getUserId());
});
});

}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +16,7 @@
class ReservedAssignmentsRegulationSchedulerTest extends AbstractUnitTest {

@InjectMocks
private ReservedAssignmentsRegulationScheduler reservedAssignmentsRegulationSchedulerMock;
private ReservedAssignmentsRegulationScheduler reservedAssignmentsRegulationScheduler;

@Mock
private AssignmentRepository assignmentRepository;
Expand All @@ -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());
}

Expand Down
Loading

0 comments on commit 8a4dcc1

Please sign in to comment.