Skip to content

Commit

Permalink
Merge pull request #62 from InseeFr/devSetLastExecutionDate
Browse files Browse the repository at this point in the history
Add service to set last date
  • Loading branch information
alicela authored May 14, 2024
2 parents 7ba43d7 + 25fba4d commit f813e94
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import fr.insee.genesis.domain.ports.api.ScheduleApiPort;
import fr.insee.genesis.exceptions.InvalidCronExpressionException;
import fr.insee.genesis.exceptions.NotFoundException;
import fr.insee.genesis.infrastructure.model.document.schedule.StoredSurveySchedule;
import fr.insee.genesis.infrastructure.model.document.schedule.ServiceToCall;
import fr.insee.genesis.infrastructure.model.document.schedule.StoredSurveySchedule;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -41,7 +41,7 @@ public ResponseEntity<Object> getAllSchedules() {

List<StoredSurveySchedule> storedSurveySchedules = scheduleApiPort.getAllSchedules();

log.info("Returning " + storedSurveySchedules.size() + " schedule documents...");
log.info("Returning {} schedule documents...", storedSurveySchedules.size());
return ResponseEntity.ok(storedSurveySchedules);
}

Expand All @@ -55,27 +55,28 @@ public ResponseEntity<Object> addSchedule(
@Parameter(description = "Schedule end date and time", example = "2024-01-01T12:00:00") @RequestParam("scheduleEndDate") LocalDateTime scheduleEndDate
){
try {
log.info("New schedule request for survey " + surveyName);
log.info("New schedule request for survey {}", surveyName);
scheduleApiPort.addSchedule(surveyName, serviceToCall, frequency, scheduleBeginDate, scheduleEndDate);
}catch (InvalidCronExpressionException e){
log.warn("Returned error for wrong frequency : " + frequency);
log.warn("Returned error for wrong frequency : {}", frequency);
return ResponseEntity.badRequest().body("Wrong frequency syntax");
}
log.info("New schedule created for survey " + surveyName);
log.info("New schedule created for survey {}", surveyName);
return ResponseEntity.ok().build();
}

