Skip to content

Commit

Permalink
review 2
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrouand committed Nov 15, 2024
1 parent f1420ea commit a7430e1
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@JsonIgnoreProperties(ignoreUnknown = true)
public class SchedulingCampaignDto {
Expand All @@ -26,9 +26,7 @@ public class SchedulingCampaignDto {
private String frequency;
private String environment;

private List<Long> campaignsId;
private List<String> campaignsTitle;
private List<String> datasetsId;
private List<CampaignExecutionRequestDto> campaignExecutionRequest = new ArrayList<>();

@JsonCreator
public SchedulingCampaignDto() {
Expand All @@ -38,17 +36,16 @@ public SchedulingCampaignDto(Long id,
LocalDateTime schedulingDate,
String frequency,
String environment,
List<Long> campaignsId,
List<String> campaignsTitle,
List<String> datasetsId
List<CampaignExecutionRequestDto> campaignExecutionRequest
) {
this.id = id;
this.schedulingDate = schedulingDate;
this.frequency = frequency;
this.environment = environment;
this.campaignsId = campaignsId;
this.campaignsTitle = campaignsTitle;
this.datasetsId = datasetsId;
this.campaignExecutionRequest = campaignExecutionRequest;
}

public record CampaignExecutionRequestDto(Long campaignId, String campaignTitle, String datasetId) {
}

public Long getId() {
Expand All @@ -67,39 +64,27 @@ public String getEnvironment() {
return environment;
}

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

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

public List<String> getDatasetsId() {
return datasetsId;
public List<CampaignExecutionRequestDto> getCampaignExecutionRequest() {
return campaignExecutionRequest;
}

public static SchedulingCampaignDto toDto(PeriodicScheduledCampaign sc) {
return new SchedulingCampaignDto(sc.id,
sc.nextExecutionDate,
sc.frequency.label,
sc.environment,
sc.campaignExecutionRequests.stream().map(CampaignExecutionRequest::campaignId).toList(),
sc.campaignExecutionRequests.stream().map(CampaignExecutionRequest::campaignTitle).toList(),
sc.campaignExecutionRequests.stream().map(CampaignExecutionRequest::datasetId).toList());
sc.campaignExecutionRequests.stream().map(cer -> new CampaignExecutionRequestDto(cer.campaignId(), cer.campaignTitle(), cer.datasetId())).toList()
);
}

public static PeriodicScheduledCampaign fromDto(SchedulingCampaignDto dto) {

List<CampaignExecutionRequest> campaignExecutionRequests = IntStream.range(0, dto.campaignsId.size())
.mapToObj(i -> new CampaignExecutionRequest(
dto.campaignsId.get(i),
dto.campaignsTitle.get(i),
guardArrayOfBoundException(dto.datasetsId, i))
)
.toList();

return new PeriodicScheduledCampaign(dto.id, dto.getSchedulingDate(), toFrequency(dto.getFrequency()), dto.getEnvironment(), campaignExecutionRequests);
return new PeriodicScheduledCampaign(
dto.id,
dto.getSchedulingDate(),
toFrequency(dto.getFrequency()),
dto.getEnvironment(),
dto.campaignExecutionRequest.stream().map(cer -> new CampaignExecutionRequest(cer.campaignId(), cer.campaignTitle(), cer.datasetId())).toList()
);
}

private static String guardArrayOfBoundException(List<String> list, int i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.chutneytesting.campaign.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand All @@ -30,6 +31,7 @@
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
Expand Down Expand Up @@ -69,9 +71,9 @@ void should_get_all_scheduled_campaigns() throws Exception {
.andExpect(jsonPath("$[0].id").value(1L))
.andExpect(jsonPath("$[0].frequency").value("Daily"))
.andExpect(jsonPath("$[0].environment").value("PROD"))
.andExpect(jsonPath("$[0].campaignsId[0]").value(1L))
.andExpect(jsonPath("$[0].campaignsTitle[0]").value("title"))
.andExpect(jsonPath("$[0].datasetsId[0]").value("datasetId"))
.andExpect(jsonPath("$[0].campaignExecutionRequest[0].campaignId").value(1L))
.andExpect(jsonPath("$[0].campaignExecutionRequest[0].campaignTitle").value("title"))
.andExpect(jsonPath("$[0].campaignExecutionRequest[0].datasetId").value("datasetId"))
.andExpect(jsonPath("$[0].schedulingDate[0]").value(2024))
.andExpect(jsonPath("$[0].schedulingDate[1]").value(10))
.andExpect(jsonPath("$[0].schedulingDate[2]").value(12))
Expand All @@ -90,11 +92,40 @@ void should_add_scheduled_campaign() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{"id":1,"schedulingDate":[2024,10,12,14,30,45],"frequency":"Daily","environment":"PROD","campaignsId":[1],"campaignsTitle":["title"],"datasetsId":["datasetId"]}
{
"id":1,
"schedulingDate":[2024,10,12,14,30,45],
"frequency":"Daily",
"environment":"PROD",
"campaignExecutionRequest":[
{"campaignId":1,"campaignTitle":"title","datasetId":"datasetId"}
]
}
"""))
.andExpect(status().isOk());

verify(scheduledCampaignRepository).add(any(PeriodicScheduledCampaign.class));
ArgumentCaptor<PeriodicScheduledCampaign> captor = ArgumentCaptor.forClass(PeriodicScheduledCampaign.class);


verify(scheduledCampaignRepository).add(captor.capture());

PeriodicScheduledCampaign capturedCampaign = captor.getValue();

assertThat(capturedCampaign)
.isNotNull()
.extracting("id", "nextExecutionDate", "frequency.label", "environment")
.containsExactly(
1L,
LocalDateTime.of(2024, 10, 12, 14, 30, 45),
"Daily",
"PROD"
);

assertThat(capturedCampaign.campaignExecutionRequests)
.hasSize(1)
.first()
.extracting("campaignId", "campaignTitle", "datasetId")
.containsExactly(1L, "title", "datasetId");
}

@Test
Expand Down
24 changes: 12 additions & 12 deletions chutney/ui/src/app/core/model/campaign/campaign-scheduling.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

import { FREQUENCY } from '@core/model/campaign/FREQUENCY';

export interface CampaignExecutionRequest {
campaignId: number;
campaignTitle: string;
datasetId: string;
}

export class CampaignScheduling {

constructor(
public campaignsId: number[],
public campaignsTitle: string[],
public schedulingDate: Date,
public frequency?: FREQUENCY,
public id?: number,
public environment?: string,
public datasetsId?: string[]
) {
}
export interface CampaignScheduling {
id?: number;
schedulingDate: Date;
frequency: FREQUENCY;
environment: string;
campaignExecutionRequest: CampaignExecutionRequest[];
}

Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class CampaignListComponent implements OnInit, OnDestroy {
}

scheduledCampaignHasMissingEnv(scheduledCampaign: CampaignScheduling): boolean {
return scheduledCampaign.campaignsId.some(campaignId => {
return scheduledCampaign.campaignExecutionRequest.map(cer => cer.campaignId).some(campaignId => {
const campaign = this.campaigns.find(campaign => campaign.id === campaignId);
return campaign == null || campaign.environment == null;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ <h4>{{ 'campaigns.scheduling.title' | translate }}</h4>
<td>#{{ scheduledCampaign.id }}</td>
<td>{{ scheduledCampaign.schedulingDate | amLocal | amDateFormat: 'YYYY-MM-DD HH:mm' }}</td>
<td>
@for (title of scheduledCampaign.campaignsTitle; track title; let i = $index) {
@for (campaignExecutionRequest of scheduledCampaign.campaignExecutionRequest; track campaignExecutionRequest; let i = $index) {
<div>
<span>{{ (i + 1) }} - {{ title }}
@if (scheduledCampaign.datasetsId[i]) {
({{scheduledCampaign.datasetsId[i]}})
<span>{{ (i + 1) }} - {{ campaignExecutionRequest.campaignTitle }}
@if (campaignExecutionRequest.datasetId) {
({{campaignExecutionRequest.datasetId}})
}
</span>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { Component, Inject, OnInit } from '@angular/core';
import { Campaign, Dataset, Environment } from '@core/model';
import { CampaignService, DataSetService, EnvironmentService } from '@core/services';
import { CampaignScheduling } from '@core/model/campaign/campaign-scheduling.model';
import { CampaignExecutionRequest, CampaignScheduling } from '@core/model/campaign/campaign-scheduling.model';
import { CampaignSchedulingService } from '@core/services/campaign-scheduling.service';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NgbDatepickerConfig, NgbDateStruct, NgbTimepickerConfig } from '@ng-bootstrap/ng-bootstrap';
Expand Down Expand Up @@ -111,15 +111,22 @@ export class CampaignSchedulingComponent implements OnInit {
const frequency: FREQUENCY = formValue['frequency'];
const environment: string = formValue['environment'];

const schedulingCampaign = new CampaignScheduling(
campaignList.map(campaign => campaign.id),
campaignList.map(campaign => campaign.title),
dateTime,
frequency,
null,
environment,
this.datasetsSelected.map(elt => elt.dataset?.id.toString())
);
const campaignExecutionRequests: CampaignExecutionRequest[] = campaignList.map((campaign, index) => {
const dataset = this.datasetsSelected[index]; // Assurez-vous que les listes ont la même longueur et que l'index est valide

return {
campaignId: campaign.id,
campaignTitle: campaign.title,
datasetId: dataset.dataset?.id.toString() || "" // On utilise une valeur par défaut (""), au cas où `dataset.dataset?.id` soit `undefined`
};
});

const schedulingCampaign: CampaignScheduling = {
schedulingDate: dateTime,
frequency: frequency,
environment: environment,
campaignExecutionRequest: campaignExecutionRequests
};

this.campaignSchedulingService.create(schedulingCampaign).subscribe({
next: () => {
Expand Down Expand Up @@ -148,7 +155,6 @@ export class CampaignSchedulingComponent implements OnInit {

selectCampaign(campaign: Campaign) {
this.datasetsSelected.push({campaign: campaign, dataset: this.EMPTY_DATASET});
this.refreshForPipe();
}

unselectCampaign(campaign: Campaign) {
Expand All @@ -157,7 +163,6 @@ export class CampaignSchedulingComponent implements OnInit {

const index = this.datasetsSelected.findIndex(elt => elt.campaign.id === campaign.id)
this.datasetsSelected.splice(index, 1);
this.refreshForPipe();
}

private loadSchedulingCampaign() {
Expand All @@ -174,18 +179,11 @@ export class CampaignSchedulingComponent implements OnInit {
selectDataset(toAdd: ListItem, campaign: Campaign) {
const foundelt = this.datasetsSelected.find(elt => elt.campaign.id === campaign.id)
foundelt.dataset = toAdd;
this.refreshForPipe();
}

unselectDataset(toRemove: Campaign) {
const foundelt = this.datasetsSelected.find(elt => elt.campaign.id === toRemove.id)
foundelt.dataset = this.EMPTY_DATASET;
this.refreshForPipe();
}

private refreshForPipe() {
// force instance to change for pipe refresh
this.datasetsSelected = Object.assign([], this.datasetsSelected);
}

// convenience getter for easy access to form fields
Expand Down

0 comments on commit a7430e1

Please sign in to comment.