generated from it-at-m/oss-repository-en-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Feat/FV 102 FV 104 Validierung der Zaehlung vor dem Speichern #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+178
−1
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
acf352c
Validator als annotation eingebaut
Der-Alex-K 79b47ea
update Validator
Der-Alex-K 39055d6
add Tests for Validator
Der-Alex-K f6aef2b
Merge remote-tracking branch 'origin/sprint' into feat/FV-102_FV-104_…
Der-Alex-K b0de8f7
anmerkungen umgesetzt
Der-Alex-K 1ea2d65
Merge remote-tracking branch 'origin/sprint' into feat/FV-102_FV-104_…
Der-Alex-K File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/de/muenchen/dave/validation/BearbeiteZaehlungValid.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package de.muenchen.dave.validation; | ||
|
|
||
| import jakarta.validation.Constraint; | ||
| import jakarta.validation.Payload; | ||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target({ ElementType.TYPE }) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Constraint(validatedBy = BearbeiteZaehlungValidator.class) | ||
| @Documented | ||
| public @interface BearbeiteZaehlungValid { | ||
| String message() default "Die zu speichernde Zählung ist nicht valide."; | ||
|
|
||
| Class<?>[] groups() default {}; | ||
|
|
||
| Class<? extends Payload>[] payload() default {}; | ||
| } |
85 changes: 85 additions & 0 deletions
85
src/main/java/de/muenchen/dave/validation/BearbeiteZaehlungValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package de.muenchen.dave.validation; | ||
|
|
||
| import de.muenchen.dave.domain.dtos.bearbeiten.BearbeiteKnotenarmDTO; | ||
| import de.muenchen.dave.domain.dtos.bearbeiten.BearbeiteZaehlungDTO; | ||
| import de.muenchen.dave.domain.enums.Fahrzeug; | ||
| import de.muenchen.dave.domain.enums.Zaehlart; | ||
| import jakarta.validation.ConstraintValidator; | ||
| import jakarta.validation.ConstraintValidatorContext; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import lombok.NoArgsConstructor; | ||
| import org.apache.commons.collections4.CollectionUtils; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @NoArgsConstructor | ||
| public class BearbeiteZaehlungValidator implements ConstraintValidator<BearbeiteZaehlungValid, BearbeiteZaehlungDTO> { | ||
|
|
||
| /** | ||
| * Prüft, ob die zu speichernde Zählung valide ist. | ||
| * | ||
| * @param toValidate {@link BearbeiteZaehlungDTO} zum Validieren. | ||
| * @param constraintValidatorContext in welchem die Validierung stattfindet. | ||
| * @return true, wenn die Zählung valide ist, sonst false. | ||
| */ | ||
| @Override | ||
| public boolean isValid(final BearbeiteZaehlungDTO toValidate, final ConstraintValidatorContext constraintValidatorContext) { | ||
| return toValidate != null && validateZaehlung(toValidate); | ||
| } | ||
|
|
||
| /** | ||
| * Ruft die einzelnen Validierungsmethoden auf und sammelt die Ergebnisse. | ||
| * | ||
| * @param toValidate {@link BearbeiteZaehlungDTO} zum Validieren. | ||
| * @return true, wenn alle Validierungen erfolgreich waren, sonst false. | ||
| */ | ||
| private boolean validateZaehlung(final BearbeiteZaehlungDTO toValidate) { | ||
| return areZaehlartAndSelectedCategoriesValid(toValidate) && areZaehlartAndSelctedKnotenarmeValid(toValidate); | ||
| } | ||
|
|
||
| /** | ||
| * Validiert anhand der ausgewaehlten Zaehlart, ob exakt 2 sich gegenueberliegende Knotenarme | ||
| * ausgewaehlt wurden. | ||
| * Erlaubte Knotenarme bei der {@link Zaehlart}.QJS : 1 & 3 || 2 & 4 || 5 & 7 || 6 & 8 | ||
| * | ||
| * @param toValidate {@link BearbeiteZaehlungDTO} zum Validieren. | ||
| * @return true, wenn die Validierung erfolgreich waren, sonst false. | ||
| */ | ||
| protected boolean areZaehlartAndSelctedKnotenarmeValid(final BearbeiteZaehlungDTO toValidate) { | ||
| boolean isValid = true; | ||
| if (Zaehlart.QJS.name().equals(toValidate.getZaehlart())) { | ||
| isValid = toValidate.getKnotenarme().size() == 2; | ||
| if (isValid) { | ||
| final Optional<Integer> reduce = toValidate.getKnotenarme() | ||
| .stream() | ||
| .map(BearbeiteKnotenarmDTO::getNummer) | ||
| .reduce((integer, integer2) -> Math.abs(integer - integer2)); | ||
| isValid = reduce.get() == 2; | ||
| } | ||
| } | ||
| return isValid; | ||
| } | ||
|
|
||
| /** | ||
| * Validiert anhand der ausgewaehlten Zaehlart, ob die richtigen Fahrzeuge ausgewaehlt wurden. | ||
| * Bei den {@link Zaehlart}.QJS FJS QU darf nur {@link Fahrzeug}.RAD und/oder .FUSS ausgewaehlt | ||
Der-Alex-K marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * sein. | ||
| * | ||
| * @param toValidate {@link BearbeiteZaehlungDTO} zum Validieren. | ||
| * @return true, wenn die Validierung erfolgreich waren, sonst false. | ||
| */ | ||
| protected boolean areZaehlartAndSelectedCategoriesValid(final BearbeiteZaehlungDTO toValidate) { | ||
| // Wenn die Zählart QJS, FJS oder QU ist, dann darf nur RAD oder FUSS ausgewählt sein | ||
| boolean isValid = true; | ||
| final List<String> zaehlarten = List.of(Zaehlart.QJS.name(), Zaehlart.FJS.name(), Zaehlart.QU.name()); | ||
| if (toValidate.getZaehlart() != null && zaehlarten.contains(toValidate.getZaehlart())) { | ||
| final List<Fahrzeug> selectedCategoriesWithoutRadAndFuss = toValidate.getKategorien() | ||
| .stream() | ||
| .filter(fahrzeug -> !(fahrzeug.equals(Fahrzeug.RAD) || fahrzeug.equals(Fahrzeug.FUSS))) | ||
| .toList(); | ||
| isValid = CollectionUtils.isEmpty(selectedCategoriesWithoutRadAndFuss); | ||
| } | ||
| return isValid; | ||
| } | ||
| } | ||
67 changes: 67 additions & 0 deletions
67
src/test/java/de/muenchen/dave/validation/BearbeiteZaehlungValidatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package de.muenchen.dave.validation; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
| import de.muenchen.dave.domain.dtos.bearbeiten.BearbeiteKnotenarmDTO; | ||
| import de.muenchen.dave.domain.dtos.bearbeiten.BearbeiteZaehlungDTO; | ||
| import de.muenchen.dave.domain.enums.Fahrzeug; | ||
| import de.muenchen.dave.domain.enums.Zaehlart; | ||
| import java.util.ArrayList; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class BearbeiteZaehlungValidatorTest { | ||
|
|
||
| private final BearbeiteZaehlungValidator validator = new BearbeiteZaehlungValidator(); | ||
|
|
||
| @Test | ||
| void isValidTest() { | ||
| assertThat(this.validator.isValid(null, null), is(false)); | ||
| assertThat(this.validator.isValid(new BearbeiteZaehlungDTO(), null), is(true)); | ||
| } | ||
|
|
||
| @Test | ||
| void areZaehlartAndSelctedKnotenarmeValidTest() { | ||
| final BearbeiteZaehlungDTO toValidate = new BearbeiteZaehlungDTO(); | ||
| toValidate.setZaehlart(Zaehlart.FJS.name()); | ||
| assertThat(this.validator.areZaehlartAndSelctedKnotenarmeValid(toValidate), is(true)); | ||
| toValidate.setZaehlart(Zaehlart.QJS.name()); | ||
| toValidate.setKnotenarme(new ArrayList<>()); | ||
| assertThat(this.validator.areZaehlartAndSelctedKnotenarmeValid(toValidate), is(false)); | ||
| final BearbeiteKnotenarmDTO node1 = new BearbeiteKnotenarmDTO(); | ||
| node1.setNummer(1); | ||
| toValidate.getKnotenarme().add(node1); | ||
| final BearbeiteKnotenarmDTO node2 = new BearbeiteKnotenarmDTO(); | ||
| toValidate.getKnotenarme().add(node2); | ||
| assertThat(this.validator.areZaehlartAndSelctedKnotenarmeValid(toValidate), is(false)); | ||
| node2.setNummer(2); | ||
| assertThat(this.validator.areZaehlartAndSelctedKnotenarmeValid(toValidate), is(false)); | ||
| node2.setNummer(3); | ||
| assertThat(this.validator.areZaehlartAndSelctedKnotenarmeValid(toValidate), is(true)); | ||
| toValidate.getKnotenarme().add(new BearbeiteKnotenarmDTO()); | ||
| assertThat(this.validator.areZaehlartAndSelctedKnotenarmeValid(toValidate), is(false)); | ||
| } | ||
|
|
||
| @Test | ||
| void areZaehlartAndSelectedCategoriesValidTest() { | ||
| final BearbeiteZaehlungDTO toValidate = new BearbeiteZaehlungDTO(); | ||
| assertThat(this.validator.areZaehlartAndSelectedCategoriesValid(toValidate), is(true)); | ||
| toValidate.setZaehlart(null); | ||
| assertThat(this.validator.areZaehlartAndSelectedCategoriesValid(toValidate), is(true)); | ||
| toValidate.setZaehlart(Zaehlart.N.name()); | ||
| assertThat(this.validator.areZaehlartAndSelectedCategoriesValid(toValidate), is(true)); | ||
| toValidate.setZaehlart(Zaehlart.QJS.name()); | ||
| toValidate.setKategorien(new ArrayList<>()); | ||
| assertThat(this.validator.areZaehlartAndSelectedCategoriesValid(toValidate), is(true)); | ||
| toValidate.getKategorien().add(Fahrzeug.FUSS); | ||
| assertThat(this.validator.areZaehlartAndSelectedCategoriesValid(toValidate), is(true)); | ||
| toValidate.setZaehlart(Zaehlart.QU.name()); | ||
| toValidate.getKategorien().add(Fahrzeug.RAD); | ||
| assertThat(this.validator.areZaehlartAndSelectedCategoriesValid(toValidate), is(true)); | ||
| toValidate.setZaehlart(Zaehlart.FJS.name()); | ||
| assertThat(this.validator.areZaehlartAndSelectedCategoriesValid(toValidate), is(true)); | ||
| toValidate.getKategorien().add(Fahrzeug.KFZ); | ||
| assertThat(this.validator.areZaehlartAndSelectedCategoriesValid(toValidate), is(false)); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.