Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
feat(): Fix test, fix PR
Browse files Browse the repository at this point in the history
  • Loading branch information
DelaunayAlex committed Feb 12, 2024
1 parent 787be8f commit 491bea1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.task.TaskExecutorBuilder;
import org.springframework.boot.task.ThreadPoolTaskExecutorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
Expand Down Expand Up @@ -75,13 +75,13 @@ public TaskScheduler taskScheduler() {
* With a default with default configuration: org.springframework.boot.autoconfigure.task.TaskExecutionProperties.Pool
*/
@Bean
public TaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) {
public TaskExecutor applicationTaskExecutor(ThreadPoolTaskExecutorBuilder builder) {
return builder.threadNamePrefix("app-task-exec").build();
}

@Bean
public ApplicationRunner scheduledMissedCampaignToExecute(CampaignScheduler campaignScheduler) {
return arg -> campaignScheduler.scheduledMissedCampaignToExecute();
return arg -> campaignScheduler.scheduledMissedCampaignIds();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class SchedulingCampaignFileRepository implements PeriodicScheduledCampai
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
private final ReadWriteLock rwLock;

public SchedulingCampaignFileRepository(@Value(CONFIGURATION_FOLDER_SPRING_VALUE) String storeFolderPath) throws UncheckedIOException {
SchedulingCampaignFileRepository(@Value(CONFIGURATION_FOLDER_SPRING_VALUE) String storeFolderPath) throws UncheckedIOException {
this.rwLock = new ReentrantReadWriteLock(true);
this.storeFolderPath = Paths.get(storeFolderPath).resolve(ROOT_DIRECTORY_NAME);
this.resolvedFilePath = this.storeFolderPath.resolve(SCHEDULING_CAMPAIGNS_FILE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ private Callable<Void> executeScheduledCampaignById(List<Long> campaignsId) {
};
}

public void scheduledMissedCampaignToExecute() {
scheduledCampaignIdsToExecute().toList();
public void scheduledMissedCampaignIds() {
scheduledCampaignIdsToExecute().forEach(campaignId -> LOGGER.info("Reschedule missed campaign with id {}", campaignId));
}

synchronized private Stream<List<Long>> scheduledCampaignIdsToExecute() {
try {
return periodicScheduledCampaignRepository.getALl().stream()
List<PeriodicScheduledCampaign> all = periodicScheduledCampaignRepository.getALl();
return all.stream()
.filter(sc -> sc.nextExecutionDate != null)
.filter(sc -> sc.nextExecutionDate.isBefore(LocalDateTime.now(clock)))
.peek(this::prepareScheduledCampaignForNextExecution)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
Expand All @@ -34,11 +34,7 @@
import com.chutneytesting.campaign.domain.Frequency;
import com.chutneytesting.campaign.domain.PeriodicScheduledCampaign;
import com.chutneytesting.campaign.domain.PeriodicScheduledCampaignRepository;
import com.chutneytesting.campaign.infra.SchedulingCampaignFileRepository;
import com.chutneytesting.execution.domain.campaign.CampaignExecutionEngine;
import com.chutneytesting.tools.file.FileUtils;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
Expand All @@ -61,10 +57,6 @@ public class CampaignSchedulerTest {

private final CampaignExecutionEngine campaignExecutionEngine = mock(CampaignExecutionEngine.class);
private final PeriodicScheduledCampaignRepository periodicScheduledCampaignRepository = mock(PeriodicScheduledCampaignRepository.class);

private static Path SCHEDULING_CAMPAIGN_FILE;
@TempDir
private static Path temporaryFolder;
private final Clock clock = mock(Clock.class);

@BeforeEach
Expand Down Expand Up @@ -125,65 +117,32 @@ void should_add_next_execution_when_executing_periodic_scheduled_campaign_except

@Test
void should_reschedule_missed_campaign() {
String tmpConfDir = temporaryFolder.toFile().getAbsolutePath();
SCHEDULING_CAMPAIGN_FILE = Paths.get(tmpConfDir + "/scheduling/schedulingCampaigns.json");
CampaignScheduler campaignScheduler = new CampaignScheduler(campaignExecutionEngine, clock, new SchedulingCampaignFileRepository(tmpConfDir), Executors.newFixedThreadPool(2));

String initialSchedules = """
{
"1" : {
"id" : "1",
"campaignsId" : [ 11 ],
"campaignsTitle" : [ "campaign title 1" ],
"schedulingDate" : [ 2024, 1, 1, 14, 0 ],
"frequency" : "Weekly"
},
"2" : {
"id" : "2",
"campaignsId" : [ 22 ],
"campaignsTitle" : [ "campaign title 2" ],
"schedulingDate" : [ 2023, 3, 4, 7, 10 ],
"frequency" : "Hourly"
},
"3" : {
"id" : "3",
"campaignsId" : [ 33 ],
"campaignsTitle" : [ "campaign title 3" ],
"schedulingDate" : [ 2024, 2, 2, 14, 0 ]
}
}
""";
FileUtils.initFolder(SCHEDULING_CAMPAIGN_FILE.getParent());
FileUtils.writeContent(SCHEDULING_CAMPAIGN_FILE, initialSchedules);


String expectedSchedules =
"""
{
"4" : {
"id" : "4",
"campaignsId" : [ 11 ],
"campaignsTitle" : [ "campaign title 1" ],
"schedulingDate" : [ 2024, 3, 18, 14, 0 ],
"frequency" : "Weekly"
},
"5" : {
"id" : "5",
"campaignsId" : [ 22 ],
"campaignsTitle" : [ "campaign title 2" ],
"schedulingDate" : [ 2024, 3, 15, 16, 10 ],
"frequency" : "Hourly"
}
}
""";
PeriodicScheduledCampaign periodicScheduledCampaign1 = new PeriodicScheduledCampaign(1L, List.of(11L), List.of("campaign title 1"), LocalDateTime.of(2024, 1, 1, 14, 0), Frequency.WEEKLY);
PeriodicScheduledCampaign periodicScheduledCampaign2 = new PeriodicScheduledCampaign(2L, List.of(22L), List.of("campaign title 2"), LocalDateTime.of(2023, 3, 4, 7, 10), Frequency.HOURLY);
PeriodicScheduledCampaign periodicScheduledCampaign3 = new PeriodicScheduledCampaign(3L, List.of(33L), List.of("campaign title 3"), LocalDateTime.of(2024, 2, 2, 14, 0));
when(periodicScheduledCampaignRepository.getALl())
.thenReturn(
List.of(periodicScheduledCampaign1, periodicScheduledCampaign2, periodicScheduledCampaign3)
);
when(periodicScheduledCampaignRepository.add(any()))
.thenReturn(
null
);
doNothing().when(periodicScheduledCampaignRepository).removeById(any());

PeriodicScheduledCampaign expected1 = new PeriodicScheduledCampaign(1L, List.of(11L), List.of("campaign title 1"), LocalDateTime.of(2024, 3, 18, 14, 0), Frequency.WEEKLY);
PeriodicScheduledCampaign expected2 = new PeriodicScheduledCampaign(2L, List.of(22L), List.of("campaign title 2"), LocalDateTime.of(2024, 3, 15, 16, 10), Frequency.HOURLY);


// WHEN
campaignScheduler.scheduledMissedCampaignToExecute();
sut.scheduledMissedCampaignIds();

// THEN
String result = FileUtils.readContent(SCHEDULING_CAMPAIGN_FILE);
assertThat(result).isEqualToIgnoringNewLines(expectedSchedules);

verify(periodicScheduledCampaignRepository).add(expected1);
verify(periodicScheduledCampaignRepository).add(expected2);
verify(periodicScheduledCampaignRepository).removeById(1L);
verify(periodicScheduledCampaignRepository).removeById(2L);
verify(periodicScheduledCampaignRepository).removeById(3L);
}

@Test
Expand Down

0 comments on commit 491bea1

Please sign in to comment.