diff --git a/src/main/java/ch/admin/bag/covidcertificate/service/CsvService.java b/src/main/java/ch/admin/bag/covidcertificate/service/CsvService.java index 3a8ab81f..5ef29670 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/service/CsvService.java +++ b/src/main/java/ch/admin/bag/covidcertificate/service/CsvService.java @@ -168,7 +168,7 @@ private boolean areCreateCertificateRequestsValid(List cre CertificateCsvBean csvBean = csvBeans.get(i); if (createDto != null && csvBean.getError() == null) { try { - validate(createDto, i); + validate(createDto); } catch (CreateCertificateException e) { hasError = true; csvBean.setError(e.getError().toString()); @@ -180,23 +180,23 @@ private boolean areCreateCertificateRequestsValid(List cre return !hasError; } - private void validate(CertificateCreateDto createDto, int index) { + private void validate(CertificateCreateDto createDto) { createDto.validate(); if (createDto instanceof RecoveryCertificateCreateDto) { - RecoveryCertificateDataDto dataDto = ((RecoveryCertificateCreateDto) createDto).getRecoveryInfo().get(index); + RecoveryCertificateDataDto dataDto = ((RecoveryCertificateCreateDto) createDto).getRecoveryInfo().get(0); CountryCode countryCode = valueSetsService.getCountryCode(dataDto.getCountryOfTest(), createDto.getLanguage()); if (countryCode == null) { throw new CreateCertificateException(INVALID_COUNTRY_OF_TEST); } } else if (createDto instanceof TestCertificateCreateDto) { - TestCertificateDataDto dataDto = ((TestCertificateCreateDto) createDto).getTestInfo().get(index); + TestCertificateDataDto dataDto = ((TestCertificateCreateDto) createDto).getTestInfo().get(0); CountryCode countryCode = valueSetsService.getCountryCode(dataDto.getMemberStateOfTest(), createDto.getLanguage()); if (countryCode == null) { throw new CreateCertificateException(INVALID_COUNTRY_OF_TEST); } valueSetsService.getTestValueSet(dataDto); } else if (createDto instanceof VaccinationCertificateCreateDto) { - VaccinationCertificateDataDto dataDto = ((VaccinationCertificateCreateDto) createDto).getVaccinationInfo().get(index); + VaccinationCertificateDataDto dataDto = ((VaccinationCertificateCreateDto) createDto).getVaccinationInfo().get(0); CountryCode countryCode = valueSetsService.getCountryCode(dataDto.getCountryOfVaccination(), createDto.getLanguage()); if (countryCode == null) { throw new CreateCertificateException(INVALID_COUNTRY_OF_TEST); diff --git a/src/test/java/ch/admin/bag/covidcertificate/service/CsvServiceTest.java b/src/test/java/ch/admin/bag/covidcertificate/service/CsvServiceTest.java index cccfd777..7dd76a87 100644 --- a/src/test/java/ch/admin/bag/covidcertificate/service/CsvServiceTest.java +++ b/src/test/java/ch/admin/bag/covidcertificate/service/CsvServiceTest.java @@ -53,6 +53,8 @@ public class CsvServiceTest { private final File validVaccinationFile; private final File emptyCsv; private final File invalidCsv; + private final File invalidMultipleCsv; + private final File validMultipleCsv; public CsvServiceTest() { validRecoveryFile = new File("src/test/resources/csv/recovery_csv_valid.csv"); @@ -60,6 +62,8 @@ public CsvServiceTest() { validVaccinationFile = new File("src/test/resources/csv/vaccination_csv_valid.csv"); emptyCsv = new File("src/test/resources/csv/recovery_csv_empty.csv"); invalidCsv = new File("src/test/resources/csv/recovery_csv_invalid.csv"); + invalidMultipleCsv = new File("src/test/resources/csv/vaccination_csv_multiple_invalid.csv"); + validMultipleCsv = new File("src/test/resources/csv/vaccination_csv_multiple_valid.csv"); } @BeforeEach @@ -99,6 +103,16 @@ public void testInvalidCsv() throws Exception { assertEquals(INVALID_CREATE_REQUESTS.getErrorCode(), exception.getError().getErrorCode()); } + @Test + public void testMultipleInvalidCsv() throws Exception { + var file = Mockito.mock(MultipartFile.class); + var inputStream = new FileInputStream(invalidMultipleCsv); + when(file.getInputStream()).thenReturn(inputStream); + var exception = assertThrows(CsvException.class, + () -> service.handleCsvRequest(file, CertificateType.vaccination.name())); + assertEquals(INVALID_CREATE_REQUESTS.getErrorCode(), exception.getError().getErrorCode()); + } + @Nested class GenerateRecoveryCertificateCsv { @Test @@ -142,5 +156,15 @@ void successful() throws IOException { inputStream.close(); } + @Test + void successfulMultiple() throws IOException { + var file = Mockito.mock(MultipartFile.class); + var inputStream = new FileInputStream(validMultipleCsv); + when(file.getInputStream()).thenReturn(inputStream); + + CsvResponseDto response = service.handleCsvRequest(file, CertificateType.vaccination.name()); + assertNotNull(response.getZip()); + inputStream.close(); + } } } diff --git a/src/test/resources/csv/vaccination_csv_multiple_invalid.csv b/src/test/resources/csv/vaccination_csv_multiple_invalid.csv new file mode 100644 index 00000000..9b8cc415 --- /dev/null +++ b/src/test/resources/csv/vaccination_csv_multiple_invalid.csv @@ -0,0 +1,5 @@ +givenName,familyName,dateOfBirth,language,streetAndNr,zipCode,city,cantonCodeSender,medicinalProductCode,numberOfDoses,totalNumberOfDoses,vaccinationDate,countryOfVaccination +Max,Muster,1985-09-20,de,MusterStrasse 1,1234,Bern,BE,EU/1/20/1507,2,2,2021-05-28,CH +Max,TooLongdklsfjsodfjlksdjflkdsjflksjflkdsjflksdjlfkjd,1985-09-20,de,MusterStrasse 1,1234,Bern,BE,EU/1/20/1507,2,2,2021-05-28,CH +TooLongdklsfjsodfjlksdjflkdsjflksjflkdsjflksdjlfkjd,Muster,1985-09-20,de,MusterStrasse 1,1234,Bern,BE,EU/1/20/1507,2,2,2021-05-28,CH +Max,Muster,2085-09-20,de,MusterStrasse 1,1234,Bern,BE,EU/1/20/1507,2,2,2021-05-28,CH diff --git a/src/test/resources/csv/vaccination_csv_multiple_valid.csv b/src/test/resources/csv/vaccination_csv_multiple_valid.csv new file mode 100644 index 00000000..6412ec60 --- /dev/null +++ b/src/test/resources/csv/vaccination_csv_multiple_valid.csv @@ -0,0 +1,5 @@ +givenName,familyName,dateOfBirth,language,streetAndNr,zipCode,city,cantonCodeSender,medicinalProductCode,numberOfDoses,totalNumberOfDoses,vaccinationDate,countryOfVaccination +Max,Muster,1985-09-20,de,MusterStrasse 1,1234,Bern,BE,EU/1/20/1507,2,2,2021-05-28,CH +Max,Muster,1985-09-20,de,MusterStrasse 1,1234,Bern,BE,EU/1/20/1507,2,2,2021-05-28,CH +Max,Muster,1985-09-20,de,MusterStrasse 1,1234,Bern,BE,EU/1/20/1507,2,2,2021-05-28,CH +Max,Muster,2000-09-20,de,MusterStrasse 1,1234,Bern,BE,EU/1/20/1507,2,2,2021-05-28,CH