Skip to content

Commit

Permalink
Improve for sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
alicela committed Aug 22, 2024
1 parent 3321bf3 commit 018a245
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 181 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package fr.insee.genesis.configuration;

import java.util.Arrays;
import java.util.Objects;
import java.util.Set;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;

@Slf4j
public class PropertiesLogger implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
Expand All @@ -30,7 +29,7 @@ public void onApplicationEvent(@NonNull ApplicationEnvironmentPreparedEvent even
.map(ps -> ((EnumerablePropertySource<?>) ps).getPropertyNames()).flatMap(Arrays::stream).distinct()
.filter(Objects::nonNull)
.filter(ps -> ps.startsWith("fr.insee") || ps.startsWith("spring")).forEach(key -> log
.info(key + " = " + hideProperties(key, environment)));
.info("{} = {}",key,hideProperties(key, environment)));
log.info("===============================================================================================");

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

import fr.insee.bpm.exceptions.MetadataParserException;
import fr.insee.bpm.metadata.model.VariablesMap;
import fr.insee.bpm.metadata.reader.ddi.DDIReader;
import fr.insee.bpm.metadata.reader.lunatic.LunaticReader;
import fr.insee.genesis.Constants;
import fr.insee.genesis.controller.adapter.LunaticXmlAdapter;
import fr.insee.genesis.controller.responses.SurveyUnitUpdateSimplified;
Expand All @@ -23,15 +27,8 @@
import fr.insee.genesis.exceptions.GenesisException;
import fr.insee.genesis.exceptions.NoDataError;
import fr.insee.genesis.infrastructure.utils.FileUtils;

import fr.insee.bpm.exceptions.MetadataParserException;
import fr.insee.bpm.metadata.model.VariablesMap;
import fr.insee.bpm.metadata.reader.ddi.DDIReader;
import fr.insee.bpm.metadata.reader.lunatic.LunaticReader;

