Skip to content

Commit

Permalink
Merge pull request #73 from abes-esr/develop
Browse files Browse the repository at this point in the history
develop to main
  • Loading branch information
pierre-maraval authored Jan 10, 2024
2 parents 11292c8 + ea848f0 commit 2d18d72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
17 changes: 10 additions & 7 deletions src/main/java/fr/abes/kbart2kafka/utils/CheckFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
@Slf4j
public class CheckFiles {

public static void detectFileName(File file) throws IllegalFileFormatException {
public static Boolean detectFileName(File file) throws IllegalFileFormatException {
String filename = file.getName();
if (!filename.matches("([a-zA-Z0-9\\-]+_){3}(\\d{4}-\\d{2}-\\d{2})+(_FORCE)?+(.tsv)$")) {
if (!filename.matches("([a-zA-Z0-9\\-]+_){3}(\\d{4}-\\d{2}-\\d{2})+(_FORCE|_BYPASS)?+(.tsv)$")) {
log.error("Message envoyé : {}", "Le nom du fichier n'est pas correct");
throw new IllegalFileFormatException("Le nom du fichier "+ filename +" n'est pas correct");
}
} else return filename.matches("([a-zA-Z0-9\\-]+_){3}(\\d{4}-\\d{2}-\\d{2})+(_BYPASS)+(.tsv)$");
}

public static void detectProvider(File file) throws IllegalProviderException {
Expand Down Expand Up @@ -73,10 +73,13 @@ public static void detectTabulations(File file) throws IOException, IllegalFileF
* @param file fichier en entrée
* @throws IOException impossible de lire le fichier
*/
public static void detectHeaderPresence(String header, File file) throws IOException, IllegalFileFormatException {
public static void detectHeaderPresence(String header, File file, Boolean isBypassOptionPresent) throws IOException, IllegalFileFormatException {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line = reader.readLine();
if(!line.contains(header)) {
if(isBypassOptionPresent && line.contains("best_ppn")) {
log.error("Message envoyé : {}", "L'en tete du fichier est incorrecte.");
throw new IllegalFileFormatException("L'en tete du fichier est incorrecte. L'option _BYPASS n'est pas compatible avec la présence d'une colonne best_pnn.");
} else if(!line.contains(header)) {
log.error("Message envoyé : {}", "L'en tete du fichier est incorrecte.");
throw new IllegalFileFormatException("L'en tete du fichier est incorrecte.");
}
Expand All @@ -92,10 +95,10 @@ public static void detectHeaderPresence(String header, File file) throws IOExcep
* @throws IOException Impossible de lire le fichier
*/
public static void verifyFile(File file, String header) throws IllegalFileFormatException, IOException, IllegalProviderException {
detectFileName(file);
Boolean isBypassOptionPresent = detectFileName(file);
detectProvider(file);
isFileWithTSVExtension(file);
detectTabulations(file);
detectHeaderPresence(header, file);
detectHeaderPresence(header, file, isBypassOptionPresent);
}
}
17 changes: 15 additions & 2 deletions src/test/java/fr/abes/kbart2kafka/utils/CheckFilesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,24 @@ void detectTabulations() throws IOException, IllegalFileFormatException {
void detectOfHeaderPresence() throws IOException, IllegalFileFormatException {
this.file = new File("test.tsv");
FileUtils.writeStringToFile(file, "test\ttest\ttest", StandardCharsets.UTF_8, true);
CheckFiles.detectHeaderPresence("test", file);
CheckFiles.detectHeaderPresence("test", file, false);

this.file2 = new File("test2.tsv");
FileUtils.writeStringToFile(file2, "toto\ttata\ttiti", StandardCharsets.UTF_8, true);
IllegalFileFormatException erreur = Assertions.assertThrows(IllegalFileFormatException.class, () -> CheckFiles.detectHeaderPresence("test", file2));
IllegalFileFormatException erreur = Assertions.assertThrows(IllegalFileFormatException.class, () -> CheckFiles.detectHeaderPresence("test", file2, false));
Assertions.assertEquals("L'en tete du fichier est incorrecte.", erreur.getMessage());
}

@Test
void detectOptionError() throws IOException {
this.file = new File("test3_BYPASS.tsv");
FileUtils.writeStringToFile(file, "test\ttest\ttest\tbest_ppn", StandardCharsets.UTF_8, true);
IllegalFileFormatException erreur1 = Assertions.assertThrows(IllegalFileFormatException.class, () -> CheckFiles.detectHeaderPresence("test\ttest\ttest\tbest_ppn", file, true));
Assertions.assertEquals("L'en tete du fichier est incorrecte. L'option _BYPASS n'est pas compatible avec la présence d'une colonne best_pnn.", erreur1.getMessage());

this.file2 = new File("test3_BYPASS_FORCE.tsv");
FileUtils.writeStringToFile(file2, "test\ttest\ttest", StandardCharsets.UTF_8, true);
IllegalFileFormatException erreur2 = Assertions.assertThrows(IllegalFileFormatException.class, () -> CheckFiles.detectFileName(file2));
Assertions.assertEquals("Le nom du fichier test3_BYPASS_FORCE.tsv n'est pas correct", erreur2.getMessage());
}
}

0 comments on commit 2d18d72

Please sign in to comment.