@Operation(summary = "Update last execution date with now")
@PostMapping(path = "/updateLastExecutionDate")
public ResponseEntity<Object> updateSurveyLastExecution(
@Parameter(description = "Survey name to call Kraftwerk on") @RequestBody String surveyName
) {
@Operation(summary = "Set last execution date with new date or empty")
@PostMapping(path = "/setLastExecutionDate")
public ResponseEntity<Object> setSurveyLastExecution(
@Parameter(description = "Survey name to call Kraftwerk on") @RequestBody String surveyName,
@Parameter(description = "Date to save as last execution date", example = "2024-01-01T12:00:00") @RequestParam("newDate") LocalDateTime newDate
) {
try {
log.debug("Got update last execution on " + surveyName);
scheduleApiPort.updateLastExecutionName(surveyName);
log.info(surveyName + " last execution updated !");
log.debug("Got update last execution on {} with data param {}", surveyName, newDate);
scheduleApiPort.updateLastExecutionName(surveyName, newDate);
log.info("{} last execution updated at {} !", surveyName, newDate);
}catch (NotFoundException e){
log.warn("Survey " + surveyName + " not found !");
log.warn("Survey {} not found !", surveyName);
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void addSchedule(String surveyName,
LocalDateTime scheduleBeginDateString,
LocalDateTime scheduleEndDateString) throws InvalidCronExpressionException;

void updateLastExecutionName(String surveyName) throws NotFoundException;
void updateLastExecutionName(String surveyName, LocalDateTime newDate) throws NotFoundException;

long countSchedules();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import fr.insee.genesis.exceptions.InvalidCronExpressionException;
import fr.insee.genesis.exceptions.NotFoundException;
import fr.insee.genesis.infrastructure.model.document.schedule.KraftwerkExecutionSchedule;
import fr.insee.genesis.infrastructure.model.document.schedule.StoredSurveySchedule;
import fr.insee.genesis.infrastructure.model.document.schedule.ServiceToCall;
import fr.insee.genesis.infrastructure.model.document.schedule.StoredSurveySchedule;
import fr.insee.genesis.infrastructure.repository.ScheduleMongoDBRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -63,12 +63,12 @@ public void addSchedule(String surveyName, ServiceToCall serviceToCall, String f
}

@Override
public void updateLastExecutionName(String surveyName) throws NotFoundException {
public void updateLastExecutionName(String surveyName, LocalDateTime newDate) throws NotFoundException {
List<StoredSurveySchedule> storedSurveySchedules = scheduleMongoDBRepository.findBySurveyName(surveyName);

if (!storedSurveySchedules.isEmpty()) {
for(StoredSurveySchedule surveySchedule : storedSurveySchedules){
surveySchedule.setLastExecution(LocalDateTime.now());
surveySchedule.setLastExecution(newDate);
}
scheduleMongoDBRepository.saveAll(storedSurveySchedules);
}else{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package fr.insee.genesis.controller.rest;

import fr.insee.genesis.domain.service.ScheduleUnicityService;
import fr.insee.genesis.exceptions.InvalidCronExpressionException;
import fr.insee.genesis.infrastructure.model.document.schedule.KraftwerkExecutionSchedule;
import fr.insee.genesis.infrastructure.model.document.schedule.StoredSurveySchedule;
import fr.insee.genesis.infrastructure.model.document.schedule.ServiceToCall;
import fr.insee.genesis.infrastructure.model.document.schedule.StoredSurveySchedule;
import fr.insee.genesis.stubs.ScheduleApiPortStub;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -141,14 +140,37 @@ void addScheduleDedupTest() throws InvalidCronExpressionException {
@Test
void updateLastExecutionTest(){
//When
scheduleController.updateSurveyLastExecution("TESTSURVEY");
scheduleController.setSurveyLastExecution("TESTSURVEY", LocalDateTime.now());

//Then
List<StoredSurveySchedule> mongoStubFiltered = scheduleApiPortStub.mongoStub.stream().filter(scheduleDocument ->
scheduleDocument.getSurveyName().equals("TESTSURVEY")).toList();
Assertions.assertThat(mongoStubFiltered.getFirst().getLastExecution()).isNotNull();
}

@Test
void setLastExecutionTestToNull(){
//When
scheduleController.setSurveyLastExecution("TESTSURVEY", null);

//Then
List<StoredSurveySchedule> mongoStubFiltered = scheduleApiPortStub.mongoStub.stream().filter(scheduleDocument ->
scheduleDocument.getSurveyName().equals("TESTSURVEY")).toList();
Assertions.assertThat(mongoStubFiltered.getFirst().getLastExecution()).isNull();
}

@Test
void setLastExecutionTest(){
LocalDateTime date = LocalDateTime.now();
//When
scheduleController.setSurveyLastExecution("TESTSURVEY", date);

//Then
List<StoredSurveySchedule> mongoStubFiltered = scheduleApiPortStub.mongoStub.stream().filter(scheduleDocument ->
scheduleDocument.getSurveyName().equals("TESTSURVEY")).toList();
Assertions.assertThat(mongoStubFiltered.getFirst().getLastExecution()).isEqualTo(date);
}

@Test
void wrongFrequencyTest(){
//When+Then
Expand All @@ -165,7 +187,7 @@ void wrongFrequencyTest(){
@Test
void notFoundTest(){
//When+Then
ResponseEntity<Object> response = scheduleController.updateSurveyLastExecution("ERROR");
ResponseEntity<Object> response = scheduleController.setSurveyLastExecution("ERROR", LocalDateTime.now());
Assertions.assertThat(response.getStatusCode().is4xxClientError()).isTrue();
}
}
6 changes: 3 additions & 3 deletions src/test/java/fr/insee/genesis/stubs/ScheduleApiPortStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import fr.insee.genesis.exceptions.InvalidCronExpressionException;
import fr.insee.genesis.exceptions.NotFoundException;
import fr.insee.genesis.infrastructure.model.document.schedule.KraftwerkExecutionSchedule;
import fr.insee.genesis.infrastructure.model.document.schedule.StoredSurveySchedule;
import fr.insee.genesis.infrastructure.model.document.schedule.ServiceToCall;
import fr.insee.genesis.infrastructure.model.document.schedule.StoredSurveySchedule;
import org.springframework.scheduling.support.CronExpression;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -87,12 +87,12 @@ public void addSchedule(String surveyName, ServiceToCall serviceToCall, String f
}

@Override
public void updateLastExecutionName(String surveyName) throws NotFoundException {
public void updateLastExecutionName(String surveyName, LocalDateTime newDate) throws NotFoundException {
List<StoredSurveySchedule> mongoStubFiltered = mongoStub.stream().filter(scheduleDocument ->
scheduleDocument.getSurveyName().equals(surveyName)).toList();
if(!mongoStubFiltered.isEmpty()) {
StoredSurveySchedule storedSurveySchedule = mongoStubFiltered.getFirst();
storedSurveySchedule.setLastExecution(LocalDateTime.now());
storedSurveySchedule.setLastExecution(newDate);
}else throw new NotFoundException();
}

Expand Down

0 comments on commit f813e94

Please sign in to comment.