Skip to content

Commit

Permalink
Merge pull request #53 from InseeFr/devProcessAllSurveysEndpoint
Browse files Browse the repository at this point in the history
 Add all campaigns files treatment
  • Loading branch information
alexisszmundy authored Apr 25, 2024
2 parents dc882c5 + c34e2c9 commit 4562463
Show file tree
Hide file tree
Showing 27 changed files with 478 additions and 229 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ src/bin/
hs_err_pid*

# test generated files
src/test/resources/OUT/*
src/test/resources/OUT/*
/src/test/resources/DONE/*
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.2.2 - [NOT-YET-RELEASED]
### Added
- Treat all campaigns endpoint
### Fixed
- Fixed health check always returning version n.a

## 1.2.1 - [2024-04-24]
First production version

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,57 @@
package fr.insee.genesis.controller.rest;

import fr.insee.genesis.domain.ports.api.ScheduleApiPort;
import fr.insee.genesis.domain.ports.api.SurveyUnitUpdateApiPort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.info.BuildProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RequestMapping("/health-check")
@RestController
public class HealthCheckController {
private final SurveyUnitUpdateApiPort surveyUnitUpdateApiPort;
private final ScheduleApiPort scheduleApiPort;
@Value("${fr.insee.genesis.version}")
private String projectVersion;

@Autowired
public HealthCheckController(SurveyUnitUpdateApiPort surveyUnitUpdateApiPort, ScheduleApiPort scheduleApiPort) {
this.surveyUnitUpdateApiPort = surveyUnitUpdateApiPort;
this.scheduleApiPort = scheduleApiPort;
}

@Autowired(required = false)
private Optional<BuildProperties> buildProperties;
@GetMapping("")
public ResponseEntity<String> healthcheck() {
return ResponseEntity.ok(
"""
OK
Version %s
User %s
"""
.formatted(
projectVersion,
SecurityContextHolder.getContext().getAuthentication().getName()
));
}

@GetMapping("")
public ResponseEntity<String> healthcheck(){
return ResponseEntity.ok(
"""
OK
Version %s
User %s
"""
@GetMapping("mongoDb")
public ResponseEntity<String> healthcheckMongo() {
return ResponseEntity.ok(
"""
MongoDB OK
%s Responses
%s Schedules
"""
.formatted(
buildProperties.map(BuildProperties::getVersion).orElse("n.a"),
SecurityContextHolder.getContext().getAuthentication().getName()
) );
}
surveyUnitUpdateApiPort.countResponses(),
scheduleApiPort.countSchedules()
));
}

}
319 changes: 171 additions & 148 deletions src/main/java/fr/insee/genesis/controller/rest/ResponseController.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -45,8 +44,7 @@ public ResponseEntity<Object> getAllSchedules() {
List<StoredSurveySchedule> storedSurveySchedules = scheduleApiPort.getAllSchedules();

log.info("Returning " + storedSurveySchedules.size() + " schedule documents...");

return new ResponseEntity<>(storedSurveySchedules, HttpStatus.OK);
return ResponseEntity.ok(storedSurveySchedules);
}

@Operation(summary = "Schedule a Kraftwerk execution")
Expand All @@ -63,10 +61,10 @@ public ResponseEntity<Object> addSchedule(
scheduleApiPort.addSchedule(surveyName, serviceToCall, frequency, scheduleBeginDate, scheduleEndDate);
}catch (InvalidCronExpressionException e){
log.warn("Returned error for wrong frequency : " + frequency);
return new ResponseEntity<>("Wrong frequency syntax",HttpStatus.BAD_REQUEST);
return ResponseEntity.badRequest().body("Wrong frequency syntax");
}
log.info("New schedule created for survey " + surveyName);
return new ResponseEntity<>(HttpStatus.OK);
return ResponseEntity.ok().build();
}

@Operation(summary = "Update last execution date (for Bangles only)", hidden = true)
Expand All @@ -80,8 +78,8 @@ public ResponseEntity<Object> updateSurveyLastExecution(
log.info(surveyName + " last execution updated !");
}catch (NotFoundException e){
log.warn("Survey " + surveyName + " not found !");
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
return ResponseEntity.notFound().build();
}
return new ResponseEntity<>(HttpStatus.OK);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import fr.insee.genesis.controller.utils.XMLSplitter;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PutMapping;
Expand All @@ -24,6 +23,6 @@ public ResponseEntity<Object> saveResponsesFromXmlFile(@RequestParam("inputFolde
throws Exception {
log.info("Split XML file : " + filename + " into " + nbSU + " SU by file");
XMLSplitter.split(inputFolder, filename, outputFolder, "SurveyUnit", nbSU);
return new ResponseEntity<>("Test", HttpStatus.OK);
return ResponseEntity.ok("Test");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ void addSchedule(String surveyName,
LocalDateTime scheduleEndDateString) throws InvalidCronExpressionException;

void updateLastExecutionName(String surveyName) throws NotFoundException;

long countSchedules();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public interface SurveyUnitUpdateApiPort {
List<Mode> findModesByIdQuestionnaire(String idQuestionnaire);

Long deleteByIdQuestionnaire(String idQuestionnaire);

long countResponses();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public interface SurveyUnitUpdatePersistencePort {

Long deleteByIdQuestionnaire(String idQuestionnaire);


long count();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,12 @@ public void updateLastExecutionName(String surveyName) throws NotFoundException
throw new NotFoundException();
}



}

@Override
public long countSchedules() {
return scheduleMongoDBRepository.count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public Long deleteByIdQuestionnaire(String idQuestionnaire) {
return surveyUnitUpdatePersistencePort.deleteByIdQuestionnaire(idQuestionnaire);
}

@Override
public long countResponses() {
return surveyUnitUpdatePersistencePort.count();
}

private static List<Mode> getDistinctsModes(List<SurveyUnitUpdateDto> surveyUnits) {
List<Mode> sources = new ArrayList<>();
surveyUnits.forEach(surveyUnitDto -> sources.add(surveyUnitDto.getMode()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public Long deleteByIdQuestionnaire(String idQuestionnaire) {
return mongoRepository.deleteByIdQuestionnaire(idQuestionnaire);
}

@Override
public long count() {
return mongoRepository.count();
}

public List<SurveyUnitDto> findIdUEsByIdQuestionnaire(String idQuestionnaire) {
List<SurveyUnitDocument> surveyUnits = mongoRepository.findIdUEsByIdQuestionnaire(idQuestionnaire);
return surveyUnits.isEmpty() ? Collections.emptyList() : SurveyUnitDocumentMapper.INSTANCE.listDocumentToListDto(surveyUnits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface ScheduleMongoDBRepository extends MongoRepository<StoredSurveyS

@Query(value = "{ 'surveyName' : ?0 }", delete = true)
void deleteBySurveyName(String surveyName);

long count();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ public interface SurveyUnitUpdateMongoDBRepository extends MongoRepository<Surve
@Meta(cursorBatchSize = 20)
Stream<SurveyUnitUpdateDocument> findByIdQuestionnaire(String idQuestionnaire);

long count();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
Expand Down Expand Up @@ -139,7 +140,15 @@ public String getDataFolder(String campaign, String dataSource) {

/**
* Get the path of the folder where the specifications files are stored
* @param campaign
* @return Path of the specifications folder
*/
public String getSpecFolder() {
return String.format("%s/%s", specFolderSource, "specs");
}

