From 29f6205b34436eed0cb3cdc9078b6cba4a85a309 Mon Sep 17 00:00:00 2001 From: Georgios Papadopoulos Date: Mon, 19 Jul 2021 15:44:13 +0200 Subject: [PATCH] Release 2.2.0-rc1 merge develop to master (#87) * updated version to 2.1 (#77) Co-authored-by: Nicola Keller * VACCINECER-1122: sampleDateTime now only checks if it is not later than the same day * Release/version 2.1 (#82) * updated version to 2.1 * VACCINECER-1122 (#79) * updated version to 2.1 (#77) Co-authored-by: Nicola Keller * VACCINECER-1122: sampleDateTime now only checks if it is not later than the same day Co-authored-by: Nicola Keller * VACCINECER-1194: updated test value set json to version 1.0.7 Co-authored-by: Nicola Keller * VACCINECER-1184 generate pdf from qr code * VACCINECER-1184 generate pdf from qr code * VACCINECER-1184 add unit tests * VACCINECER-1184 add controller unit tests * VACCINECER-1184 add unit tests * VACCINECER-1119: ignore bom if in the csv file encoding (#86) Co-authored-by: Nicola Keller * VACCINECER-1184 add get all test valueSets in ValueSetsService Co-authored-by: NicolaKellerTIM <43634595+NicolaKellerTIM@users.noreply.github.com> Co-authored-by: Nicola Keller Co-authored-by: Georgios Papadopoulos --- .../api/mapper/TestCertificatePdfMapper.java | 4 +- .../VaccinationCertificatePdfMapper.java | 4 +- .../mapper/ValueSetsResponseDtoMapper.java | 17 + ...ertificatePdfGenerateRequestDtoMapper.java | 36 ++ ...ertificatePdfGenerateRequestDtoMapper.java | 41 ++ ...ertificatePdfGenerateRequestDtoMapper.java | 40 ++ ...ePdfGenerateDiseaseOrAgentTargetedDto.java | 25 + .../CertificatePdfGenerateMetadataDto.java | 15 + .../CertificatePdfGeneratePersonDto.java | 17 + .../CertificatePdfGeneratePersonNameDto.java | 19 + ...ecoveryCertificateHcertDecodedDataDto.java | 27 + .../RecoveryCertificateHcertDecodedDto.java | 20 + ...veryCertificatePdfGenerateMetadataDto.java | 25 + ...overyCertificatePdfGenerateRequestDto.java | 22 + .../TestCertificateHcertDecodedDataDto.java | 33 ++ .../TestCertificateHcertDecodedDto.java | 20 + .../TestCertificatePdfGenerateRequestDto.java | 19 + ...inationCertificateHcertDecodedDataDto.java | 33 ++ ...VaccinationCertificateHcertDecodedDto.java | 20 + ...ationCertificatePdfGenerateRequestDto.java | 19 + .../api/response/ValueSetsResponseDto.java | 17 + .../api/valueset/VaccinationValueSet.java | 2 + .../api/valueset/ValueSetsDto.java | 3 +- .../CovidCertificateDtoMapperService.java | 23 +- .../CovidCertificateGenerationService.java | 78 ++- ...atePdfGenerateRequestDtoMapperService.java | 55 ++ .../covidcertificate/service/CsvService.java | 68 ++- .../service/ValueSetsLoader.java | 29 +- .../service/ValueSetsService.java | 42 +- .../CovidPdfCertificateGenerationService.java | 14 +- .../domain/AbstractCertificatePdf.java | 2 + .../service/domain/TestCertificatePdf.java | 7 +- .../domain/VaccinationCertificatePdf.java | 9 +- .../CovidCertificateGenerationController.java | 24 + .../web/controller/ValueSetsController.java | 7 +- .../ValueSetsResponseDtoMapperTest.java | 32 ++ ...ficatePdfGenerateRequestDtoMapperTest.java | 108 ++++ ...ficatePdfGenerateRequestDtoMapperTest.java | 117 ++++ ...ficatePdfGenerateRequestDtoMapperTest.java | 118 ++++ .../CovidCertificateDtoMapperServiceTest.java | 503 ++++++++++++++---- ...CovidCertificateGenerationServiceTest.java | 302 ++++++++++- ...dfGenerateRequestDtoMapperServiceTest.java | 427 +++++++++++++++ .../service/CsvServiceTest.java | 3 +- .../service/ValueSetsLoaderTest.java | 4 +- .../service/ValueSetsServiceTest.java | 97 +++- ...idPdfCertificateGenerationServiceTest.java | 7 +- ...icateGenerationControllerSecurityTest.java | 90 ++++ ...idCertificateGenerationControllerTest.java | 114 ++++ .../controller/ValueSetsControllerTest.java | 42 +- src/test/resources/csv/test_utf8_bom.csv | 13 + 50 files changed, 2562 insertions(+), 251 deletions(-) create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/mapper/ValueSetsResponseDtoMapper.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/RecoveryCertificatePdfGenerateRequestDtoMapper.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/TestCertificatePdfGenerateRequestDtoMapper.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/VaccinationCertificatePdfGenerateRequestDtoMapper.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGenerateDiseaseOrAgentTargetedDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGenerateMetadataDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGeneratePersonDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGeneratePersonNameDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificateHcertDecodedDataDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificateHcertDecodedDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificatePdfGenerateMetadataDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificatePdfGenerateRequestDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/TestCertificateHcertDecodedDataDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/TestCertificateHcertDecodedDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/TestCertificatePdfGenerateRequestDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/VaccinationCertificateHcertDecodedDataDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/VaccinationCertificateHcertDecodedDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/VaccinationCertificatePdfGenerateRequestDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/api/response/ValueSetsResponseDto.java create mode 100644 src/main/java/ch/admin/bag/covidcertificate/service/CovidCertificatePdfGenerateRequestDtoMapperService.java create mode 100644 src/test/java/ch/admin/bag/covidcertificate/api/mapper/ValueSetsResponseDtoMapperTest.java create mode 100644 src/test/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/RecoveryCertificatePdfGenerateRequestDtoMapperTest.java create mode 100644 src/test/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/TestCertificatePdfGenerateRequestDtoMapperTest.java create mode 100644 src/test/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/VaccinationCertificatePdfGenerateRequestDtoMapperTest.java create mode 100644 src/test/java/ch/admin/bag/covidcertificate/service/CovidCertificatePdfGenerateRequestDtoMapperServiceTest.java create mode 100644 src/test/resources/csv/test_utf8_bom.csv diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/mapper/TestCertificatePdfMapper.java b/src/main/java/ch/admin/bag/covidcertificate/api/mapper/TestCertificatePdfMapper.java index 4b0543ba..4a11914c 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/api/mapper/TestCertificatePdfMapper.java +++ b/src/main/java/ch/admin/bag/covidcertificate/api/mapper/TestCertificatePdfMapper.java @@ -23,7 +23,9 @@ public static TestCertificatePdf toTestCertificatePdf( ) { CovidCertificateDiseaseOrAgentTargeted diseaseOrAgentTargeted = CovidCertificateDiseaseOrAgentTargeted.getStandardInstance(); return new TestCertificatePdf( - testCertificateCreateDto.getPersonData(), + testCertificateCreateDto.getPersonData().getName().getFamilyName(), + testCertificateCreateDto.getPersonData().getName().getGivenName(), + testCertificateCreateDto.getPersonData().getDateOfBirth(), testCertificateCreateDto.getLanguage(), diseaseOrAgentTargeted.getCode(), diseaseOrAgentTargeted.getSystem(), diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/mapper/VaccinationCertificatePdfMapper.java b/src/main/java/ch/admin/bag/covidcertificate/api/mapper/VaccinationCertificatePdfMapper.java index 39ef7567..8925f9a7 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/api/mapper/VaccinationCertificatePdfMapper.java +++ b/src/main/java/ch/admin/bag/covidcertificate/api/mapper/VaccinationCertificatePdfMapper.java @@ -22,7 +22,9 @@ public static VaccinationCertificatePdf toVaccinationCertificatePdf( ) { CovidCertificateDiseaseOrAgentTargeted diseaseOrAgentTargeted = CovidCertificateDiseaseOrAgentTargeted.getStandardInstance(); return new VaccinationCertificatePdf( - vaccinationCertificateCreateDto.getPersonData(), + vaccinationCertificateCreateDto.getPersonData().getName().getFamilyName(), + vaccinationCertificateCreateDto.getPersonData().getName().getGivenName(), + vaccinationCertificateCreateDto.getPersonData().getDateOfBirth(), vaccinationCertificateCreateDto.getLanguage(), diseaseOrAgentTargeted.getCode(), diseaseOrAgentTargeted.getSystem(), diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/mapper/ValueSetsResponseDtoMapper.java b/src/main/java/ch/admin/bag/covidcertificate/api/mapper/ValueSetsResponseDtoMapper.java new file mode 100644 index 00000000..b3ccf42f --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/mapper/ValueSetsResponseDtoMapper.java @@ -0,0 +1,17 @@ +package ch.admin.bag.covidcertificate.api.mapper; + +import ch.admin.bag.covidcertificate.api.response.ValueSetsResponseDto; +import ch.admin.bag.covidcertificate.api.valueset.ValueSetsDto; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class ValueSetsResponseDtoMapper { + public static ValueSetsResponseDto create(ValueSetsDto valueSetsDto){ + return new ValueSetsResponseDto( + valueSetsDto.getCountryCodes(), + valueSetsDto.getVaccinationSets(), + valueSetsDto.getChAcceptedTestValueSets() + ); + } +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/RecoveryCertificatePdfGenerateRequestDtoMapper.java b/src/main/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/RecoveryCertificatePdfGenerateRequestDtoMapper.java new file mode 100644 index 00000000..cd660790 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/RecoveryCertificatePdfGenerateRequestDtoMapper.java @@ -0,0 +1,36 @@ +package ch.admin.bag.covidcertificate.api.mapper.pdfgeneration; + +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.RecoveryCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.service.domain.CovidCertificateDiseaseOrAgentTargeted; +import ch.admin.bag.covidcertificate.service.domain.RecoveryCertificatePdf; +import ch.admin.bag.covidcertificate.util.DateHelper; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import static ch.admin.bag.covidcertificate.api.Constants.ISSUER; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class RecoveryCertificatePdfGenerateRequestDtoMapper { + + public static RecoveryCertificatePdf toRecoveryCertificatePdf( + RecoveryCertificatePdfGenerateRequestDto recoveryCertificateCreateDto, + String countryOfTest, + String countryOfTestEn + ) { + var diseaseOrAgentTargeted = CovidCertificateDiseaseOrAgentTargeted.getStandardInstance(); + return new RecoveryCertificatePdf( + recoveryCertificateCreateDto.getDecodedCert().getPersonData().getName().getFamilyName(), + recoveryCertificateCreateDto.getDecodedCert().getPersonData().getName().getGivenName(), + recoveryCertificateCreateDto.getDecodedCert().getPersonData().getDateOfBirth(), + recoveryCertificateCreateDto.getLanguage(), + diseaseOrAgentTargeted.getCode(), + diseaseOrAgentTargeted.getSystem(), + recoveryCertificateCreateDto.getDecodedCert().getRecoveryInfo().get(0).getDateOfFirstPositiveTestResult(), + countryOfTest, + countryOfTestEn, + ISSUER, + DateHelper.calculateValidFrom(recoveryCertificateCreateDto.getDecodedCert().getRecoveryInfo().get(0).getDateOfFirstPositiveTestResult()), + DateHelper.calculateValidUntil(recoveryCertificateCreateDto.getDecodedCert().getRecoveryInfo().get(0).getDateOfFirstPositiveTestResult()), + recoveryCertificateCreateDto.getDecodedCert().getRecoveryInfo().get(0).getIdentifier()); + } +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/TestCertificatePdfGenerateRequestDtoMapper.java b/src/main/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/TestCertificatePdfGenerateRequestDtoMapper.java new file mode 100644 index 00000000..588215fa --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/TestCertificatePdfGenerateRequestDtoMapper.java @@ -0,0 +1,41 @@ +package ch.admin.bag.covidcertificate.api.mapper.pdfgeneration; + +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.TestCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.valueset.NegativeTestResult; +import ch.admin.bag.covidcertificate.api.valueset.TestValueSet; +import ch.admin.bag.covidcertificate.service.domain.CovidCertificateDiseaseOrAgentTargeted; +import ch.admin.bag.covidcertificate.service.domain.TestCertificatePdf; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import static ch.admin.bag.covidcertificate.api.Constants.ISSUER; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class TestCertificatePdfGenerateRequestDtoMapper { + + public static TestCertificatePdf toTestCertificatePdf( + TestCertificatePdfGenerateRequestDto testCertificateCreateDto, + TestValueSet testValueSet, + String memberStateOfTest, + String memberStateOfTestEn + ) { + var diseaseOrAgentTargeted = CovidCertificateDiseaseOrAgentTargeted.getStandardInstance(); + return new TestCertificatePdf( + testCertificateCreateDto.getDecodedCert().getPersonData().getName().getFamilyName(), + testCertificateCreateDto.getDecodedCert().getPersonData().getName().getGivenName(), + testCertificateCreateDto.getDecodedCert().getPersonData().getDateOfBirth(), + testCertificateCreateDto.getLanguage(), + diseaseOrAgentTargeted.getCode(), + diseaseOrAgentTargeted.getSystem(), + testValueSet.getType(), + testValueSet.getName(), + testValueSet.getManufacturer(), + testCertificateCreateDto.getDecodedCert().getTestInfo().get(0).getSampleDateTime(), + NegativeTestResult.DISPLAY, + testCertificateCreateDto.getDecodedCert().getTestInfo().get(0).getTestingCentreOrFacility(), + memberStateOfTest, + memberStateOfTestEn, + ISSUER, + testCertificateCreateDto.getDecodedCert().getTestInfo().get(0).getIdentifier()); + } +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/VaccinationCertificatePdfGenerateRequestDtoMapper.java b/src/main/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/VaccinationCertificatePdfGenerateRequestDtoMapper.java new file mode 100644 index 00000000..7955ff9e --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/VaccinationCertificatePdfGenerateRequestDtoMapper.java @@ -0,0 +1,40 @@ +package ch.admin.bag.covidcertificate.api.mapper.pdfgeneration; + +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.VaccinationCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.valueset.VaccinationValueSet; +import ch.admin.bag.covidcertificate.service.domain.CovidCertificateDiseaseOrAgentTargeted; +import ch.admin.bag.covidcertificate.service.domain.VaccinationCertificatePdf; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import static ch.admin.bag.covidcertificate.api.Constants.ISSUER; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class VaccinationCertificatePdfGenerateRequestDtoMapper { + + public static VaccinationCertificatePdf toVaccinationCertificatePdf( + VaccinationCertificatePdfGenerateRequestDto vaccinationCertificateCreateDto, + VaccinationValueSet vaccinationValueSet, + String countryOfVaccinationDisplay, + String countryOfVaccinationDisplayEn + ) { + var diseaseOrAgentTargeted = CovidCertificateDiseaseOrAgentTargeted.getStandardInstance(); + return new VaccinationCertificatePdf( + vaccinationCertificateCreateDto.getDecodedCert().getPersonData().getName().getFamilyName(), + vaccinationCertificateCreateDto.getDecodedCert().getPersonData().getName().getGivenName(), + vaccinationCertificateCreateDto.getDecodedCert().getPersonData().getDateOfBirth(), + vaccinationCertificateCreateDto.getLanguage(), + diseaseOrAgentTargeted.getCode(), + diseaseOrAgentTargeted.getSystem(), + vaccinationValueSet.getProphylaxis(), + vaccinationValueSet.getMedicinalProduct(), + vaccinationValueSet.getAuthHolder(), + vaccinationCertificateCreateDto.getDecodedCert().getVaccinationInfo().get(0).getNumberOfDoses(), + vaccinationCertificateCreateDto.getDecodedCert().getVaccinationInfo().get(0).getTotalNumberOfDoses(), + vaccinationCertificateCreateDto.getDecodedCert().getVaccinationInfo().get(0).getVaccinationDate(), + countryOfVaccinationDisplay, + countryOfVaccinationDisplayEn, + ISSUER, + vaccinationCertificateCreateDto.getDecodedCert().getVaccinationInfo().get(0).getIdentifier()); + } +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGenerateDiseaseOrAgentTargetedDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGenerateDiseaseOrAgentTargetedDto.java new file mode 100644 index 00000000..77899eb1 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGenerateDiseaseOrAgentTargetedDto.java @@ -0,0 +1,25 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; + +import static ch.admin.bag.covidcertificate.api.Constants.DEFAULT_DISEASE_OR_AGENT_SYSTEM; +import static ch.admin.bag.covidcertificate.api.Constants.DEFAULT_DISEASE_OR_AGENT_TARGETED; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class CertificatePdfGenerateDiseaseOrAgentTargetedDto { + @JsonProperty("code") + private String code; + @JsonProperty("system") + private String system; + + public static CertificatePdfGenerateDiseaseOrAgentTargetedDto getStandardInstance() { + return new CertificatePdfGenerateDiseaseOrAgentTargetedDto( + DEFAULT_DISEASE_OR_AGENT_TARGETED, + DEFAULT_DISEASE_OR_AGENT_SYSTEM + ); + } +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGenerateMetadataDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGenerateMetadataDto.java new file mode 100644 index 00000000..5e6cd1a1 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGenerateMetadataDto.java @@ -0,0 +1,15 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class CertificatePdfGenerateMetadataDto { + @JsonProperty("is") + private String issuer; + @JsonProperty("ci") + private String identifier; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGeneratePersonDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGeneratePersonDto.java new file mode 100644 index 00000000..9ddc6d8c --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGeneratePersonDto.java @@ -0,0 +1,17 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; + +import java.time.LocalDate; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class CertificatePdfGeneratePersonDto { + @JsonProperty("nam") + private CertificatePdfGeneratePersonNameDto name; + @JsonProperty("dob") + private LocalDate dateOfBirth; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGeneratePersonNameDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGeneratePersonNameDto.java new file mode 100644 index 00000000..a0397eaa --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/CertificatePdfGeneratePersonNameDto.java @@ -0,0 +1,19 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class CertificatePdfGeneratePersonNameDto { + @JsonProperty("fn") + private String familyName; + @JsonProperty("fnt") + private String familyNameStandardised; + @JsonProperty("gn") + private String givenName; + @JsonProperty("gnt") + private String givenNameStandardised; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificateHcertDecodedDataDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificateHcertDecodedDataDto.java new file mode 100644 index 00000000..b2f43fd2 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificateHcertDecodedDataDto.java @@ -0,0 +1,27 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; + +import java.time.LocalDate; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class RecoveryCertificateHcertDecodedDataDto { + @JsonProperty("tg") + private String diseaseOrAgentTargeted; + @JsonProperty("fr") + private LocalDate dateOfFirstPositiveTestResult; + @JsonProperty("co") + private String countryOfTest; + @JsonProperty("df") + private LocalDate validFrom; + @JsonProperty("du") + private LocalDate validUntil; + @JsonProperty("is") + private String issuer; + @JsonProperty("ci") + private String identifier; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificateHcertDecodedDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificateHcertDecodedDto.java new file mode 100644 index 00000000..e8af8dd0 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificateHcertDecodedDto.java @@ -0,0 +1,20 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonUnwrapped; +import lombok.*; + +import java.util.List; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class RecoveryCertificateHcertDecodedDto { + @JsonProperty("ver") + private String version; + @JsonUnwrapped + private CertificatePdfGeneratePersonDto personData; + @JsonProperty("r") + private List recoveryInfo; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificatePdfGenerateMetadataDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificatePdfGenerateMetadataDto.java new file mode 100644 index 00000000..a0856ca9 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificatePdfGenerateMetadataDto.java @@ -0,0 +1,25 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import java.time.LocalDate; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class RecoveryCertificatePdfGenerateMetadataDto extends CertificatePdfGenerateMetadataDto { + @JsonProperty("df") + private LocalDate validFrom; + @JsonProperty("du") + private LocalDate validUntil; + + public RecoveryCertificatePdfGenerateMetadataDto(String issuer, String identifier, LocalDate validFrom, LocalDate validUntil) { + super(issuer, identifier); + this.validFrom = validFrom; + this.validUntil = validUntil; + } +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificatePdfGenerateRequestDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificatePdfGenerateRequestDto.java new file mode 100644 index 00000000..0691f605 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/RecoveryCertificatePdfGenerateRequestDto.java @@ -0,0 +1,22 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonUnwrapped; +import lombok.*; + +import java.util.List; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class RecoveryCertificatePdfGenerateRequestDto { + @JsonProperty("issuedAt") + private Long issuedAt; + @JsonProperty("language") + private String language; + @JsonProperty("hcert") + private String hcert; + @JsonProperty("decodedCert") + private RecoveryCertificateHcertDecodedDto decodedCert; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/TestCertificateHcertDecodedDataDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/TestCertificateHcertDecodedDataDto.java new file mode 100644 index 00000000..fc6816dd --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/TestCertificateHcertDecodedDataDto.java @@ -0,0 +1,33 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; + +import java.time.ZonedDateTime; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class TestCertificateHcertDecodedDataDto { + @JsonProperty("tg") + private String diseaseOrAgentTargeted; + @JsonProperty("tt") + private String typeOfTest; + @JsonProperty("nm") + private String testName; + @JsonProperty("ma") + private String testManufacturer; + @JsonProperty("sc") + private ZonedDateTime sampleDateTime; + @JsonProperty("tr") + private String result; + @JsonProperty("tc") + private String testingCentreOrFacility; + @JsonProperty("co") + private String memberStateOfTest; + @JsonProperty("is") + private String issuer; + @JsonProperty("ci") + private String identifier; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/TestCertificateHcertDecodedDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/TestCertificateHcertDecodedDto.java new file mode 100644 index 00000000..5dcab558 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/TestCertificateHcertDecodedDto.java @@ -0,0 +1,20 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonUnwrapped; +import lombok.*; + +import java.util.List; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class TestCertificateHcertDecodedDto { + @JsonProperty("ver") + private String version; + @JsonUnwrapped + private CertificatePdfGeneratePersonDto personData; + @JsonProperty("t") + private List testInfo; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/TestCertificatePdfGenerateRequestDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/TestCertificatePdfGenerateRequestDto.java new file mode 100644 index 00000000..b369a229 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/TestCertificatePdfGenerateRequestDto.java @@ -0,0 +1,19 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class TestCertificatePdfGenerateRequestDto { + @JsonProperty("issuedAt") + private Long issuedAt; + @JsonProperty("language") + private String language; + @JsonProperty("hcert") + private String hcert; + @JsonProperty("decodedCert") + private TestCertificateHcertDecodedDto decodedCert; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/VaccinationCertificateHcertDecodedDataDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/VaccinationCertificateHcertDecodedDataDto.java new file mode 100644 index 00000000..3206b022 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/VaccinationCertificateHcertDecodedDataDto.java @@ -0,0 +1,33 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; + +import java.time.LocalDate; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class VaccinationCertificateHcertDecodedDataDto { + @JsonProperty("tg") + private String diseaseOrAgentTargeted; + @JsonProperty("vp") + private String vaccineProphylaxis; + @JsonProperty("mp") + private String medicinalProduct; + @JsonProperty("ma") + private String marketingAuthorizationHolder; + @JsonProperty("dn") + private Integer numberOfDoses; + @JsonProperty("sd") + private Integer totalNumberOfDoses; + @JsonProperty("dt") + private LocalDate vaccinationDate; + @JsonProperty("co") + private String countryOfVaccination; + @JsonProperty("is") + private String issuer; + @JsonProperty("ci") + private String identifier; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/VaccinationCertificateHcertDecodedDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/VaccinationCertificateHcertDecodedDto.java new file mode 100644 index 00000000..eb1a1fa9 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/VaccinationCertificateHcertDecodedDto.java @@ -0,0 +1,20 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonUnwrapped; +import lombok.*; + +import java.util.List; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class VaccinationCertificateHcertDecodedDto { + @JsonProperty("ver") + private String version; + @JsonUnwrapped + private CertificatePdfGeneratePersonDto personData; + @JsonProperty("v") + private List vaccinationInfo; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/VaccinationCertificatePdfGenerateRequestDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/VaccinationCertificatePdfGenerateRequestDto.java new file mode 100644 index 00000000..19497a66 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/request/pdfgeneration/VaccinationCertificatePdfGenerateRequestDto.java @@ -0,0 +1,19 @@ +package ch.admin.bag.covidcertificate.api.request.pdfgeneration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class VaccinationCertificatePdfGenerateRequestDto { + @JsonProperty("issuedAt") + private Long issuedAt; + @JsonProperty("language") + private String language; + @JsonProperty("hcert") + private String hcert; + @JsonProperty("decodedCert") + private VaccinationCertificateHcertDecodedDto decodedCert; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/response/ValueSetsResponseDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/response/ValueSetsResponseDto.java new file mode 100644 index 00000000..8a230cb8 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/api/response/ValueSetsResponseDto.java @@ -0,0 +1,17 @@ +package ch.admin.bag.covidcertificate.api.response; + +import ch.admin.bag.covidcertificate.api.valueset.CountryCodes; +import ch.admin.bag.covidcertificate.api.valueset.TestValueSet; +import ch.admin.bag.covidcertificate.api.valueset.VaccinationValueSet; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.List; + +@Getter +@AllArgsConstructor +public class ValueSetsResponseDto { + private final CountryCodes countryCodes; + private final List vaccinationSets; + private final List testSets; +} diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/valueset/VaccinationValueSet.java b/src/main/java/ch/admin/bag/covidcertificate/api/valueset/VaccinationValueSet.java index e6c12e8f..14722933 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/api/valueset/VaccinationValueSet.java +++ b/src/main/java/ch/admin/bag/covidcertificate/api/valueset/VaccinationValueSet.java @@ -1,9 +1,11 @@ package ch.admin.bag.covidcertificate.api.valueset; import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.EqualsAndHashCode; import lombok.Getter; @Getter +@EqualsAndHashCode public class VaccinationValueSet { @JsonProperty("name") private String medicinalProduct; diff --git a/src/main/java/ch/admin/bag/covidcertificate/api/valueset/ValueSetsDto.java b/src/main/java/ch/admin/bag/covidcertificate/api/valueset/ValueSetsDto.java index c35790e9..a29182cb 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/api/valueset/ValueSetsDto.java +++ b/src/main/java/ch/admin/bag/covidcertificate/api/valueset/ValueSetsDto.java @@ -10,5 +10,6 @@ public class ValueSetsDto { private final CountryCodes countryCodes; private final List vaccinationSets; - private final List testSets; + private final List chAcceptedTestValueSets; + private final List allTestValueSets; } diff --git a/src/main/java/ch/admin/bag/covidcertificate/service/CovidCertificateDtoMapperService.java b/src/main/java/ch/admin/bag/covidcertificate/service/CovidCertificateDtoMapperService.java index c01c5906..ed724ebf 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/service/CovidCertificateDtoMapperService.java +++ b/src/main/java/ch/admin/bag/covidcertificate/service/CovidCertificateDtoMapperService.java @@ -5,9 +5,6 @@ import ch.admin.bag.covidcertificate.api.request.RecoveryCertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.TestCertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.VaccinationCertificateCreateDto; -import ch.admin.bag.covidcertificate.api.valueset.CountryCode; -import ch.admin.bag.covidcertificate.api.valueset.TestValueSet; -import ch.admin.bag.covidcertificate.api.valueset.VaccinationValueSet; import ch.admin.bag.covidcertificate.service.domain.*; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -22,14 +19,14 @@ public class CovidCertificateDtoMapperService { private final ValueSetsService valueSetsService; public VaccinationCertificateQrCode toVaccinationCertificateQrCode(VaccinationCertificateCreateDto createDto) { - VaccinationValueSet vaccinationValueSet = valueSetsService.getVaccinationValueSet(createDto.getVaccinationInfo().get(0).getMedicinalProductCode()); + var vaccinationValueSet = valueSetsService.getVaccinationValueSet(createDto.getVaccinationInfo().get(0).getMedicinalProductCode()); return VaccinationCertificateQrCodeMapper.toVaccinationCertificateQrCode(createDto, vaccinationValueSet); } public VaccinationCertificatePdf toVaccinationCertificatePdf(VaccinationCertificateCreateDto createDto, VaccinationCertificateQrCode qrCodeData){ - VaccinationValueSet vaccinationValueSet = valueSetsService.getVaccinationValueSet(createDto.getVaccinationInfo().get(0).getMedicinalProductCode()); - CountryCode countryCode = valueSetsService.getCountryCode(createDto.getVaccinationInfo().get(0).getCountryOfVaccination(), createDto.getLanguage()); - CountryCode countryCodeEn = valueSetsService.getCountryCodeEn(createDto.getVaccinationInfo().get(0).getCountryOfVaccination()); + var vaccinationValueSet = valueSetsService.getVaccinationValueSet(createDto.getVaccinationInfo().get(0).getMedicinalProductCode()); + var countryCode = valueSetsService.getCountryCode(createDto.getVaccinationInfo().get(0).getCountryOfVaccination(), createDto.getLanguage()); + var countryCodeEn = valueSetsService.getCountryCodeEn(createDto.getVaccinationInfo().get(0).getCountryOfVaccination()); if (countryCode == null || countryCodeEn == null) { throw new CreateCertificateException(INVALID_COUNTRY_OF_VACCINATION); } @@ -37,15 +34,15 @@ public VaccinationCertificatePdf toVaccinationCertificatePdf(VaccinationCertific } public TestCertificateQrCode toTestCertificateQrCode(TestCertificateCreateDto createDto) { - TestValueSet testValueSet = valueSetsService.getTestValueSet(createDto.getTestInfo().get(0)); + var testValueSet = valueSetsService.getChAcceptedTestValueSet(createDto.getTestInfo().get(0)); return TestCertificateQrCodeMapper.toTestCertificateQrCode(createDto, testValueSet); } public TestCertificatePdf toTestCertificatePdf(TestCertificateCreateDto createDto, TestCertificateQrCode qrCodeData) { - TestValueSet testValueSet = valueSetsService.getTestValueSet(createDto.getTestInfo().get(0)); - CountryCode countryCode = valueSetsService.getCountryCode(createDto.getTestInfo().get(0).getMemberStateOfTest(), createDto.getLanguage()); - CountryCode countryCodeEn = valueSetsService.getCountryCodeEn(createDto.getTestInfo().get(0).getMemberStateOfTest()); + var testValueSet = valueSetsService.getChAcceptedTestValueSet(createDto.getTestInfo().get(0)); + var countryCode = valueSetsService.getCountryCode(createDto.getTestInfo().get(0).getMemberStateOfTest(), createDto.getLanguage()); + var countryCodeEn = valueSetsService.getCountryCodeEn(createDto.getTestInfo().get(0).getMemberStateOfTest()); if (countryCode == null || countryCodeEn == null) { throw new CreateCertificateException(INVALID_MEMBER_STATE_OF_TEST); } @@ -58,8 +55,8 @@ public RecoveryCertificateQrCode toRecoveryCertificateQrCode(RecoveryCertificate } public RecoveryCertificatePdf toRecoveryCertificatePdf(RecoveryCertificateCreateDto createDto, RecoveryCertificateQrCode qrCodeData) { - CountryCode countryCode = valueSetsService.getCountryCode(createDto.getRecoveryInfo().get(0).getCountryOfTest(), createDto.getLanguage()); - CountryCode countryCodeEn = valueSetsService.getCountryCodeEn(createDto.getRecoveryInfo().get(0).getCountryOfTest()); + var countryCode = valueSetsService.getCountryCode(createDto.getRecoveryInfo().get(0).getCountryOfTest(), createDto.getLanguage()); + var countryCodeEn = valueSetsService.getCountryCodeEn(createDto.getRecoveryInfo().get(0).getCountryOfTest()); if (countryCode == null || countryCodeEn == null) { throw new CreateCertificateException(INVALID_COUNTRY_OF_TEST); } diff --git a/src/main/java/ch/admin/bag/covidcertificate/service/CovidCertificateGenerationService.java b/src/main/java/ch/admin/bag/covidcertificate/service/CovidCertificateGenerationService.java index 0a752172..a6dcf7c2 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/service/CovidCertificateGenerationService.java +++ b/src/main/java/ch/admin/bag/covidcertificate/service/CovidCertificateGenerationService.java @@ -1,25 +1,38 @@ package ch.admin.bag.covidcertificate.service; +import ch.admin.bag.covidcertificate.api.exception.CreateCertificateException; import ch.admin.bag.covidcertificate.api.mapper.CertificatePrintRequestDtoMapper; import ch.admin.bag.covidcertificate.api.request.CertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.RecoveryCertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.TestCertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.VaccinationCertificateCreateDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.RecoveryCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.TestCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.VaccinationCertificatePdfGenerateRequestDto; import ch.admin.bag.covidcertificate.api.response.CovidCertificateCreateResponseDto; import ch.admin.bag.covidcertificate.client.inapp_delivery.InAppDeliveryClient; import ch.admin.bag.covidcertificate.client.inapp_delivery.domain.InAppDeliveryRequestDto; import ch.admin.bag.covidcertificate.client.printing.PrintQueueClient; import ch.admin.bag.covidcertificate.service.document.CovidPdfCertificateGenerationService; -import ch.admin.bag.covidcertificate.service.domain.*; +import ch.admin.bag.covidcertificate.service.domain.AbstractCertificatePdf; +import ch.admin.bag.covidcertificate.service.domain.AbstractCertificateQrCode; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; -import se.digg.dgc.encoding.Barcode; +import se.digg.dgc.encoding.BarcodeException; +import se.digg.dgc.encoding.impl.DefaultBarcodeCreator; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.Base64; +import static ch.admin.bag.covidcertificate.api.Constants.CREATE_BARCODE_FAILED; + @Service @Slf4j @RequiredArgsConstructor @@ -30,33 +43,74 @@ public class CovidCertificateGenerationService { private final ObjectMapper objectMapper; private final CovidPdfCertificateGenerationService covidPdfCertificateGenerationService; private final CovidCertificateDtoMapperService covidCertificateDtoMapperService; + private final CovidCertificatePdfGenerateRequestDtoMapperService covidCertificatePdfGenerateRequestDtoMapperService; + + public CovidCertificateCreateResponseDto generateFromExistingCovidCertificate(VaccinationCertificatePdfGenerateRequestDto pdfGenerateRequestDto) { + var pdfData = covidCertificatePdfGenerateRequestDtoMapperService.toVaccinationCertificatePdf(pdfGenerateRequestDto); + return generateFromExistingCovidCertificate(pdfData, + pdfGenerateRequestDto.getHcert(), + pdfGenerateRequestDto.getIssuedAt(), + pdfGenerateRequestDto.getDecodedCert().getVaccinationInfo().get(0).getIdentifier()); + } + + public CovidCertificateCreateResponseDto generateFromExistingCovidCertificate(TestCertificatePdfGenerateRequestDto pdfGenerateRequestDto) { + var pdfData = covidCertificatePdfGenerateRequestDtoMapperService.toTestCertificatePdf(pdfGenerateRequestDto); + return generateFromExistingCovidCertificate(pdfData, + pdfGenerateRequestDto.getHcert(), + pdfGenerateRequestDto.getIssuedAt(), + pdfGenerateRequestDto.getDecodedCert().getTestInfo().get(0).getIdentifier()); + } + + public CovidCertificateCreateResponseDto generateFromExistingCovidCertificate(RecoveryCertificatePdfGenerateRequestDto pdfGenerateRequestDto) { + var pdfData = covidCertificatePdfGenerateRequestDtoMapperService.toRecoveryCertificatePdf(pdfGenerateRequestDto); + return generateFromExistingCovidCertificate(pdfData, + pdfGenerateRequestDto.getHcert(), + pdfGenerateRequestDto.getIssuedAt(), + pdfGenerateRequestDto.getDecodedCert().getRecoveryInfo().get(0).getIdentifier()); + } + + private CovidCertificateCreateResponseDto generateFromExistingCovidCertificate(AbstractCertificatePdf pdfData, String hcert, long issuedAtMillis, String uvci) { + try { + var issuedAt = getLocalDateTimeFromEpochMillis(issuedAtMillis); + var barcode = new DefaultBarcodeCreator().create(hcert, StandardCharsets.US_ASCII); + var pdf = covidPdfCertificateGenerationService.generateCovidCertificate(pdfData, hcert, issuedAt); + return new CovidCertificateCreateResponseDto(pdf, barcode.getImage(), uvci); + } catch (BarcodeException e) { + throw new CreateCertificateException(CREATE_BARCODE_FAILED); + } + } + + private LocalDateTime getLocalDateTimeFromEpochMillis(long millis) { + var instant = Instant.ofEpochMilli(millis); + return ZonedDateTime.from(instant.atZone(ZoneId.systemDefault())).toLocalDateTime(); + } public CovidCertificateCreateResponseDto generateCovidCertificate(VaccinationCertificateCreateDto createDto) throws JsonProcessingException { - VaccinationCertificateQrCode qrCodeData = covidCertificateDtoMapperService.toVaccinationCertificateQrCode(createDto); - VaccinationCertificatePdf pdfData = covidCertificateDtoMapperService.toVaccinationCertificatePdf(createDto, qrCodeData); + var qrCodeData = covidCertificateDtoMapperService.toVaccinationCertificateQrCode(createDto); + var pdfData = covidCertificateDtoMapperService.toVaccinationCertificatePdf(createDto, qrCodeData); return generateCovidCertificate(qrCodeData, pdfData, qrCodeData.getVaccinationInfo().get(0).getIdentifier(), createDto); } public CovidCertificateCreateResponseDto generateCovidCertificate(TestCertificateCreateDto createDto) throws JsonProcessingException { - TestCertificateQrCode qrCodeData = covidCertificateDtoMapperService.toTestCertificateQrCode(createDto); - TestCertificatePdf pdfData = covidCertificateDtoMapperService.toTestCertificatePdf(createDto, qrCodeData); + var qrCodeData = covidCertificateDtoMapperService.toTestCertificateQrCode(createDto); + var pdfData = covidCertificateDtoMapperService.toTestCertificatePdf(createDto, qrCodeData); return generateCovidCertificate(qrCodeData, pdfData, qrCodeData.getTestInfo().get(0).getIdentifier(), createDto); } public CovidCertificateCreateResponseDto generateCovidCertificate(RecoveryCertificateCreateDto createDto) throws JsonProcessingException { - RecoveryCertificateQrCode qrCodeData = covidCertificateDtoMapperService.toRecoveryCertificateQrCode(createDto); - RecoveryCertificatePdf pdfData = covidCertificateDtoMapperService.toRecoveryCertificatePdf(createDto, qrCodeData); + var qrCodeData = covidCertificateDtoMapperService.toRecoveryCertificateQrCode(createDto); + var pdfData = covidCertificateDtoMapperService.toRecoveryCertificatePdf(createDto, qrCodeData); return generateCovidCertificate(qrCodeData, pdfData, qrCodeData.getRecoveryInfo().get(0).getIdentifier(), createDto); } private CovidCertificateCreateResponseDto generateCovidCertificate(AbstractCertificateQrCode qrCodeData, AbstractCertificatePdf pdfData, String uvci, CertificateCreateDto createDto) throws JsonProcessingException { - String contents = objectMapper.writer().writeValueAsString(qrCodeData); + var contents = objectMapper.writer().writeValueAsString(qrCodeData); log.info("Create barcode"); - Barcode code = barcodeService.createBarcode(contents); + var code = barcodeService.createBarcode(contents); log.info("Create certificate pdf"); - byte[] pdf = covidPdfCertificateGenerationService.generateCovidCertificate(pdfData, code); + var pdf = covidPdfCertificateGenerationService.generateCovidCertificate(pdfData, code.getPayload(), LocalDateTime.now()); - CovidCertificateCreateResponseDto responseDto = new CovidCertificateCreateResponseDto(pdf, code.getImage(), uvci); + var responseDto = new CovidCertificateCreateResponseDto(pdf, code.getImage(), uvci); if (createDto.sendToPrint()) { printQueueClient.sendPrintJob(CertificatePrintRequestDtoMapper.toCertificatePrintRequestDto(pdf, uvci, createDto)); } else if (createDto.sendToApp()) { diff --git a/src/main/java/ch/admin/bag/covidcertificate/service/CovidCertificatePdfGenerateRequestDtoMapperService.java b/src/main/java/ch/admin/bag/covidcertificate/service/CovidCertificatePdfGenerateRequestDtoMapperService.java new file mode 100644 index 00000000..96cb65b1 --- /dev/null +++ b/src/main/java/ch/admin/bag/covidcertificate/service/CovidCertificatePdfGenerateRequestDtoMapperService.java @@ -0,0 +1,55 @@ +package ch.admin.bag.covidcertificate.service; + +import ch.admin.bag.covidcertificate.api.exception.CreateCertificateException; +import ch.admin.bag.covidcertificate.api.mapper.pdfgeneration.RecoveryCertificatePdfGenerateRequestDtoMapper; +import ch.admin.bag.covidcertificate.api.mapper.pdfgeneration.TestCertificatePdfGenerateRequestDtoMapper; +import ch.admin.bag.covidcertificate.api.mapper.pdfgeneration.VaccinationCertificatePdfGenerateRequestDtoMapper; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.RecoveryCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.TestCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.VaccinationCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.service.domain.RecoveryCertificatePdf; +import ch.admin.bag.covidcertificate.service.domain.TestCertificatePdf; +import ch.admin.bag.covidcertificate.service.domain.VaccinationCertificatePdf; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import static ch.admin.bag.covidcertificate.api.Constants.*; + +@Service +@Slf4j +@RequiredArgsConstructor +public class CovidCertificatePdfGenerateRequestDtoMapperService { + private final ValueSetsService valueSetsService; + + public VaccinationCertificatePdf toVaccinationCertificatePdf(VaccinationCertificatePdfGenerateRequestDto pdfGenerateRequestDto){ + var vaccinationValueSet = valueSetsService.getVaccinationValueSet(pdfGenerateRequestDto.getDecodedCert().getVaccinationInfo().get(0).getMedicinalProduct()); + var countryCode = valueSetsService.getCountryCode(pdfGenerateRequestDto.getDecodedCert().getVaccinationInfo().get(0).getCountryOfVaccination(), pdfGenerateRequestDto.getLanguage()); + var countryCodeEn = valueSetsService.getCountryCodeEn(pdfGenerateRequestDto.getDecodedCert().getVaccinationInfo().get(0).getCountryOfVaccination()); + if (countryCode == null || countryCodeEn == null) { + throw new CreateCertificateException(INVALID_COUNTRY_OF_VACCINATION); + } + return VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(pdfGenerateRequestDto, vaccinationValueSet, countryCode.getDisplay(), countryCodeEn.getDisplay()); + } + + public TestCertificatePdf toTestCertificatePdf(TestCertificatePdfGenerateRequestDto pdfGenerateRequestDto) { + var testValueSet = valueSetsService.getAllTestValueSet(pdfGenerateRequestDto.getDecodedCert().getTestInfo().get(0).getTypeOfTest(), pdfGenerateRequestDto.getDecodedCert().getTestInfo().get(0).getTestManufacturer()); + var countryCode = valueSetsService.getCountryCode(pdfGenerateRequestDto.getDecodedCert().getTestInfo().get(0).getMemberStateOfTest(), pdfGenerateRequestDto.getLanguage()); + var countryCodeEn = valueSetsService.getCountryCodeEn(pdfGenerateRequestDto.getDecodedCert().getTestInfo().get(0).getMemberStateOfTest()); + if (countryCode == null || countryCodeEn == null) { + throw new CreateCertificateException(INVALID_MEMBER_STATE_OF_TEST); + } + + return TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(pdfGenerateRequestDto, testValueSet, countryCode.getDisplay(), countryCodeEn.getDisplay()); + } + + public RecoveryCertificatePdf toRecoveryCertificatePdf(RecoveryCertificatePdfGenerateRequestDto pdfGenerateRequestDto) { + var countryCode = valueSetsService.getCountryCode(pdfGenerateRequestDto.getDecodedCert().getRecoveryInfo().get(0).getCountryOfTest(), pdfGenerateRequestDto.getLanguage()); + var countryCodeEn = valueSetsService.getCountryCodeEn(pdfGenerateRequestDto.getDecodedCert().getRecoveryInfo().get(0).getCountryOfTest()); + if (countryCode == null || countryCodeEn == null) { + throw new CreateCertificateException(INVALID_COUNTRY_OF_TEST); + } + + return RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(pdfGenerateRequestDto, countryCode.getDisplay(), countryCodeEn.getDisplay()); + } +} 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 f96b2dea..931126a3 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/service/CsvService.java +++ b/src/main/java/ch/admin/bag/covidcertificate/service/CsvService.java @@ -6,7 +6,6 @@ import ch.admin.bag.covidcertificate.api.request.*; import ch.admin.bag.covidcertificate.api.response.CovidCertificateCreateResponseDto; import ch.admin.bag.covidcertificate.api.response.CsvResponseDto; -import ch.admin.bag.covidcertificate.api.valueset.CountryCode; import ch.admin.bag.covidcertificate.config.security.authentication.ServletJeapAuthorization; import ch.admin.bag.covidcertificate.domain.KpiData; import com.fasterxml.jackson.core.JsonProcessingException; @@ -17,6 +16,7 @@ import com.opencsv.bean.StatefulBeanToCsvBuilder; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.mozilla.universalchardet.UnicodeBOMInputStream; import org.mozilla.universalchardet.UniversalDetector; import org.springframework.security.oauth2.jwt.Jwt; import org.springframework.stereotype.Service; @@ -81,20 +81,20 @@ private byte[] handleCsvRequest(MultipartFile file, Class createCertificates(List createDtos, Class csvBeanClass) throws JsonProcessingException { if (csvBeanClass == RecoveryCertificateCsvBean.class) { - return createRecoveryCertificates(createDtos.stream().map(createDto -> (RecoveryCertificateCreateDto) createDto).collect(Collectors.toList())); + return createRecoveryCertificates(createDtos.stream().map(RecoveryCertificateCreateDto.class::cast).collect(Collectors.toList())); } else if (csvBeanClass == TestCertificateCsvBean.class) { - return createTestCertificates(createDtos.stream().map(createDto -> (TestCertificateCreateDto) createDto).collect(Collectors.toList())); + return createTestCertificates(createDtos.stream().map(TestCertificateCreateDto.class::cast).collect(Collectors.toList())); } else if (csvBeanClass == VaccinationCertificateCsvBean.class) { - return createVaccinationCertificates(createDtos.stream().map(createDto -> (VaccinationCertificateCreateDto) createDto).collect(Collectors.toList())); + return createVaccinationCertificates(createDtos.stream().map(VaccinationCertificateCreateDto.class::cast).collect(Collectors.toList())); } else { throw new CreateCertificateException(INVALID_CSV); } } private byte[] createCsvException(List csvBeans) throws IOException { - File returnFile = writeCsv(csvBeans); + var returnFile = writeCsv(csvBeans); byte[] errorCsv = Files.readAllBytes(returnFile.toPath()); - returnFile.delete(); + Files.delete(returnFile.toPath()); throw new CsvException(new CsvError(INVALID_CREATE_REQUESTS, errorCsv)); } @@ -135,9 +135,9 @@ private List createVaccinationCertificates(Li } private List mapToBean(MultipartFile file, Class csvBeanClass) throws IOException { - char separator = getSeparator(file); - String encoding = UniversalDetector.detectCharset(file.getInputStream()); - try (Reader reader = new BufferedReader(new InputStreamReader(file.getInputStream(), Charset.forName(encoding)))) { + var separator = getSeparator(file); + var encoding = UniversalDetector.detectCharset(file.getInputStream()); + try (Reader reader = new BufferedReader(new InputStreamReader(new UnicodeBOMInputStream(file.getInputStream()), Charset.forName(encoding)))) { CsvToBean csvToBean = new CsvToBeanBuilder(reader) .withSeparator(separator) @@ -145,9 +145,7 @@ private List mapToBean(MultipartFile file, Class beans = csvToBean.parse(); - reader.close(); - return beans; + return csvToBean.parse(); } catch (Exception ex) { throw new CreateCertificateException(INVALID_CSV); @@ -155,8 +153,8 @@ private List mapToBean(MultipartFile file, Class mapToCreateDtos(List csvB } private boolean areCreateCertificateRequestsValid(List createDtos, List csvBeans) { - boolean hasError = false; - for (int i = 0; i < createDtos.size(); i++) { - CertificateCreateDto createDto = createDtos.get(i); - CertificateCsvBean csvBean = csvBeans.get(i); + var hasError = false; + for (var i = 0; i < createDtos.size(); i++) { + var createDto = createDtos.get(i); + var csvBean = csvBeans.get(i); if (createDto != null && csvBean.getError() == null) { try { validate(createDto); @@ -205,21 +203,21 @@ private boolean areCreateCertificateRequestsValid(List cre private void validate(CertificateCreateDto createDto) { createDto.validate(); if (createDto instanceof RecoveryCertificateCreateDto) { - RecoveryCertificateDataDto dataDto = ((RecoveryCertificateCreateDto) createDto).getRecoveryInfo().get(0); - CountryCode countryCode = valueSetsService.getCountryCode(dataDto.getCountryOfTest(), createDto.getLanguage()); + var dataDto = ((RecoveryCertificateCreateDto) createDto).getRecoveryInfo().get(0); + var 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(0); - CountryCode countryCode = valueSetsService.getCountryCode(dataDto.getMemberStateOfTest(), createDto.getLanguage()); + var dataDto = ((TestCertificateCreateDto) createDto).getTestInfo().get(0); + var countryCode = valueSetsService.getCountryCode(dataDto.getMemberStateOfTest(), createDto.getLanguage()); if (countryCode == null) { throw new CreateCertificateException(INVALID_MEMBER_STATE_OF_TEST); } - valueSetsService.getTestValueSet(dataDto); + valueSetsService.getChAcceptedTestValueSet(dataDto); } else if (createDto instanceof VaccinationCertificateCreateDto) { - VaccinationCertificateDataDto dataDto = ((VaccinationCertificateCreateDto) createDto).getVaccinationInfo().get(0); - CountryCode countryCode = valueSetsService.getCountryCode(dataDto.getCountryOfVaccination(), createDto.getLanguage()); + var dataDto = ((VaccinationCertificateCreateDto) createDto).getVaccinationInfo().get(0); + var countryCode = valueSetsService.getCountryCode(dataDto.getCountryOfVaccination(), createDto.getLanguage()); if (countryCode == null) { throw new CreateCertificateException(INVALID_COUNTRY_OF_VACCINATION); } @@ -227,10 +225,10 @@ private void validate(CertificateCreateDto createDto) { } } - private File writeCsv(List certificateCsvBeans) { - UUID tempId = UUID.randomUUID(); - File file = new File("temp" + tempId + ".csv"); - try (CSVWriter csvWriter = new CSVWriter(new FileWriter(file))) { + private File writeCsv(List certificateCsvBeans) throws IOException { + var tempId = UUID.randomUUID(); + var file = new File("temp" + tempId + ".csv"); + try (var csvWriter = new CSVWriter(new FileWriter(file))) { StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(csvWriter) .withSeparator(';') .withApplyQuotesToAll(false) @@ -238,7 +236,7 @@ private File writeCsv(List certificateCsvBeans) { beanToCsv.write(certificateCsvBeans); return file; } catch (Exception e) { - file.delete(); + Files.delete(file.toPath()); throw new CreateCertificateException(WRITING_RETURN_CSV_FAILED); } } @@ -263,11 +261,11 @@ private Map getPdfMap(List re } private byte[] zipGeneratedCertificates(Map fileNameAndContentMap) throws IOException { - String extension = ".pdf"; - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream(baos); + var extension = ".pdf"; + var baos = new ByteArrayOutputStream(); + var zos = new ZipOutputStream(baos); for (Map.Entry fileNameAndContentEntry : fileNameAndContentMap.entrySet()) { - ZipEntry entry = new ZipEntry(fileNameAndContentEntry.getKey() + extension); + var entry = new ZipEntry(fileNameAndContentEntry.getKey() + extension); entry.setSize(fileNameAndContentEntry.getValue().length); zos.putNextEntry(entry); zos.write(fileNameAndContentEntry.getValue()); @@ -284,7 +282,7 @@ private void logUvci(String uvci) { private void logKpi(String type) { Jwt token = jeapAuthorization.getJeapAuthenticationToken().getToken(); if (token != null && token.getClaimAsString(USER_EXT_ID_CLAIM_KEY) != null) { - LocalDateTime kpiTimestamp = LocalDateTime.now(); + var kpiTimestamp = LocalDateTime.now(); log.info("kpi: {} {} {} {}", kv(KPI_TIMESTAMP_KEY, kpiTimestamp.format(LOG_FORMAT)), kv(KPI_CREATE_CERTIFICATE_SYSTEM_KEY, KPI_SYSTEM_UI), kv(KPI_TYPE_KEY, type), kv(KPI_UUID_KEY, token.getClaimAsString(USER_EXT_ID_CLAIM_KEY))); kpiLogService.log(new KpiData(kpiTimestamp, type, token.getClaimAsString(USER_EXT_ID_CLAIM_KEY))); } diff --git a/src/main/java/ch/admin/bag/covidcertificate/service/ValueSetsLoader.java b/src/main/java/ch/admin/bag/covidcertificate/service/ValueSetsLoader.java index ac54e9e1..de472949 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/service/ValueSetsLoader.java +++ b/src/main/java/ch/admin/bag/covidcertificate/service/ValueSetsLoader.java @@ -8,6 +8,7 @@ import javax.annotation.PostConstruct; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -24,7 +25,8 @@ private void loadValueSets() throws IOException { valueSetsDto = new ValueSetsDto( getCountryCodes(), getVaccinationValueSet(), - getChAcceptedTestValueSet() + getChAcceptedTestValueSet(), + getAllTestValueSet() ); } } @@ -34,11 +36,11 @@ public ValueSetsDto getValueSets() { } private CountryCodes getCountryCodes() throws IOException { - CountryJson deJson = getCountryJson("country-alpha-2-de"); - CountryJson frJson = getCountryJson("country-alpha-2-fr"); - CountryJson itJson = getCountryJson("country-alpha-2-it"); - CountryJson enJson = getCountryJson("country-alpha-2-en"); - CountryJson rmJson = getCountryJson("country-alpha-2-rm"); + var deJson = getCountryJson("country-alpha-2-de"); + var frJson = getCountryJson("country-alpha-2-fr"); + var itJson = getCountryJson("country-alpha-2-it"); + var enJson = getCountryJson("country-alpha-2-en"); + var rmJson = getCountryJson("country-alpha-2-rm"); return new CountryCodes( deJson.getValueSetValues(), enJson.getValueSetValues(), @@ -52,9 +54,14 @@ private List getVaccinationValueSet() throws IOException { return getVaccinationJson().getEntries(); } + private List getAllTestValueSet() throws IOException { + return new ArrayList<>(getTestSetJson() + .getEntries()); + } + + private List getChAcceptedTestValueSet() throws IOException { - return getTestSetJson() - .getEntries() + return getAllTestValueSet() .stream() .filter(TestValueSet::isChAccepted) .collect(Collectors.toList()); @@ -65,17 +72,17 @@ private InputStream getInputStream(String fileName) { } private CountryJson getCountryJson(String fileName) throws IOException { - InputStream is = getInputStream(fileName); + var is = getInputStream(fileName); return objectMapper.readValue(is, CountryJson.class); } private VaccinationSetJson getVaccinationJson() throws IOException { - InputStream is = getInputStream("covid-19-vaccines"); + var is = getInputStream("covid-19-vaccines"); return objectMapper.readValue(is, VaccinationSetJson.class); } private TestSetJson getTestSetJson() throws IOException { - InputStream is = getInputStream("covid-19-tests"); + var is = getInputStream("covid-19-tests"); return objectMapper.readValue(is, TestSetJson.class); } } diff --git a/src/main/java/ch/admin/bag/covidcertificate/service/ValueSetsService.java b/src/main/java/ch/admin/bag/covidcertificate/service/ValueSetsService.java index afb4cba0..8136d04b 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/service/ValueSetsService.java +++ b/src/main/java/ch/admin/bag/covidcertificate/service/ValueSetsService.java @@ -10,6 +10,7 @@ import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -27,7 +28,7 @@ public ValueSetsDto getValueSets() { } public VaccinationValueSet getVaccinationValueSet(String medicinalProductCode) { - VaccinationValueSet vaccinationValueSet = getValueSets() + var vaccinationValueSet = getValueSets() .getVaccinationSets() .stream() .filter(valueSet -> valueSet.getMedicinalProductCode().equals(medicinalProductCode)) @@ -39,19 +40,26 @@ public VaccinationValueSet getVaccinationValueSet(String medicinalProductCode) { return vaccinationValueSet; } - public TestValueSet getTestValueSet(TestCertificateDataDto testCertificateDataDto) { - if(!validPCRTest(testCertificateDataDto) && !validNonPCRTest(testCertificateDataDto)){ + public TestValueSet getAllTestValueSet(String testTypeCode, String manufacturerCode) { + return getTestValueSet(getValueSets().getAllTestValueSets(), testTypeCode, manufacturerCode); + } + + public TestValueSet getChAcceptedTestValueSet(TestCertificateDataDto testCertificateDataDto) { + return getTestValueSet(getValueSets().getChAcceptedTestValueSets(), testCertificateDataDto.getTypeCode(), testCertificateDataDto.getManufacturerCode()); + } + + private TestValueSet getTestValueSet(Collection testValueSets, String testTypeCode, String manufacturerCode) { + if(!validPCRTest(testTypeCode, manufacturerCode) && !validNonPCRTest(testTypeCode, manufacturerCode)){ throw new CreateCertificateException(INVALID_TYP_OF_TEST); } - TestValueSet testValueSet = getValueSets() - .getTestSets() - .stream() - .filter(valueSet -> - (validPCRTest(testCertificateDataDto) && valueSet.getTypeCode().equals(PCR_TYPE_CODE)) || - (validNonPCRTest(testCertificateDataDto) && valueSet.getManufacturerCodeEu().equals(testCertificateDataDto.getManufacturerCode()))) - .findFirst() - .orElse(null); + var testValueSet = testValueSets + .stream() + .filter(valueSet -> + (validPCRTest(testTypeCode, manufacturerCode) && valueSet.getTypeCode().equals(PCR_TYPE_CODE)) || + (validNonPCRTest(testTypeCode, manufacturerCode) && valueSet.getManufacturerCodeEu().equals(manufacturerCode))) + .findFirst() + .orElse(null); if (testValueSet == null) { throw new CreateCertificateException(INVALID_TYP_OF_TEST); @@ -59,14 +67,14 @@ public TestValueSet getTestValueSet(TestCertificateDataDto testCertificateDataDt return testValueSet; } - private boolean validPCRTest(TestCertificateDataDto testCertificateDataDto){ - return Objects.equals(testCertificateDataDto.getTypeCode(), PCR_TYPE_CODE) && !StringUtils.hasText(testCertificateDataDto.getManufacturerCode()); + private boolean validPCRTest(String testTypeCode, String manufacturerCode){ + return Objects.equals(testTypeCode, PCR_TYPE_CODE) && !StringUtils.hasText(manufacturerCode); } - private boolean validNonPCRTest(TestCertificateDataDto testCertificateDataDto){ - return (Objects.equals(testCertificateDataDto.getTypeCode(), NONE_PCR_TYPE_CODE) - || !StringUtils.hasText(testCertificateDataDto.getTypeCode())) - && StringUtils.hasText(testCertificateDataDto.getManufacturerCode()); + private boolean validNonPCRTest(String testTypeCode, String manufacturerCode){ + return (Objects.equals(testTypeCode, NONE_PCR_TYPE_CODE) + || !StringUtils.hasText(testTypeCode)) + && StringUtils.hasText(manufacturerCode); } public CountryCode getCountryCodeEn(String countryShort) { diff --git a/src/main/java/ch/admin/bag/covidcertificate/service/document/CovidPdfCertificateGenerationService.java b/src/main/java/ch/admin/bag/covidcertificate/service/document/CovidPdfCertificateGenerationService.java index f9892a3e..4a6ae3ec 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/service/document/CovidPdfCertificateGenerationService.java +++ b/src/main/java/ch/admin/bag/covidcertificate/service/document/CovidPdfCertificateGenerationService.java @@ -120,7 +120,7 @@ private ResourceBundleMessageSource messageSource() { } - public byte[] generateCovidCertificate(AbstractCertificatePdf data, Barcode barcode) { + public byte[] generateCovidCertificate(AbstractCertificatePdf data, String barcodePayload, LocalDateTime issuedAt) { try { ByteArrayOutputStream stream = new ByteArrayOutputStream(); @@ -140,9 +140,9 @@ public byte[] generateCovidCertificate(AbstractCertificatePdf data, Barcode barc document.add(headerTable(locale, isPartialVaccination)); - Image qrCode = renderQRCode(writer, barcode.getPayload()); + Image qrCode = renderQRCode(writer, barcodePayload); - document.add(mainTable(locale, data, qrCode, isPartialVaccination)); + document.add(mainTable(locale, data, qrCode, isPartialVaccination, issuedAt)); document.add(issuerTable(locale, isPartialVaccination)); @@ -217,12 +217,12 @@ private PdfPTable headerTable(Locale locale, boolean isPartialVaccination) { return table; } - private PdfPTable mainTable(Locale locale, AbstractCertificatePdf data, Image qrCode, boolean isPartialVaccination) { + private PdfPTable mainTable(Locale locale, AbstractCertificatePdf data, Image qrCode, boolean isPartialVaccination, LocalDateTime issuedAt) { float[] pointColumnWidths = {50F, 20F, 30F}; PdfPTable table = new PdfPTable(pointColumnWidths); table.setWidthPercentage(100); - PdfPCell cell = new PdfPCell(addLeftColumn(locale, qrCode, data, isPartialVaccination)); + PdfPCell cell = new PdfPCell(addLeftColumn(locale, qrCode, data, isPartialVaccination, issuedAt)); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setBorder(Rectangle.NO_BORDER); cell.setRowspan(30); @@ -275,7 +275,7 @@ private void addTestData(Locale locale, TestCertificatePdf data, PdfPTable table addLocaleAndEnglishRow(table, locale, "test.country.label", data.getMemberStateOfTest(), data.getMemberStateOfTestEn()); } - private PdfPTable addLeftColumn(Locale locale, Image qrCode, AbstractCertificatePdf data, boolean isPartialVaccination) { + private PdfPTable addLeftColumn(Locale locale, Image qrCode, AbstractCertificatePdf data, boolean isPartialVaccination, LocalDateTime issuedAt) { float[] pointColumnWidths = {50F, 50F}; PdfPTable table = new PdfPTable(pointColumnWidths); @@ -296,7 +296,7 @@ private PdfPTable addLeftColumn(Locale locale, Image qrCode, AbstractCertificate cell2.setPaddingTop(5); table.addCell(cell2); - addQrLabelCell(table, locale, LocalDateTime.now(), isPartialVaccination); + addQrLabelCell(table, locale, issuedAt, isPartialVaccination); addIssuerRow(table, locale, "personalData.title", 20, true, PADDING_LEFT); addNameRow(table, locale, "personalData.name.label", data.getFamilyName() + " " + data.getGivenName(), PADDING_LEFT); diff --git a/src/main/java/ch/admin/bag/covidcertificate/service/domain/AbstractCertificatePdf.java b/src/main/java/ch/admin/bag/covidcertificate/service/domain/AbstractCertificatePdf.java index b91af04e..e8602633 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/service/domain/AbstractCertificatePdf.java +++ b/src/main/java/ch/admin/bag/covidcertificate/service/domain/AbstractCertificatePdf.java @@ -1,6 +1,7 @@ package ch.admin.bag.covidcertificate.service.domain; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.ToString; @@ -8,6 +9,7 @@ @Getter @ToString +@EqualsAndHashCode public abstract class AbstractCertificatePdf { protected String familyName; protected String givenName; diff --git a/src/main/java/ch/admin/bag/covidcertificate/service/domain/TestCertificatePdf.java b/src/main/java/ch/admin/bag/covidcertificate/service/domain/TestCertificatePdf.java index 54d6d0c5..be7c228c 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/service/domain/TestCertificatePdf.java +++ b/src/main/java/ch/admin/bag/covidcertificate/service/domain/TestCertificatePdf.java @@ -5,6 +5,7 @@ import lombok.Getter; import lombok.ToString; +import java.time.LocalDate; import java.time.ZonedDateTime; @Getter @@ -25,7 +26,9 @@ public class TestCertificatePdf extends AbstractCertificatePdf { private final String issuer; public TestCertificatePdf( - CovidCertificatePersonDto personDto, + String familyName, + String givenName, + LocalDate dateOfBirth, String language, String diseaseOrAgentTargetedCode, String diseaseOrAgentTargetedSystem, @@ -40,7 +43,7 @@ public TestCertificatePdf( String issuer, String identifier ) { - super(personDto.getName().getFamilyName(), personDto.getName().getGivenName(), personDto.getDateOfBirth(), identifier, language); + super(familyName, givenName, dateOfBirth, identifier, language); this.diseaseOrAgentTargetedCode = diseaseOrAgentTargetedCode; this.diseaseOrAgentTargetedSystem = diseaseOrAgentTargetedSystem; this.typeOfTest = typeOfTest; diff --git a/src/main/java/ch/admin/bag/covidcertificate/service/domain/VaccinationCertificatePdf.java b/src/main/java/ch/admin/bag/covidcertificate/service/domain/VaccinationCertificatePdf.java index b4bde537..4b818f8f 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/service/domain/VaccinationCertificatePdf.java +++ b/src/main/java/ch/admin/bag/covidcertificate/service/domain/VaccinationCertificatePdf.java @@ -1,6 +1,5 @@ package ch.admin.bag.covidcertificate.service.domain; -import ch.admin.bag.covidcertificate.api.request.CovidCertificatePersonDto; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.ToString; @@ -9,7 +8,7 @@ @Getter @ToString -@EqualsAndHashCode +@EqualsAndHashCode(callSuper = true) public class VaccinationCertificatePdf extends AbstractCertificatePdf { private final String diseaseOrAgentTargetedCode; private final String diseaseOrAgentTargetedSystem; @@ -25,7 +24,9 @@ public class VaccinationCertificatePdf extends AbstractCertificatePdf { private final String issuer; public VaccinationCertificatePdf( - CovidCertificatePersonDto personDto, + String familyName, + String givenName, + LocalDate dateOfBirth, String language, String diseaseOrAgentTargetedCode, String diseaseOrAgentTargetedSystem, @@ -40,7 +41,7 @@ public VaccinationCertificatePdf( String issuer, String identifier ) { - super(personDto.getName().getFamilyName(), personDto.getName().getGivenName(), personDto.getDateOfBirth(), identifier, language); + super(familyName, givenName, dateOfBirth, identifier, language); this.diseaseOrAgentTargetedCode = diseaseOrAgentTargetedCode; this.diseaseOrAgentTargetedSystem = diseaseOrAgentTargetedSystem; this.vaccineProphylaxis = vaccineProphylaxis; diff --git a/src/main/java/ch/admin/bag/covidcertificate/web/controller/CovidCertificateGenerationController.java b/src/main/java/ch/admin/bag/covidcertificate/web/controller/CovidCertificateGenerationController.java index 709ca727..54132c31 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/web/controller/CovidCertificateGenerationController.java +++ b/src/main/java/ch/admin/bag/covidcertificate/web/controller/CovidCertificateGenerationController.java @@ -3,6 +3,9 @@ import ch.admin.bag.covidcertificate.api.request.RecoveryCertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.TestCertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.VaccinationCertificateCreateDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.RecoveryCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.TestCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.VaccinationCertificatePdfGenerateRequestDto; import ch.admin.bag.covidcertificate.api.response.CovidCertificateCreateResponseDto; import ch.admin.bag.covidcertificate.config.security.authentication.ServletJeapAuthorization; import ch.admin.bag.covidcertificate.domain.KpiData; @@ -16,6 +19,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import se.digg.dgc.encoding.BarcodeException; import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; @@ -74,6 +78,26 @@ public CovidCertificateCreateResponseDto createRecoveryCertificate(@Valid @Reque return responseDto; } + @PostMapping("/fromexisting/vaccination") + @PreAuthorize("hasAnyRole('bag-cc-certificatecreator', 'bag-cc-superuser')") + public CovidCertificateCreateResponseDto generateVaccinationPdfFromExistingCertificate(@Valid @RequestBody VaccinationCertificatePdfGenerateRequestDto pdfGenerateRequestDto, HttpServletRequest request) throws BarcodeException { + log.info("Call of Create for vaccination certificate"); + return covidCertificateGenerationService.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + } + + @PostMapping("/fromexisting/test") + @PreAuthorize("hasAnyRole('bag-cc-certificatecreator', 'bag-cc-superuser')") + public CovidCertificateCreateResponseDto generateTestPdfFromExistingCertificate(@Valid @RequestBody TestCertificatePdfGenerateRequestDto pdfGenerateRequestDto, HttpServletRequest request) throws BarcodeException { + log.info("Call of Create for test certificate"); + return covidCertificateGenerationService.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + } + + @PostMapping("/fromexisting/recovery") + @PreAuthorize("hasAnyRole('bag-cc-certificatecreator', 'bag-cc-superuser')") + public CovidCertificateCreateResponseDto generateRecoveryPdfFromExistingCertificate(@Valid @RequestBody RecoveryCertificatePdfGenerateRequestDto pdfGenerateRequestDto, HttpServletRequest request) throws BarcodeException { + log.info("Call of Create for recovery certificate"); + return covidCertificateGenerationService.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + } private void logKpi(String type) { Jwt token = jeapAuthorization.getJeapAuthenticationToken().getToken(); if (token != null && token.getClaimAsString(USER_EXT_ID_CLAIM_KEY) != null) { diff --git a/src/main/java/ch/admin/bag/covidcertificate/web/controller/ValueSetsController.java b/src/main/java/ch/admin/bag/covidcertificate/web/controller/ValueSetsController.java index a3cc5556..33a9b656 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/web/controller/ValueSetsController.java +++ b/src/main/java/ch/admin/bag/covidcertificate/web/controller/ValueSetsController.java @@ -1,6 +1,7 @@ package ch.admin.bag.covidcertificate.web.controller; -import ch.admin.bag.covidcertificate.api.valueset.ValueSetsDto; +import ch.admin.bag.covidcertificate.api.mapper.ValueSetsResponseDtoMapper; +import ch.admin.bag.covidcertificate.api.response.ValueSetsResponseDto; import ch.admin.bag.covidcertificate.service.ValueSetsService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -21,10 +22,10 @@ public class ValueSetsController { @GetMapping() @PreAuthorize("hasAnyRole('bag-cc-certificatecreator', 'bag-cc-superuser')") - public ValueSetsDto get(HttpServletRequest request) { + public ValueSetsResponseDto get(HttpServletRequest request) { log.info("Call to get value sets."); securityHelper.authorizeUser(request); - return valueSetsService.getValueSets(); + return ValueSetsResponseDtoMapper.create(valueSetsService.getValueSets()); } } diff --git a/src/test/java/ch/admin/bag/covidcertificate/api/mapper/ValueSetsResponseDtoMapperTest.java b/src/test/java/ch/admin/bag/covidcertificate/api/mapper/ValueSetsResponseDtoMapperTest.java new file mode 100644 index 00000000..66e2e933 --- /dev/null +++ b/src/test/java/ch/admin/bag/covidcertificate/api/mapper/ValueSetsResponseDtoMapperTest.java @@ -0,0 +1,32 @@ +package ch.admin.bag.covidcertificate.api.mapper; + +import ch.admin.bag.covidcertificate.api.valueset.ValueSetsDto; +import com.flextrade.jfixture.JFixture; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ValueSetsResponseDtoMapperTest { + private final JFixture jFixture = new JFixture(); + + @Test + void shouldMapCountryCodes() { + var valueSetDto = jFixture.create(ValueSetsDto.class); + var actual = ValueSetsResponseDtoMapper.create(valueSetDto); + assertEquals(valueSetDto.getCountryCodes(), actual.getCountryCodes()); + } + + @Test + void shouldMapVaccinationValueSets() { + var valueSetDto = jFixture.create(ValueSetsDto.class); + var actual = ValueSetsResponseDtoMapper.create(valueSetDto); + assertEquals(valueSetDto.getVaccinationSets(), actual.getVaccinationSets()); + } + + @Test + void shouldMapTestValueSets() { + var valueSetDto = jFixture.create(ValueSetsDto.class); + var actual = ValueSetsResponseDtoMapper.create(valueSetDto); + assertEquals(valueSetDto.getChAcceptedTestValueSets(), actual.getTestSets()); + } +} \ No newline at end of file diff --git a/src/test/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/RecoveryCertificatePdfGenerateRequestDtoMapperTest.java b/src/test/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/RecoveryCertificatePdfGenerateRequestDtoMapperTest.java new file mode 100644 index 00000000..4b0d74b5 --- /dev/null +++ b/src/test/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/RecoveryCertificatePdfGenerateRequestDtoMapperTest.java @@ -0,0 +1,108 @@ +package ch.admin.bag.covidcertificate.api.mapper.pdfgeneration; + +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.RecoveryCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.service.domain.CovidCertificateDiseaseOrAgentTargeted; +import ch.admin.bag.covidcertificate.service.domain.RecoveryCertificatePdf; +import com.flextrade.jfixture.JFixture; +import org.junit.Test; + +import static ch.admin.bag.covidcertificate.api.Constants.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class RecoveryCertificatePdfGenerateRequestDtoMapperTest { + + private final JFixture fixture = new JFixture(); + private final String countryOfTest = "Schweiz"; + private final String countryOfTestEn = "Switzerland"; + + @Test + public void mapsFamilyName() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(incoming.getDecodedCert().getPersonData().getName().getFamilyName(), actual.getFamilyName()); + } + + @Test + public void mapsGivenName() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(incoming.getDecodedCert().getPersonData().getName().getGivenName(), actual.getGivenName()); + } + + @Test + public void mapsDateOfBirth() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(incoming.getDecodedCert().getPersonData().getDateOfBirth(), actual.getDateOfBirth()); + } + + @Test + public void mapsLanguage() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(incoming.getLanguage(), actual.getLanguage()); + } + + @Test + public void mapsDiseaseOrAgentTargetedCode() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(CovidCertificateDiseaseOrAgentTargeted.getStandardInstance().getCode(), actual.getDiseaseOrAgentTargetedCode()); + } + + @Test + public void mapsDiseaseOrAgentTargetedSystem() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(CovidCertificateDiseaseOrAgentTargeted.getStandardInstance().getSystem(), actual.getDiseaseOrAgentTargetedSystem()); + } + + @Test + public void mapsDateOfFirstPositiveTestResult() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(incoming.getDecodedCert().getRecoveryInfo().get(0).getDateOfFirstPositiveTestResult(), actual.getDateOfFirstPositiveTestResult()); + } + + @Test + public void mapsCountryOfTest() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(countryOfTest, actual.getCountryOfTest()); + } + + @Test + public void mapsCountryOfTestEn() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(countryOfTestEn, actual.getCountryOfTestEn()); + } + + @Test + public void mapsIssuer() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(ISSUER, actual.getIssuer()); + } + + @Test + public void mapsValidFrom() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(incoming.getDecodedCert().getRecoveryInfo().get(0).getDateOfFirstPositiveTestResult().plusDays(DAYS_UNTIL_RECOVERY_VALID), actual.getValidFrom()); + } + + @Test + public void mapsValidUntil() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(incoming.getDecodedCert().getRecoveryInfo().get(0).getDateOfFirstPositiveTestResult().plusDays(RECOVERY_CERTIFICATE_VALIDITY_IN_DAYS), actual.getValidUntil()); + } + + @Test + public void mapsUvci() { + RecoveryCertificatePdfGenerateRequestDto incoming = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + RecoveryCertificatePdf actual = RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(incoming, countryOfTest, countryOfTestEn); + assertEquals(incoming.getDecodedCert().getRecoveryInfo().get(0).getIdentifier(), actual.getIdentifier()); + } +} \ No newline at end of file diff --git a/src/test/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/TestCertificatePdfGenerateRequestDtoMapperTest.java b/src/test/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/TestCertificatePdfGenerateRequestDtoMapperTest.java new file mode 100644 index 00000000..46daeaef --- /dev/null +++ b/src/test/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/TestCertificatePdfGenerateRequestDtoMapperTest.java @@ -0,0 +1,117 @@ +package ch.admin.bag.covidcertificate.api.mapper.pdfgeneration; + +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.TestCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.valueset.NegativeTestResult; +import ch.admin.bag.covidcertificate.api.valueset.TestValueSet; +import ch.admin.bag.covidcertificate.service.domain.CovidCertificateDiseaseOrAgentTargeted; +import ch.admin.bag.covidcertificate.service.domain.TestCertificatePdf; +import com.flextrade.jfixture.JFixture; +import org.junit.Test; + +import static ch.admin.bag.covidcertificate.api.Constants.ISSUER; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestCertificatePdfGenerateRequestDtoMapperTest { + + private final JFixture fixture = new JFixture(); + private final TestCertificatePdfGenerateRequestDto incoming = fixture.create(TestCertificatePdfGenerateRequestDto.class); + private final TestValueSet testValueSet = fixture.create(TestValueSet.class); + private final String memberStateOfTest = "Schweiz"; + private final String memberStateOfTestEn = "Switzerland"; + + @Test + public void mapsFamilyName() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(incoming.getDecodedCert().getPersonData().getName().getFamilyName(), actual.getFamilyName()); + } + + @Test + public void mapsGivenName() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(incoming.getDecodedCert().getPersonData().getName().getGivenName(), actual.getGivenName()); + } + + @Test + public void mapsDateOfBirth() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(incoming.getDecodedCert().getPersonData().getDateOfBirth(), actual.getDateOfBirth()); + } + + @Test + public void mapsLanguage() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(incoming.getLanguage(), actual.getLanguage()); + } + + @Test + public void mapsDiseaseOrAgentTargetedCode() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(CovidCertificateDiseaseOrAgentTargeted.getStandardInstance().getCode(), actual.getDiseaseOrAgentTargetedCode()); + } + + @Test + public void mapsDiseaseOrAgentTargetedSystem() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(CovidCertificateDiseaseOrAgentTargeted.getStandardInstance().getSystem(), actual.getDiseaseOrAgentTargetedSystem()); + } + + @Test + public void mapsTypeOfTest() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(testValueSet.getType(), actual.getTypeOfTest()); + } + + @Test + public void mapsTestName() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(testValueSet.getName(), actual.getTestName()); + } + + @Test + public void mapsTestManufacturer() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(testValueSet.getManufacturer(), actual.getTestManufacturer()); + } + + @Test + public void mapsSampleDateTime() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(incoming.getDecodedCert().getTestInfo().get(0).getSampleDateTime(), actual.getSampleDateTime()); + } + + @Test + public void mapsResult() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(NegativeTestResult.DISPLAY, actual.getResult()); + } + + @Test + public void mapsTestingCentreOrFacility() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(incoming.getDecodedCert().getTestInfo().get(0).getTestingCentreOrFacility(), actual.getTestingCentreOrFacility()); + } + + @Test + public void mapsMemberStateOfTest() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(memberStateOfTest, actual.getMemberStateOfTest()); + } + + @Test + public void mapsMemberStateOfTestEn() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(memberStateOfTestEn, actual.getMemberStateOfTestEn()); + } + + @Test + public void mapsIssuer() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(ISSUER, actual.getIssuer()); + } + + @Test + public void mapsUvci() { + TestCertificatePdf actual = TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(incoming, testValueSet, memberStateOfTest, memberStateOfTestEn); + assertEquals(incoming.getDecodedCert().getTestInfo().get(0).getIdentifier(), actual.getIdentifier()); + } +} \ No newline at end of file diff --git a/src/test/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/VaccinationCertificatePdfGenerateRequestDtoMapperTest.java b/src/test/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/VaccinationCertificatePdfGenerateRequestDtoMapperTest.java new file mode 100644 index 00000000..082aa545 --- /dev/null +++ b/src/test/java/ch/admin/bag/covidcertificate/api/mapper/pdfgeneration/VaccinationCertificatePdfGenerateRequestDtoMapperTest.java @@ -0,0 +1,118 @@ +package ch.admin.bag.covidcertificate.api.mapper.pdfgeneration; + +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.VaccinationCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.valueset.VaccinationValueSet; +import ch.admin.bag.covidcertificate.service.domain.CovidCertificateDiseaseOrAgentTargeted; +import ch.admin.bag.covidcertificate.service.domain.VaccinationCertificatePdf; +import com.flextrade.jfixture.JFixture; +import org.junit.Test; + +import static ch.admin.bag.covidcertificate.api.Constants.ISSUER; +import static org.junit.jupiter.api.Assertions.assertEquals; + + +public class VaccinationCertificatePdfGenerateRequestDtoMapperTest { + + private final JFixture jFixture = new JFixture(); + private final VaccinationCertificatePdfGenerateRequestDto incoming = jFixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + private final VaccinationValueSet vaccinationValueSet = jFixture.create(VaccinationValueSet.class); + private final String countryOfVaccination = "Schweiz"; + private final String countryOfVaccinationEn = "Switzerland"; + + @Test + public void mapsFamilyName() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(incoming.getDecodedCert().getPersonData().getName().getFamilyName(), actual.getFamilyName()); + } + + @Test + public void mapsGivenName() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(incoming.getDecodedCert().getPersonData().getName().getGivenName(), actual.getGivenName()); + } + + @Test + public void mapsDateOfBirth() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(incoming.getDecodedCert().getPersonData().getDateOfBirth(), actual.getDateOfBirth()); + } + + + @Test + public void mapsLanguage() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(incoming.getLanguage(), actual.getLanguage()); + } + + @Test + public void mapsDiseaseOrAgentTargetedCode() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(CovidCertificateDiseaseOrAgentTargeted.getStandardInstance().getCode(), actual.getDiseaseOrAgentTargetedCode()); + } + + @Test + public void mapsDiseaseOrAgentTargetedSystem() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(CovidCertificateDiseaseOrAgentTargeted.getStandardInstance().getSystem(), actual.getDiseaseOrAgentTargetedSystem()); + } + + @Test + public void mapsVaccineProphylaxis() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(vaccinationValueSet.getProphylaxis(), actual.getVaccineProphylaxis()); + } + + @Test + public void mapsMedicinalProduct() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(vaccinationValueSet.getMedicinalProduct(), actual.getMedicinalProduct()); + } + + @Test + public void mapsMarketingAuthorizationHolder() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(vaccinationValueSet.getAuthHolder(), actual.getMarketingAuthorizationHolder()); + } + + @Test + public void mapsNumberOfDoses() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(incoming.getDecodedCert().getVaccinationInfo().get(0).getNumberOfDoses(), actual.getNumberOfDoses()); + } + + @Test + public void mapsTotalNumberOfDoses() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(incoming.getDecodedCert().getVaccinationInfo().get(0).getTotalNumberOfDoses(), actual.getTotalNumberOfDoses()); + } + + @Test + public void mapsVaccinationDate() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(incoming.getDecodedCert().getVaccinationInfo().get(0).getVaccinationDate(), actual.getVaccinationDate()); + } + + @Test + public void mapsCountryOfVaccination() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(countryOfVaccination, actual.getCountryOfVaccination()); + } + + @Test + public void mapsCountryOfVaccinationEn() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(countryOfVaccinationEn, actual.getCountryOfVaccinationEn()); + } + + @Test + public void mapsIssuer() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(ISSUER, actual.getIssuer()); + } + + @Test + public void mapsUvci() { + VaccinationCertificatePdf actual = VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(incoming, vaccinationValueSet, countryOfVaccination, countryOfVaccinationEn); + assertEquals(incoming.getDecodedCert().getVaccinationInfo().get(0).getIdentifier(), actual.getIdentifier()); + } +} \ No newline at end of file diff --git a/src/test/java/ch/admin/bag/covidcertificate/service/CovidCertificateDtoMapperServiceTest.java b/src/test/java/ch/admin/bag/covidcertificate/service/CovidCertificateDtoMapperServiceTest.java index 9c0da678..21c60607 100644 --- a/src/test/java/ch/admin/bag/covidcertificate/service/CovidCertificateDtoMapperServiceTest.java +++ b/src/test/java/ch/admin/bag/covidcertificate/service/CovidCertificateDtoMapperServiceTest.java @@ -1,15 +1,14 @@ package ch.admin.bag.covidcertificate.service; import ch.admin.bag.covidcertificate.api.exception.CreateCertificateException; +import ch.admin.bag.covidcertificate.api.mapper.*; import ch.admin.bag.covidcertificate.api.request.RecoveryCertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.TestCertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.VaccinationCertificateCreateDto; import ch.admin.bag.covidcertificate.api.valueset.CountryCode; import ch.admin.bag.covidcertificate.api.valueset.TestValueSet; import ch.admin.bag.covidcertificate.api.valueset.VaccinationValueSet; -import ch.admin.bag.covidcertificate.service.domain.RecoveryCertificateQrCode; -import ch.admin.bag.covidcertificate.service.domain.TestCertificateQrCode; -import ch.admin.bag.covidcertificate.service.domain.VaccinationCertificateQrCode; +import ch.admin.bag.covidcertificate.service.domain.*; import com.flextrade.jfixture.JFixture; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; @@ -18,6 +17,8 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension; @@ -43,7 +44,7 @@ class CovidCertificateDtoMapperServiceTest { public void setUp() { customizeTestValueSet(fixture); lenient().when(valueSetsService.getVaccinationValueSet(any())).thenReturn(fixture.create(VaccinationValueSet.class)); - lenient().when(valueSetsService.getTestValueSet(any())).thenReturn(fixture.create(TestValueSet.class)); + lenient().when(valueSetsService.getChAcceptedTestValueSet(any())).thenReturn(fixture.create(TestValueSet.class)); lenient().when(valueSetsService.getCountryCode(any(), any())).thenReturn(fixture.create(CountryCode.class)); lenient().when(valueSetsService.getCountryCodeEn(any())).thenReturn(fixture.create(CountryCode.class)); } @@ -69,6 +70,51 @@ void throwsCreateCertificateException_ifValueSetServiceThrowsCreateCertificateEx assertEquals(expected.getError(), exception.getError()); } + + @Test + void shouldMapToVaccinationCertificateQrCode_withCorrectCertificateCreateDto(){ + var createDto = fixture.create(VaccinationCertificateCreateDto.class); + try (MockedStatic vaccinationCertificateQrMapperMock = + Mockito.mockStatic(VaccinationCertificateQrCodeMapper.class)) { + vaccinationCertificateQrMapperMock + .when(() -> VaccinationCertificateQrCodeMapper.toVaccinationCertificateQrCode(any(), any())) + .thenReturn(fixture.create(VaccinationCertificateQrCode.class)); + service.toVaccinationCertificateQrCode(createDto); + + vaccinationCertificateQrMapperMock.verify(()-> + VaccinationCertificateQrCodeMapper.toVaccinationCertificateQrCode(eq(createDto), any())); + } + } + + @Test + void shouldMapToVaccinationCertificateQrCode_withCorrectVaccinationValueSet(){ + var vaccinationValueSet = fixture.create(VaccinationValueSet.class); + when(valueSetsService.getVaccinationValueSet(any())).thenReturn(vaccinationValueSet); + try (MockedStatic vaccinationCertificateQrMapperMock = + Mockito.mockStatic(VaccinationCertificateQrCodeMapper.class)) { + vaccinationCertificateQrMapperMock + .when(() -> VaccinationCertificateQrCodeMapper.toVaccinationCertificateQrCode(any(), any())) + .thenReturn(fixture.create(VaccinationCertificateQrCode.class)); + service.toVaccinationCertificateQrCode(fixture.create(VaccinationCertificateCreateDto.class)); + + vaccinationCertificateQrMapperMock.verify(()-> + VaccinationCertificateQrCodeMapper.toVaccinationCertificateQrCode(any(), eq(vaccinationValueSet))); + } + } + + @Test + void shouldReturnVaccinationCertificateQrCode(){ + var vaccinationCertificateQrCode = fixture.create(VaccinationCertificateQrCode.class); + try (MockedStatic vaccinationCertificateQrMapperMock = + Mockito.mockStatic(VaccinationCertificateQrCodeMapper.class)) { + vaccinationCertificateQrMapperMock + .when(() -> VaccinationCertificateQrCodeMapper.toVaccinationCertificateQrCode(any(), any())) + .thenReturn(vaccinationCertificateQrCode); + var actual = service.toVaccinationCertificateQrCode(fixture.create(VaccinationCertificateCreateDto.class)); + + assertEquals(vaccinationCertificateQrCode, actual); + } + } } @Nested @@ -136,6 +182,100 @@ void throwsInvalidCountryOfVaccination_ifCountryCodeEnValueSetIsNull() { assertEquals(INVALID_COUNTRY_OF_VACCINATION, exception.getError()); } + + @Test + void shouldMapToVaccinationCertificatePdf_withCorrectCertificateCreateDto(){ + var createDto = fixture.create(VaccinationCertificateCreateDto.class); + try (MockedStatic vaccinationCertificatePdfMapperMock = + Mockito.mockStatic(VaccinationCertificatePdfMapper.class)) { + vaccinationCertificatePdfMapperMock + .when(() -> VaccinationCertificatePdfMapper.toVaccinationCertificatePdf(any(), any(), any(), any(), any())) + .thenReturn(fixture.create(VaccinationCertificatePdf.class)); + service.toVaccinationCertificatePdf(createDto, fixture.create(VaccinationCertificateQrCode.class)); + + vaccinationCertificatePdfMapperMock.verify(()-> + VaccinationCertificatePdfMapper.toVaccinationCertificatePdf(eq(createDto), any(), any(), any(), any())); + } + } + + @Test + void shouldMapToVaccinationCertificatePdf_withCorrectVaccinationValueSet(){ + var vaccinationValueSet = fixture.create(VaccinationValueSet.class); + when(valueSetsService.getVaccinationValueSet(any())).thenReturn(vaccinationValueSet); + try (MockedStatic vaccinationCertificatePdfMapperMock = + Mockito.mockStatic(VaccinationCertificatePdfMapper.class)) { + vaccinationCertificatePdfMapperMock + .when(() -> VaccinationCertificatePdfMapper.toVaccinationCertificatePdf(any(), any(), any(), any(), any())) + .thenReturn(fixture.create(VaccinationCertificatePdf.class)); + service.toVaccinationCertificatePdf(fixture.create(VaccinationCertificateCreateDto.class), fixture.create(VaccinationCertificateQrCode.class)); + + vaccinationCertificatePdfMapperMock.verify(()-> + VaccinationCertificatePdfMapper.toVaccinationCertificatePdf(any(), eq(vaccinationValueSet), any(), any(), any())); + } + } + + @Test + void shouldMapToVaccinationCertificatePdf_withCorrectQrCodeData(){ + var qrCodeData = fixture.create(VaccinationCertificateQrCode.class); + try (MockedStatic vaccinationCertificatePdfMapperMock = + Mockito.mockStatic(VaccinationCertificatePdfMapper.class)) { + vaccinationCertificatePdfMapperMock + .when(() -> VaccinationCertificatePdfMapper.toVaccinationCertificatePdf(any(), any(), any(), any(), any())) + .thenReturn(fixture.create(VaccinationCertificatePdf.class)); + service.toVaccinationCertificatePdf(fixture.create(VaccinationCertificateCreateDto.class), qrCodeData); + + vaccinationCertificatePdfMapperMock.verify(()-> + VaccinationCertificatePdfMapper.toVaccinationCertificatePdf(any(), any(), eq(qrCodeData), any(), any())); + } + } + + @Test + void shouldMapToVaccinationCertificatePdf_withCorrectCountryValueSet(){ + var countryValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCode(any(), any())).thenReturn(countryValueSet); + try (MockedStatic vaccinationCertificatePdfMapperMock = + Mockito.mockStatic(VaccinationCertificatePdfMapper.class)) { + vaccinationCertificatePdfMapperMock + .when(() -> VaccinationCertificatePdfMapper.toVaccinationCertificatePdf(any(), any(), any(), any(), any())) + .thenReturn(fixture.create(VaccinationCertificatePdf.class)); + service.toVaccinationCertificatePdf(fixture.create(VaccinationCertificateCreateDto.class), fixture.create(VaccinationCertificateQrCode.class)); + + vaccinationCertificatePdfMapperMock.verify(()-> + VaccinationCertificatePdfMapper.toVaccinationCertificatePdf(any(), any(), any(), eq(countryValueSet.getDisplay()), any())); + } + } + + @Test + void shouldMapToVaccinationCertificatePdf_withCorrectCountryEnValueSet(){ + var countryEnValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCodeEn(any())).thenReturn(countryEnValueSet); + try (MockedStatic vaccinationCertificatePdfMapperMock = + Mockito.mockStatic(VaccinationCertificatePdfMapper.class)) { + vaccinationCertificatePdfMapperMock + .when(() -> VaccinationCertificatePdfMapper.toVaccinationCertificatePdf(any(), any(), any(), any(), any())) + .thenReturn(fixture.create(VaccinationCertificatePdf.class)); + service.toVaccinationCertificatePdf(fixture.create(VaccinationCertificateCreateDto.class), fixture.create(VaccinationCertificateQrCode.class)); + + vaccinationCertificatePdfMapperMock.verify(()-> + VaccinationCertificatePdfMapper.toVaccinationCertificatePdf(any(), any(), any(), any(), eq(countryEnValueSet.getDisplay()))); + } + } + + @Test + void shouldReturnVaccinationCertificatePdf(){ + var vaccinationCertificatePdf = fixture.create(VaccinationCertificatePdf.class); + var countryEnValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCodeEn(any())).thenReturn(countryEnValueSet); + try (MockedStatic vaccinationCertificatePdfMapperMock = + Mockito.mockStatic(VaccinationCertificatePdfMapper.class)) { + vaccinationCertificatePdfMapperMock + .when(() -> VaccinationCertificatePdfMapper.toVaccinationCertificatePdf(any(), any(), any(), any(), any())) + .thenReturn(vaccinationCertificatePdf); + var actual = service.toVaccinationCertificatePdf(fixture.create(VaccinationCertificateCreateDto.class), fixture.create(VaccinationCertificateQrCode.class)); + + assertEquals(vaccinationCertificatePdf, actual); + } + } } @@ -145,14 +285,14 @@ class ToTestCertificateQrCode { void shouldLoadTestValueSet() { var createDto = fixture.create(TestCertificateCreateDto.class); service.toTestCertificateQrCode(createDto); - verify(valueSetsService).getTestValueSet(createDto.getTestInfo().get(0)); + verify(valueSetsService).getChAcceptedTestValueSet(createDto.getTestInfo().get(0)); } @Test void throwsCreateCertificateException_ifValueSetServiceThrowsCreateCertificateException() { var createDto = fixture.create(TestCertificateCreateDto.class); var expected = fixture.create(CreateCertificateException.class); - lenient().when(valueSetsService.getTestValueSet(any())).thenThrow(expected); + lenient().when(valueSetsService.getChAcceptedTestValueSet(any())).thenThrow(expected); CreateCertificateException exception = assertThrows(CreateCertificateException.class, () -> service.toTestCertificateQrCode(createDto) @@ -160,6 +300,52 @@ void throwsCreateCertificateException_ifValueSetServiceThrowsCreateCertificateEx assertEquals(expected.getError(), exception.getError()); } + + + @Test + void shouldMapToTestCertificateQrCode_withCorrectCertificateCreateDto(){ + var createDto = fixture.create(TestCertificateCreateDto.class); + try (MockedStatic vaccinationCertificateQrMapperMock = + Mockito.mockStatic(TestCertificateQrCodeMapper.class)) { + vaccinationCertificateQrMapperMock + .when(() -> TestCertificateQrCodeMapper.toTestCertificateQrCode(any(), any())) + .thenReturn(fixture.create(TestCertificateQrCode.class)); + service.toTestCertificateQrCode(createDto); + + vaccinationCertificateQrMapperMock.verify(()-> + TestCertificateQrCodeMapper.toTestCertificateQrCode(eq(createDto), any())); + } + } + + @Test + void shouldMapToTestCertificateQrCode_withCorrectTestValueSet(){ + var vaccinationValueSet = fixture.create(TestValueSet.class); + when(valueSetsService.getChAcceptedTestValueSet(any())).thenReturn(vaccinationValueSet); + try (MockedStatic vaccinationCertificateQrMapperMock = + Mockito.mockStatic(TestCertificateQrCodeMapper.class)) { + vaccinationCertificateQrMapperMock + .when(() -> TestCertificateQrCodeMapper.toTestCertificateQrCode(any(), any())) + .thenReturn(fixture.create(TestCertificateQrCode.class)); + service.toTestCertificateQrCode(fixture.create(TestCertificateCreateDto.class)); + + vaccinationCertificateQrMapperMock.verify(()-> + TestCertificateQrCodeMapper.toTestCertificateQrCode(any(), eq(vaccinationValueSet))); + } + } + + @Test + void shouldReturnTestCertificateQrCode(){ + var vaccinationCertificateQrCode = fixture.create(TestCertificateQrCode.class); + try (MockedStatic vaccinationCertificateQrMapperMock = + Mockito.mockStatic(TestCertificateQrCodeMapper.class)) { + vaccinationCertificateQrMapperMock + .when(() -> TestCertificateQrCodeMapper.toTestCertificateQrCode(any(), any())) + .thenReturn(vaccinationCertificateQrCode); + var actual = service.toTestCertificateQrCode(fixture.create(TestCertificateCreateDto.class)); + + assertEquals(vaccinationCertificateQrCode, actual); + } + } } @Nested @@ -169,7 +355,7 @@ void shouldLoadTestValueSet() { var createDto = fixture.create(TestCertificateCreateDto.class); var qrCodeData = fixture.create(TestCertificateQrCode.class); service.toTestCertificatePdf(createDto, qrCodeData); - verify(valueSetsService).getTestValueSet(createDto.getTestInfo().get(0)); + verify(valueSetsService).getChAcceptedTestValueSet(createDto.getTestInfo().get(0)); } @Test @@ -193,7 +379,7 @@ void throwsCreateCertificateException_ifValueSetServiceThrowsCreateCertificateEx var createDto = fixture.create(TestCertificateCreateDto.class); var qrCodeData = fixture.create(TestCertificateQrCode.class); var expected = fixture.create(CreateCertificateException.class); - lenient().when(valueSetsService.getTestValueSet(any())).thenThrow(expected); + lenient().when(valueSetsService.getChAcceptedTestValueSet(any())).thenThrow(expected); CreateCertificateException exception = assertThrows(CreateCertificateException.class, () -> service.toTestCertificatePdf(createDto, qrCodeData) @@ -227,13 +413,136 @@ void throwsInvalidCountryOfTest_ifCountryCodeEnValueSetIsNull() { assertEquals(INVALID_MEMBER_STATE_OF_TEST, exception.getError()); } + + + @Test + void shouldMapToTestCertificatePdf_withCorrectCertificateCreateDto(){ + var createDto = fixture.create(TestCertificateCreateDto.class); + try (MockedStatic testCertificatePdfMapperMock = + Mockito.mockStatic(TestCertificatePdfMapper.class)) { + testCertificatePdfMapperMock + .when(() -> TestCertificatePdfMapper.toTestCertificatePdf(any(), any(), any(), any(), any())) + .thenReturn(fixture.create(TestCertificatePdf.class)); + service.toTestCertificatePdf(createDto, fixture.create(TestCertificateQrCode.class)); + + testCertificatePdfMapperMock.verify(()-> + TestCertificatePdfMapper.toTestCertificatePdf(eq(createDto), any(), any(), any(), any())); + } + } + + @Test + void shouldMapToTestCertificatePdf_withCorrectTestValueSet(){ + var testValueSet = fixture.create(TestValueSet.class); + when(valueSetsService.getChAcceptedTestValueSet(any())).thenReturn(testValueSet); + try (MockedStatic testCertificatePdfMapperMock = + Mockito.mockStatic(TestCertificatePdfMapper.class)) { + testCertificatePdfMapperMock + .when(() -> TestCertificatePdfMapper.toTestCertificatePdf(any(), any(), any(), any(), any())) + .thenReturn(fixture.create(TestCertificatePdf.class)); + service.toTestCertificatePdf(fixture.create(TestCertificateCreateDto.class), fixture.create(TestCertificateQrCode.class)); + + testCertificatePdfMapperMock.verify(()-> + TestCertificatePdfMapper.toTestCertificatePdf(any(), eq(testValueSet), any(), any(), any())); + } + } + + @Test + void shouldMapToTestCertificatePdf_withCorrectQrCodeData(){ + var qrCodeData = fixture.create(TestCertificateQrCode.class); + try (MockedStatic testCertificatePdfMapperMock = + Mockito.mockStatic(TestCertificatePdfMapper.class)) { + testCertificatePdfMapperMock + .when(() -> TestCertificatePdfMapper.toTestCertificatePdf(any(), any(), any(), any(), any())) + .thenReturn(fixture.create(TestCertificatePdf.class)); + service.toTestCertificatePdf(fixture.create(TestCertificateCreateDto.class), qrCodeData); + + testCertificatePdfMapperMock.verify(()-> + TestCertificatePdfMapper.toTestCertificatePdf(any(), any(), eq(qrCodeData), any(), any())); + } + } + + @Test + void shouldMapToTestCertificatePdf_withCorrectCountryValueSet(){ + var countryValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCode(any(), any())).thenReturn(countryValueSet); + try (MockedStatic testCertificatePdfMapperMock = + Mockito.mockStatic(TestCertificatePdfMapper.class)) { + testCertificatePdfMapperMock + .when(() -> TestCertificatePdfMapper.toTestCertificatePdf(any(), any(), any(), any(), any())) + .thenReturn(fixture.create(TestCertificatePdf.class)); + service.toTestCertificatePdf(fixture.create(TestCertificateCreateDto.class), fixture.create(TestCertificateQrCode.class)); + + testCertificatePdfMapperMock.verify(()-> + TestCertificatePdfMapper.toTestCertificatePdf(any(), any(), any(), eq(countryValueSet.getDisplay()), any())); + } + } + + @Test + void shouldMapToTestCertificatePdf_withCorrectCountryEnValueSet(){ + + var countryEnValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCodeEn(any())).thenReturn(countryEnValueSet); + try (MockedStatic testCertificatePdfMapperMock = + Mockito.mockStatic(TestCertificatePdfMapper.class)) { + testCertificatePdfMapperMock + .when(() -> TestCertificatePdfMapper.toTestCertificatePdf(any(), any(), any(), any(), any())) + .thenReturn(fixture.create(TestCertificatePdf.class)); + service.toTestCertificatePdf(fixture.create(TestCertificateCreateDto.class), fixture.create(TestCertificateQrCode.class)); + + testCertificatePdfMapperMock.verify(()-> + TestCertificatePdfMapper.toTestCertificatePdf(any(), any(), any(), any(), eq(countryEnValueSet.getDisplay()))); + } + } + + @Test + void shouldReturnTestCertificatePdf(){ + var testCertificatePdf = fixture.create(TestCertificatePdf.class); + var countryEnValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCodeEn(any())).thenReturn(countryEnValueSet); + try (MockedStatic testCertificatePdfMapperMock = + Mockito.mockStatic(TestCertificatePdfMapper.class)) { + testCertificatePdfMapperMock + .when(() -> TestCertificatePdfMapper.toTestCertificatePdf(any(), any(), any(), any(), any())) + .thenReturn(testCertificatePdf); + var actual = service.toTestCertificatePdf(fixture.create(TestCertificateCreateDto.class), fixture.create(TestCertificateQrCode.class)); + + assertEquals(testCertificatePdf, actual); + } + } } @Nested class ToRecoveryCertificateQrCode { + @Test + void shouldMapToRecoveryCertificateQrCode_withCorrectCertificateCreateDto(){ + var createDto = fixture.create(RecoveryCertificateCreateDto.class); + try (MockedStatic vaccinationCertificateQrMapperMock = + Mockito.mockStatic(RecoveryCertificateQrCodeMapper.class)) { + vaccinationCertificateQrMapperMock + .when(() -> RecoveryCertificateQrCodeMapper.toRecoveryCertificateQrCode(any())) + .thenReturn(fixture.create(RecoveryCertificateQrCode.class)); + service.toRecoveryCertificateQrCode(createDto); + + vaccinationCertificateQrMapperMock.verify(()-> + RecoveryCertificateQrCodeMapper.toRecoveryCertificateQrCode(eq(createDto))); + } + } + @Test + void shouldReturnRecoveryCertificateQrCode(){ + var vaccinationCertificateQrCode = fixture.create(RecoveryCertificateQrCode.class); + try (MockedStatic vaccinationCertificateQrMapperMock = + Mockito.mockStatic(RecoveryCertificateQrCodeMapper.class)) { + vaccinationCertificateQrMapperMock + .when(() -> RecoveryCertificateQrCodeMapper.toRecoveryCertificateQrCode(any())) + .thenReturn(vaccinationCertificateQrCode); + var actual = service.toRecoveryCertificateQrCode(fixture.create(RecoveryCertificateCreateDto.class)); + + assertEquals(vaccinationCertificateQrCode, actual); + } + } } @Nested @@ -279,105 +588,83 @@ void throwsInvalidCountryOfRecovery_ifCountryCodeEnValueSetIsNull() { assertEquals(INVALID_COUNTRY_OF_TEST, exception.getError()); } + + @Test + void shouldMapToRecoveryCertificatePdf_withCorrectCertificateCreateDto(){ + var createDto = fixture.create(RecoveryCertificateCreateDto.class); + try (MockedStatic recoveryCertificatePdfMapperMock = + Mockito.mockStatic(RecoveryCertificatePdfMapper.class)) { + recoveryCertificatePdfMapperMock + .when(() -> RecoveryCertificatePdfMapper.toRecoveryCertificatePdf(any(), any(), any(), any())) + .thenReturn(fixture.create(RecoveryCertificatePdf.class)); + service.toRecoveryCertificatePdf(createDto, fixture.create(RecoveryCertificateQrCode.class)); + + recoveryCertificatePdfMapperMock.verify(()-> + RecoveryCertificatePdfMapper.toRecoveryCertificatePdf(eq(createDto), any(), any(), any())); + } + } + + @Test + void shouldMapToRecoveryCertificatePdf_withCorrectQrCodeData(){ + var qrCodeData = fixture.create(RecoveryCertificateQrCode.class); + try (MockedStatic recoveryCertificatePdfMapperMock = + Mockito.mockStatic(RecoveryCertificatePdfMapper.class)) { + recoveryCertificatePdfMapperMock + .when(() -> RecoveryCertificatePdfMapper.toRecoveryCertificatePdf(any(), any(), any(), any())) + .thenReturn(fixture.create(RecoveryCertificatePdf.class)); + service.toRecoveryCertificatePdf(fixture.create(RecoveryCertificateCreateDto.class), qrCodeData); + + recoveryCertificatePdfMapperMock.verify(()-> + RecoveryCertificatePdfMapper.toRecoveryCertificatePdf(any(), eq(qrCodeData), any(), any())); + } + } + + @Test + void shouldMapToRecoveryCertificatePdf_withCorrectCountryValueSet(){ + var countryValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCode(any(), any())).thenReturn(countryValueSet); + try (MockedStatic recoveryCertificatePdfMapperMock = + Mockito.mockStatic(RecoveryCertificatePdfMapper.class)) { + recoveryCertificatePdfMapperMock + .when(() -> RecoveryCertificatePdfMapper.toRecoveryCertificatePdf(any(), any(), any(), any())) + .thenReturn(fixture.create(RecoveryCertificatePdf.class)); + service.toRecoveryCertificatePdf(fixture.create(RecoveryCertificateCreateDto.class), fixture.create(RecoveryCertificateQrCode.class)); + + recoveryCertificatePdfMapperMock.verify(()-> + RecoveryCertificatePdfMapper.toRecoveryCertificatePdf(any(), any(), eq(countryValueSet.getDisplay()), any())); + } + } + + @Test + void shouldMapToRecoveryCertificatePdf_withCorrectCountryEnValueSet(){ + var countryEnValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCodeEn(any())).thenReturn(countryEnValueSet); + try (MockedStatic recoveryCertificatePdfMapperMock = + Mockito.mockStatic(RecoveryCertificatePdfMapper.class)) { + recoveryCertificatePdfMapperMock + .when(() -> RecoveryCertificatePdfMapper.toRecoveryCertificatePdf(any(), any(), any(), any())) + .thenReturn(fixture.create(RecoveryCertificatePdf.class)); + service.toRecoveryCertificatePdf(fixture.create(RecoveryCertificateCreateDto.class), fixture.create(RecoveryCertificateQrCode.class)); + + recoveryCertificatePdfMapperMock.verify(()-> + RecoveryCertificatePdfMapper.toRecoveryCertificatePdf(any(), any(), any(), eq(countryEnValueSet.getDisplay()))); + } + } + + @Test + void shouldReturnRecoveryCertificatePdf(){ + var recoveryCertificatePdf = fixture.create(RecoveryCertificatePdf.class); + var countryEnValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCodeEn(any())).thenReturn(countryEnValueSet); + try (MockedStatic recoveryCertificatePdfMapperMock = + Mockito.mockStatic(RecoveryCertificatePdfMapper.class)) { + recoveryCertificatePdfMapperMock + .when(() -> RecoveryCertificatePdfMapper.toRecoveryCertificatePdf(any(), any(), any(), any())) + .thenReturn(recoveryCertificatePdf); + var actual = service.toRecoveryCertificatePdf(fixture.create(RecoveryCertificateCreateDto.class), fixture.create(RecoveryCertificateQrCode.class)); + + assertEquals(recoveryCertificatePdf, actual); + } + } } -// -// @Nested -// class GenerateRecoveryCovidCertificate { -// @Test -// void shouldLoadCountryCodeValueSetForSelectedLanguage() throws IOException { -// var createDto = fixture.create(RecoveryCertificateCreateDto.class); -// service.generateCovidCertificate(createDto); -// verify(valueSetsService).getCountryCode(createDto.getRecoveryInfo().get(0).getCountryOfTest(), createDto.getLanguage()); -// } -// -// @Test -// void shouldLoadCountryCodeEnValueSet() throws IOException { -// var createDto = fixture.create(RecoveryCertificateCreateDto.class); -// service.generateCovidCertificate(createDto); -// verify(valueSetsService).getCountryCodeEn(createDto.getRecoveryInfo().get(0).getCountryOfTest()); -// } -// -// @Test -// void shouldCreateBarcode() throws IOException { -// var createDto = fixture.create(RecoveryCertificateCreateDto.class); -// var contents = fixture.create(String.class); -// var objectWriter = mock(ObjectWriter.class); -// when(objectMapper.writer()).thenReturn(objectWriter); -// lenient().when(objectWriter.writeValueAsString(any())).thenReturn(contents); -// -// service.generateCovidCertificate(createDto); -// -// verify(barcodeService).createBarcode(contents); -// } -// -// @Test -// void shouldCreatePdf() throws IOException { -// var createDto = fixture.create(RecoveryCertificateCreateDto.class); -// var qrCodeData = RecoveryCertificateQrCodeMapper.toRecoveryCertificateQrCode(createDto); -// var countryCode = fixture.create(CountryCode.class); -// var countryCodeEn = fixture.create(CountryCode.class); -// var recoveryPdf = RecoveryCertificatePdfMapper.toRecoveryCertificatePdf(createDto, qrCodeData, countryCode.getDisplay(), countryCodeEn.getDisplay()); -// var barcode = fixture.create(Barcode.class); -// -// when(valueSetsService.getCountryCode(createDto.getRecoveryInfo().get(0).getCountryOfTest(), createDto.getLanguage())).thenReturn(countryCode); -// when(valueSetsService.getCountryCodeEn(createDto.getRecoveryInfo().get(0).getCountryOfTest())).thenReturn(countryCodeEn); -// when(barcodeService.createBarcode(any())).thenReturn(barcode); -// -// service.generateCovidCertificate(createDto); -// -// verify(covidPdfCertificateGenerationService).generateCovidCertificate(recoveryPdf, barcode); -// } -// -// @Test -// void shouldReturnBarcode() throws IOException { -// var createDto = fixture.create(RecoveryCertificateCreateDto.class); -// var barcode = fixture.create(Barcode.class); -// when(barcodeService.createBarcode(any())).thenReturn(barcode); -// -// var actual = service.generateCovidCertificate(createDto); -// -// assertEquals(barcode.getImage(), actual.getQrCode()); -// } -// -// @Test -// void shouldReturnPdf() throws IOException { -// var createDto = fixture.create(RecoveryCertificateCreateDto.class); -// var pdf = fixture.create(byte[].class); -// when(covidPdfCertificateGenerationService.generateCovidCertificate(any(), any())).thenReturn(pdf); -// -// var actual = service.generateCovidCertificate(createDto); -// -// assertEquals(pdf, actual.getPdf()); -// } -// -// @Test -// void shouldUVCI() throws IOException { -// var createDto = fixture.create(RecoveryCertificateCreateDto.class); -// -// var actual = service.generateCovidCertificate(createDto); -// -// assertNotNull(actual.getUvci()); -// } -// -// -// @Test -// void throwsInvalidCountryOfRecovery_ifCountryCodeValueSetIsNull() { -// var createDto = fixture.create(RecoveryCertificateCreateDto.class); -// when(valueSetsService.getCountryCode(any(), any())).thenReturn(null); -// -// CreateCertificateException exception = assertThrows(CreateCertificateException.class, () -> service.generateCovidCertificate(createDto)); -// -// assertEquals(INVALID_COUNTRY_OF_TEST, exception.getError()); -// } -// -// @Test -// void throwsInvalidCountryOfRecovery_ifCountryCodeEnValueSetIsNull() { -// var createDto = fixture.create(RecoveryCertificateCreateDto.class); -// when(valueSetsService.getCountryCodeEn(any())).thenReturn(null); -// -// CreateCertificateException exception = assertThrows(CreateCertificateException.class, () -> service.generateCovidCertificate(createDto)); -// -// assertEquals(INVALID_COUNTRY_OF_TEST, exception.getError()); -// } -// } } \ No newline at end of file diff --git a/src/test/java/ch/admin/bag/covidcertificate/service/CovidCertificateGenerationServiceTest.java b/src/test/java/ch/admin/bag/covidcertificate/service/CovidCertificateGenerationServiceTest.java index 5d0bccb5..1ff6a7ff 100644 --- a/src/test/java/ch/admin/bag/covidcertificate/service/CovidCertificateGenerationServiceTest.java +++ b/src/test/java/ch/admin/bag/covidcertificate/service/CovidCertificateGenerationServiceTest.java @@ -1,6 +1,9 @@ package ch.admin.bag.covidcertificate.service; import ch.admin.bag.covidcertificate.api.exception.CreateCertificateException; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.RecoveryCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.TestCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.VaccinationCertificatePdfGenerateRequestDto; import ch.admin.bag.covidcertificate.client.inapp_delivery.InAppDeliveryClient; import ch.admin.bag.covidcertificate.client.printing.PrintQueueClient; import ch.admin.bag.covidcertificate.service.document.CovidPdfCertificateGenerationService; @@ -15,11 +18,20 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension; import se.digg.dgc.encoding.Barcode; +import se.digg.dgc.encoding.BarcodeException; +import se.digg.dgc.encoding.impl.DefaultBarcodeCreator; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; import static ch.admin.bag.covidcertificate.TestModelProvider.*; import static org.junit.jupiter.api.Assertions.*; @@ -39,6 +51,8 @@ class CovidCertificateGenerationServiceTest { @Mock private CovidCertificateDtoMapperService covidCertificateDtoMapperService; @Mock + private CovidCertificatePdfGenerateRequestDtoMapperService covidCertificatePdfGenerateRequestDtoMapperService; + @Mock private ObjectMapper objectMapper; @Mock private PrintQueueClient printQueueClient; @@ -50,7 +64,7 @@ class CovidCertificateGenerationServiceTest { @BeforeEach public void setUp() throws IOException { lenient().when(barcodeService.createBarcode(any())).thenReturn(fixture.create(Barcode.class)); - lenient().when(covidPdfCertificateGenerationService.generateCovidCertificate(any(), any())).thenReturn(fixture.create(byte[].class)); + lenient().when(covidPdfCertificateGenerationService.generateCovidCertificate(any(), any(), any())).thenReturn(fixture.create(byte[].class)); lenient().when(covidCertificateDtoMapperService.toVaccinationCertificateQrCode(any())).thenReturn(fixture.create(VaccinationCertificateQrCode.class)); lenient().when(covidCertificateDtoMapperService.toVaccinationCertificatePdf(any(), any())).thenReturn(fixture.create(VaccinationCertificatePdf.class)); @@ -64,6 +78,255 @@ public void setUp() throws IOException { lenient().when(objectWriter.writeValueAsString(any())).thenReturn(fixture.create(String.class)); } + @Nested + class GenerateVaccinationFromExistingCovidCertificate { + @Test + void shouldMapDtoToVaccinationCertificatePdf() throws BarcodeException { + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + verify(covidCertificatePdfGenerateRequestDtoMapperService).toVaccinationCertificatePdf(pdfGenerateRequestDto); + } + + @Test + void throwsCreateCertificateException_ifMapDtoToVaccinationCertificatePdfThrowsCreateCertificateException() { + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + var expected = fixture.create(CreateCertificateException.class); + when(covidCertificatePdfGenerateRequestDtoMapperService.toVaccinationCertificatePdf(any())).thenThrow(expected); + + CreateCertificateException exception = assertThrows(CreateCertificateException.class, + () -> service.generateFromExistingCovidCertificate(pdfGenerateRequestDto) + ); + + assertEquals(expected.getError(), exception.getError()); + } + + @Test + void shouldCreatePdf_withCorrectPdfData() throws BarcodeException { + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + var vaccinationPdf = fixture.create(VaccinationCertificatePdf.class); + when(covidCertificatePdfGenerateRequestDtoMapperService.toVaccinationCertificatePdf(any())).thenReturn(vaccinationPdf); + + service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + verify(covidPdfCertificateGenerationService).generateCovidCertificate(eq(vaccinationPdf), any(), any()); + } + + @Test + void shouldCreatePdf_withCorrectBarcode() throws BarcodeException { + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + var barcode = new DefaultBarcodeCreator().create(pdfGenerateRequestDto.getHcert(), StandardCharsets.US_ASCII); + + service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + verify(covidPdfCertificateGenerationService).generateCovidCertificate(any(), eq(barcode.getPayload()), any()); + } + + @Test + void shouldCreatePdf_withCorrectIssuedAt() throws BarcodeException { + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + var issuedAt = getLocalDateTimeFromEpochMillis(pdfGenerateRequestDto.getIssuedAt()); + + service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + verify(covidPdfCertificateGenerationService).generateCovidCertificate(any(), any(), eq(issuedAt)); + } + + @Test + void shouldReturnBarcode() throws IOException, BarcodeException { + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + var barcode = new DefaultBarcodeCreator().create(pdfGenerateRequestDto.getHcert(), StandardCharsets.US_ASCII); + + var actual = service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + assertArrayEquals(barcode.getImage(), actual.getQrCode()); + } + + @Test + void shouldReturnPdf() throws IOException, BarcodeException { + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + var pdf = fixture.create(byte[].class); + when(covidPdfCertificateGenerationService.generateCovidCertificate(any(), any(), any())).thenReturn(pdf); + + var actual = service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + assertEquals(pdf, actual.getPdf()); + } + + @Test + void shouldUVCI() throws IOException, BarcodeException { + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + var actual = service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + assertNotNull(actual.getUvci()); + } + } + + @Nested + class GenerateTestFromExistingCovidCertificate { + @Test + void shouldMapDtoToTestCertificatePdf() throws BarcodeException { + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + verify(covidCertificatePdfGenerateRequestDtoMapperService).toTestCertificatePdf(pdfGenerateRequestDto); + } + + @Test + void throwsCreateCertificateException_ifMapDtoToTestCertificatePdfThrowsCreateCertificateException() { + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + var expected = fixture.create(CreateCertificateException.class); + when(covidCertificatePdfGenerateRequestDtoMapperService.toTestCertificatePdf(any())).thenThrow(expected); + + CreateCertificateException exception = assertThrows(CreateCertificateException.class, + () -> service.generateFromExistingCovidCertificate(pdfGenerateRequestDto) + ); + + assertEquals(expected.getError(), exception.getError()); + } + + @Test + void shouldCreatePdf_withCorrectPdfData() throws BarcodeException { + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + var testPdf = fixture.create(TestCertificatePdf.class); + when(covidCertificatePdfGenerateRequestDtoMapperService.toTestCertificatePdf(any())).thenReturn(testPdf); + + service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + verify(covidPdfCertificateGenerationService).generateCovidCertificate(eq(testPdf), any(), any()); + } + + @Test + void shouldCreatePdf_withCorrectBarcode() throws BarcodeException { + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + var barcode = new DefaultBarcodeCreator().create(pdfGenerateRequestDto.getHcert(), StandardCharsets.US_ASCII); + + service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + verify(covidPdfCertificateGenerationService).generateCovidCertificate(any(), eq(barcode.getPayload()), any()); + } + + @Test + void shouldCreatePdf_withCorrectIssuedAt() throws BarcodeException { + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + var issuedAt = getLocalDateTimeFromEpochMillis(pdfGenerateRequestDto.getIssuedAt()); + + service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + verify(covidPdfCertificateGenerationService).generateCovidCertificate(any(), any(), eq(issuedAt)); + } + + @Test + void shouldReturnBarcode() throws IOException, BarcodeException { + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + var barcode = new DefaultBarcodeCreator().create(pdfGenerateRequestDto.getHcert(), StandardCharsets.US_ASCII); + + var actual = service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + assertArrayEquals(barcode.getImage(), actual.getQrCode()); + } + + @Test + void shouldReturnPdf() throws IOException, BarcodeException { + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + var pdf = fixture.create(byte[].class); + when(covidPdfCertificateGenerationService.generateCovidCertificate(any(), any(), any())).thenReturn(pdf); + + var actual = service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + assertEquals(pdf, actual.getPdf()); + } + + @Test + void shouldUVCI() throws IOException, BarcodeException { + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + var actual = service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + assertNotNull(actual.getUvci()); + } + } + + @Nested + class GenerateRecoveryFromExistingCovidCertificate { + @Test + void shouldMapDtoToRecoveryCertificatePdf() throws BarcodeException { + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + verify(covidCertificatePdfGenerateRequestDtoMapperService).toRecoveryCertificatePdf(pdfGenerateRequestDto); + } + + @Test + void throwsCreateCertificateException_ifMapDtoToRecoveryCertificatePdfThrowsCreateCertificateException() { + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + var expected = fixture.create(CreateCertificateException.class); + when(covidCertificatePdfGenerateRequestDtoMapperService.toRecoveryCertificatePdf(any())).thenThrow(expected); + + CreateCertificateException exception = assertThrows(CreateCertificateException.class, + () -> service.generateFromExistingCovidCertificate(pdfGenerateRequestDto) + ); + + assertEquals(expected.getError(), exception.getError()); + } + + @Test + void shouldCreatePdf_withCorrectPdfData() throws BarcodeException { + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + var recoveryPdf = fixture.create(RecoveryCertificatePdf.class); + when(covidCertificatePdfGenerateRequestDtoMapperService.toRecoveryCertificatePdf(any())).thenReturn(recoveryPdf); + + service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + verify(covidPdfCertificateGenerationService).generateCovidCertificate(eq(recoveryPdf), any(), any()); + } + + @Test + void shouldCreatePdf_withCorrectBarcode() throws BarcodeException { + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + var barcode = new DefaultBarcodeCreator().create(pdfGenerateRequestDto.getHcert(), StandardCharsets.US_ASCII); + + service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + verify(covidPdfCertificateGenerationService).generateCovidCertificate(any(), eq(barcode.getPayload()), any()); + } + + @Test + void shouldCreatePdf_withCorrectIssuedAt() throws BarcodeException { + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + var issuedAt = getLocalDateTimeFromEpochMillis(pdfGenerateRequestDto.getIssuedAt()); + + service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + verify(covidPdfCertificateGenerationService).generateCovidCertificate(any(), any(), eq(issuedAt)); + } + + @Test + void shouldReturnBarcode() throws IOException, BarcodeException { + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + var barcode = new DefaultBarcodeCreator().create(pdfGenerateRequestDto.getHcert(), StandardCharsets.US_ASCII); + + var actual = service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + assertArrayEquals(barcode.getImage(), actual.getQrCode()); + } + + @Test + void shouldReturnPdf() throws IOException, BarcodeException { + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + var pdf = fixture.create(byte[].class); + when(covidPdfCertificateGenerationService.generateCovidCertificate(any(), any(), any())).thenReturn(pdf); + + var actual = service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + assertEquals(pdf, actual.getPdf()); + } + + @Test + void shouldUVCI() throws IOException, BarcodeException { + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + var actual = service.generateFromExistingCovidCertificate(pdfGenerateRequestDto); + + assertNotNull(actual.getUvci()); + } + } + @Nested class GenerateVaccinationCovidCertificate { @Test @@ -124,12 +387,16 @@ void shouldCreatePdf() throws IOException { var createDto = getVaccinationCertificateCreateDto("EU/1/20/1507", "de"); var vaccinationPdf = fixture.create(VaccinationCertificatePdf.class); var barcode = fixture.create(Barcode.class); + var now = LocalDateTime.now(); when(covidCertificateDtoMapperService.toVaccinationCertificatePdf(any(), any())).thenReturn(vaccinationPdf); when(barcodeService.createBarcode(any())).thenReturn(barcode); - service.generateCovidCertificate(createDto); + try (MockedStatic localDateTimeMock = Mockito.mockStatic(LocalDateTime.class)) { + localDateTimeMock.when(LocalDateTime::now).thenReturn(now); + service.generateCovidCertificate(createDto); - verify(covidPdfCertificateGenerationService).generateCovidCertificate(vaccinationPdf, barcode); + verify(covidPdfCertificateGenerationService).generateCovidCertificate(vaccinationPdf, barcode.getPayload(), now); + } } @Test @@ -147,7 +414,7 @@ void shouldReturnBarcode() throws IOException { void shouldReturnPdf() throws IOException { var createDto = getVaccinationCertificateCreateDto("EU/1/20/1507", "de"); var pdf = fixture.create(byte[].class); - when(covidPdfCertificateGenerationService.generateCovidCertificate(any(), any())).thenReturn(pdf); + when(covidPdfCertificateGenerationService.generateCovidCertificate(any(), any(), any())).thenReturn(pdf); var actual = service.generateCovidCertificate(createDto); @@ -240,12 +507,17 @@ void shouldCreatePdf() throws IOException { var createDto = getTestCertificateCreateDto(null, "1833", "de"); var TestPdf = fixture.create(TestCertificatePdf.class); var barcode = fixture.create(Barcode.class); + var now = LocalDateTime.now(); when(covidCertificateDtoMapperService.toTestCertificatePdf(any(), any())).thenReturn(TestPdf); when(barcodeService.createBarcode(any())).thenReturn(barcode); - service.generateCovidCertificate(createDto); + try (MockedStatic localDateTimeMock = Mockito.mockStatic(LocalDateTime.class)) { + localDateTimeMock.when(LocalDateTime::now).thenReturn(now); + + service.generateCovidCertificate(createDto); - verify(covidPdfCertificateGenerationService).generateCovidCertificate(TestPdf, barcode); + verify(covidPdfCertificateGenerationService).generateCovidCertificate(TestPdf, barcode.getPayload(), LocalDateTime.now()); + } } @Test @@ -263,7 +535,7 @@ void shouldReturnBarcode() throws IOException { void shouldReturnPdf() throws IOException { var createDto = getTestCertificateCreateDto(null, "1833", "de"); var pdf = fixture.create(byte[].class); - when(covidPdfCertificateGenerationService.generateCovidCertificate(any(), any())).thenReturn(pdf); + when(covidPdfCertificateGenerationService.generateCovidCertificate(any(), any(), any())).thenReturn(pdf); var actual = service.generateCovidCertificate(createDto); @@ -356,12 +628,17 @@ void shouldCreatePdf() throws IOException { var createDto = getRecoveryCertificateCreateDto("de"); var RecoveryPdf = fixture.create(RecoveryCertificatePdf.class); var barcode = fixture.create(Barcode.class); + var now = LocalDateTime.now(); when(covidCertificateDtoMapperService.toRecoveryCertificatePdf(any(), any())).thenReturn(RecoveryPdf); when(barcodeService.createBarcode(any())).thenReturn(barcode); - service.generateCovidCertificate(createDto); + try (MockedStatic localDateTimeMock = Mockito.mockStatic(LocalDateTime.class)) { + localDateTimeMock.when(LocalDateTime::now).thenReturn(now); + + service.generateCovidCertificate(createDto); - verify(covidPdfCertificateGenerationService).generateCovidCertificate(RecoveryPdf, barcode); + verify(covidPdfCertificateGenerationService).generateCovidCertificate(RecoveryPdf, barcode.getPayload(), now); + } } @Test @@ -379,7 +656,7 @@ void shouldReturnBarcode() throws IOException { void shouldReturnPdf() throws IOException { var createDto = getRecoveryCertificateCreateDto("de"); var pdf = fixture.create(byte[].class); - when(covidPdfCertificateGenerationService.generateCovidCertificate(any(), any())).thenReturn(pdf); + when(covidPdfCertificateGenerationService.generateCovidCertificate(any(), any(), any())).thenReturn(pdf); var actual = service.generateCovidCertificate(createDto); @@ -411,4 +688,9 @@ void shouldCallPrintingService__whenAddressPassed() { verify(printQueueClient, times(1)).sendPrintJob(any()); } } + + LocalDateTime getLocalDateTimeFromEpochMillis(long millis) { + var instant = Instant.ofEpochMilli(millis); + return ZonedDateTime.from(instant.atZone(ZoneId.systemDefault())).toLocalDateTime(); + } } diff --git a/src/test/java/ch/admin/bag/covidcertificate/service/CovidCertificatePdfGenerateRequestDtoMapperServiceTest.java b/src/test/java/ch/admin/bag/covidcertificate/service/CovidCertificatePdfGenerateRequestDtoMapperServiceTest.java new file mode 100644 index 00000000..90a45004 --- /dev/null +++ b/src/test/java/ch/admin/bag/covidcertificate/service/CovidCertificatePdfGenerateRequestDtoMapperServiceTest.java @@ -0,0 +1,427 @@ +package ch.admin.bag.covidcertificate.service; + +import ch.admin.bag.covidcertificate.api.exception.CreateCertificateException; +import ch.admin.bag.covidcertificate.api.mapper.pdfgeneration.RecoveryCertificatePdfGenerateRequestDtoMapper; +import ch.admin.bag.covidcertificate.api.mapper.pdfgeneration.TestCertificatePdfGenerateRequestDtoMapper; +import ch.admin.bag.covidcertificate.api.mapper.pdfgeneration.VaccinationCertificatePdfGenerateRequestDtoMapper; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.RecoveryCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.TestCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.VaccinationCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.valueset.CountryCode; +import ch.admin.bag.covidcertificate.api.valueset.TestValueSet; +import ch.admin.bag.covidcertificate.api.valueset.VaccinationValueSet; +import ch.admin.bag.covidcertificate.service.domain.RecoveryCertificatePdf; +import ch.admin.bag.covidcertificate.service.domain.TestCertificatePdf; +import ch.admin.bag.covidcertificate.service.domain.VaccinationCertificatePdf; +import com.flextrade.jfixture.JFixture; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import static ch.admin.bag.covidcertificate.FixtureCustomization.customizeCountryCode; +import static ch.admin.bag.covidcertificate.api.Constants.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class CovidCertificatePdfGenerateRequestDtoMapperServiceTest { + @InjectMocks + private CovidCertificatePdfGenerateRequestDtoMapperService service; + @Mock + private ValueSetsService valueSetsService; + + private final JFixture fixture = new JFixture(); + + @BeforeEach + private void init(){ + customizeCountryCode(fixture); + lenient().when(valueSetsService.getVaccinationValueSet(any())).thenReturn(fixture.create(VaccinationValueSet.class)); + lenient().when(valueSetsService.getAllTestValueSet(any(), any())).thenReturn(fixture.create(TestValueSet.class)); + lenient().when(valueSetsService.getCountryCode(any(), any())).thenReturn(fixture.create(CountryCode.class)); + lenient().when(valueSetsService.getCountryCodeEn(any())).thenReturn(fixture.create(CountryCode.class)); + } + + @Nested + class ToVaccinationCertificatePdf{ + @Test + void shouldLoadVaccinationValueSet(){ + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + service.toVaccinationCertificatePdf(pdfGenerateRequestDto); + verify(valueSetsService).getVaccinationValueSet(pdfGenerateRequestDto.getDecodedCert().getVaccinationInfo().get(0).getMedicinalProduct()); + } + + @Test + void shouldLoadCountryForCorrectCountryCode(){ + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + service.toVaccinationCertificatePdf(pdfGenerateRequestDto); + verify(valueSetsService).getCountryCode(eq(pdfGenerateRequestDto.getDecodedCert().getVaccinationInfo().get(0).getCountryOfVaccination()), any()); + } + + @Test + void shouldLoadCountryInCorrectLanguage(){ + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + service.toVaccinationCertificatePdf(pdfGenerateRequestDto); + verify(valueSetsService).getCountryCode(any(), eq(pdfGenerateRequestDto.getLanguage())); + } + + @Test + void shouldLoadCountryInEnglishForCorrectCountryCode(){ + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + service.toVaccinationCertificatePdf(pdfGenerateRequestDto); + verify(valueSetsService).getCountryCodeEn(pdfGenerateRequestDto.getDecodedCert().getVaccinationInfo().get(0).getCountryOfVaccination()); + } + + @Test + void shouldThrowAnExceptionIfCountryValueSetIsNullInTheSelectedLanguage(){ + when(valueSetsService.getCountryCode(any(), any())).thenReturn(null); + when(valueSetsService.getCountryCodeEn(any())).thenReturn(fixture.create(CountryCode.class)); + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + var exception = assertThrows(CreateCertificateException.class, () -> + service.toVaccinationCertificatePdf(pdfGenerateRequestDto) + ); + assertEquals(INVALID_COUNTRY_OF_VACCINATION, exception.getError()); + } + + @Test + void shouldThrowAnExceptionIfCountryValueSetIsNullInEnglish(){ + when(valueSetsService.getCountryCodeEn(any())).thenReturn(null); + when(valueSetsService.getCountryCode(any(), any())).thenReturn(fixture.create(CountryCode.class)); + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + var exception = assertThrows(CreateCertificateException.class, () -> + service.toVaccinationCertificatePdf(pdfGenerateRequestDto) + ); + assertEquals(INVALID_COUNTRY_OF_VACCINATION, exception.getError()); + } + + @Test + void shouldMapToVaccinationCertificatePdfWithCorrectPdfGenerateRequestDto(){ + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + try (MockedStatic vaccinationCertificatePdfGenerateRequestDtoMapperMock = Mockito.mockStatic(VaccinationCertificatePdfGenerateRequestDtoMapper.class)) { + vaccinationCertificatePdfGenerateRequestDtoMapperMock + .when(() -> VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(any(), any(), any(), any())) + .thenReturn(fixture.create(VaccinationCertificatePdf.class)); + service.toVaccinationCertificatePdf(pdfGenerateRequestDto); + + vaccinationCertificatePdfGenerateRequestDtoMapperMock.verify(() -> + VaccinationCertificatePdfGenerateRequestDtoMapper + .toVaccinationCertificatePdf(eq(pdfGenerateRequestDto), any(), any(), any())); + } + } + + @Test + void shouldMapToVaccinationCertificatePdfWithCorrectVaccinationValueSet(){ + var vaccinationValueSet = fixture.create(VaccinationValueSet.class); + when(valueSetsService.getVaccinationValueSet(any())).thenReturn(vaccinationValueSet); + try (MockedStatic vaccinationCertificatePdfGenerateRequestDtoMapperMock = + Mockito.mockStatic(VaccinationCertificatePdfGenerateRequestDtoMapper.class)) { + vaccinationCertificatePdfGenerateRequestDtoMapperMock + .when(() -> VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(any(), any(), any(), any())) + .thenReturn(fixture.create(VaccinationCertificatePdf.class)); + service.toVaccinationCertificatePdf(fixture.create(VaccinationCertificatePdfGenerateRequestDto.class)); + + vaccinationCertificatePdfGenerateRequestDtoMapperMock.verify(()-> + VaccinationCertificatePdfGenerateRequestDtoMapper + .toVaccinationCertificatePdf(any(), eq(vaccinationValueSet), any(), any())); + } + } + + @Test + void shouldMapToVaccinationCertificatePdfWithCorrectCountryValueSet(){ + var countryValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCode(any(), any())).thenReturn(countryValueSet); + try (MockedStatic vaccinationCertificatePdfGenerateRequestDtoMapperMock = + Mockito.mockStatic(VaccinationCertificatePdfGenerateRequestDtoMapper.class)) { + vaccinationCertificatePdfGenerateRequestDtoMapperMock + .when(() -> VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(any(), any(), any(), any())) + .thenReturn(fixture.create(VaccinationCertificatePdf.class)); + service.toVaccinationCertificatePdf(fixture.create(VaccinationCertificatePdfGenerateRequestDto.class)); + + vaccinationCertificatePdfGenerateRequestDtoMapperMock.verify(()-> + VaccinationCertificatePdfGenerateRequestDtoMapper + .toVaccinationCertificatePdf(any(), any(), eq(countryValueSet.getDisplay()), any())); + } + } + + @Test + void shouldMapToVaccinationCertificatePdfWithCorrectCountryEnValueSet(){ + var countryEnValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCodeEn(any())).thenReturn(countryEnValueSet); + try (MockedStatic vaccinationCertificatePdfGenerateRequestDtoMapperMock = + Mockito.mockStatic(VaccinationCertificatePdfGenerateRequestDtoMapper.class)) { + vaccinationCertificatePdfGenerateRequestDtoMapperMock + .when(() -> VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(any(), any(), any(), any())) + .thenReturn(fixture.create(VaccinationCertificatePdf.class)); + service.toVaccinationCertificatePdf(fixture.create(VaccinationCertificatePdfGenerateRequestDto.class)); + + vaccinationCertificatePdfGenerateRequestDtoMapperMock.verify(()-> + VaccinationCertificatePdfGenerateRequestDtoMapper + .toVaccinationCertificatePdf(any(), any(), any(), eq(countryEnValueSet.getDisplay()))); + } + } + + @Test + void shouldReturnVaccinationCertificatePdf(){ + var vaccinationCertificatePdf = fixture.create(VaccinationCertificatePdf.class); + try (MockedStatic vaccinationCertificatePdfGenerateRequestDtoMapperMock = + Mockito.mockStatic(VaccinationCertificatePdfGenerateRequestDtoMapper.class)) { + vaccinationCertificatePdfGenerateRequestDtoMapperMock + .when(() -> VaccinationCertificatePdfGenerateRequestDtoMapper.toVaccinationCertificatePdf(any(), any(), any(), any())) + .thenReturn(vaccinationCertificatePdf); + var actual = service.toVaccinationCertificatePdf(fixture.create(VaccinationCertificatePdfGenerateRequestDto.class)); + + assertEquals(vaccinationCertificatePdf, actual); + } + } + } + + @Nested + class ToTestCertificatePdf{ + @Test + void shouldLoadTestValueSetWithCorrectTestType(){ + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + service.toTestCertificatePdf(pdfGenerateRequestDto); + verify(valueSetsService).getAllTestValueSet(eq(pdfGenerateRequestDto.getDecodedCert().getTestInfo().get(0).getTypeOfTest()), any()); + } + + @Test + void shouldLoadTestValueSetWithCorrectManufacturer(){ + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + service.toTestCertificatePdf(pdfGenerateRequestDto); + verify(valueSetsService).getAllTestValueSet(any(), eq(pdfGenerateRequestDto.getDecodedCert().getTestInfo().get(0).getTestManufacturer())); + } + + @Test + void shouldLoadCountryForCorrectCountryCode(){ + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + service.toTestCertificatePdf(pdfGenerateRequestDto); + verify(valueSetsService).getCountryCode(eq(pdfGenerateRequestDto.getDecodedCert().getTestInfo().get(0).getMemberStateOfTest()), any()); + } + + @Test + void shouldLoadCountryInCorrectLanguage(){ + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + service.toTestCertificatePdf(pdfGenerateRequestDto); + verify(valueSetsService).getCountryCode(any(), eq(pdfGenerateRequestDto.getLanguage())); + } + + @Test + void shouldLoadCountryInEnglishForCorrectCountryCode(){ + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + service.toTestCertificatePdf(pdfGenerateRequestDto); + verify(valueSetsService).getCountryCodeEn(pdfGenerateRequestDto.getDecodedCert().getTestInfo().get(0).getMemberStateOfTest()); + } + + @Test + void shouldThrowAnExceptionIfCountryValueSetIsNullInTheSelectedLanguage(){ + when(valueSetsService.getCountryCode(any(), any())).thenReturn(null); + when(valueSetsService.getCountryCodeEn(any())).thenReturn(fixture.create(CountryCode.class)); + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + var exception = assertThrows(CreateCertificateException.class, () -> + service.toTestCertificatePdf(pdfGenerateRequestDto) + ); + assertEquals(INVALID_MEMBER_STATE_OF_TEST, exception.getError()); + } + + @Test + void shouldThrowAnExceptionIfCountryValueSetIsNullInEnglish(){ + when(valueSetsService.getCountryCodeEn(any())).thenReturn(null); + when(valueSetsService.getCountryCode(any(), any())).thenReturn(fixture.create(CountryCode.class)); + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + var exception = assertThrows(CreateCertificateException.class, () -> + service.toTestCertificatePdf(pdfGenerateRequestDto) + ); + assertEquals(INVALID_MEMBER_STATE_OF_TEST, exception.getError()); + } + + @Test + void shouldMapToTestCertificatePdfWithCorrectPdfGenerateRequestDto(){ + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + try (MockedStatic testCertificatePdfGenerateRequestDtoMapperMock = Mockito.mockStatic(TestCertificatePdfGenerateRequestDtoMapper.class)) { + testCertificatePdfGenerateRequestDtoMapperMock + .when(() -> TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(any(), any(), any(), any())) + .thenReturn(fixture.create(TestCertificatePdf.class)); + service.toTestCertificatePdf(pdfGenerateRequestDto); + + testCertificatePdfGenerateRequestDtoMapperMock.verify(() -> + TestCertificatePdfGenerateRequestDtoMapper + .toTestCertificatePdf(eq(pdfGenerateRequestDto), any(), any(), any())); + } + } + + @Test + void shouldMapToTestCertificatePdfWithCorrectVaccinationValueSet(){ + var testValueSet = fixture.create(TestValueSet.class); + when(valueSetsService.getAllTestValueSet(any(), any())).thenReturn(testValueSet); + try (MockedStatic testCertificatePdfGenerateRequestDtoMapperMock = + Mockito.mockStatic(TestCertificatePdfGenerateRequestDtoMapper.class)) { + testCertificatePdfGenerateRequestDtoMapperMock + .when(() -> TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(any(), any(), any(), any())) + .thenReturn(fixture.create(TestCertificatePdf.class)); + service.toTestCertificatePdf(fixture.create(TestCertificatePdfGenerateRequestDto.class)); + + testCertificatePdfGenerateRequestDtoMapperMock.verify(()-> + TestCertificatePdfGenerateRequestDtoMapper + .toTestCertificatePdf(any(), eq(testValueSet), any(), any())); + } + } + + @Test + void shouldMapToTestCertificatePdfWithCorrectCountryValueSet(){ + var countryValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCode(any(), any())).thenReturn(countryValueSet); + try (MockedStatic testCertificatePdfGenerateRequestDtoMapperMock = + Mockito.mockStatic(TestCertificatePdfGenerateRequestDtoMapper.class)) { + testCertificatePdfGenerateRequestDtoMapperMock + .when(() -> TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(any(), any(), any(), any())) + .thenReturn(fixture.create(TestCertificatePdf.class)); + service.toTestCertificatePdf(fixture.create(TestCertificatePdfGenerateRequestDto.class)); + + testCertificatePdfGenerateRequestDtoMapperMock.verify(()-> + TestCertificatePdfGenerateRequestDtoMapper + .toTestCertificatePdf(any(), any(), eq(countryValueSet.getDisplay()), any())); + } + } + + @Test + void shouldMapToTestCertificatePdfWithCorrectCountryEnValueSet(){ + var countryEnValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCodeEn(any())).thenReturn(countryEnValueSet); + try (MockedStatic testCertificatePdfGenerateRequestDtoMapperMock = + Mockito.mockStatic(TestCertificatePdfGenerateRequestDtoMapper.class)) { + testCertificatePdfGenerateRequestDtoMapperMock + .when(() -> TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(any(), any(), any(), any())) + .thenReturn(fixture.create(TestCertificatePdf.class)); + service.toTestCertificatePdf(fixture.create(TestCertificatePdfGenerateRequestDto.class)); + + testCertificatePdfGenerateRequestDtoMapperMock.verify(()-> + TestCertificatePdfGenerateRequestDtoMapper + .toTestCertificatePdf(any(), any(), any(), eq(countryEnValueSet.getDisplay()))); + } + } + + @Test + void shouldReturnVaccinationCertificatePdf(){ + var testCertificatePdf = fixture.create(TestCertificatePdf.class); + try (MockedStatic testCertificatePdfGenerateRequestDtoMapperMock = + Mockito.mockStatic(TestCertificatePdfGenerateRequestDtoMapper.class)) { + testCertificatePdfGenerateRequestDtoMapperMock + .when(() -> TestCertificatePdfGenerateRequestDtoMapper.toTestCertificatePdf(any(), any(), any(), any())) + .thenReturn(testCertificatePdf); + var actual = service.toTestCertificatePdf(fixture.create(TestCertificatePdfGenerateRequestDto.class)); + + assertEquals(testCertificatePdf, actual); + } + } + } + + @Nested + class ToRecoveryCertificatePdf{ + @Test + void shouldLoadCountryInCorrectLanguage(){ + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + service.toRecoveryCertificatePdf(pdfGenerateRequestDto); + verify(valueSetsService).getCountryCode(any(), eq(pdfGenerateRequestDto.getLanguage())); + } + + @Test + void shouldLoadCountryInEnglishForCorrectCountryCode(){ + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + service.toRecoveryCertificatePdf(pdfGenerateRequestDto); + verify(valueSetsService).getCountryCodeEn(pdfGenerateRequestDto.getDecodedCert().getRecoveryInfo().get(0).getCountryOfTest()); + } + + @Test + void shouldThrowAnExceptionIfCountryValueSetIsNullInTheSelectedLanguage(){ + when(valueSetsService.getCountryCode(any(), any())).thenReturn(null); + when(valueSetsService.getCountryCodeEn(any())).thenReturn(fixture.create(CountryCode.class)); + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + var exception = assertThrows(CreateCertificateException.class, () -> + service.toRecoveryCertificatePdf(pdfGenerateRequestDto) + ); + assertEquals(INVALID_COUNTRY_OF_TEST, exception.getError()); + } + + @Test + void shouldThrowAnExceptionIfCountryValueSetIsNullInEnglish(){ + when(valueSetsService.getCountryCodeEn(any())).thenReturn(null); + when(valueSetsService.getCountryCode(any(), any())).thenReturn(fixture.create(CountryCode.class)); + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + var exception = assertThrows(CreateCertificateException.class, () -> + service.toRecoveryCertificatePdf(pdfGenerateRequestDto) + ); + assertEquals(INVALID_COUNTRY_OF_TEST, exception.getError()); + } + + @Test + void shouldMapToRecoveryCertificatePdfWithCorrectPdfGenerateRequestDto(){ + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + try (MockedStatic recoveryCertificatePdfGenerateRequestDtoMapperMock = Mockito.mockStatic(RecoveryCertificatePdfGenerateRequestDtoMapper.class)) { + recoveryCertificatePdfGenerateRequestDtoMapperMock + .when(() -> RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(any(), any(), any())) + .thenReturn(fixture.create(RecoveryCertificatePdf.class)); + service.toRecoveryCertificatePdf(pdfGenerateRequestDto); + + recoveryCertificatePdfGenerateRequestDtoMapperMock.verify(() -> + RecoveryCertificatePdfGenerateRequestDtoMapper + .toRecoveryCertificatePdf(eq(pdfGenerateRequestDto), any(), any())); + } + } + + @Test + void shouldMapToRecoveryCertificatePdfWithCorrectCountryValueSet(){ + var countryValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCode(any(), any())).thenReturn(countryValueSet); + try (MockedStatic recoveryCertificatePdfGenerateRequestDtoMapperMock = + Mockito.mockStatic(RecoveryCertificatePdfGenerateRequestDtoMapper.class)) { + recoveryCertificatePdfGenerateRequestDtoMapperMock + .when(() -> RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(any(), any(), any())) + .thenReturn(fixture.create(RecoveryCertificatePdf.class)); + service.toRecoveryCertificatePdf(fixture.create(RecoveryCertificatePdfGenerateRequestDto.class)); + + recoveryCertificatePdfGenerateRequestDtoMapperMock.verify(()-> + RecoveryCertificatePdfGenerateRequestDtoMapper + .toRecoveryCertificatePdf(any(), eq(countryValueSet.getDisplay()), any())); + } + } + + @Test + void shouldMapToRecoveryCertificatePdfWithCorrectCountryEnValueSet(){ + var countryEnValueSet = fixture.create(CountryCode.class); + when(valueSetsService.getCountryCodeEn(any())).thenReturn(countryEnValueSet); + try (MockedStatic recoveryCertificatePdfGenerateRequestDtoMapperMock = + Mockito.mockStatic(RecoveryCertificatePdfGenerateRequestDtoMapper.class)) { + recoveryCertificatePdfGenerateRequestDtoMapperMock + .when(() -> RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(any(), any(), any())) + .thenReturn(fixture.create(RecoveryCertificatePdf.class)); + service.toRecoveryCertificatePdf(fixture.create(RecoveryCertificatePdfGenerateRequestDto.class)); + + recoveryCertificatePdfGenerateRequestDtoMapperMock.verify(()-> + RecoveryCertificatePdfGenerateRequestDtoMapper + .toRecoveryCertificatePdf(any(), any(), eq(countryEnValueSet.getDisplay()))); + } + } + + @Test + void shouldReturnRecoveryCertificatePdf(){ + var recoveryCertificatePdf = fixture.create(RecoveryCertificatePdf.class); + try (MockedStatic recoveryCertificatePdfGenerateRequestDtoMapperMock = + Mockito.mockStatic(RecoveryCertificatePdfGenerateRequestDtoMapper.class)) { + recoveryCertificatePdfGenerateRequestDtoMapperMock + .when(() -> RecoveryCertificatePdfGenerateRequestDtoMapper.toRecoveryCertificatePdf(any(), any(), any())) + .thenReturn(recoveryCertificatePdf); + var actual = service.toRecoveryCertificatePdf(fixture.create(RecoveryCertificatePdfGenerateRequestDto.class)); + + assertEquals(recoveryCertificatePdf, actual); + } + } + } +} \ No newline at end of file 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 f380a686..dd99eccf 100644 --- a/src/test/java/ch/admin/bag/covidcertificate/service/CsvServiceTest.java +++ b/src/test/java/ch/admin/bag/covidcertificate/service/CsvServiceTest.java @@ -207,7 +207,8 @@ void successful() throws IOException { @ParameterizedTest @ValueSource(strings = {"src/test/resources/csv/test_ansi.csv", - "src/test/resources/csv/test_utf8.csv"}) + "src/test/resources/csv/test_utf8.csv", + "src/test/resources/csv/test_utf8_bom.csv"}) void massTest(String path) throws IOException { var file = Mockito.mock(MultipartFile.class); var inputStream = new FileInputStream(path); diff --git a/src/test/java/ch/admin/bag/covidcertificate/service/ValueSetsLoaderTest.java b/src/test/java/ch/admin/bag/covidcertificate/service/ValueSetsLoaderTest.java index 08eca35e..820895ed 100644 --- a/src/test/java/ch/admin/bag/covidcertificate/service/ValueSetsLoaderTest.java +++ b/src/test/java/ch/admin/bag/covidcertificate/service/ValueSetsLoaderTest.java @@ -119,14 +119,14 @@ class TestValueSetInitializationTest { @Test void allTestValueSetsAreLoaded() { var dto = valueSetsLoader.getValueSets(); - assertEquals(8, dto.getTestSets().size()); + assertEquals(8, dto.getChAcceptedTestValueSets().size()); } @Test void testValueSetsAreCorrectlyInitialized() { var dto = valueSetsLoader.getValueSets(); - var result = dto.getTestSets().stream().filter(it -> it.getName().equals("testName")).findFirst().orElse(null); + var result = dto.getChAcceptedTestValueSets().stream().filter(it -> it.getName().equals("testName")).findFirst().orElse(null); assertNotNull(result); assertEquals("testName", result.getName()); diff --git a/src/test/java/ch/admin/bag/covidcertificate/service/ValueSetsServiceTest.java b/src/test/java/ch/admin/bag/covidcertificate/service/ValueSetsServiceTest.java index 45c9cf48..950741c5 100644 --- a/src/test/java/ch/admin/bag/covidcertificate/service/ValueSetsServiceTest.java +++ b/src/test/java/ch/admin/bag/covidcertificate/service/ValueSetsServiceTest.java @@ -70,7 +70,84 @@ void shouldThrowCreateCertificateException_ifMedicinalProductCodeNotExists(){ } @Nested - class GetTestValueSet{ + class GetAllTestValueSet{ + @ParameterizedTest + @CsvSource(value = {":","'':''", "' ':' '", "'\t':'\t'", "'\n':'\n'"}, delimiter = ':') + void shouldThrowCreateCertificateException_ifTestTypeCodeAndManufacturerCodeAreNullOrBlank(String typeCode, String manufacturerCode){ + var actual = assertThrows(CreateCertificateException.class, + () -> service.getAllTestValueSet(typeCode, manufacturerCode) + ); + + assertEquals(INVALID_TYP_OF_TEST, actual.getError()); + } + + @ParameterizedTest + @NullAndEmptySource + @ValueSource(strings = {" ", "\t", "\n"}) + void shouldReturnTestValueSetUsingTheTestTypeCode_ifTypeCodeIsPCR_andManufacturerIsNullOrBlank(String manufacturerCode){ + var expected = fixture.create(TestValueSet.class); + ReflectionTestUtils.setField(expected, "typeCode", PCR_TYPE_CODE); + var valueSetsDto = fixture.create(ValueSetsDto.class); + valueSetsDto.getAllTestValueSets().add(expected); + when(valueSetsLoader.getValueSets()).thenReturn(valueSetsDto); + + var actual= service.getAllTestValueSet(PCR_TYPE_CODE, manufacturerCode); + + assertEquals(expected, actual); + } + + @Test + void shouldThrowCreateCertificateException_ifTypeCodeIsPCR_andManufacturerIsNotBlank(){ + var manufacturer = fixture.create(String.class); + var actual= assertThrows(CreateCertificateException.class, + () -> service.getAllTestValueSet(PCR_TYPE_CODE, manufacturer) + ); + + assertEquals(INVALID_TYP_OF_TEST, actual.getError()); + } + + @Test + void shouldReturnTestValueSetUsingTheManufacturerCode_ifTypeCodeIsNotPCR_andManufacturerIsNotEmpty(){ + var manufacturer = fixture.create(String.class); + var expected = fixture.create(TestValueSet.class); + ReflectionTestUtils.setField(expected, "manufacturerCodeEu", manufacturer); + var valueSetsDto = fixture.create(ValueSetsDto.class); + valueSetsDto.getAllTestValueSets().add(expected); + when(valueSetsLoader.getValueSets()).thenReturn(valueSetsDto); + + var actual= service.getAllTestValueSet(NONE_PCR_TYPE_CODE, manufacturer); + + assertEquals(expected, actual); + } + + @ParameterizedTest + @NullAndEmptySource + @ValueSource(strings = {" ", "\t", "\n"}) + void shouldReturnTestValueSetUsingTheManufacturerCode_ifTypeCodeIsNullOrBlank_andManufacturerIsNotEmpty(String typeCode){ + var manufacturer = fixture.create(String.class); + var expected = fixture.create(TestValueSet.class); + ReflectionTestUtils.setField(expected, "manufacturerCodeEu", manufacturer); + var valueSetsDto = fixture.create(ValueSetsDto.class); + valueSetsDto.getAllTestValueSets().add(expected); + when(valueSetsLoader.getValueSets()).thenReturn(valueSetsDto); + + var actual= service.getAllTestValueSet(typeCode, manufacturer); + + assertEquals(expected, actual); + } + + @Test + void shouldThrowCreateCertificateException_ifTypeCodeIsNotPCR_andManufacturerIsEmpty(){ + var actual = assertThrows(CreateCertificateException.class, + () -> service.getAllTestValueSet(NONE_PCR_TYPE_CODE, null) + ); + + assertEquals(INVALID_TYP_OF_TEST, actual.getError()); + } + } + + @Nested + class GetChAcceptedTestValueSet{ @ParameterizedTest @CsvSource(value = {":","'':''", "' ':' '", "'\t':'\t'", "'\n':'\n'"}, delimiter = ':') void shouldThrowCreateCertificateException_ifTestTypeCodeAndManufacturerCodeAreNullOrBlank(String typeCode, String manufacturerCode){ @@ -79,7 +156,7 @@ void shouldThrowCreateCertificateException_ifTestTypeCodeAndManufacturerCodeAreN ReflectionTestUtils.setField(testCertificateDataDto, "manufacturerCode", manufacturerCode); var actual = assertThrows(CreateCertificateException.class, - () -> service.getTestValueSet(testCertificateDataDto) + () -> service.getChAcceptedTestValueSet(testCertificateDataDto) ); assertEquals(INVALID_TYP_OF_TEST, actual.getError()); @@ -95,10 +172,10 @@ void shouldReturnTestValueSetUsingTheTestTypeCode_ifTypeCodeIsPCR_andManufacture ReflectionTestUtils.setField(testCertificateDataDto, "typeCode", PCR_TYPE_CODE); ReflectionTestUtils.setField(testCertificateDataDto, "manufacturerCode", manufacturerCode); var valueSetsDto = fixture.create(ValueSetsDto.class); - valueSetsDto.getTestSets().add(expected); + valueSetsDto.getChAcceptedTestValueSets().add(expected); when(valueSetsLoader.getValueSets()).thenReturn(valueSetsDto); - var actual= service.getTestValueSet(testCertificateDataDto); + var actual= service.getChAcceptedTestValueSet(testCertificateDataDto); assertEquals(expected, actual); } @@ -110,7 +187,7 @@ void shouldThrowCreateCertificateException_ifTypeCodeIsPCR_andManufacturerIsNotB ReflectionTestUtils.setField(testCertificateDataDto, "manufacturerCode", fixture.create(String.class)); var actual= assertThrows(CreateCertificateException.class, - () -> service.getTestValueSet(testCertificateDataDto) + () -> service.getChAcceptedTestValueSet(testCertificateDataDto) ); assertEquals(INVALID_TYP_OF_TEST, actual.getError()); @@ -125,10 +202,10 @@ void shouldReturnTestValueSetUsingTheManufacturerCode_ifTypeCodeIsNotPCR_andManu ReflectionTestUtils.setField(testCertificateDataDto, "typeCode", NONE_PCR_TYPE_CODE); ReflectionTestUtils.setField(testCertificateDataDto, "manufacturerCode", manufacturer); var valueSetsDto = fixture.create(ValueSetsDto.class); - valueSetsDto.getTestSets().add(expected); + valueSetsDto.getChAcceptedTestValueSets().add(expected); when(valueSetsLoader.getValueSets()).thenReturn(valueSetsDto); - var actual= service.getTestValueSet(testCertificateDataDto); + var actual= service.getChAcceptedTestValueSet(testCertificateDataDto); assertEquals(expected, actual); } @@ -144,10 +221,10 @@ void shouldReturnTestValueSetUsingTheManufacturerCode_ifTypeCodeIsNullOrBlank_an ReflectionTestUtils.setField(testCertificateDataDto, "typeCode", typeCode); ReflectionTestUtils.setField(testCertificateDataDto, "manufacturerCode", manufacturer); var valueSetsDto = fixture.create(ValueSetsDto.class); - valueSetsDto.getTestSets().add(expected); + valueSetsDto.getChAcceptedTestValueSets().add(expected); when(valueSetsLoader.getValueSets()).thenReturn(valueSetsDto); - var actual= service.getTestValueSet(testCertificateDataDto); + var actual= service.getChAcceptedTestValueSet(testCertificateDataDto); assertEquals(expected, actual); } @@ -159,7 +236,7 @@ void shouldThrowCreateCertificateException_ifTypeCodeIsNotPCR_andManufacturerIsE ReflectionTestUtils.setField(testCertificateDataDto, "manufacturerCode", null); var actual = assertThrows(CreateCertificateException.class, - () -> service.getTestValueSet(testCertificateDataDto) + () -> service.getChAcceptedTestValueSet(testCertificateDataDto) ); assertEquals(INVALID_TYP_OF_TEST, actual.getError()); diff --git a/src/test/java/ch/admin/bag/covidcertificate/service/document/CovidPdfCertificateGenerationServiceTest.java b/src/test/java/ch/admin/bag/covidcertificate/service/document/CovidPdfCertificateGenerationServiceTest.java index c91ae5a6..d4aa1142 100644 --- a/src/test/java/ch/admin/bag/covidcertificate/service/document/CovidPdfCertificateGenerationServiceTest.java +++ b/src/test/java/ch/admin/bag/covidcertificate/service/document/CovidPdfCertificateGenerationServiceTest.java @@ -14,7 +14,6 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.test.util.ReflectionTestUtils; -import se.digg.dgc.encoding.Barcode; import java.io.FileOutputStream; import java.io.OutputStream; @@ -22,7 +21,6 @@ import java.time.format.DateTimeFormatter; import static ch.admin.bag.covidcertificate.TestModelProvider.*; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @@ -141,11 +139,10 @@ private void generateAllDocuments(String familyName, String givenName) throws Ex void doTest(AbstractCertificatePdf pdfData, String filename, String language) throws Exception { - Barcode barcode = mock(Barcode.class); - when(barcode.getPayload()).thenReturn("HC1:NCFOXNYTSFDHJI8-.O0:A%1W RI%.BI06%BF1WG21QKP85NPV*JVH5QWKIW18WA%NE/P3F/8X*G3M9FQH+4JZW4V/AY73CIBVQFSA36238FNB939PJ*KN%DJ3239L7BRNHKBWINEV40AT0C7LS4AZKZ73423ZQT-EJEG3LS4JXITAFK1HG%8SC91Z8YA7-TIP+PQE1W9L $N3-Q-*OGF2F%M RFUS2CPA-DG:A3AGJLC1788M7DD-I/2DBAJDAJCNB-439Y4.$SINOPK3.T4RZ4E%5MK9QM9DB9E%5:I9YHQ1FDIV4RB4VIOTNPS46UDBQEAJJKHHGQA8EL4QN9J9E6LF6JC1A5N11+N1X*8O13E20ZO8%3"); + var barcodePayload = "HC1:NCFOXNYTSFDHJI8-.O0:A%1W RI%.BI06%BF1WG21QKP85NPV*JVH5QWKIW18WA%NE/P3F/8X*G3M9FQH+4JZW4V/AY73CIBVQFSA36238FNB939PJ*KN%DJ3239L7BRNHKBWINEV40AT0C7LS4AZKZ73423ZQT-EJEG3LS4JXITAFK1HG%8SC91Z8YA7-TIP+PQE1W9L $N3-Q-*OGF2F%M RFUS2CPA-DG:A3AGJLC1788M7DD-I/2DBAJDAJCNB-439Y4.$SINOPK3.T4RZ4E%5MK9QM9DB9E%5:I9YHQ1FDIV4RB4VIOTNPS46UDBQEAJJKHHGQA8EL4QN9J9E6LF6JC1A5N11+N1X*8O13E20ZO8%3"; - byte[] document = service.generateCovidCertificate(pdfData, barcode); + byte[] document = service.generateCovidCertificate(pdfData, barcodePayload, LocalDateTime.now()); boolean storeDocument = false; diff --git a/src/test/java/ch/admin/bag/covidcertificate/web/controller/CovidCertificateGenerationControllerSecurityTest.java b/src/test/java/ch/admin/bag/covidcertificate/web/controller/CovidCertificateGenerationControllerSecurityTest.java index 1b41ce66..8e321241 100644 --- a/src/test/java/ch/admin/bag/covidcertificate/web/controller/CovidCertificateGenerationControllerSecurityTest.java +++ b/src/test/java/ch/admin/bag/covidcertificate/web/controller/CovidCertificateGenerationControllerSecurityTest.java @@ -3,6 +3,9 @@ import ch.admin.bag.covidcertificate.api.request.RecoveryCertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.TestCertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.VaccinationCertificateCreateDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.RecoveryCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.TestCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.VaccinationCertificatePdfGenerateRequestDto; import ch.admin.bag.covidcertificate.api.response.CovidCertificateCreateResponseDto; import ch.admin.bag.covidcertificate.config.security.OAuth2SecuredWebConfiguration; import ch.admin.bag.covidcertificate.config.security.authentication.JeapAuthenticationToken; @@ -290,6 +293,93 @@ private void callCreateRecoveryCertificateWithToken(LocalDateTime tokenExpiratio } } + @Nested + class GenerateVaccinationPdfFromExistingCertificate { + private static final String URL = BASE_URL + "fromexisting/vaccination"; + + @Test + void returnsOKIfAuthorizationTokenValid() throws Exception { + callCreateVaccinationCertificateWithToken(EXPIRED_IN_FUTURE, VALID_USER_ROLE, HttpStatus.OK); + callCreateVaccinationCertificateWithToken(EXPIRED_IN_FUTURE, VALID_SUPERUSER_ROLE, HttpStatus.OK); + Mockito.verify(covidCertificateGenerationService, times(2)).generateFromExistingCovidCertificate(any(VaccinationCertificatePdfGenerateRequestDto.class)); + } + + @Test + void returnsForbiddenIfAuthorizationTokenWithInvalidUserRole() throws Exception { + callCreateVaccinationCertificateWithToken(EXPIRED_IN_FUTURE, INVALID_USER_ROLE, HttpStatus.FORBIDDEN); + Mockito.verify(covidCertificateGenerationService, times(0)).generateFromExistingCovidCertificate(any(VaccinationCertificatePdfGenerateRequestDto.class)); + } + + @Test + void returnsUnauthorizedIfAuthorizationTokenExpired() throws Exception { + callCreateVaccinationCertificateWithToken(EXPIRED_IN_PAST, VALID_USER_ROLE, HttpStatus.UNAUTHORIZED); + Mockito.verify(covidCertificateGenerationService, times(0)).generateFromExistingCovidCertificate(any(VaccinationCertificatePdfGenerateRequestDto.class)); + } + + private void callCreateVaccinationCertificateWithToken(LocalDateTime tokenExpiration, String userRole, HttpStatus status) throws Exception { + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + callCreateCertificateWithToken(URL, mapper.writeValueAsString(pdfGenerateRequestDto), tokenExpiration, userRole, status); + } + } + + @Nested + class GenerateTestPdfFromExistingCertificate { + private static final String URL = BASE_URL + "fromexisting/test"; + + @Test + void returnsOKIfAuthorizationTokenValid() throws Exception { + callCreateVaccinationCertificateWithToken(EXPIRED_IN_FUTURE, VALID_USER_ROLE, HttpStatus.OK); + callCreateVaccinationCertificateWithToken(EXPIRED_IN_FUTURE, VALID_SUPERUSER_ROLE, HttpStatus.OK); + Mockito.verify(covidCertificateGenerationService, times(2)).generateFromExistingCovidCertificate(any(TestCertificatePdfGenerateRequestDto.class)); + } + + @Test + void returnsForbiddenIfAuthorizationTokenWithInvalidUserRole() throws Exception { + callCreateVaccinationCertificateWithToken(EXPIRED_IN_FUTURE, INVALID_USER_ROLE, HttpStatus.FORBIDDEN); + Mockito.verify(covidCertificateGenerationService, times(0)).generateFromExistingCovidCertificate(any(TestCertificatePdfGenerateRequestDto.class)); + } + + @Test + void returnsUnauthorizedIfAuthorizationTokenExpired() throws Exception { + callCreateVaccinationCertificateWithToken(EXPIRED_IN_PAST, VALID_USER_ROLE, HttpStatus.UNAUTHORIZED); + Mockito.verify(covidCertificateGenerationService, times(0)).generateFromExistingCovidCertificate(any(TestCertificatePdfGenerateRequestDto.class)); + } + + private void callCreateVaccinationCertificateWithToken(LocalDateTime tokenExpiration, String userRole, HttpStatus status) throws Exception { + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + callCreateCertificateWithToken(URL, mapper.writeValueAsString(pdfGenerateRequestDto), tokenExpiration, userRole, status); + } + } + + @Nested + class GenerateRecoveryPdfFromExistingCertificate { + private static final String URL = BASE_URL + "fromexisting/recovery"; + + @Test + void returnsOKIfAuthorizationTokenValid() throws Exception { + callCreateVaccinationCertificateWithToken(EXPIRED_IN_FUTURE, VALID_USER_ROLE, HttpStatus.OK); + callCreateVaccinationCertificateWithToken(EXPIRED_IN_FUTURE, VALID_SUPERUSER_ROLE, HttpStatus.OK); + Mockito.verify(covidCertificateGenerationService, times(2)).generateFromExistingCovidCertificate(any(RecoveryCertificatePdfGenerateRequestDto.class)); + } + + @Test + void returnsForbiddenIfAuthorizationTokenWithInvalidUserRole() throws Exception { + callCreateVaccinationCertificateWithToken(EXPIRED_IN_FUTURE, INVALID_USER_ROLE, HttpStatus.FORBIDDEN); + Mockito.verify(covidCertificateGenerationService, times(0)).generateFromExistingCovidCertificate(any(RecoveryCertificatePdfGenerateRequestDto.class)); + } + + @Test + void returnsUnauthorizedIfAuthorizationTokenExpired() throws Exception { + callCreateVaccinationCertificateWithToken(EXPIRED_IN_PAST, VALID_USER_ROLE, HttpStatus.UNAUTHORIZED); + Mockito.verify(covidCertificateGenerationService, times(0)).generateFromExistingCovidCertificate(any(RecoveryCertificatePdfGenerateRequestDto.class)); + } + + private void callCreateVaccinationCertificateWithToken(LocalDateTime tokenExpiration, String userRole, HttpStatus status) throws Exception { + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + callCreateCertificateWithToken(URL, mapper.writeValueAsString(pdfGenerateRequestDto), tokenExpiration, userRole, status); + } + } + private void callCreateCertificateWithToken(String url, String requestBody, LocalDateTime tokenExpiration, String userRole, HttpStatus status) throws Exception { String token = JwtTestUtil.getJwtTestToken(PRIVATE_KEY, tokenExpiration, userRole); mockMvc.perform(post(url) diff --git a/src/test/java/ch/admin/bag/covidcertificate/web/controller/CovidCertificateGenerationControllerTest.java b/src/test/java/ch/admin/bag/covidcertificate/web/controller/CovidCertificateGenerationControllerTest.java index 1877cb4d..7a7fef98 100644 --- a/src/test/java/ch/admin/bag/covidcertificate/web/controller/CovidCertificateGenerationControllerTest.java +++ b/src/test/java/ch/admin/bag/covidcertificate/web/controller/CovidCertificateGenerationControllerTest.java @@ -4,6 +4,9 @@ import ch.admin.bag.covidcertificate.api.request.RecoveryCertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.TestCertificateCreateDto; import ch.admin.bag.covidcertificate.api.request.VaccinationCertificateCreateDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.RecoveryCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.TestCertificatePdfGenerateRequestDto; +import ch.admin.bag.covidcertificate.api.request.pdfgeneration.VaccinationCertificatePdfGenerateRequestDto; import ch.admin.bag.covidcertificate.api.response.CovidCertificateCreateResponseDto; import ch.admin.bag.covidcertificate.config.security.authentication.JeapAuthenticationToken; import ch.admin.bag.covidcertificate.config.security.authentication.ServletJeapAuthorization; @@ -292,4 +295,115 @@ void returns403StatusCode_ifAccessDeniedExceptionWasThrown() throws Exception { .andExpect(status().is(HttpStatus.FORBIDDEN.value())); } } + + @Nested + class GenerateVaccinationPdfFromExistingCertificate { + private static final String URL = BASE_URL + "fromexisting/vaccination"; + + @Test + void returnsCertificateWithOkStatus() throws Exception { + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + var responseDto = fixture.create(CovidCertificateCreateResponseDto.class); + when(covidCertificateGenerationService.generateFromExistingCovidCertificate(any(VaccinationCertificatePdfGenerateRequestDto.class))).thenReturn(responseDto); + + MvcResult result = mockMvc.perform(post(URL) + .accept(MediaType.APPLICATION_JSON_VALUE) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .header("Authorization", fixture.create(String.class)) + .content(mapper.writeValueAsString(pdfGenerateRequestDto))) + .andExpect(status().isOk()) + .andReturn(); + + CovidCertificateCreateResponseDto expectedDto = mapper.readValue(result.getResponse().getContentAsString(), CovidCertificateCreateResponseDto.class); + assertEquals(responseDto, expectedDto); + } + + @Test + void returnsStatusCodeOfCreateCertificateException_ifOneWasThrown() throws Exception { + var pdfGenerateRequestDto = fixture.create(VaccinationCertificatePdfGenerateRequestDto.class); + var exception = fixture.create(CreateCertificateException.class); + when(covidCertificateGenerationService.generateFromExistingCovidCertificate(any(VaccinationCertificatePdfGenerateRequestDto.class))).thenThrow(exception); + + mockMvc.perform(post(URL) + .accept(MediaType.APPLICATION_JSON_VALUE) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .header("Authorization", fixture.create(String.class)) + .content(mapper.writeValueAsString(pdfGenerateRequestDto))) + .andExpect(status().is(exception.getError().getHttpStatus().value())); + } + } + + @Nested + class GenerateTestPdfFromExistingCertificate { + private static final String URL = BASE_URL + "fromexisting/test"; + + @Test + void returnsCertificateWithOkStatus() throws Exception { + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + var responseDto = fixture.create(CovidCertificateCreateResponseDto.class); + when(covidCertificateGenerationService.generateFromExistingCovidCertificate(any(TestCertificatePdfGenerateRequestDto.class))).thenReturn(responseDto); + + MvcResult result = mockMvc.perform(post(URL) + .accept(MediaType.APPLICATION_JSON_VALUE) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .header("Authorization", fixture.create(String.class)) + .content(mapper.writeValueAsString(pdfGenerateRequestDto))) + .andExpect(status().isOk()) + .andReturn(); + + CovidCertificateCreateResponseDto expectedDto = mapper.readValue(result.getResponse().getContentAsString(), CovidCertificateCreateResponseDto.class); + assertEquals(responseDto, expectedDto); + } + + @Test + void returnsStatusCodeOfCreateCertificateException_ifOneWasThrown() throws Exception { + var pdfGenerateRequestDto = fixture.create(TestCertificatePdfGenerateRequestDto.class); + var exception = fixture.create(CreateCertificateException.class); + when(covidCertificateGenerationService.generateFromExistingCovidCertificate(any(TestCertificatePdfGenerateRequestDto.class))).thenThrow(exception); + + mockMvc.perform(post(URL) + .accept(MediaType.APPLICATION_JSON_VALUE) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .header("Authorization", fixture.create(String.class)) + .content(mapper.writeValueAsString(pdfGenerateRequestDto))) + .andExpect(status().is(exception.getError().getHttpStatus().value())); + } + } + + @Nested + class GenerateRecoveryPdfFromExistingCertificate { + private static final String URL = BASE_URL + "fromexisting/recovery"; + + @Test + void returnsCertificateWithOkStatus() throws Exception { + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + var responseDto = fixture.create(CovidCertificateCreateResponseDto.class); + when(covidCertificateGenerationService.generateFromExistingCovidCertificate(any(RecoveryCertificatePdfGenerateRequestDto.class))).thenReturn(responseDto); + + MvcResult result = mockMvc.perform(post(URL) + .accept(MediaType.APPLICATION_JSON_VALUE) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .header("Authorization", fixture.create(String.class)) + .content(mapper.writeValueAsString(pdfGenerateRequestDto))) + .andExpect(status().isOk()) + .andReturn(); + + CovidCertificateCreateResponseDto expectedDto = mapper.readValue(result.getResponse().getContentAsString(), CovidCertificateCreateResponseDto.class); + assertEquals(responseDto, expectedDto); + } + + @Test + void returnsStatusCodeOfCreateCertificateException_ifOneWasThrown() throws Exception { + var pdfGenerateRequestDto = fixture.create(RecoveryCertificatePdfGenerateRequestDto.class); + var exception = fixture.create(CreateCertificateException.class); + when(covidCertificateGenerationService.generateFromExistingCovidCertificate(any(RecoveryCertificatePdfGenerateRequestDto.class))).thenThrow(exception); + + mockMvc.perform(post(URL) + .accept(MediaType.APPLICATION_JSON_VALUE) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .header("Authorization", fixture.create(String.class)) + .content(mapper.writeValueAsString(pdfGenerateRequestDto))) + .andExpect(status().is(exception.getError().getHttpStatus().value())); + } + } } diff --git a/src/test/java/ch/admin/bag/covidcertificate/web/controller/ValueSetsControllerTest.java b/src/test/java/ch/admin/bag/covidcertificate/web/controller/ValueSetsControllerTest.java index cac59d33..b2d3d491 100644 --- a/src/test/java/ch/admin/bag/covidcertificate/web/controller/ValueSetsControllerTest.java +++ b/src/test/java/ch/admin/bag/covidcertificate/web/controller/ValueSetsControllerTest.java @@ -1,5 +1,7 @@ package ch.admin.bag.covidcertificate.web.controller; +import ch.admin.bag.covidcertificate.api.mapper.ValueSetsResponseDtoMapper; +import ch.admin.bag.covidcertificate.api.response.ValueSetsResponseDto; import ch.admin.bag.covidcertificate.api.valueset.ValueSetsDto; import ch.admin.bag.covidcertificate.service.ValueSetsService; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,6 +14,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -49,7 +53,7 @@ class ValueSetsControllerTest { private static final JFixture fixture = new JFixture(); @BeforeAll - static void setup(){ + static void setup() { customizeVaccinationValueSet(fixture); customizeTestValueSet(fixture); customizeCountryCode(fixture); @@ -65,17 +69,39 @@ void setupMocks() { @Nested class Get { @Test - void returnsValueSetsWithOkStatus() throws Exception { + void mapsValueSetsToValueSetResponseDto() throws Exception { var responseDto = fixture.create(ValueSetsDto.class); when(valueSetsService.getValueSets()).thenReturn(responseDto); - MvcResult result = mockMvc.perform(get(URL) - .contentType(MediaType.APPLICATION_JSON_VALUE) - .header("Authorization", fixture.create(String.class))) - .andExpect(status().isOk()) - .andReturn(); + try (MockedStatic valueSetsResponseDtoMapperMock = Mockito.mockStatic(ValueSetsResponseDtoMapper.class)) { + valueSetsResponseDtoMapperMock + .when(() -> ValueSetsResponseDtoMapper.create(any())) + .thenReturn(fixture.create(ValueSetsResponseDto.class)); + + mockMvc.perform(get(URL) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .header("Authorization", fixture.create(String.class))); - assertEquals(mapper.writeValueAsString(responseDto), result.getResponse().getContentAsString()); + valueSetsResponseDtoMapperMock.verify(() -> ValueSetsResponseDtoMapper.create(responseDto)); + } + } + + @Test + void returnsValueSetsWithOkStatus() throws Exception { + var valueSetResponseDto = fixture.create(ValueSetsResponseDto.class); + try (MockedStatic valueSetsResponseDtoMapperMock = Mockito.mockStatic(ValueSetsResponseDtoMapper.class)) { + valueSetsResponseDtoMapperMock + .when(() -> ValueSetsResponseDtoMapper.create(any())) + .thenReturn(valueSetResponseDto); + + MvcResult result = mockMvc.perform(get(URL) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .header("Authorization", fixture.create(String.class))) + .andExpect(status().isOk()) + .andReturn(); + + assertEquals(mapper.writeValueAsString(valueSetResponseDto), result.getResponse().getContentAsString()); + } } @Test diff --git a/src/test/resources/csv/test_utf8_bom.csv b/src/test/resources/csv/test_utf8_bom.csv new file mode 100644 index 00000000..792fc0b3 --- /dev/null +++ b/src/test/resources/csv/test_utf8_bom.csv @@ -0,0 +1,13 @@ +givenName;familyName;dateOfBirth;language;manufacturerCode;typeCode;sampleDateTime;testingCentreOrFacility;memberStateOfTest +Max;Muster;1985-09-20;de;1304;LP217198-3;2021-06-01T11:34Z;test;CH +Remo;Spichtig;1986-11-13;de;1604;LP217198-3;2021-07-08T14:54Z;MiSANTO AG;CH +Stanislaw Wladyslaw;Januszkiewicz;1969-12-23;de;1604;LP217198-3;2021-07-08T00:00Z;MiSANTO AG;CH +Sina;Hess;2004-03-22;de;1604;LP217198-3;2021-07-08T18:21Z;MiSANTO AG;CH +Tara;Hess;2002-04-13;de;1604;LP217198-3;2021-07-08T18:26Z;MiSANTO AG;CH +Benjamin;Kuster;2002-05-26;de;1604;LP217198-3;2021-07-08T12:47Z;MiSANTO AG;CH +Stefan;Kiser;1991-07-19;de;1604;LP217198-3;2021-07-08T12:26Z;MiSANTO AG;CH +Dario;Ruckli;2002-11-07;de;1604;LP217198-3;2021-07-08T14:51Z;MiSANTO AG;CH +Justin;Ifanger;2002-06-04;de;1604;LP217198-3;2021-07-08T14:17Z;MiSANTO AG;CH +Lara Heidi;Heiniger;1997-04-18;de;1604;LP217198-3;2021-07-08T12:29Z;MiSANTO AG;CH +Thomas;Schumacher;1981-05-05;de;1604;LP217198-3;2021-07-08T09:00Z;MiSANTO AG;CH +Dünnenberger;Simon;1998-12-16;de;1604;LP217198-3;2021-07-08T09:23Z;MiSANTO AG;CH