Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrouand committed Oct 8, 2024
1 parent 2d0fffb commit 4c09d8c
Show file tree
Hide file tree
Showing 14 changed files with 409 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import static com.chutneytesting.campaign.domain.Frequency.toFrequency;

import com.chutneytesting.campaign.api.dto.SchedulingCampaignDto;
import com.chutneytesting.campaign.api.dto.SchedulingCampaignDto.CampaignExecutionRequestDto;
import com.chutneytesting.campaign.domain.PeriodicScheduledCampaign;
import com.chutneytesting.campaign.domain.PeriodicScheduledCampaignRepository;
import com.chutneytesting.campaign.domain.PeriodicScheduledCampaign.CampaignExecutionRequest;
import com.chutneytesting.campaign.domain.ScheduledCampaignRepository;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -31,31 +33,31 @@
@CrossOrigin(origins = "*")
public class ScheduleCampaignController {

private final PeriodicScheduledCampaignRepository periodicScheduledCampaignRepository;
private final ScheduledCampaignRepository scheduledCampaignRepository;

public ScheduleCampaignController(PeriodicScheduledCampaignRepository periodicScheduledCampaignRepository) {
this.periodicScheduledCampaignRepository = periodicScheduledCampaignRepository;
public ScheduleCampaignController(ScheduledCampaignRepository scheduledCampaignRepository) {
this.scheduledCampaignRepository = scheduledCampaignRepository;
}

@PreAuthorize("hasAuthority('CAMPAIGN_READ')")
@GetMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE)
public List<SchedulingCampaignDto> getAll() {
return periodicScheduledCampaignRepository.getAll().stream()
.map(sc -> new SchedulingCampaignDto(sc.id, sc.campaignsId, sc.campaignsTitle, sc.nextExecutionDate, sc.frequency.label))
return scheduledCampaignRepository.getAll().stream()
.map(sc -> new SchedulingCampaignDto(sc.id, sc.nextExecutionDate, sc.frequency.label, sc.environment, sc.campaignExecutionRequests.stream().map(aa -> new CampaignExecutionRequestDto(aa.campaignId(), aa.campaignTitle(), aa.datasetId())).toList()))
.sorted(Comparator.comparing(SchedulingCampaignDto::getSchedulingDate))
.collect(Collectors.toList());
}

@PreAuthorize("hasAuthority('CAMPAIGN_WRITE')")
@PostMapping(path = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void add(@RequestBody SchedulingCampaignDto dto) {
periodicScheduledCampaignRepository.add(new PeriodicScheduledCampaign(null, dto.getCampaignsId(), dto.getCampaignsTitle(), dto.getSchedulingDate(), toFrequency(dto.getFrequency())));
scheduledCampaignRepository.add(new PeriodicScheduledCampaign(null, dto.getSchedulingDate(), toFrequency(dto.getFrequency()), dto.getEnvironment(), dto.getCampaignExecutionRequestDto().stream().map(aa -> new CampaignExecutionRequest(aa.campaignId(), aa.campaignTitle(), aa.datasetId())).toList()));
}

@PreAuthorize("hasAuthority('CAMPAIGN_WRITE')")
@DeleteMapping(path = "/{schedulingCampaignId}", produces = MediaType.APPLICATION_JSON_VALUE)
public void delete(@PathVariable("schedulingCampaignId") Long schedulingCampaignId) {
periodicScheduledCampaignRepository.removeById(schedulingCampaignId);
scheduledCampaignRepository.removeById(schedulingCampaignId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,47 @@
public class SchedulingCampaignDto {

private Long id;
private List<Long> campaignsId;
private List<String> campaignsTitle;
private LocalDateTime schedulingDate;
private String frequency;
private String environment;
private List<CampaignExecutionRequestDto> campaignExecutionRequestDto;

public SchedulingCampaignDto() {
}

public SchedulingCampaignDto(Long id,
List<Long> campaignsId,
List<String> campaignsTitle,
LocalDateTime schedulingDate,
String frequency
String frequency,
String environment,
List<CampaignExecutionRequestDto> campaignExecutionRequestDto
) {
this.id = id;
this.campaignsId = campaignsId;
this.campaignsTitle = campaignsTitle;
this.schedulingDate = schedulingDate;
this.frequency = frequency;
this.environment = environment;
this.campaignExecutionRequestDto = campaignExecutionRequestDto;
}

public Long getId() {
return id;
}

public List<Long> getCampaignsId() {
return campaignsId;
}

public List<String> getCampaignsTitle() {
return campaignsTitle;
}

public LocalDateTime getSchedulingDate() {
return schedulingDate;
}

public String getFrequency() {
return frequency;
}

public String getEnvironment() {
return environment;
}

public List<CampaignExecutionRequestDto> getCampaignExecutionRequestDto() {
return campaignExecutionRequestDto;
}

public record CampaignExecutionRequestDto(Long campaignId, String campaignTitle, String datasetId) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,58 @@
public class PeriodicScheduledCampaign {

public final Long id;
public final List<Long> campaignsId;
public final List<String> campaignsTitle;
public final LocalDateTime nextExecutionDate;
public final Frequency frequency;
public final String environment;
public final List<CampaignExecutionRequest> campaignExecutionRequests;

public PeriodicScheduledCampaign(Long id, List<Long> campaignsId, List<String> campaignsTitle, LocalDateTime nextExecutionDate, Frequency frequency) {
public PeriodicScheduledCampaign(Long id, LocalDateTime nextExecutionDate, Frequency frequency, String environment, List<CampaignExecutionRequest> campaignExecutionRequests) {
this.id = id;
this.campaignsId = campaignsId;
this.campaignsTitle = campaignsTitle;
this.nextExecutionDate = nextExecutionDate;
this.frequency = frequency;
this.environment = environment;
this.campaignExecutionRequests = campaignExecutionRequests;
}

public PeriodicScheduledCampaign(Long id, List<Long> campaignsId, List<String> campaignsTitle, LocalDateTime nextExecutionDate) {
this(id, campaignsId, campaignsTitle, nextExecutionDate, Frequency.EMPTY);
public PeriodicScheduledCampaign nextScheduledExecution() {
LocalDateTime scheduledDate = switch (this.frequency) {
case HOURLY -> this.nextExecutionDate.plusHours(1);
case DAILY -> this.nextExecutionDate.plusDays(1);
case WEEKLY -> this.nextExecutionDate.plusWeeks(1);
case MONTHLY -> this.nextExecutionDate.plusMonths(1);
default -> throw new IllegalStateException("Unexpected value: " + this.frequency);
};
return new PeriodicScheduledCampaign(id, scheduledDate, frequency, environment, campaignExecutionRequests);
}

public PeriodicScheduledCampaign(Long id, Long campaignId, String campaignTitle, LocalDateTime nextExecutionDate) {
this(id, List.of(campaignId), List.of(campaignTitle), nextExecutionDate, Frequency.EMPTY);
public record CampaignExecutionRequest(Long campaignId, String campaignTitle, String datasetId) {
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PeriodicScheduledCampaign that = (PeriodicScheduledCampaign) o;
return Objects.equals(id, that.id) && Objects.equals(campaignsId, that.campaignsId) && Objects.equals(campaignsTitle, that.campaignsTitle) && Objects.equals(nextExecutionDate, that.nextExecutionDate) && Objects.equals(frequency, that.frequency);
return Objects.equals(id, that.id) &&
Objects.equals(nextExecutionDate, that.nextExecutionDate) &&
Objects.equals(frequency, that.frequency) &&
Objects.equals(environment, that.environment) &&
Objects.equals(campaignExecutionRequests, that.campaignExecutionRequests);
}

@Override
public int hashCode() {
return Objects.hash(id, campaignsId, campaignsTitle, nextExecutionDate, frequency);
return Objects.hash(id, nextExecutionDate, frequency, environment, campaignExecutionRequests);
}

@Override
public String toString() {
return "SchedulingCampaign{" +
"id=" + id +
", campaignId=" + campaignsId +
", campaignTitle='" + campaignsTitle + '\'' +
", schedulingDate=" + nextExecutionDate +
", frequency='" + frequency + '\'' +
", environment='" + environment + '\'' +
", campaignExecutionRequests='" + campaignExecutionRequests + '\'' +
'}';
}

public PeriodicScheduledCampaign nextScheduledExecution() {
LocalDateTime scheduledDate = switch (this.frequency) {
case HOURLY -> this.nextExecutionDate.plusHours(1);
case DAILY -> this.nextExecutionDate.plusDays(1);
case WEEKLY -> this.nextExecutionDate.plusWeeks(1);
case MONTHLY -> this.nextExecutionDate.plusMonths(1);
default -> throw new IllegalStateException("Unexpected value: " + this.frequency);
};
return new PeriodicScheduledCampaign(id, campaignsId, campaignsTitle, scheduledDate, frequency);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* CRUD for SchedulingCampaign
*/
public interface PeriodicScheduledCampaignRepository {
public interface ScheduledCampaignRepository {

PeriodicScheduledCampaign add(PeriodicScheduledCampaign periodicScheduledCampaign);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.chutneytesting.campaign.domain.CampaignExecutionRepository;
import com.chutneytesting.campaign.domain.CampaignNotFoundException;
import com.chutneytesting.campaign.domain.CampaignRepository;
import com.chutneytesting.campaign.domain.PeriodicScheduledCampaignRepository;
import com.chutneytesting.campaign.domain.ScheduledCampaignRepository;
import com.chutneytesting.campaign.infra.jpa.CampaignEntity;
import com.chutneytesting.campaign.infra.jpa.CampaignScenarioEntity;
import com.chutneytesting.server.core.domain.scenario.campaign.Campaign;
Expand All @@ -33,15 +33,15 @@ public class DatabaseCampaignRepository implements CampaignRepository {
private final CampaignJpaRepository campaignJpaRepository;
private final CampaignScenarioJpaRepository campaignScenarioJpaRepository;
private final CampaignExecutionRepository campaignExecutionRepository;
private final PeriodicScheduledCampaignRepository periodicScheduledCampaignRepository;
private final ScheduledCampaignRepository scheduledCampaignRepository;

public DatabaseCampaignRepository(CampaignJpaRepository campaignJpaRepository,
CampaignScenarioJpaRepository campaignScenarioJpaRepository,
CampaignExecutionDBRepository campaignExecutionRepository, PeriodicScheduledCampaignRepository periodicScheduledCampaignRepository) {
CampaignExecutionDBRepository campaignExecutionRepository, ScheduledCampaignRepository scheduledCampaignRepository) {
this.campaignJpaRepository = campaignJpaRepository;
this.campaignScenarioJpaRepository = campaignScenarioJpaRepository;
this.campaignExecutionRepository = campaignExecutionRepository;
this.periodicScheduledCampaignRepository = periodicScheduledCampaignRepository;
this.scheduledCampaignRepository = scheduledCampaignRepository;
}

@Override
Expand Down Expand Up @@ -75,7 +75,7 @@ public boolean removeById(Long id) {
if (campaignJpaRepository.existsById(id)) {
campaignExecutionRepository.clearAllExecutionHistory(id);
campaignJpaRepository.deleteById(id);
periodicScheduledCampaignRepository.removeCampaignId(id);
scheduledCampaignRepository.removeCampaignId(id);
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,68 @@

package com.chutneytesting.campaign.infra;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.time.LocalDateTime;
import java.util.List;

public class SchedulingCampaignDto {
public final String id;
public final List<Long> campaignsId;
public final List<String> campaignsTitle;
public final LocalDateTime schedulingDate;
public final String frequency;
public final String environment;

private final List<Long> campaignsId;
private final List<String> campaignsTitle;
private final List<String> datasetsId;

@JsonIgnore
public final List<CampaignExecutionRequestDto> campaignExecutionRequestDto;

/**
* for ObjectMapper only
**/
@JsonCreator
public SchedulingCampaignDto() {
id = null;
campaignsId = null;
schedulingDate = null;
campaignsTitle = null;
frequency = null;
environment = null;
campaignExecutionRequestDto = null;
campaignsId = null;
campaignsTitle = null;
datasetsId = null;
}

@JsonIgnore
public SchedulingCampaignDto(String id,
List<Long> campaignsId,
List<String> campaignsTitle,
LocalDateTime schedulingDate,
String frequency) {
String frequency,
String environment,
List<CampaignExecutionRequestDto> campaignExecutionRequestDto) {
this.id = id;
this.campaignsId = campaignsId;
this.campaignsTitle = campaignsTitle;
this.schedulingDate = schedulingDate;
this.frequency = frequency;
this.campaignExecutionRequestDto = campaignExecutionRequestDto;
this.environment = environment;

this.campaignsId = campaignExecutionRequestDto.stream().map( cer -> cer.campaignId).toList();
this.campaignsTitle = campaignExecutionRequestDto.stream().map( cer -> cer.campaignTitle).toList();
this.datasetsId = campaignExecutionRequestDto.stream().map( cer -> cer.datasetId).toList();
}

public List<Long> getCampaignsId() {
return campaignExecutionRequestDto.stream().map( cer -> cer.campaignId).toList();
}

public List<String> getCampaignsTitle() {
return campaignExecutionRequestDto.stream().map( cer -> cer.campaignTitle).toList();
}

public List<String> getDatasetsId() {
return campaignExecutionRequestDto.stream().map( cer -> cer.datasetId).toList();
}

public record CampaignExecutionRequestDto(Long campaignId, String campaignTitle, String datasetId) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import static com.chutneytesting.tools.file.FileUtils.initFolder;

import com.chutneytesting.campaign.domain.PeriodicScheduledCampaign;
import com.chutneytesting.campaign.domain.PeriodicScheduledCampaignRepository;
import com.chutneytesting.campaign.domain.PeriodicScheduledCampaign.CampaignExecutionRequest;
import com.chutneytesting.campaign.domain.ScheduledCampaignRepository;
import com.chutneytesting.campaign.infra.SchedulingCampaignDto.CampaignExecutionRequestDto;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -27,6 +29,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand All @@ -38,7 +41,7 @@
* Scheduling campaign persistence.
*/
@Repository
public class SchedulingCampaignFileRepository implements PeriodicScheduledCampaignRepository {
public class SchedulingCampaignFileRepository implements ScheduledCampaignRepository {

private static final Path ROOT_DIRECTORY_NAME = Paths.get("scheduling");
private static final String SCHEDULING_CAMPAIGNS_FILE = "schedulingCampaigns.json";
Expand Down Expand Up @@ -99,12 +102,10 @@ public void removeCampaignId(Long id) {
Map<String, SchedulingCampaignDto> schedulingCampaigns = readFromDisk();
Map<String, SchedulingCampaignDto> schedulingCampaignsFiltered = new HashMap<>();
schedulingCampaigns.forEach((key, schedulingCampaignDto) -> {
int indexCampaignId = schedulingCampaignDto.campaignsId.indexOf(id);
if (indexCampaignId != -1) { // Remove id and title if the campaignId has been found
schedulingCampaignDto.campaignsTitle.remove(indexCampaignId);
schedulingCampaignDto.campaignsId.remove(indexCampaignId);
}
if (!schedulingCampaignDto.campaignsId.isEmpty()) { // Set the schedule only if a campaign is present after removal
Optional<CampaignExecutionRequestDto> foundRequest = schedulingCampaignDto.campaignExecutionRequestDto.stream().filter(cer -> cer.campaignId().equals(id)).findFirst();
// Remove id and title if the campaignId has been found
foundRequest.ifPresent(schedulingCampaignDto.campaignExecutionRequestDto::remove);
if (!schedulingCampaignDto.campaignExecutionRequestDto.isEmpty()) { // Set the schedule only if a campaign is present after removal
schedulingCampaignsFiltered.put(key, schedulingCampaignDto);
}
});
Expand Down Expand Up @@ -154,11 +155,11 @@ private void writeOnDisk(Path filePath, Map<String, SchedulingCampaignDto> sched
}

private PeriodicScheduledCampaign fromDto(SchedulingCampaignDto dto) {
return new PeriodicScheduledCampaign(Long.valueOf(dto.id), dto.campaignsId, dto.campaignsTitle, dto.schedulingDate, toFrequency(dto.frequency));
return new PeriodicScheduledCampaign(Long.valueOf(dto.id), dto.schedulingDate, toFrequency(dto.frequency), dto.environment, dto.campaignExecutionRequestDto.stream().map(aa -> new CampaignExecutionRequest(aa.campaignId(), aa.campaignTitle(), aa.datasetId())).toList());
}

private SchedulingCampaignDto toDto(long id, PeriodicScheduledCampaign periodicScheduledCampaign) {
return new SchedulingCampaignDto(String.valueOf(id), periodicScheduledCampaign.campaignsId, periodicScheduledCampaign.campaignsTitle, periodicScheduledCampaign.nextExecutionDate, periodicScheduledCampaign.frequency.label);
return new SchedulingCampaignDto(String.valueOf(id), periodicScheduledCampaign.nextExecutionDate, periodicScheduledCampaign.frequency.label, periodicScheduledCampaign.environment, periodicScheduledCampaign.campaignExecutionRequests.stream().map(aa -> new CampaignExecutionRequestDto(aa.campaignId(), aa.campaignTitle(), aa.datasetId())).toList());
}

private Long getCurrentMaxId(Map<String, SchedulingCampaignDto> schedulingCampaigns) {
Expand Down
Loading

0 comments on commit 4c09d8c

Please sign in to comment.