/**
* Get the path of the folder where the specifications files are stored for specific campaign
* @param campaign name of campaign
* @return Path of the specifications folder
*/
public String getSpecFolder(String campaign) {
Expand Down Expand Up @@ -206,4 +215,18 @@ public void writeSuUpdatesInFile(Path filePath, Stream<SurveyUnitUpdateDto> resp
throw e.getCause();
}
}

/**
* List all folders in the specs folder
* @return List of specs folders
*/
public List<File> listAllSpecsFolders() {
File[] objs = new File(getSpecFolder()).listFiles();
if (objs == null) {
return List.of();
}
return Arrays.stream(objs)
.filter(File::isDirectory)
.toList();
}
}
2 changes: 1 addition & 1 deletion src/test/java/cucumber/TestConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class TestConstants {
//Functional tests
public static final String FUNCTIONAL_TESTS_INPUT_DIRECTORY = TEST_RESOURCES_DIRECTORY + "/IN";
public static final String FUNCTIONAL_TESTS_WEB_DIRECTORY = FUNCTIONAL_TESTS_INPUT_DIRECTORY + "/WEB";
public static final String FUNCTIONAL_TESTS_DDI_DIRECTORY = FUNCTIONAL_TESTS_INPUT_DIRECTORY + "/specs";
public static final String FUNCTIONAL_TESTS_DDI_DIRECTORY = TEST_RESOURCES_DIRECTORY + "/specs";
public static final String FUNCTIONAL_TESTS_TEMP_DIRECTORY = TEST_RESOURCES_DIRECTORY + "/functional_tests/temp";
public static final String FUNCTIONAL_TESTS_OUTPUT_DIRECTORY = TEST_RESOURCES_DIRECTORY + "/functional_tests/out";
public static final String FUNCTIONAL_TESTS_API_URL = "http://localhost:8080";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package fr.insee.genesis.controller.rest;

import fr.insee.genesis.domain.dtos.CollectedVariableDto;
import fr.insee.genesis.domain.dtos.DataState;
import fr.insee.genesis.domain.dtos.Mode;
import fr.insee.genesis.domain.dtos.SurveyUnitUpdateDto;
import fr.insee.genesis.domain.dtos.VariableDto;
import fr.insee.genesis.domain.ports.api.SurveyUnitUpdateApiPort;
import fr.insee.genesis.domain.service.SurveyUnitUpdateImpl;
import fr.insee.genesis.stubs.ScheduleApiPortStub;
import fr.insee.genesis.stubs.SurveyUnitUpdatePersistencePortStub;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.http.ResponseEntity;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
class HealthCheckControllerTest {
static HealthCheckController healthCheckController;

static SurveyUnitUpdatePersistencePortStub surveyUnitUpdatePersistencePortStub;
static ScheduleApiPortStub scheduleApiPortStub;

@BeforeAll
static void init() {
surveyUnitUpdatePersistencePortStub = new SurveyUnitUpdatePersistencePortStub();
SurveyUnitUpdateApiPort surveyUnitUpdateApiPort = new SurveyUnitUpdateImpl(surveyUnitUpdatePersistencePortStub);
List<VariableDto> externalVariableDtoList = new ArrayList<>();
VariableDto variableDto = VariableDto.builder().idVar("TESTIDVAR").values(List.of(new String[]{"V1", "V2"})).build();
externalVariableDtoList.add(variableDto);

List<CollectedVariableDto> collectedVariableDtoList = new ArrayList<>();
CollectedVariableDto collectedVariableDto = new CollectedVariableDto("TESTIDVAR", List.of(new String[]{"V1", "V2"}), "TESTIDLOOP", "TESTIDPARENT");
collectedVariableDtoList.add(collectedVariableDto);
surveyUnitUpdatePersistencePortStub.getMongoStub().add(SurveyUnitUpdateDto.builder()
.idCampaign("TESTIDCAMPAIGN")
.mode(Mode.WEB)
.idUE("TESTIDUE")
.idQuest("TESTIDQUESTIONNAIRE")
.state(DataState.COLLECTED)
.fileDate(LocalDateTime.of(2023, 1, 1, 0, 0, 0))
.recordDate(LocalDateTime.of(2024, 1, 1, 0, 0, 0))
.externalVariables(externalVariableDtoList)
.collectedVariables(collectedVariableDtoList)
.build());


scheduleApiPortStub = new ScheduleApiPortStub();

healthCheckController = new HealthCheckController(
surveyUnitUpdateApiPort,
scheduleApiPortStub
);
}

@Test
void mongoCountTest() {
ResponseEntity<String> response = healthCheckController.healthcheckMongo();
log.info(response.getBody());
Assertions.assertThat(response.getStatusCode().is2xxSuccessful()).isTrue();
}
}
Loading

0 comments on commit 4562463

Please sign in to comment.