feature/add-scenario-test-for-deceased-partner-with-community-of-heirs #137

Merged
2 changed files with 54 additions and 0 deletions
Showing only changes of commit 1cdb01b6d4 - Show all commits

View File

@ -0,0 +1,23 @@
package net.hostsharing.hsadminng.errors;
import lombok.AllArgsConstructor;
import jakarta.validation.ValidationException;
@AllArgsConstructor
public class Validate {
final String variableNames;
public static Validate validate(final String variableNames) {
return new Validate(variableNames);
}
public final void atMaxOneNonNull(final Object var1, final Object var2) {
if (var1 != null && var2 != null) {
throw new ValidationException(
"Exactly one of (" + variableNames + ") must be non-null, " +
"but are (" + var1 + ", " + var2 + ")");
}
}
}

View File

@ -0,0 +1,31 @@
package net.hostsharing.hsadminng.errors;
import org.junit.jupiter.api.Test;
import jakarta.validation.ValidationException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
class ValidateUnitTest {
@Test
void shouldFailValidationIfBothParametersAreNotNull() {
final var throwable = catchThrowable(() ->
Validate.validate("var1, var2").atMaxOneNonNull("val1", "val2")
);
assertThat(throwable).isInstanceOf(ValidationException.class)
.hasMessage("Exactly one of (var1, var2) must be non-null, but are (val1, val2)");
}
@Test
void shouldNotFailValidationIfBothParametersAreull() {
Validate.validate("var1, var2").atMaxOneNonNull(null, null);
}
@Test
void shouldNotFailValidationIfExactlyOneParameterIsNonNull() {
Validate.validate("var1, var2").atMaxOneNonNull("val1", null);
Validate.validate("var1, var2").atMaxOneNonNull(null, "val2");
}
}