diff --git a/CHANGELOG.md b/CHANGELOG.md index b70fa433..e44347ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Improvements: * [OLMIS-7989](https://openlmis.atlassian.net/browse/OLMIS-7989): Extension of import functionality by adding possibility to import facilities and supported programs. * Improved performance. + * Improved csv files validation * [OLMIS-7895](https://openlmis.atlassian.net/browse/OLMIS-7895): Add demo data for BUQ and TB Monthly * [OLMIS-7988](https://openlmis.atlassian.net/browse/OLMIS-7988): Add DHIS2 integration rights * [OLMIS-7953](https://openlmis.atlassian.net/browse/OLMIS-7953): Improve some API calls performance diff --git a/src/main/java/org/openlmis/referencedata/service/export/DataImportService.java b/src/main/java/org/openlmis/referencedata/service/export/DataImportService.java index 72030c2d..b8f6fc4b 100644 --- a/src/main/java/org/openlmis/referencedata/service/export/DataImportService.java +++ b/src/main/java/org/openlmis/referencedata/service/export/DataImportService.java @@ -20,6 +20,7 @@ import java.util.Arrays; import java.util.List; import java.util.Map; + import org.openlmis.referencedata.dto.BaseDto; import org.openlmis.referencedata.exception.ValidationMessageException; import org.openlmis.referencedata.util.FileHelper; @@ -61,6 +62,11 @@ public List importData(MultipartFile zipFile, Profiler profiler) profiler.start("CONVERT_TO_ZIP_FILE_MAP"); final Map fileMap = fileHelper.convertMultipartFileToZipFileMap(zipFile); + profiler.start("VALIDATE_CSV_FILES"); + for (String fileName : fileMap.keySet()) { + fileHelper.validateCsvFile(fileName, IMPORT_ORDER); + } + final List result = new ArrayList<>(); for (String importFileName : IMPORT_ORDER) { final InputStream fileStream = fileMap.get(importFileName); @@ -71,10 +77,6 @@ public List importData(MultipartFile zipFile, Profiler profiler) try { final Profiler entryProfiler = profiler.startNested("IMPORT_ZIP_ENTRY: " + importFileName); - - entryProfiler.start("VALIDATE_CSV_FILE"); - fileHelper.validateCsvFile(importFileName); - final DataImportPersister persister = beanFactory.getBean(importFileName, DataImportPersister.class); result.addAll(persister.processAndPersist(fileStream, entryProfiler)); diff --git a/src/main/java/org/openlmis/referencedata/util/FileHelper.java b/src/main/java/org/openlmis/referencedata/util/FileHelper.java index e46798ed..226c7dcf 100644 --- a/src/main/java/org/openlmis/referencedata/util/FileHelper.java +++ b/src/main/java/org/openlmis/referencedata/util/FileHelper.java @@ -134,8 +134,16 @@ public void validateMultipartFile(MultipartFile multipartFile) { * @param fileName the name of CSV file * @throws ValidationMessageException if any of check fails */ - public void validateCsvFile(String fileName) { + public void validateCsvFile(String fileName, List expectedFileNames) { hasExpectedExtension(fileName, ".csv"); + hasExpectedName(fileName, expectedFileNames); + } + + private void hasExpectedName(String fileName, List expectedFileNames) { + if (!expectedFileNames.contains(fileName)) { + throw new ValidationMessageException( + new Message(CsvUploadMessageKeys.ERROR_FILE_NAME_INVALID, fileName)); + } } private void hasExpectedExtension(String fileName, String expectedExtension) { diff --git a/src/test/java/org/openlmis/referencedata/util/FileHelperTest.java b/src/test/java/org/openlmis/referencedata/util/FileHelperTest.java index 270b631b..65a77c70 100644 --- a/src/test/java/org/openlmis/referencedata/util/FileHelperTest.java +++ b/src/test/java/org/openlmis/referencedata/util/FileHelperTest.java @@ -23,6 +23,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; +import java.util.List; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -38,6 +40,14 @@ @RunWith(MockitoJUnitRunner.class) public class FileHelperTest { + private static final List IMPORTABLE_FILES = + Arrays.asList( + "facility.csv", + "supportedProgram.csv", + "orderable.csv", + "programOrderable.csv", + "tradeItem.csv"); + @InjectMocks private FileHelper fileHelper; @@ -84,8 +94,14 @@ private byte[] createValidZipFileContent() throws IOException { @Test(expected = ValidationMessageException.class) public void shouldThrowExceptionWhenValidateCsvWithWrongExtension() { - String fileName = "test.txt"; - fileHelper.validateCsvFile(fileName); + String fileName = "facility.txt"; + fileHelper.validateCsvFile(fileName, IMPORTABLE_FILES); + } + + @Test(expected = ValidationMessageException.class) + public void shouldThrowExceptionWhenValidateCsvWithWrongFileName() { + String fileName = "invalidName.csv"; + fileHelper.validateCsvFile(fileName, IMPORTABLE_FILES); } @Test(expected = ValidationMessageException.class)