diff --git a/stack/keycloak/migration/add-authorities-basisdaten-wahltage.yml b/stack/keycloak/migration/add-authorities-basisdaten-wahltage.yml index 31c385c98..2c7b9415a 100644 --- a/stack/keycloak/migration/add-authorities-basisdaten-wahltage.yml +++ b/stack/keycloak/migration/add-authorities-basisdaten-wahltage.yml @@ -11,6 +11,15 @@ changes: role: Basisdaten_BUSINESSACTION_GetWahltage clientId: ${SSO_CLIENT_ID} + - addRole: + name: Basisdaten_BUSINESSACTION_GetWahltag + clientRole: true + clientId: ${SSO_CLIENT_ID} + - assignRoleToGroup: + group: allBasisdatenAuthorities + role: Basisdaten_BUSINESSACTION_GetWahltag + clientId: ${SSO_CLIENT_ID} + - addRole: name: Basisdaten_READ_Wahltag clientRole: true diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/DummyClientImpl.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/DummyClientImpl.java index 914ad11ff..53c480b1a 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/DummyClientImpl.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/DummyClientImpl.java @@ -24,6 +24,7 @@ import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlbezirke.WahlbezirkeClient; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlModel; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlenClient; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahltagWithNummer; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahltag.WahltagModel; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahltag.WahltageClient; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlvorschlag.KandidatModel; @@ -66,11 +67,11 @@ public List getWahltage(LocalDate tag) { } @Override - public List getWahlen(LocalDate wahltag, String wahltagNummer) throws WlsException { + public List getWahlen(final WahltagWithNummer wahltagWithNummer) throws WlsException { return List.of( - new WahlModel("wahl1", "0", 1L, 1L, wahltag, BTW, new Farbe(0, 1, 2), "1"), - new WahlModel("wahl2", "1", 2L, 1L, wahltag, EUW, new Farbe(3, 4, 5), "1"), - new WahlModel("wahl3", "2", 3L, 1L, wahltag, LTW, new Farbe(6, 7, 8), "1")); + new WahlModel("wahl1", "0", 1L, 1L, wahltagWithNummer.wahltag(), BTW, new Farbe(0, 1, 2), "1"), + new WahlModel("wahl2", "1", 2L, 1L, wahltagWithNummer.wahltag(), EUW, new Farbe(3, 4, 5), "1"), + new WahlModel("wahl3", "2", 3L, 1L, wahltagWithNummer.wahltag(), LTW, new Farbe(6, 7, 8), "1")); } @Override diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientImpl.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientImpl.java index a41cdfc21..fc1e771f2 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientImpl.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientImpl.java @@ -6,6 +6,7 @@ import de.muenchen.oss.wahllokalsystem.basisdatenservice.exception.ExceptionConstants; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlModel; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlenClient; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahltagWithNummer; import de.muenchen.oss.wahllokalsystem.wls.common.exception.util.ExceptionFactory; import java.util.List; import java.util.Set; @@ -26,10 +27,10 @@ public class WahlenClientImpl implements WahlenClient { private final WahlenClientMapper wahlenClientMapper; @Override - public List getWahlen(java.time.LocalDate wahltag, String wahltagNummer) { + public List getWahlen(final WahltagWithNummer wahltagWithNummer) { final Set wahlDTOs; try { - wahlDTOs = wahldatenControllerApi.loadWahlen(wahltag, wahltagNummer); + wahlDTOs = wahldatenControllerApi.loadWahlen(wahltagWithNummer.wahltag(), wahltagWithNummer.wahltagNummer()); } catch (final Exception exception) { log.info("exception on loadwahl from external", exception); throw exceptionFactory.createTechnischeWlsException(ExceptionConstants.FAILED_COMMUNICATION_WITH_EAI); diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientMapper.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientMapper.java index 7f19624d2..614554dcd 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientMapper.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientMapper.java @@ -1,41 +1,21 @@ package de.muenchen.oss.wahllokalsystem.basisdatenservice.clients; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.Farbe; import de.muenchen.oss.wahllokalsystem.basisdatenservice.eai.aou.model.WahlDTO; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlModel; -import de.muenchen.oss.wahllokalsystem.basisdatenservice.utils.ObjectPropertyChecker; import java.util.List; import java.util.Set; import org.mapstruct.Mapper; import org.mapstruct.Mapping; -import org.mapstruct.Named; -import org.mapstruct.factory.Mappers; -@Mapper +@Mapper(imports = Farbe.class) public interface WahlenClientMapper { - WahlenClientMapper INSTANCE = Mappers.getMapper(WahlenClientMapper.class); - - @Mapping(target = "waehlerverzeichnisnummer", source = ".", qualifiedByName = "setWZeroIfNotExisting") - @Mapping(target = "reihenfolge", source = ".", qualifiedByName = "setRZeroIfNotExisting") - @Mapping(target = "farbe", ignore = true) + @Mapping(target = "waehlerverzeichnisnummer", constant = "1l") + @Mapping(target = "reihenfolge", constant = "1l") + @Mapping(target = "farbe", expression = "java(new Farbe(0, 0, 0))") @Mapping(target = "wahlID", source = "identifikator") WahlModel toModel(WahlDTO wahlDTO); - @Named("setWZeroIfNotExisting") - default Long setWZeroIfNotExisting(final WahlDTO wahlDTO) throws NoSuchFieldException, IllegalAccessException { - if (ObjectPropertyChecker.objectHasProperty(wahlDTO, "waehlerverzeichnisnummer")) { - ObjectPropertyChecker.getValueFromField(wahlDTO, "waehlerverzeichnisnummer"); - } - return 0L; - } - - @Named("setRZeroIfNotExisting") - default Long setRZeroIfNotExisting(final WahlDTO wahlDTO) throws NoSuchFieldException, IllegalAccessException { - if (ObjectPropertyChecker.objectHasProperty(wahlDTO, "reihenfolge")) { - ObjectPropertyChecker.getValueFromField(wahlDTO, "reihenfolge"); - } - return 0L; - } - List fromRemoteClientSetOfWahlDTOtoListOfWahlModel(Set wahlDTO); } diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/domain/wahl/WahlRepository.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/domain/wahl/WahlRepository.java index 8932e429e..663ea7f7e 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/domain/wahl/WahlRepository.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/domain/wahl/WahlRepository.java @@ -52,5 +52,5 @@ public interface WahlRepository extends CrudRepository { List findByWahltagOrderByReihenfolge(LocalDate wahltag); - int countByWahltag(LocalDate wahltag); + boolean existsByWahltag(LocalDate wahltag); } diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/exception/ExceptionConstants.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/exception/ExceptionConstants.java index 915e4c7b2..8cf6ba839 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/exception/ExceptionConstants.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/exception/ExceptionConstants.java @@ -51,19 +51,16 @@ public class ExceptionConstants { public static ExceptionDataWrapper GETREFERENDUMVORLAGEN_PARAMETER_UNVOLLSTAENDIG = new ExceptionDataWrapper("308", "getReferendumvorlagen: Suchkriterien unvollständig."); - public static ExceptionDataWrapper WAHL_SPEICHERN_FEHLGESCHLAGEN = new ExceptionDataWrapper("351", - "postUngueltigeWahl: Das Speichern der Wahl ist fehlgeschlagen"); - - public static ExceptionDataWrapper CODE_GETWAHLEN_PARAMETER_UNVOLLSTAENDIG = new ExceptionDataWrapper("314", + public static ExceptionDataWrapper GETWAHLEN_PARAMETER_UNVOLLSTAENDIG = new ExceptionDataWrapper("314", "getWahlen: Der Wahltag zu den gesuchten Wahlen existiert nicht."); - public static ExceptionDataWrapper CODE_POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG = new ExceptionDataWrapper("317", + public static ExceptionDataWrapper POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG = new ExceptionDataWrapper("317", "postWahlen: Suchkriterien unvollständig. Die wahltagID ist leer oder null."); - public static ExceptionDataWrapper CODE_POSTWAHLEN_UNSAVEABLE = new ExceptionDataWrapper("318", + public static ExceptionDataWrapper POSTWAHLEN_UNSAVEABLE = new ExceptionDataWrapper("318", "postWahlen: Die Wahlen konnten nicht gespeichert werden."); public static ExceptionDataWrapper CODE_GETWAHLBEZIRKE_PARAMETER_UNVOLLSTAENDIG = new ExceptionDataWrapper("312", "getWahlbezirke: Suchkriterien unvollständig. Die wahltagID ist leer oder null."); - public static ExceptionDataWrapper CODE_GETWAHLBEZIRKE_NO_WAHLTAG = new ExceptionDataWrapper("313", + public static ExceptionDataWrapper GETWAHLBEZIRKE_NO_WAHLTAG = new ExceptionDataWrapper("313", "Es wurde kein Wahltag zu dieser WahltagID gefunden. Bitte legen Sie die Basisdaten an."); public static final ExceptionDataWrapper GETKOPFDATEN_PARAMETER_UNVOLLSTAENDIG = new ExceptionDataWrapper("303", diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/rest/wahlen/WahlenController.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/rest/wahlen/WahlenController.java index 098658981..caeaeebfb 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/rest/wahlen/WahlenController.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/rest/wahlen/WahlenController.java @@ -1,6 +1,7 @@ package de.muenchen.oss.wahllokalsystem.basisdatenservice.rest.wahlen; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlenService; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlenWriteModel; import de.muenchen.oss.wahllokalsystem.wls.common.exception.rest.model.WlsExceptionDTO; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.ArraySchema; @@ -57,7 +58,7 @@ public List getWahlen(@PathVariable("wahltagID") String wahltagID) { ) @ResponseStatus(HttpStatus.OK) public void postWahlen(@PathVariable("wahltagID") String wahltagID, @RequestBody List wahlDTOs) { - wahlenService.postWahlen(wahltagID, wahlDTOMapper.fromListOfWahlDTOtoListOfWahlModel(wahlDTOs)); + wahlenService.postWahlen(new WahlenWriteModel(wahltagID, wahlDTOMapper.fromListOfWahlDTOtoListOfWahlModel(wahlDTOs))); } @Operation( diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeService.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeService.java index 863d0b9e5..055e79c1d 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeService.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeService.java @@ -59,7 +59,7 @@ public List getWahlbezirke(final String wahltagID) { } return wahlbezirkModelMapper.fromListOfWahlbezirkEntityToListOfWahlbezirkModel(wahlbezirkRepository.findByWahltag(wahltagObjekt.getWahltag())); } else { - throw exceptionFactory.createFachlicheWlsException(ExceptionConstants.CODE_GETWAHLBEZIRKE_NO_WAHLTAG); + throw exceptionFactory.createFachlicheWlsException(ExceptionConstants.GETWAHLBEZIRKE_NO_WAHLTAG); } } } diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeValidator.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeValidator.java index 1b27b038e..8603694f5 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeValidator.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeValidator.java @@ -22,7 +22,7 @@ public void validWahltagIDParamOrThrow(final String wahltagID) { public void validateWahltagForSearchingWahltagID(final Optional wahltag) { if (wahltag.isEmpty() || null == wahltag.get().getWahltag()) { - throw exceptionFactory.createFachlicheWlsException(ExceptionConstants.CODE_GETWAHLBEZIRKE_NO_WAHLTAG); + throw exceptionFactory.createFachlicheWlsException(ExceptionConstants.GETWAHLBEZIRKE_NO_WAHLTAG); } } diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenClient.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenClient.java index c8d8c87b5..9895c7e21 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenClient.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenClient.java @@ -1,15 +1,12 @@ package de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen; import de.muenchen.oss.wahllokalsystem.wls.common.exception.WlsException; -import java.time.LocalDate; import java.util.List; public interface WahlenClient { /** - * @param wahltag The Request Wahltag - Reference for requested Wahlen - * @param wahltagNummer The Number of the Wahltag, it could exist more then one Wahltag at same day - * if more elections on the day + * @param wahltagWithNummer reference to a specific event on a date of election * @return List * @throws WlsException * {@link de.muenchen.oss.wahllokalsystem.wls.common.exception.FachlicheWlsException} if @@ -17,5 +14,5 @@ public interface WahlenClient { * {@link de.muenchen.oss.wahllokalsystem.wls.common.exception.TechnischeWlsException} * if there were trouble during communication */ - List getWahlen(final LocalDate wahltag, final String wahltagNummer) throws WlsException; + List getWahlen(final WahltagWithNummer wahltagWithNummer) throws WlsException; } diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenService.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenService.java index 6be65dda0..882b21881 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenService.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenService.java @@ -1,16 +1,15 @@ package de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen; -import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.WahltagRepository; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.Farbe; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.Wahl; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.WahlRepository; import de.muenchen.oss.wahllokalsystem.basisdatenservice.exception.ExceptionConstants; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahltag.WahltageService; import de.muenchen.oss.wahllokalsystem.wls.common.exception.util.ExceptionFactory; import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import lombok.val; -import org.springframework.http.HttpMethod; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -22,7 +21,7 @@ public class WahlenService { private final WahlRepository wahlRepository; - private final WahltagRepository wahltagRepository; + private final WahltageService wahltageService; private final ExceptionFactory exceptionFactory; @@ -35,29 +34,30 @@ public class WahlenService { @PreAuthorize("hasAuthority('Basisdaten_BUSINESSACTION_GetWahlen')") @Transactional public List getWahlen(String wahltagID) { - wahlenValidator.validWahltagIDParamOrThrow(wahltagID, HttpMethod.GET); - val wahltag = wahltagRepository.findById(wahltagID); - wahlenValidator.validateWahltagForSearchingWahltagID(wahltag); + wahlenValidator.validWahlenCriteriaOrThrow(wahltagID); - if (wahlRepository.countByWahltag(wahltag.get().getWahltag()) == 0) { + val wahltagValue = wahltageService.getWahltagByID(wahltagID); + + if (!wahlRepository.existsByWahltag(wahltagValue.wahltag())) { log.info("#getWahlen: Für wahltagID {} waren keine Wahlen in der Datenbank", wahltagID); List wahlEntities = wahlModelMapper - .fromListOfWahlModeltoListOfWahlEntities(wahlenClient.getWahlen(wahltag.get().getWahltag(), wahltag.get().getNummer())); + .fromListOfWahlModeltoListOfWahlEntities( + wahlenClient.getWahlen(new WahltagWithNummer(wahltagValue.wahltag(), wahltagValue.nummer()))); wahlRepository.saveAll(wahlEntities); } - return wahlModelMapper.fromListOfWahlEntityToListOfWahlModel(wahlRepository.findByWahltagOrderByReihenfolge(wahltag.get().getWahltag())); + return wahlModelMapper.fromListOfWahlEntityToListOfWahlModel(wahlRepository.findByWahltagOrderByReihenfolge(wahltagValue.wahltag())); } @PreAuthorize("hasAuthority('Basisdaten_BUSINESSACTION_PostWahlen')") @Transactional - public void postWahlen(String wahltagID, List wahlen) { + public void postWahlen(final WahlenWriteModel wahlenWriteModel) { log.info("#postWahlen"); - wahlenValidator.validWahltagIDParamOrThrow(wahltagID, HttpMethod.POST); + wahlenValidator.validWahlenWriteModelOrThrow(wahlenWriteModel); try { - wahlRepository.saveAll(wahlModelMapper.fromListOfWahlModeltoListOfWahlEntities(wahlen)); + wahlRepository.saveAll(wahlModelMapper.fromListOfWahlModeltoListOfWahlEntities(wahlenWriteModel.wahlen())); } catch (Exception e) { - log.error("#postWahlen: Die Wahlen konnten aufgrund eines Fehlers nicht gespeichert werden {}:", e); - throw exceptionFactory.createFachlicheWlsException(ExceptionConstants.CODE_POSTWAHLEN_UNSAVEABLE); + log.error("#postWahlen: Die Wahlen konnten aufgrund eines Fehlers nicht gespeichert werden:", e); + throw exceptionFactory.createFachlicheWlsException(ExceptionConstants.POSTWAHLEN_UNSAVEABLE); } } diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenValidator.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenValidator.java index 727f2f971..971e0ff9a 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenValidator.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenValidator.java @@ -1,13 +1,11 @@ package de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen; -import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.Wahltag; import de.muenchen.oss.wahllokalsystem.basisdatenservice.exception.ExceptionConstants; import de.muenchen.oss.wahllokalsystem.wls.common.exception.util.ExceptionFactory; -import java.util.Optional; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.StringUtils; -import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; @Component @RequiredArgsConstructor @@ -15,18 +13,15 @@ public class WahlenValidator { private final ExceptionFactory exceptionFactory; - public void validWahltagIDParamOrThrow(final String wahltagID, HttpMethod httpMethod) { - if (wahltagID == null || StringUtils.isBlank(wahltagID) || StringUtils.isEmpty(wahltagID)) { - switch (httpMethod.toString()) { - case "GET" -> throw exceptionFactory.createFachlicheWlsException(ExceptionConstants.CODE_GETWAHLEN_PARAMETER_UNVOLLSTAENDIG); - case "POST" -> throw exceptionFactory.createFachlicheWlsException(ExceptionConstants.CODE_POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG); - } + public void validWahlenCriteriaOrThrow(final String wahltagID) { + if (StringUtils.isBlank(wahltagID)) { + throw exceptionFactory.createFachlicheWlsException(ExceptionConstants.GETWAHLEN_PARAMETER_UNVOLLSTAENDIG); } } - public void validateWahltagForSearchingWahltagID(final Optional wahltag) { - if (wahltag.isEmpty() || null == wahltag.get().getWahltag()) { - throw exceptionFactory.createFachlicheWlsException(ExceptionConstants.CODE_GETWAHLBEZIRKE_NO_WAHLTAG); + public void validWahlenWriteModelOrThrow(final WahlenWriteModel wahlenWriteModel) { + if (wahlenWriteModel == null || StringUtils.isBlank(wahlenWriteModel.wahltagID()) || CollectionUtils.isEmpty(wahlenWriteModel.wahlen())) { + throw exceptionFactory.createFachlicheWlsException(ExceptionConstants.POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG); } } diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenWriteModel.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenWriteModel.java new file mode 100644 index 000000000..abf49939f --- /dev/null +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenWriteModel.java @@ -0,0 +1,9 @@ +package de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen; + +import java.util.List; + +public record WahlenWriteModel( + String wahltagID, + List wahlen +) { +} diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahltagWithNummer.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahltagWithNummer.java new file mode 100644 index 000000000..cf4401615 --- /dev/null +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahltagWithNummer.java @@ -0,0 +1,9 @@ +package de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen; + +import jakarta.validation.constraints.NotNull; +import java.time.LocalDate; + +public record WahltagWithNummer( + @NotNull LocalDate wahltag, + @NotNull String wahltagNummer) { +} diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltagModelMapper.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltagModelMapper.java index c7d148efd..90b5657bf 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltagModelMapper.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltagModelMapper.java @@ -7,6 +7,8 @@ @Mapper public interface WahltagModelMapper { + WahltagModel toModel(Wahltag wahltag); + List fromWahltagEntityToWahltagModelList(List entities); List fromWahltagModelToWahltagEntityList(List entities); diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltageService.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltageService.java index a5ce66c99..8e852a6af 100644 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltageService.java +++ b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltageService.java @@ -1,6 +1,9 @@ package de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahltag; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.Wahltag; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.WahltagRepository; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.exception.ExceptionConstants; +import de.muenchen.oss.wahllokalsystem.wls.common.exception.util.ExceptionFactory; import java.time.LocalDate; import java.util.List; import lombok.RequiredArgsConstructor; @@ -14,6 +17,8 @@ @Slf4j public class WahltageService { + private final ExceptionFactory exceptionFactory; + private final WahltagRepository wahltagRepository; private final WahltagModelMapper wahltagModelMapper; @@ -29,4 +34,16 @@ public List getWahltage() { wahltagRepository.saveAll(wahltagModelMapper.fromWahltagModelToWahltagEntityList(wahltage)); return wahltagModelMapper.fromWahltagEntityToWahltagModelList(wahltagRepository.findAllByOrderByWahltagAsc()); } + + @PreAuthorize( + "hasAuthority('Basisdaten_BUSINESSACTION_GetWahltag')" + ) + public WahltagModel getWahltagByID(final String wahltagID) { + return wahltagModelMapper.toModel(getWahltagByIDOrThrow(wahltagID)); + } + + private Wahltag getWahltagByIDOrThrow(final String wahltagID) { + return wahltagRepository.findById(wahltagID) + .orElseThrow(() -> exceptionFactory.createFachlicheWlsException(ExceptionConstants.GETWAHLBEZIRKE_NO_WAHLTAG)); + } } diff --git a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/utils/ObjectPropertyChecker.java b/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/utils/ObjectPropertyChecker.java deleted file mode 100644 index 165b8b9fd..000000000 --- a/wls-basisdaten-service/src/main/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/utils/ObjectPropertyChecker.java +++ /dev/null @@ -1,39 +0,0 @@ -package de.muenchen.oss.wahllokalsystem.basisdatenservice.utils; - -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class ObjectPropertyChecker { - - public static Boolean objectHasProperty(Object obj, String propertyName) { - List properties = getAllFields(obj); - for (Field field : properties) { - if (field.getName().equalsIgnoreCase(propertyName)) { - return true; - } - } - return false; - } - - public static Object getValueFromField(Object theObjectInstance, String propertyName) throws NoSuchFieldException, IllegalAccessException { - return theObjectInstance.getClass().getField(propertyName).get(propertyName); - } - - private static List getAllFields(Object obj) { - List fields = new ArrayList<>(); - getAllFieldsRecursive(fields, obj.getClass()); - return fields; - } - - private static List getAllFieldsRecursive(List fields, Class type) { - fields.addAll(Arrays.asList(type.getDeclaredFields())); - - if (type.getSuperclass() != null) { - fields = getAllFieldsRecursive(fields, type.getSuperclass()); - } - - return fields; - } -} diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/DummyClientImplTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/DummyClientImplTest.java index 7d4a2cba6..3d9258729 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/DummyClientImplTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/DummyClientImplTest.java @@ -2,6 +2,7 @@ import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.kopfdaten.BasisdatenModel; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.kopfdaten.StimmzettelgebietModel; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahltagWithNummer; import de.muenchen.oss.wahllokalsystem.wls.common.security.domain.BezirkUndWahlID; import java.time.LocalDate; import lombok.val; @@ -39,7 +40,7 @@ class GetWahlen { @Test void resultIsAnArrayWithPositiveSize() { - val result = unitUnderTest.getWahlen(LocalDate.now(), "0"); + val result = unitUnderTest.getWahlen(new WahltagWithNummer(LocalDate.now(), "0")); Assertions.assertThat(result).size().isPositive(); } } diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahldatenClientMapperTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahldatenClientMapperTest.java index c171f983f..84c084fd4 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahldatenClientMapperTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahldatenClientMapperTest.java @@ -1,5 +1,6 @@ package de.muenchen.oss.wahllokalsystem.basisdatenservice.clients; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.Farbe; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.Wahlart; import de.muenchen.oss.wahllokalsystem.basisdatenservice.eai.aou.model.WahlDTO; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlModel; @@ -35,11 +36,11 @@ void isMapped() { val expectedWahl = new WahlModel( "identifikatorWahl1", "nameWahl1", - 0L, - 0L, + 1L, + 1L, aNowMoment, Wahlart.BAW, - null, + new Farbe(0, 0, 0), "nummerWahl1"); Assertions.assertThat(result).usingRecursiveComparison().isEqualTo(expectedWahl); diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientImplTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientImplTest.java index d11832ca8..d3ee3cb2b 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientImplTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientImplTest.java @@ -1,10 +1,12 @@ package de.muenchen.oss.wahllokalsystem.basisdatenservice.clients; import static org.mockito.ArgumentMatchers.any; + import de.muenchen.oss.wahllokalsystem.basisdatenservice.eai.aou.client.WahldatenControllerApi; import de.muenchen.oss.wahllokalsystem.basisdatenservice.eai.aou.model.WahlDTO; import de.muenchen.oss.wahllokalsystem.basisdatenservice.exception.ExceptionConstants; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlModel; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahltagWithNummer; import de.muenchen.oss.wahllokalsystem.wls.common.exception.FachlicheWlsException; import de.muenchen.oss.wahllokalsystem.wls.common.exception.TechnischeWlsException; import de.muenchen.oss.wahllokalsystem.wls.common.exception.util.ExceptionFactory; @@ -52,7 +54,7 @@ void clientResponseIsMapped() { Mockito.when(wahlenClientMapper.fromRemoteClientSetOfWahlDTOtoListOfWahlModel(mockedClientResponse)) .thenReturn(mockedMappedClientResponse); - val result = unitUnderTest.getWahlen(testDate, "1"); + val result = unitUnderTest.getWahlen(new WahltagWithNummer(testDate, "1")); Assertions.assertThat(result).isSameAs(mockedMappedClientResponse); } @@ -64,7 +66,8 @@ void exceptionWhenClientResponseIsNull() { Mockito.when(wahldatenControllerApi.loadWahlen(any(), any())).thenReturn(null); Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.NULL_FROM_CLIENT)).thenReturn(mockedWlsException); - Assertions.assertThatException().isThrownBy(() -> unitUnderTest.getWahlen(LocalDate.now(), "1")).isSameAs(mockedWlsException); + Assertions.assertThatException().isThrownBy(() -> unitUnderTest.getWahlen(new WahltagWithNummer(LocalDate.now(), "1"))) + .isSameAs(mockedWlsException); } @Test @@ -76,7 +79,7 @@ void controllerApiExceptionIsMapped() { Mockito.when(wahldatenControllerApi.loadWahlen(any(), any())) .thenThrow(new RestClientException("error occurs while attempting to invoke the API")); Mockito.when(exceptionFactory.createTechnischeWlsException(ExceptionConstants.FAILED_COMMUNICATION_WITH_EAI)).thenThrow(mockedException); - Assertions.assertThatException().isThrownBy(() -> unitUnderTest.getWahlen(testDate, "1")).isSameAs(mockedException); + Assertions.assertThatException().isThrownBy(() -> unitUnderTest.getWahlen(new WahltagWithNummer(testDate, "1"))).isSameAs(mockedException); } private Set createClientWahlenDTO() { diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientMapperTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientMapperTest.java index 54fd93fc8..8bbab9581 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientMapperTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/clients/WahlenClientMapperTest.java @@ -1,5 +1,6 @@ package de.muenchen.oss.wahllokalsystem.basisdatenservice.clients; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.Farbe; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.Wahlart; import de.muenchen.oss.wahllokalsystem.basisdatenservice.eai.aou.model.WahlDTO; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlModel; @@ -36,11 +37,11 @@ void isMapped() { val expectedWahl = new WahlModel( "identifikatorWahl1", "nameWahl1", - 0L, - 0L, + 1L, + 1L, aNowMoment, Wahlart.BAW, - null, + new Farbe(0, 0, 0), "nummerWahl1"); Assertions.assertThat(result).usingRecursiveComparison().isEqualTo(expectedWahl); @@ -84,29 +85,29 @@ void isMapped() { new WahlModel( "identifikatorWahl1", "nameWahl1", - 0L, - 0L, + 1L, + 1L, LocalDate.now(), Wahlart.BAW, - null, + new Farbe(0, 0, 0), "nummerWahl1"), new WahlModel( "identifikatorWahl2", "nameWahl2", - 0L, - 0L, + 1L, + 1L, LocalDate.now(), Wahlart.BAW, - null, + new Farbe(0, 0, 0), "nummerWahl2"), new WahlModel( "identifikatorWahl3", "nameWahl3", - 0L, - 0L, + 1L, + 1L, LocalDate.now(), Wahlart.BAW, - null, + new Farbe(0, 0, 0), "nummerWahl3")); Assertions.assertThat(result).containsExactlyInAnyOrderElementsOf(expectedWahltage); diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/domain/WahlRepositoryTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/domain/WahlRepositoryTest.java index 6acd8ab53..9a84e08da 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/domain/WahlRepositoryTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/domain/WahlRepositoryTest.java @@ -2,6 +2,7 @@ import static de.muenchen.oss.wahllokalsystem.basisdatenservice.TestConstants.SPRING_NO_SECURITY_PROFILE; import static de.muenchen.oss.wahllokalsystem.basisdatenservice.TestConstants.SPRING_TEST_PROFILE; + import de.muenchen.oss.wahllokalsystem.basisdatenservice.MicroServiceApplication; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.Farbe; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.Wahl; @@ -17,6 +18,7 @@ import lombok.val; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -51,20 +53,42 @@ void findByWahltagOrderByReihenfolge() { Assertions.assertThat(foundWahlen).isEqualTo(expectedWahlen); } - @Test - void countByWahltag() { - val wahlenToSave = createWahlenList(); - repository.saveAll(wahlenToSave); - val allFoundWahl = repository.findAll(); - Assertions.assertThat(allFoundWahl.size()).isEqualTo(4); + @Nested + class ExistsByWahltag { - val wahltagToFind = LocalDate.now().minusMonths(1); - val foundNumberOfWahlByWahltag = repository.countByWahltag(wahltagToFind); - Assertions.assertThat(foundNumberOfWahlByWahltag).isEqualTo(3); + @Test + void trueWhenOneWahltagExists() { + val wahltagDateToFind = LocalDate.of(2024, 9, 3); + val wahlenToSave = List.of( + new Wahl("wahltagID1", "name1", 1, 1, wahltagDateToFind, Wahlart.BTW, null, "1"), + new Wahl("wahltagID2", "name2", 1, 1, wahltagDateToFind.plusDays(1), Wahlart.BTW, null, "1")); + repository.saveAll(wahlenToSave); + + Assertions.assertThat(repository.existsByWahltag(wahltagDateToFind)).isTrue(); + } + + @Test + void trueWhenMoreThanOneWahltagExists() { + val wahltagDateToFind = LocalDate.of(2024, 9, 3); + val wahlenToSave = List.of( + new Wahl("wahltagID1", "name1", 1, 1, wahltagDateToFind, Wahlart.BTW, null, "1"), + new Wahl("wahltagID11", "name11", 2, 2, wahltagDateToFind, Wahlart.BTW, null, "2"), + new Wahl("wahltagID2", "name2", 1, 1, wahltagDateToFind.plusDays(1), Wahlart.BTW, null, "1")); + repository.saveAll(wahlenToSave); + + Assertions.assertThat(repository.existsByWahltag(wahltagDateToFind)).isTrue(); + } + + @Test + void falseWhenNoWahltagExists() { + val wahltagDateToFind = LocalDate.of(2024, 9, 3); + val wahlenToSave = List.of( + new Wahl("wahltagID1", "name1", 1, 1, wahltagDateToFind.minusDays(1), Wahlart.BTW, null, "1"), + new Wahl("wahltagID2", "name2", 1, 1, wahltagDateToFind.plusDays(1), Wahlart.BTW, null, "1")); + repository.saveAll(wahlenToSave); - val wahltagToFind_0 = LocalDate.now().minusMonths(99); - val foundNumberOfWahlByWahltag_0 = repository.countByWahltag(wahltagToFind_0); - Assertions.assertThat(foundNumberOfWahlByWahltag_0).isEqualTo(0); + Assertions.assertThat(repository.existsByWahltag(wahltagDateToFind)).isFalse(); + } } private List createWahlenList() { diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/rest/wahlen/WahlenControllerIntegrationTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/rest/wahlen/WahlenControllerIntegrationTest.java index 6715f7462..81a26f508 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/rest/wahlen/WahlenControllerIntegrationTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/rest/wahlen/WahlenControllerIntegrationTest.java @@ -4,10 +4,10 @@ import static de.muenchen.oss.wahllokalsystem.basisdatenservice.TestConstants.SPRING_TEST_PROFILE; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + import com.fasterxml.jackson.databind.ObjectMapper; import com.github.tomakehurst.wiremock.client.WireMock; import de.muenchen.oss.wahllokalsystem.basisdatenservice.MicroServiceApplication; - import de.muenchen.oss.wahllokalsystem.basisdatenservice.clients.WahlenClientMapper; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.Wahltag; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.WahltagRepository; @@ -234,8 +234,8 @@ void fachlicheWlsExceptionWhenRequestIsInvalid() throws Exception { val response = api.perform(request).andExpect(status().isBadRequest()).andReturn(); val responseBodyAsWlsExceptionDTO = objectMapper.readValue(response.getResponse().getContentAsByteArray(), WlsExceptionDTO.class); - val expectedWlsExceptionDTO = new WlsExceptionDTO(WlsExceptionCategory.F, ExceptionConstants.CODE_POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG.code(), - serviceID, ExceptionConstants.CODE_POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG.message()); + val expectedWlsExceptionDTO = new WlsExceptionDTO(WlsExceptionCategory.F, ExceptionConstants.POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG.code(), + serviceID, ExceptionConstants.POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG.message()); Assertions.assertThat(responseBodyAsWlsExceptionDTO).isEqualTo(expectedWlsExceptionDTO); } diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/rest/wahlen/WahlenControllerTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/rest/wahlen/WahlenControllerTest.java index 4fc71bd6c..fc401554f 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/rest/wahlen/WahlenControllerTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/rest/wahlen/WahlenControllerTest.java @@ -2,12 +2,14 @@ import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlModel; import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlenService; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen.WahlenWriteModel; import java.util.List; import lombok.val; import org.assertj.core.api.Assertions; 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.Mockito; import org.mockito.junit.jupiter.MockitoExtension; @@ -18,13 +20,28 @@ class WahlenControllerTest { @Mock WahlenService wahlenService; + @Mock + WahlDTOMapper wahlDTOMapper; + + @InjectMocks + WahlenController unitUnderTest; + @Nested class GetWahlen { @Test void serviceIsCalledWithoutExceptions() { - Assertions.assertThatCode(() -> wahlenService.getWahlen("wahltagIDTest")).doesNotThrowAnyException(); - Mockito.verify(wahlenService).getWahlen("wahltagIDTest"); + val wahltagID = "wahltagID"; + + val mockedServiceResponse = List.of(Mockito.mock(WahlModel.class), Mockito.mock(WahlModel.class)); + val mockedMappedServiceResponse = List.of(Mockito.mock(WahlDTO.class), Mockito.mock(WahlDTO.class)); + + Mockito.when(wahlenService.getWahlen(wahltagID)).thenReturn(mockedServiceResponse); + Mockito.when(wahlDTOMapper.fromListOfWahlModelToListOfWahlDTO(mockedServiceResponse)).thenReturn(mockedMappedServiceResponse); + + val result = unitUnderTest.getWahlen(wahltagID); + + Assertions.assertThat(result).isSameAs(mockedMappedServiceResponse); } } @@ -33,9 +50,16 @@ class PostWahlen { @Test void serviceIsCalledWithoutExceptions() { - val theWahlModelList = List.of(WahlModel.builder().build(), WahlModel.builder().build()); - Assertions.assertThatCode(() -> wahlenService.postWahlen("wahltagIDTest", theWahlModelList)).doesNotThrowAnyException(); - Mockito.verify(wahlenService).postWahlen("wahltagIDTest", theWahlModelList); + val wahltagID = "wahltagID"; + val requestBody = List.of(Mockito.mock(WahlDTO.class)); + + val mockedMappedRequest = List.of(Mockito.mock(WahlModel.class)); + + Mockito.when(wahlDTOMapper.fromListOfWahlDTOtoListOfWahlModel(requestBody)).thenReturn(mockedMappedRequest); + + unitUnderTest.postWahlen(wahltagID, requestBody); + + Mockito.verify(wahlenService).postWahlen(new WahlenWriteModel(wahltagID, mockedMappedRequest)); } } @@ -44,7 +68,8 @@ class ResetWahlen { @Test void serviceIsCalledWithoutExceptions() { - Assertions.assertThatCode(() -> wahlenService.resetWahlen()).doesNotThrowAnyException(); + unitUnderTest.resetWahlen(); + Mockito.verify(wahlenService).resetWahlen(); } } diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeServiceTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeServiceTest.java index b8353344a..ce6234e79 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeServiceTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeServiceTest.java @@ -56,7 +56,7 @@ void throwsFachlicheWlsExceptionIfNoWahltagPresentInRepository() { val wahltagID = "_identifikatorWahltag1"; Mockito.when(wahltagRepository.findById(wahltagID)).thenReturn(Optional.empty()); val mockedWlsException = FachlicheWlsException.withCode("").buildWithMessage(""); - Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.CODE_GETWAHLBEZIRKE_NO_WAHLTAG)) + Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.GETWAHLBEZIRKE_NO_WAHLTAG)) .thenReturn(mockedWlsException); Assertions.assertThatThrownBy(() -> unitUnderTest.getWahlbezirke(wahltagID)).isInstanceOf(FachlicheWlsException.class); Mockito.verify(wahlbezirkeValidator).validWahltagIDParamOrThrow(wahltagID); diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeValidatorTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeValidatorTest.java index dbc842de7..a957e1415 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeValidatorTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlbezirke/WahlbezirkeValidatorTest.java @@ -64,7 +64,7 @@ void exceptionWhenWahlIDisEmptyOrHasNoWahltagProperty() { val noWahltagRequestParam = MockDataFactory.createWahltagList("").stream().findFirst(); noWahltagRequestParam.ifPresent(wahltag -> wahltag.setWahltag(null)); - Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.CODE_GETWAHLBEZIRKE_NO_WAHLTAG)) + Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.GETWAHLBEZIRKE_NO_WAHLTAG)) .thenReturn(mockedWlsException); Assertions.assertThatThrownBy(() -> unitUnderTest.validateWahltagForSearchingWahltagID(emptyRequestParam)).isSameAs(mockedWlsException); Assertions.assertThatThrownBy(() -> unitUnderTest.validateWahltagForSearchingWahltagID(noWahltagRequestParam)).isSameAs(mockedWlsException); diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenServiceSecurityTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenServiceSecurityTest.java index 1ef24ad4d..a5237e33d 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenServiceSecurityTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenServiceSecurityTest.java @@ -70,18 +70,10 @@ void accessGranted() { List mockedListOfEntities = createWahlEntities(); wahltagRepository.save(searchingForWahltag); wahlRepository.saveAll(mockedListOfEntities); - SecurityUtils.runWith(Authorities.REPOSITORY_READ_WAHLTAG, Authorities.SERVICE_GET_WAHLEN, Authorities.REPOSITORY_READ_WAHL, - Authorities.REPOSITORY_WRITE_WAHL); + SecurityUtils.runWith(Authorities.ALL_AUTHORITIES_GET_WAHLEN); Assertions.assertThatNoException().isThrownBy(() -> wahlenService.getWahlen(searchingForWahltag.getWahltagID())); } - @Test - void accessDeniedWhenServiceAuthoritiyIsMissing() { - SecurityUtils.runWith(Authorities.REPOSITORY_READ_WAHL, Authorities.REPOSITORY_WRITE_WAHL); - var searchingForWahltag = new Wahltag("wahltagID", LocalDate.now(), "beschreibung9", "1"); - Assertions.assertThatThrownBy(() -> wahlenService.getWahlen(searchingForWahltag.getWahltagID())).isInstanceOf(AccessDeniedException.class); - } - @ParameterizedTest(name = "{index} - {1} missing") @MethodSource("getMissingAuthoritiesVariations") void anyMissingAuthorityCausesFail(final ArgumentsAccessor argumentsAccessor) throws Exception { @@ -105,8 +97,7 @@ void anyMissingAuthorityCausesFail(final ArgumentsAccessor argumentsAccessor) th private static Stream getMissingAuthoritiesVariations() { return SecurityUtils - .buildArgumentsForMissingAuthoritiesVariations( - new String[] { Authorities.SERVICE_GET_WAHLEN, Authorities.REPOSITORY_READ_WAHL, Authorities.REPOSITORY_WRITE_WAHL }); + .buildArgumentsForMissingAuthoritiesVariations(Authorities.ALL_AUTHORITIES_GET_WAHLEN); } } @@ -116,26 +107,31 @@ class PostWahlen { @Test void accessGranted() { SecurityUtils.runWith(Authorities.SERVICE_POST_WAHLEN, Authorities.REPOSITORY_WRITE_WAHL); - var searchingForWahltag = new Wahltag("wahltagID", LocalDate.now(), "beschreibung11", "1"); + val wahltagID = "wahltagID"; List mockedListOfModels = createWahlModels(); - Assertions.assertThatNoException().isThrownBy(() -> wahlenService.postWahlen(searchingForWahltag.getWahltagID(), mockedListOfModels)); + val wahlenToWrite = new WahlenWriteModel(wahltagID, mockedListOfModels); + Assertions.assertThatNoException().isThrownBy(() -> wahlenService.postWahlen(wahlenToWrite)); } @Test void accessDeniedWhenServiceAuthorityIsMissing() { SecurityUtils.runWith(Authorities.REPOSITORY_WRITE_WAHL); - var searchingForWahltag = new Wahltag("wahltagID", LocalDate.now(), "beschreibung12", "1"); + val wahltagID = "wahltagID"; List mockedListOfModels = createWahlModels(); - Assertions.assertThatThrownBy(() -> wahlenService.postWahlen(searchingForWahltag.getWahltagID(), mockedListOfModels)) + val wahlenToWrite = new WahlenWriteModel(wahltagID, mockedListOfModels); + + Assertions.assertThatThrownBy(() -> wahlenService.postWahlen(wahlenToWrite)) .isInstanceOf(AccessDeniedException.class); } @Test void fachlicheWlsExceptionWhenRepoAuthorityIsMissing() { SecurityUtils.runWith(Authorities.SERVICE_POST_WAHLEN); - var searchingForWahltag = new Wahltag("wahltagID", LocalDate.now(), "beschreibung13", "1"); + val wahltagID = "wahltagID"; List mockedListOfModels = createWahlModels(); - Assertions.assertThatThrownBy(() -> wahlenService.postWahlen(searchingForWahltag.getWahltagID(), mockedListOfModels)).isInstanceOf( + val wahlenToWrite = new WahlenWriteModel(wahltagID, mockedListOfModels); + + Assertions.assertThatThrownBy(() -> wahlenService.postWahlen(wahlenToWrite)).isInstanceOf( FachlicheWlsException.class); } } diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenServiceTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenServiceTest.java index 9f4ef63e7..a86efb9f1 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenServiceTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenServiceTest.java @@ -1,18 +1,17 @@ package de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen; -import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.Wahltag; -import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.WahltagRepository; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.Farbe; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.Wahl; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.WahlRepository; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.wahl.Wahlart; import de.muenchen.oss.wahllokalsystem.basisdatenservice.exception.ExceptionConstants; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahltag.WahltagModel; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahltag.WahltageService; import de.muenchen.oss.wahllokalsystem.wls.common.exception.FachlicheWlsException; import de.muenchen.oss.wahllokalsystem.wls.common.exception.util.ExceptionFactory; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; -import java.util.Optional; import lombok.val; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Nested; @@ -23,7 +22,6 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.http.HttpMethod; @ExtendWith(MockitoExtension.class) class WahlenServiceTest { @@ -32,7 +30,7 @@ class WahlenServiceTest { WahlRepository wahlRepository; @Mock - WahltagRepository wahltagRepository; + WahltageService wahltageService; @Mock WahlenValidator wahlenValidator; @@ -54,45 +52,37 @@ class GetWahlen { @Test void ifRepoDataFoundThanReturnsRepoDataAndMakesNoCallToRemoteClient() { - val numberOfWahlenInRepo = 3; - var searchingForWahltag = new Wahltag("wahltagID", LocalDate.now(), "beschreibung14", "1"); - Optional mOp = Optional.of(searchingForWahltag); + var searchingForWahltag = new WahltagModel("wahltagID", LocalDate.now(), "beschreibung14", "1"); List mockedListOfEntities = createWahlEntities(); List mockedListOfModels = createWahlModels(""); - List mockedListOfModelsIfClientCall = createWahlModels("clientPraefix"); - Mockito.doNothing().when(wahlenValidator).validWahltagIDParamOrThrow("wahltagID", HttpMethod.GET); - Mockito.doNothing().when(wahlenValidator).validateWahltagForSearchingWahltagID(mOp); + Mockito.doNothing().when(wahlenValidator).validWahlenCriteriaOrThrow("wahltagID"); - Mockito.when(wahltagRepository.findById("wahltagID")).thenReturn(Optional.of(searchingForWahltag)); - Mockito.when(wahlRepository.countByWahltag(searchingForWahltag.getWahltag())).thenReturn(numberOfWahlenInRepo); - Mockito.when(wahlRepository.findByWahltagOrderByReihenfolge(searchingForWahltag.getWahltag())).thenReturn(mockedListOfEntities); - Mockito.lenient().when(wahlenClient.getWahlen(searchingForWahltag.getWahltag(), searchingForWahltag.getNummer())) - .thenReturn(mockedListOfModelsIfClientCall); - Mockito.lenient().when(wahlModelMapper.fromListOfWahlModeltoListOfWahlEntities(mockedListOfModelsIfClientCall)).thenReturn(mockedListOfEntities); + Mockito.when(wahltageService.getWahltagByID("wahltagID")).thenReturn(searchingForWahltag); + Mockito.when(wahlRepository.existsByWahltag(searchingForWahltag.wahltag())).thenReturn(true); + Mockito.when(wahlRepository.findByWahltagOrderByReihenfolge(searchingForWahltag.wahltag())).thenReturn(mockedListOfEntities); Mockito.when(wahlModelMapper.fromListOfWahlEntityToListOfWahlModel(mockedListOfEntities)).thenReturn(mockedListOfModels); val expectedResult = wahlModelMapper.fromListOfWahlEntityToListOfWahlModel(mockedListOfEntities); val result = unitUnderTest.getWahlen("wahltagID"); Assertions.assertThatCode(() -> unitUnderTest.getWahlen("wahltagID")).doesNotThrowAnyException(); Assertions.assertThat(result).isEqualTo(expectedResult); + + Mockito.verifyNoInteractions(wahlenClient); } @Test void ifRepoDataNotFoundThanReturnsRemoteClientData() { - val numberOfWahlenInRepo = 0; - var searchingForWahltag = new Wahltag("wahltagID", LocalDate.now(), "beschreibung15", "1"); - Optional mOp = Optional.of(searchingForWahltag); + var searchingForWahltag = new WahltagModel("wahltagID", LocalDate.now(), "beschreibung15", "1"); List mockedListOfEntities = createWahlEntities(); List mockedListOfModelsIfClientCall = createWahlModels("clientPraefix"); - Mockito.doNothing().when(wahlenValidator).validWahltagIDParamOrThrow("wahltagID", HttpMethod.GET); - Mockito.doNothing().when(wahlenValidator).validateWahltagForSearchingWahltagID(mOp); + Mockito.doNothing().when(wahlenValidator).validWahlenCriteriaOrThrow("wahltagID"); - Mockito.when(wahltagRepository.findById("wahltagID")).thenReturn(Optional.of(searchingForWahltag)); - Mockito.when(wahlRepository.countByWahltag(searchingForWahltag.getWahltag())).thenReturn(numberOfWahlenInRepo); - Mockito.when(wahlRepository.findByWahltagOrderByReihenfolge(searchingForWahltag.getWahltag())).thenReturn(mockedListOfEntities); - Mockito.lenient().when(wahlenClient.getWahlen(searchingForWahltag.getWahltag(), searchingForWahltag.getNummer())) + Mockito.when(wahltageService.getWahltagByID("wahltagID")).thenReturn(searchingForWahltag); + Mockito.when(wahlRepository.existsByWahltag(searchingForWahltag.wahltag())).thenReturn(false); + Mockito.when(wahlRepository.findByWahltagOrderByReihenfolge(searchingForWahltag.wahltag())).thenReturn(mockedListOfEntities); + Mockito.when(wahlenClient.getWahlen(new WahltagWithNummer(searchingForWahltag.wahltag(), searchingForWahltag.nummer()))) .thenReturn(mockedListOfModelsIfClientCall); - Mockito.lenient().when(wahlModelMapper.fromListOfWahlModeltoListOfWahlEntities(mockedListOfModelsIfClientCall)).thenReturn(mockedListOfEntities); + Mockito.when(wahlModelMapper.fromListOfWahlModeltoListOfWahlEntities(mockedListOfModelsIfClientCall)).thenReturn(mockedListOfEntities); Mockito.when(wahlModelMapper.fromListOfWahlEntityToListOfWahlModel(mockedListOfEntities)).thenReturn(mockedListOfModelsIfClientCall); val result = unitUnderTest.getWahlen("wahltagID"); @@ -106,32 +96,34 @@ class PostWahlen { @Test void dataSaved() { - var searchingForWahltag = new Wahltag("wahltagID", LocalDate.now(), "beschreibung16", "1"); + val wahltagID = "wahltagID"; List mockedListOfModels = createWahlModels(""); List mockedListOfEntities = createWahlEntities(); + val wahlenToWrite = new WahlenWriteModel(wahltagID, mockedListOfModels); Mockito.when(wahlModelMapper.fromListOfWahlModeltoListOfWahlEntities(mockedListOfModels)).thenReturn(mockedListOfEntities); - Assertions.assertThatNoException().isThrownBy(() -> unitUnderTest.postWahlen(searchingForWahltag.getWahltagID(), mockedListOfModels)); - Mockito.verify(wahlenValidator).validWahltagIDParamOrThrow(searchingForWahltag.getWahltagID(), HttpMethod.POST); + Assertions.assertThatNoException().isThrownBy(() -> unitUnderTest.postWahlen(wahlenToWrite)); + Mockito.verify(wahlenValidator).validWahlenWriteModelOrThrow(new WahlenWriteModel(wahltagID, mockedListOfModels)); Mockito.verify(wahlRepository).saveAll(mockedListOfEntities); } @Test void wlsExceptionWhenSavingFailed() { - var searchingForWahltag = new Wahltag("wahltagID", LocalDate.now(), "beschreibung17", "1"); + val wahltagID = "wahltagID"; List mockedListOfModels = createWahlModels(""); List mockedListOfEntities = createWahlEntities(); + val wahlenToWrite = new WahlenWriteModel(wahltagID, mockedListOfModels); val mockedRepoSaveException = new RuntimeException("saving failed"); val mockedWlsException = FachlicheWlsException.withCode("").buildWithMessage(""); Mockito.when(wahlModelMapper.fromListOfWahlModeltoListOfWahlEntities(mockedListOfModels)).thenReturn(mockedListOfEntities); Mockito.doThrow(mockedRepoSaveException).when(wahlRepository).saveAll(mockedListOfEntities); - Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.CODE_POSTWAHLEN_UNSAVEABLE)) + Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.POSTWAHLEN_UNSAVEABLE)) .thenReturn(mockedWlsException); - Assertions.assertThatThrownBy(() -> unitUnderTest.postWahlen(searchingForWahltag.getWahltagID(), mockedListOfModels)).isSameAs(mockedWlsException); + Assertions.assertThatThrownBy(() -> unitUnderTest.postWahlen(wahlenToWrite)).isSameAs(mockedWlsException); } } diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenValidatorTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenValidatorTest.java new file mode 100644 index 000000000..dc7bf80dd --- /dev/null +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahlen/WahlenValidatorTest.java @@ -0,0 +1,133 @@ +package de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahlen; + +import de.muenchen.oss.wahllokalsystem.basisdatenservice.exception.ExceptionConstants; +import de.muenchen.oss.wahllokalsystem.wls.common.exception.FachlicheWlsException; +import de.muenchen.oss.wahllokalsystem.wls.common.exception.util.ExceptionFactory; +import java.util.Collections; +import java.util.List; +import lombok.val; +import org.assertj.core.api.Assertions; +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.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class WahlenValidatorTest { + + @Mock + ExceptionFactory exceptionFactory; + + @InjectMocks + WahlenValidator unitUnderTest; + + @Nested + class ValidWahlenCriteriaOrThrow { + + @Test + void noExceptionWhenCriteriaIsValid() { + Assertions.assertThatNoException().isThrownBy(() -> unitUnderTest.validWahlenCriteriaOrThrow("validWahltagID")); + } + + @Test + void exceptionWhenCriteriaIsNull() { + val mockedException = FachlicheWlsException.withCode("").buildWithMessage(""); + + Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.GETWAHLEN_PARAMETER_UNVOLLSTAENDIG)).thenReturn(mockedException); + + Assertions.assertThatThrownBy(() -> unitUnderTest.validWahlenCriteriaOrThrow(null)).isSameAs(mockedException); + } + + @Test + void exceptionWhenCriteriaIsEmpty() { + val mockedException = FachlicheWlsException.withCode("").buildWithMessage(""); + + Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.GETWAHLEN_PARAMETER_UNVOLLSTAENDIG)).thenReturn(mockedException); + + Assertions.assertThatThrownBy(() -> unitUnderTest.validWahlenCriteriaOrThrow("")).isSameAs(mockedException); + } + + @Test + void exceptionWhenCriteriaIsBlank() { + val mockedException = FachlicheWlsException.withCode("").buildWithMessage(""); + + Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.GETWAHLEN_PARAMETER_UNVOLLSTAENDIG)).thenReturn(mockedException); + + Assertions.assertThatThrownBy(() -> unitUnderTest.validWahlenCriteriaOrThrow(" ")).isSameAs(mockedException); + } + } + + @Nested + class ValidWahlenWriteModelOrThrow { + + @Test + void noExceptionWhenModelIsValid() { + val validModel = new WahlenWriteModel("wahltagID", List.of(createEmptyWahlModel())); + + Assertions.assertThatNoException().isThrownBy(() -> unitUnderTest.validWahlenWriteModelOrThrow(validModel)); + } + + @Test + void exceptionWhenWahltagIDIsNull() { + val mockedException = FachlicheWlsException.withCode("").buildWithMessage(""); + + Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG)).thenReturn(mockedException); + + val invalidModel = new WahlenWriteModel(null, List.of(createEmptyWahlModel())); + + Assertions.assertThatThrownBy(() -> unitUnderTest.validWahlenWriteModelOrThrow(invalidModel)).isSameAs(mockedException); + } + + @Test + void exceptionWhenWahltagIDIsEmpty() { + val mockedException = FachlicheWlsException.withCode("").buildWithMessage(""); + + Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG)).thenReturn(mockedException); + + val invalidModel = new WahlenWriteModel("", List.of(createEmptyWahlModel())); + + Assertions.assertThatThrownBy(() -> unitUnderTest.validWahlenWriteModelOrThrow(invalidModel)).isSameAs(mockedException); + } + + @Test + void exceptionWhenWahltagIDIsBlank() { + val mockedException = FachlicheWlsException.withCode("").buildWithMessage(""); + + Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG)).thenReturn(mockedException); + + val invalidModel = new WahlenWriteModel(" ", List.of(createEmptyWahlModel())); + + Assertions.assertThatThrownBy(() -> unitUnderTest.validWahlenWriteModelOrThrow(invalidModel)).isSameAs(mockedException); + } + + @Test + void exceptionWhenWahlenIsNull() { + val mockedException = FachlicheWlsException.withCode("").buildWithMessage(""); + + Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG)).thenReturn(mockedException); + + val invalidModel = new WahlenWriteModel("wahltagID", null); + + Assertions.assertThatThrownBy(() -> unitUnderTest.validWahlenWriteModelOrThrow(invalidModel)).isSameAs(mockedException); + } + + @Test + void exceptionWhenWahlenIsEmpty() { + val mockedException = FachlicheWlsException.withCode("").buildWithMessage(""); + + Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.POSTWAHLEN_PARAMETER_UNVOLLSTAENDIG)).thenReturn(mockedException); + + val invalidModel = new WahlenWriteModel("wahltagID", Collections.emptyList()); + + Assertions.assertThatThrownBy(() -> unitUnderTest.validWahlenWriteModelOrThrow(invalidModel)).isSameAs(mockedException); + } + + private WahlModel createEmptyWahlModel() { + return new WahlModel(null, null, null, null, null, null, null, null); + } + } + +} diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltagModelMapperTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltagModelMapperTest.java index af8fbab2a..506cb5b71 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltagModelMapperTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltagModelMapperTest.java @@ -32,6 +32,21 @@ void fromWahltagEntityToWahltagModelList() { Assertions.assertThat(result).isEqualTo(expectedResult); } + @Test + void toModel() { + val wahltagID = "wahltagID"; + val wahltag = LocalDate.now(); + val beschreibung = "beschreibung"; + val nummer = "nummer"; + val entityToMap = new Wahltag(wahltagID, wahltag, beschreibung, nummer); + + val result = unitUnderTest.toModel(entityToMap); + + val expectedResult = new WahltagModel(wahltagID, wahltag, beschreibung, nummer); + + Assertions.assertThat(result).isEqualTo(expectedResult); + } + private List createWahltagList() { val wahltag1 = new Wahltag("identifikatorWahltag1", LocalDate.now().minusMonths(2), "beschreibungWahltag1", "nummerWahltag1"); val wahltag2 = new Wahltag("identifikatorWahltag2", LocalDate.now().minusMonths(1), "beschreibungWahltag2", "nummerWahltag2"); diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltageServiceTest.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltageServiceTest.java index 3cb0cd932..90332be1f 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltageServiceTest.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/services/wahltag/WahltageServiceTest.java @@ -1,11 +1,15 @@ package de.muenchen.oss.wahllokalsystem.basisdatenservice.services.wahltag; -import org.assertj.core.api.Assertions; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.Wahltag; import de.muenchen.oss.wahllokalsystem.basisdatenservice.domain.WahltagRepository; +import de.muenchen.oss.wahllokalsystem.basisdatenservice.exception.ExceptionConstants; +import de.muenchen.oss.wahllokalsystem.wls.common.exception.FachlicheWlsException; +import de.muenchen.oss.wahllokalsystem.wls.common.exception.util.ExceptionFactory; import java.time.LocalDate; import java.util.List; +import java.util.Optional; import lombok.val; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -24,6 +28,9 @@ class WahltageServiceTest { @Mock WahltageClient wahltageClient; + @Mock + ExceptionFactory exceptionFactory; + @InjectMocks WahltageService unitUnderTest; @@ -66,4 +73,34 @@ private List createWahltagModelList(String wahltagIDPrefix) { return List.of(wahltag1, wahltag2, wahltag3); } } + + @Nested + class GetWahltagById { + + @Test + void wahltagFound() { + val wahltagID = "wahltagID"; + + val mockedRepoResponse = new Wahltag(); + val mockedMappedRepoResponse = new WahltagModel(wahltagID, LocalDate.now(), null, null); + + Mockito.when(wahltagRepository.findById(wahltagID)).thenReturn(Optional.of(mockedRepoResponse)); + Mockito.when(wahltagModelMapper.toModel(mockedRepoResponse)).thenReturn(mockedMappedRepoResponse); + + Assertions.assertThat(unitUnderTest.getWahltagByID(wahltagID)).isSameAs(mockedMappedRepoResponse); + } + + @Test + void exceptionWhenNotFound() { + val wahltagID = "wahltagID"; + + val mockedRepoResponse = new Wahltag(); + val mockedWlsException = FachlicheWlsException.withCode("").buildWithMessage(""); + + Mockito.when(wahltagRepository.findById(wahltagID)).thenReturn(Optional.empty()); + Mockito.when(exceptionFactory.createFachlicheWlsException(ExceptionConstants.GETWAHLBEZIRKE_NO_WAHLTAG)).thenReturn(mockedWlsException); + + Assertions.assertThatException().isThrownBy(() -> unitUnderTest.getWahltagByID(wahltagID)).isSameAs(mockedWlsException); + } + } } diff --git a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/utils/Authorities.java b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/utils/Authorities.java index 6476927a3..2ac8632af 100644 --- a/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/utils/Authorities.java +++ b/wls-basisdaten-service/src/test/java/de/muenchen/oss/wahllokalsystem/basisdatenservice/utils/Authorities.java @@ -8,6 +8,7 @@ public class Authorities { public static final String SERVICE_GET_WAHLVORSCHLAEGE = "Basisdaten_BUSINESSACTION_GetWahlvorschlaege"; public static final String SERVICE_GET_WAHLTAGE = "Basisdaten_BUSINESSACTION_GetWahltage"; + public static final String SERVICE_GET_WAHLTAG = "Basisdaten_BUSINESSACTION_GetWahltag"; public static final String SERVICE_RESET_WAHLEN = "Basisdaten_BUSINESSACTION_ResetWahlen"; public static final String SERVICE_GET_WAHLEN = "Basisdaten_BUSINESSACTION_GetWahlen"; @@ -141,6 +142,13 @@ public class Authorities { REPOSITORY_DELETE_WAHL }; + public static final String[] ALL_AUTHORITIES_GET_WAHLEN = { + SERVICE_GET_WAHLEN, + SERVICE_GET_WAHLTAG, + REPOSITORY_READ_WAHL, + REPOSITORY_READ_WAHLTAG + }; + public static final String[] ALL_AUTHORITIES_GET_REFERENDUMVORLAGEN = { SERVICE_GET_REFERENDUMVORLAGEN, REPOSITORY_READ_REFERENDUMVORLAGEN,