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

Delete campaign from schedule after deleting campaign #1249

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ public interface PeriodicScheduledCampaignRepository {

void removeById(Long id);

void removeCampaignId(Long id);

List<PeriodicScheduledCampaign> getALl();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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.infra.jpa.CampaignEntity;
import com.chutneytesting.campaign.infra.jpa.CampaignScenarioEntity;
import com.chutneytesting.server.core.domain.scenario.campaign.Campaign;
Expand All @@ -41,13 +42,15 @@ public class DatabaseCampaignRepository implements CampaignRepository {
private final CampaignJpaRepository campaignJpaRepository;
private final CampaignScenarioJpaRepository campaignScenarioJpaRepository;
private final CampaignExecutionRepository campaignExecutionRepository;
private final PeriodicScheduledCampaignRepository periodicScheduledCampaignRepository;

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

@Override
Expand All @@ -66,6 +69,7 @@ public boolean removeById(Long id) {
if (campaignJpaRepository.existsById(id)) {
campaignExecutionRepository.clearAllExecutionHistory(id);
campaignJpaRepository.deleteById(id);
periodicScheduledCampaignRepository.removeCampaignId(id);
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand All @@ -55,7 +54,6 @@ public class SchedulingCampaignFileRepository implements PeriodicScheduledCampai

private final Path storeFolderPath;
private final Path resolvedFilePath;
private final AtomicLong currentMaxId = new AtomicLong();

private final ObjectMapper objectMapper = new ObjectMapper()
.findAndRegisterModules()
Expand All @@ -71,7 +69,6 @@ public class SchedulingCampaignFileRepository implements PeriodicScheduledCampai
this.storeFolderPath = Paths.get(storeFolderPath).resolve(ROOT_DIRECTORY_NAME);
this.resolvedFilePath = this.storeFolderPath.resolve(SCHEDULING_CAMPAIGNS_FILE);
initFolder(this.storeFolderPath);
currentMaxId.set(this.getALl().stream().map(sm -> sm.id).max(Long::compare).orElse(0L));
}

@Override
Expand All @@ -80,8 +77,8 @@ public PeriodicScheduledCampaign add(PeriodicScheduledCampaign periodicScheduled
(writeLock = rwLock.writeLock()).lock();
try {
Map<String, SchedulingCampaignDto> schedulingCampaigns = readFromDisk();
long id = currentMaxId.incrementAndGet();
schedulingCampaigns.put(String.valueOf(id), toDto(id, periodicScheduledCampaign));
Long nextId = getCurrentMaxId(schedulingCampaigns) + 1L;
schedulingCampaigns.put(String.valueOf(nextId), toDto(nextId, periodicScheduledCampaign));
writeOnDisk(resolvedFilePath, schedulingCampaigns);

return periodicScheduledCampaign;
Expand All @@ -92,8 +89,8 @@ public PeriodicScheduledCampaign add(PeriodicScheduledCampaign periodicScheduled

@Override
public void removeById(Long id) {
final Lock writeLock;
(writeLock = rwLock.writeLock()).lock();
final Lock writeLock = rwLock.writeLock();
writeLock.lock();
try {
Map<String, SchedulingCampaignDto> schedulingCampaigns = readFromDisk();
schedulingCampaigns.remove(String.valueOf(id));
Expand All @@ -103,6 +100,29 @@ public void removeById(Long id) {
}
}

@Override
public void removeCampaignId(Long id) {
final Lock writeLock = rwLock.writeLock();
writeLock.lock();
try {
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
schedulingCampaignsFiltered.put(key, schedulingCampaignDto);
}
});
writeOnDisk(resolvedFilePath, schedulingCampaignsFiltered);
} finally {
writeLock.unlock();
}
}

@Override
public List<PeriodicScheduledCampaign> getALl() {
return readFromDisk().values().stream()
Expand Down Expand Up @@ -149,4 +169,8 @@ private PeriodicScheduledCampaign fromDto(SchedulingCampaignDto dto) {
private SchedulingCampaignDto toDto(long id, PeriodicScheduledCampaign periodicScheduledCampaign) {
return new SchedulingCampaignDto(String.valueOf(id), periodicScheduledCampaign.campaignsId, periodicScheduledCampaign.campaignsTitle, periodicScheduledCampaign.nextExecutionDate, periodicScheduledCampaign.frequency.label);
}

private Long getCurrentMaxId(Map<String, SchedulingCampaignDto> schedulingCampaigns) {
return schedulingCampaigns.keySet().stream().mapToLong(Long::valueOf).max().orElse(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

Expand All @@ -42,9 +43,15 @@ public class PeriodicScheduledCampaignFileRepositoryTest {
@TempDir
private static Path temporaryFolder;

@BeforeEach
public void setup() {
String tmpConfDir = temporaryFolder.toFile().getAbsolutePath();
SCHEDULING_CAMPAIGN_FILE = Paths.get(tmpConfDir + "/scheduling/schedulingCampaigns.json");
sut = new SchedulingCampaignFileRepository(tmpConfDir);
}

@Test
public void should_add_get_and_remove_scheduled_campaign() {
initSut(null);
//// ADD
// Given
PeriodicScheduledCampaign sc1 = new PeriodicScheduledCampaign(null, 11L, "campaign title 1", LocalDateTime.of(2020, 2, 4, 7, 10));
Expand Down Expand Up @@ -131,6 +138,61 @@ public void should_add_get_and_remove_scheduled_campaign() {
assertThat(periodicScheduledCampaigns).contains(sc1WithId, sc3WithId);
}

@Test
public void should_remove_campaign_from_scheduled() {
// Given
FileUtils.writeContent(SCHEDULING_CAMPAIGN_FILE, "{}");
PeriodicScheduledCampaign periodicScheduledCampaign = new PeriodicScheduledCampaign(null, List.of(1L, 2L, 3L), List.of("campaign title 1","campaign title 2","campaign title 3"), LocalDateTime.of(2024, 2, 4, 7, 10));
sut.add(periodicScheduledCampaign);

String expectedAdded =
"""
{
"1" : {
"id" : "1",
"campaignsId" : [ 1, 3 ],
"campaignsTitle" : [ "campaign title 1", "campaign title 3" ],
"schedulingDate" : [ 2024, 2, 4, 7, 10 ]
}
}
""";

// When
sut.removeCampaignId(2L);

// Then
String actualContent = FileUtils.readContent(SCHEDULING_CAMPAIGN_FILE);
assertThat(actualContent).isEqualToIgnoringNewLines(expectedAdded);
}

@Test
public void should_remove_schedule_without_campaign_after_removing_campaign() {
// Given
FileUtils.writeContent(SCHEDULING_CAMPAIGN_FILE, "{}");
PeriodicScheduledCampaign sc1 = new PeriodicScheduledCampaign(null, 11L, "campaign title 1", LocalDateTime.of(2020, 2, 4, 7, 10));
PeriodicScheduledCampaign sc2 = new PeriodicScheduledCampaign(null, 22L, "campaign title 2", LocalDateTime.of(2021, 3, 5, 8, 11));
sut.add(sc1);
sut.add(sc2);
String expectedAdded =
"""
{
"2" : {
"id" : "2",
"campaignsId" : [ 22 ],
"campaignsTitle" : [ "campaign title 2" ],
"schedulingDate" : [ 2021, 3, 5, 8, 11 ]
}
}
""";

// When
sut.removeCampaignId(11L);

// Then
String actualContent = FileUtils.readContent(SCHEDULING_CAMPAIGN_FILE);
assertThat(actualContent).isEqualToIgnoringNewLines(expectedAdded);
}

@Test
public void should_get_and_update_old_scheduled_campaign() {
//// Get
Expand All @@ -147,7 +209,7 @@ public void should_get_and_update_old_scheduled_campaign() {
}
""";

initSut(old_scheduled_campaign);
FileUtils.writeContent(SCHEDULING_CAMPAIGN_FILE, old_scheduled_campaign);

PeriodicScheduledCampaign sc1 = new PeriodicScheduledCampaign(1L, 11L, "campaign title 1", LocalDateTime.of(2020, 2, 4, 7, 10));
PeriodicScheduledCampaign sc2 = new PeriodicScheduledCampaign(2L, 22L, "campaign title 2", LocalDateTime.of(2023, 3, 4, 7, 10));
Expand Down Expand Up @@ -191,7 +253,6 @@ public void should_get_and_update_old_scheduled_campaign() {

@Test
void should_read_and_write_concurrently() throws InterruptedException {
initSut(null);
List<Exception> exceptions = new ArrayList<>();
Runnable addScheduledCampaign = () -> {
try {
Expand Down Expand Up @@ -220,16 +281,4 @@ void should_read_and_write_concurrently() throws InterruptedException {
fail("Pool termination timeout ...");
}
}

private void initSut(String content) {
String tmpConfDir = temporaryFolder.toFile().getAbsolutePath();
SCHEDULING_CAMPAIGN_FILE = Paths.get(tmpConfDir + "/scheduling/schedulingCampaigns.json");

if (content != null) {
FileUtils.initFolder(SCHEDULING_CAMPAIGN_FILE.getParent());
FileUtils.writeContent(SCHEDULING_CAMPAIGN_FILE, content);
}

sut = new SchedulingCampaignFileRepository(tmpConfDir);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class CampaignListComponent implements OnInit, OnDestroy {
this.campaigns.splice(this.getIndexFromId(id), 1);
this.campaigns = this.campaigns.slice();
this.applyFilters();
this.loadSchedulingCampaign();
});
}
}
Expand Down
Loading