import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -65,6 +62,8 @@
@Slf4j
public class ResponseController {

private static final String DDI_REGEX = "ddi[\\w,\\s-]+\\.xml";
public static final String S_S = "%s/%s";
private final SurveyUnitUpdateApiPort surveyUnitService;
private final SurveyUnitQualityService surveyUnitQualityService;
private final VolumetryLogService volumetryLogService;
Expand Down Expand Up @@ -349,8 +348,7 @@ public ResponseEntity<List<CampaignWithQuestionnaire>> getCampaignsWithQuestionn
private void treatCampaignWithMode(String campaignName, Mode currentMode, List<GenesisError> errors, String rootDataFolder) throws GenesisException,
SAXException, XMLStreamException {
try {
fileUtils.findFile(String.format("%s/%s", fileUtils.getSpecFolder(campaignName),currentMode), "ddi[\\w," +
"\\s-]+\\.xml");
fileUtils.findFile(String.format(S_S, fileUtils.getSpecFolder(campaignName),currentMode), DDI_REGEX);
//DDI if DDI file found
treatCampaignWithMode(campaignName, currentMode, errors, rootDataFolder, true);
}catch (RuntimeException re){
Expand Down Expand Up @@ -396,7 +394,7 @@ private void treatCampaignWithMode(String campaignName, Mode mode, List<GenesisE

//For each XML data file
for (String fileName : dataFiles.stream().filter(s -> s.endsWith(".xml")).toList()) {
String filepathString = String.format("%s/%s", dataFolder, fileName);
String filepathString = String.format(S_S, dataFolder, fileName);
Path filepath = Paths.get(filepathString);
//Check if file not in done folder, delete if true
if(isDataFileInDoneFolder(filepath, campaignName, mode.getFolder())){
Expand Down Expand Up @@ -426,8 +424,8 @@ private VariablesMap readMetadatas(String campaignName, Mode mode, List<GenesisE
if(withDDI){
//Read DDI
try {
Path ddiFilePath = fileUtils.findFile(String.format("%s/%s", fileUtils.getSpecFolder(campaignName),
mode.getModeName()), "ddi[\\w," + "\\s-]+\\.xml");
Path ddiFilePath = fileUtils.findFile(String.format(S_S, fileUtils.getSpecFolder(campaignName),
mode.getModeName()), DDI_REGEX);
variablesMap = DDIReader.getMetadataFromDDI(ddiFilePath.toUri().toURL().toString(),
new FileInputStream(ddiFilePath.toString())).getVariables();
} catch (Exception e) {
Expand All @@ -438,7 +436,7 @@ private VariablesMap readMetadatas(String campaignName, Mode mode, List<GenesisE
}else{
//Read Lunatic
try {
Path lunaticFilePath = fileUtils.findFile(String.format("%s/%s", fileUtils.getSpecFolder(campaignName),
Path lunaticFilePath = fileUtils.findFile(String.format(S_S, fileUtils.getSpecFolder(campaignName),
mode.getModeName()), "lunatic[\\w," + "\\s-]+\\.json");
variablesMap = LunaticReader.getMetadataFromLunatic(new FileInputStream(lunaticFilePath.toString())).getVariables();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.List;
Expand Down Expand Up @@ -70,7 +76,7 @@ public ResponseEntity<Object> deleteSchedule(
log.info("Delete schedule request for survey {}", surveyName);
scheduleApiPort.deleteSchedule(surveyName);
}catch (NotFoundException e){
log.warn("Survey {} not found !", surveyName);
log.warn("Survey {} not found for deletion !", surveyName);
return ResponseEntity.notFound().build();
}
log.info("Schedule deleted for survey {}", surveyName);
Expand All @@ -89,7 +95,7 @@ public ResponseEntity<Object> setSurveyLastExecution(
scheduleApiPort.updateLastExecutionName(surveyName, newDate);
log.info("{} last execution updated at {} !", surveyName, newDate);
}catch (NotFoundException e){
log.warn("Survey {} not found !", surveyName);
log.warn("Survey {} not found for setting last execution !", surveyName);
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ResponseEntity<Object> saveResponsesFromXmlFile(@RequestParam("inputFolde
@RequestParam("filename") String filename,
@RequestParam("nbResponsesByFile") int nbSU)
throws Exception {
log.info("Split XML file : " + filename + " into " + nbSU + " SU by file");
log.info("Split XML file : {} into {} SU by file", filename, nbSU);
XMLSplitter.split(inputFolder, filename, outputFolder, "SurveyUnit", nbSU);
return ResponseEntity.ok("Test");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private Document readXmlFile(Path filePath) throws IOException, SAXException, Ge
File file = filePath.toFile();
//Extraction of the file last modified date
BasicFileAttributes attr = Files.readAttributes(filePath, BasicFileAttributes.class);
log.info("lastModifiedTime: " + attr.lastModifiedTime());
log.info("lastModifiedTime: {}", attr.lastModifiedTime());

//Parse xml
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static SurveyUnitUpdateDto createForcedDto(
List<CollectedVariableDto> correctedCollectedVariables,
List<VariableDto> correctedExternalVariables
) {
SurveyUnitUpdateDto sampleSuDto = suDtosList.stream().filter(element -> element.getIdUE().equals(idUE)).toList().get(0);
SurveyUnitUpdateDto sampleSuDto = suDtosList.stream().filter(element -> element.getIdUE().equals(idUE)).toList().getFirst();
SurveyUnitUpdateDto newForcedSuDto = SurveyUnitUpdateDto.builder()
.idQuest(sampleSuDto.getIdQuest())
.idCampaign(sampleSuDto.getIdCampaign())
Expand Down Expand Up @@ -248,7 +248,7 @@ private static boolean isParseError(String value, VariableType type){
Matcher matcher = pattern.matcher(value);
if(!matcher.find()){
// We only monitor parsing date errors, so we always return false
log.debug("Can't parse date " + value);
log.debug("Can't parse date {}", value);
return false;
}
break;
Expand Down

This file was deleted.

13 changes: 4 additions & 9 deletions src/main/java/fr/insee/genesis/domain/dtos/SurveyUnitId.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package fr.insee.genesis.domain.dtos;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

@Setter
@Getter
@AllArgsConstructor
public class SurveyUnitId {

public SurveyUnitId(String idUE) {
this.idUE = idUE;
}

public SurveyUnitId() {
}

@Getter
@Setter
@JsonProperty("idUE")
private String idUE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void addSchedule(String surveyName, ServiceToCall serviceToCall, String f
}

@Override
public void deleteSchedule(String surveyName) throws NotFoundException {
public void deleteSchedule(String surveyName) {
scheduleMongoDBRepository.deleteBySurveyName(surveyName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public StoredSurveySchedule deduplicateSurveySchedules(String surveyName, List<S
return storedSurveySchedules.getFirst();
}

log.info(storedSurveySchedules.size() + " survey descriptions found for " + surveyName + ", deduplicating...");
log.info("{} survey descriptions found for {}, deduplicating...",storedSurveySchedules.size(), surveyName);

StoredSurveySchedule deduplicatedSurveySchedule = new StoredSurveySchedule(
surveyName,
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/fr/insee/genesis/exceptions/GenesisError.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import lombok.Getter;

@Getter
public class GenesisError {

@Getter
private String message;
private final String message;

public GenesisError(String message) {
this.message = message;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/fr/insee/genesis/exceptions/NoDataError.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import fr.insee.genesis.domain.dtos.Mode;
import lombok.Getter;

@Getter
public class NoDataError extends GenesisError {

@Getter
private Mode mode;
private final Mode mode;

public NoDataError(String message, Mode mode) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void writeFile(Path filePath, String fileContent) {
myFile = path.toFile();
fileCreated = myFile.createNewFile();
} catch (IOException e) {
log.error("Permission refused to create folder: " + path.getParent(), e);
log.error(String.format("Permission refused to create folder: %s", path.getParent()), e);
}
if (!fileCreated){return ;}
try (FileWriter myWriter = new FileWriter(myFile)) {
Expand Down
14 changes: 9 additions & 5 deletions src/test/java/cucumber/functional_tests/MainDefinitions.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package cucumber.functional_tests;

import cucumber.TestConstants;
import fr.insee.bpm.exceptions.MetadataParserException;
import fr.insee.bpm.metadata.model.VariablesMap;
import fr.insee.bpm.metadata.reader.ddi.DDIReader;
import fr.insee.genesis.controller.adapter.LunaticXmlAdapter;
import fr.insee.genesis.controller.service.SurveyUnitQualityService;
import fr.insee.genesis.controller.sources.xml.LunaticXmlCampaign;
import fr.insee.genesis.controller.sources.xml.LunaticXmlDataParser;
import fr.insee.genesis.controller.sources.xml.LunaticXmlSurveyUnit;
import fr.insee.genesis.domain.dtos.*;
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.exceptions.GenesisException;
import fr.insee.bpm.exceptions.MetadataParserException;
import fr.insee.bpm.metadata.model.VariablesMap;
import fr.insee.bpm.metadata.reader.ddi.DDIReader;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
Expand All @@ -29,7 +33,7 @@
public class MainDefinitions {
String directory;
Path inDirectory = Paths.get(TestConstants.FUNCTIONAL_TESTS_WEB_DIRECTORY);
Path ddiDirectory = Paths.get(TestConstants.FUNCTIONAL_TESTS_DDI_DIRECTORY);
final Path ddiDirectory = Paths.get(TestConstants.FUNCTIONAL_TESTS_DDI_DIRECTORY);

SurveyUnitQualityService surveyUnitQualityService = new SurveyUnitQualityService();

Expand Down
Loading

0 comments on commit 018a245

Please sign in to comment.