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, rename getALl(), no mock of Clock
Browse files Browse the repository at this point in the history
  • Loading branch information
DelaunayAlex committed Feb 13, 2024
1 parent 050c83c commit fef5822
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ScheduleCampaignController(PeriodicScheduledCampaignRepository periodicSc
@PreAuthorize("hasAuthority('CAMPAIGN_READ')")
@GetMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE)
public List<SchedulingCampaignDto> getAll() {
return periodicScheduledCampaignRepository.getALl().stream()
return periodicScheduledCampaignRepository.getAll().stream()
.map(sc -> new SchedulingCampaignDto(sc.id, sc.campaignsId, sc.campaignsTitle, sc.nextExecutionDate, sc.frequency.label))
.sorted(Comparator.comparing(SchedulingCampaignDto::getSchedulingDate))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ public interface PeriodicScheduledCampaignRepository {

void removeCampaignId(Long id);

List<PeriodicScheduledCampaign> getALl();
List<PeriodicScheduledCampaign> getAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void removeCampaignId(Long id) {
}

@Override
public List<PeriodicScheduledCampaign> getALl() {
public List<PeriodicScheduledCampaign> getAll() {
return readFromDisk().values().stream()
.map(this::fromDto)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void scheduledMissedCampaignIds() {

synchronized private Stream<List<Long>> scheduledCampaignIdsToExecute() {
try {
List<PeriodicScheduledCampaign> all = periodicScheduledCampaignRepository.getALl();
List<PeriodicScheduledCampaign> all = periodicScheduledCampaignRepository.getAll();
return all.stream()
.filter(sc -> sc.nextExecutionDate != null)
.filter(sc -> sc.nextExecutionDate.isBefore(LocalDateTime.now(clock)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void should_add_get_and_remove_scheduled_campaign() {

//// GET
// When
List<PeriodicScheduledCampaign> periodicScheduledCampaigns = sut.getALl();
List<PeriodicScheduledCampaign> periodicScheduledCampaigns = sut.getAll();

// Then
assertThat(periodicScheduledCampaigns).hasSize(2);
Expand Down Expand Up @@ -215,7 +215,7 @@ public void should_get_and_update_old_scheduled_campaign() {
PeriodicScheduledCampaign sc2 = new PeriodicScheduledCampaign(2L, 22L, "campaign title 2", LocalDateTime.of(2023, 3, 4, 7, 10));

// When
List<PeriodicScheduledCampaign> periodicScheduledCampaigns = sut.getALl();
List<PeriodicScheduledCampaign> periodicScheduledCampaigns = sut.getAll();
//Then
assertThat(periodicScheduledCampaigns).hasSize(1);
assertThat(periodicScheduledCampaigns).contains(sc1);
Expand All @@ -225,7 +225,7 @@ public void should_get_and_update_old_scheduled_campaign() {
sut.add(sc2);

// Then
periodicScheduledCampaigns = sut.getALl();
periodicScheduledCampaigns = sut.getAll();
assertThat(periodicScheduledCampaigns).hasSize(2);
assertThat(periodicScheduledCampaigns).contains(sc1);
assertThat(periodicScheduledCampaigns).contains(sc2);
Expand Down Expand Up @@ -263,7 +263,7 @@ void should_read_and_write_concurrently() throws InterruptedException {
};
Runnable readScheduledCampaigns = () -> {
try {
sut.getALl();
sut.getAll();
} catch (Exception e) {
exceptions.add(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,35 @@
import com.chutneytesting.execution.domain.campaign.CampaignExecutionEngine;
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.mockito.InOrder;
import org.mockito.Mock;

public class CampaignSchedulerTest {

private CampaignScheduler sut;

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

@BeforeEach
public void setUp() {
Clock fixedClock = Clock.fixed(LocalDateTime.of(2024, 3, 15, 15, 0, 0).toInstant(ZoneOffset.UTC), ZoneId.of("Europe/Paris"));
doReturn(fixedClock.instant()).when(clock).instant();
doReturn(fixedClock.getZone()).when(clock).getZone();
clock = Clock.systemDefaultZone();
sut = new CampaignScheduler(campaignExecutionEngine, clock, periodicScheduledCampaignRepository, Executors.newFixedThreadPool(2));
}

@ParameterizedTest()
@EnumSource(Frequency.class)
void should_execute_campaign_as_internal_user_named_auto_when_executing_periodic_scheduled_campaign(Frequency frequency) {
List<PeriodicScheduledCampaign> periodicScheduledCampaign = createPeriodicScheduledCampaigns(singletonList(frequency));
when(periodicScheduledCampaignRepository.getALl())
when(periodicScheduledCampaignRepository.getAll())
.thenReturn(
periodicScheduledCampaign
);
Expand All @@ -85,7 +79,7 @@ void should_execute_campaign_as_internal_user_named_auto_when_executing_periodic
@EnumSource(Frequency.class)
void should_remove_last_execution_when_executing_periodic_scheduled_campaign(Frequency frequency) {
List<PeriodicScheduledCampaign> periodicScheduledCampaign = createPeriodicScheduledCampaigns(singletonList(frequency));
when(periodicScheduledCampaignRepository.getALl())
when(periodicScheduledCampaignRepository.getAll())
.thenReturn(
periodicScheduledCampaign
);
Expand All @@ -99,7 +93,7 @@ void should_remove_last_execution_when_executing_periodic_scheduled_campaign(Fre
@EnumSource(Frequency.class)
void should_add_next_execution_when_executing_periodic_scheduled_campaign_except_for_EMPTY_frequency(Frequency frequency) {
List<PeriodicScheduledCampaign> periodicScheduledCampaign = createPeriodicScheduledCampaigns(singletonList(frequency));
when(periodicScheduledCampaignRepository.getALl())
when(periodicScheduledCampaignRepository.getAll())
.thenReturn(
periodicScheduledCampaign
);
Expand All @@ -116,22 +110,22 @@ void should_add_next_execution_when_executing_periodic_scheduled_campaign_except
}

@Test
void should_reschedule_missed_campaign() {
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())
void should_reschedule_missed_campaign_with_frequency_from_now_in_the_future() {
PeriodicScheduledCampaign scheduledCampaignWeeklyFrequency = new PeriodicScheduledCampaign(1L, List.of(11L), List.of("campaign title 1"), LocalDateTime.now().minusWeeks(5).minusHours(1).plusMinutes(15).withSecond(0).withNano(0), Frequency.WEEKLY);
PeriodicScheduledCampaign scheduledCampaignHourlyFrequency = new PeriodicScheduledCampaign(2L, List.of(22L), List.of("campaign title 2"), LocalDateTime.now().minusHours(6).plusMinutes(15).withSecond(0).withNano(0), Frequency.HOURLY);
PeriodicScheduledCampaign scheduledCampaignNoFrequency = new PeriodicScheduledCampaign(3L, List.of(33L), List.of("campaign title 3"), LocalDateTime.now().minusYears(1).minusHours(6).plusMinutes(15).withSecond(0).withNano(0));
when(periodicScheduledCampaignRepository.getAll())
.thenReturn(
List.of(periodicScheduledCampaign1, periodicScheduledCampaign2, periodicScheduledCampaign3)
List.of(scheduledCampaignWeeklyFrequency, scheduledCampaignHourlyFrequency, scheduledCampaignNoFrequency)
);
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);
PeriodicScheduledCampaign expected1 = new PeriodicScheduledCampaign(1L, List.of(11L), List.of("campaign title 1"), LocalDateTime.now().plusWeeks(1).minusHours(1).plusMinutes(15).withSecond(0).withNano(0), Frequency.WEEKLY);
PeriodicScheduledCampaign expected2 = new PeriodicScheduledCampaign(2L, List.of(22L), List.of("campaign title 2"), LocalDateTime.now().plusMinutes(15).withSecond(0).withNano(0), Frequency.HOURLY);


// WHEN
Expand All @@ -147,19 +141,19 @@ void should_reschedule_missed_campaign() {

@Test
void should_not_explode_when_runtime_exceptions_occur_retrieving_campaigns_to_execute() {
when(periodicScheduledCampaignRepository.getALl())
when(periodicScheduledCampaignRepository.getAll())
.thenThrow(new RuntimeException("scheduledCampaignRepository.getAll()"));
Assertions.assertDoesNotThrow(
() -> sut.executeScheduledCampaigns()
);

verify(periodicScheduledCampaignRepository).getALl();
verify(periodicScheduledCampaignRepository).getAll();
}

@Test
void should_not_explode_when_runtime_exceptions_occur_executing_campaigns() {
List<PeriodicScheduledCampaign> periodicScheduledCampaigns = createPeriodicScheduledCampaigns(asList(Frequency.MONTHLY, Frequency.DAILY));
when(periodicScheduledCampaignRepository.getALl())
when(periodicScheduledCampaignRepository.getAll())
.thenReturn(
periodicScheduledCampaigns
);
Expand All @@ -178,7 +172,7 @@ void should_execute_sequentially_when_executing_periodic_scheduled_campaigns() {
PeriodicScheduledCampaign sc1 = new PeriodicScheduledCampaign(1L, List.of(11L, 22L), List.of("cpg 11", "cpg 22"), now(clock).minusSeconds(5), Frequency.HOURLY);

List<PeriodicScheduledCampaign> periodicScheduledCampaign = List.of(sc1);
when(periodicScheduledCampaignRepository.getALl())
when(periodicScheduledCampaignRepository.getAll())
.thenReturn(
periodicScheduledCampaign
);
Expand Down

0 comments on commit fef5822

Please sign in to comment.