Compare commits
2 commits
7e38b5a2e0
...
ca2a6c1795
Author | SHA1 | Date | |
---|---|---|---|
ca2a6c1795 | |||
5555e57f34 |
6 changed files with 150 additions and 24 deletions
|
@ -0,0 +1,24 @@
|
||||||
|
package eu.ztsh.wymiana.validation;
|
||||||
|
|
||||||
|
import jakarta.validation.Constraint;
|
||||||
|
import jakarta.validation.Payload;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.TYPE_USE;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@Target({ TYPE_USE })
|
||||||
|
@Documented
|
||||||
|
@Constraint(validatedBy = {ValidExchangeRequestValidator.class })
|
||||||
|
public @interface ValidExchangeRequest {
|
||||||
|
|
||||||
|
String message() default "Exchange request is not valid";
|
||||||
|
Class<?>[] groups() default { };
|
||||||
|
Class<? extends Payload>[] payload() default { };
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package eu.ztsh.wymiana.validation;
|
||||||
|
|
||||||
|
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
||||||
|
import jakarta.validation.ConstraintValidator;
|
||||||
|
import jakarta.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
|
public class ValidExchangeRequestValidator implements ConstraintValidator<ValidExchangeRequest, CurrencyExchangeRequest> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(CurrencyExchangeRequest currencyExchangeRequest,
|
||||||
|
ConstraintValidatorContext constraintValidatorContext) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package eu.ztsh.wymiana.web.model;
|
||||||
|
|
||||||
|
import eu.ztsh.wymiana.validation.ValidExchangeRequest;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.Builder;
|
||||||
|
import org.hibernate.validator.constraints.pl.PESEL;
|
||||||
|
|
||||||
|
@Builder
|
||||||
|
@ValidExchangeRequest
|
||||||
|
public record CurrencyExchangeRequest(
|
||||||
|
@PESEL String pesel,
|
||||||
|
@NotNull String from,
|
||||||
|
@NotNull String to,
|
||||||
|
Double toBuy,
|
||||||
|
Double toSell
|
||||||
|
) {
|
||||||
|
|
||||||
|
}
|
|
@ -1,58 +1,42 @@
|
||||||
package eu.ztsh.wymiana.validation;
|
package eu.ztsh.wymiana.validation;
|
||||||
|
|
||||||
import jakarta.validation.ConstraintValidatorContext;
|
|
||||||
import org.hibernate.validator.internal.engine.DefaultClockProvider;
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
class AdultValidatorTest extends ValidatorTest<AdultValidator, String> {
|
||||||
|
|
||||||
class AdultValidatorTest {
|
protected AdultValidatorTest() {
|
||||||
|
super(new AdultValidator());
|
||||||
private static AdultValidator validator;
|
|
||||||
private static ConstraintValidatorContext validatorContext;
|
|
||||||
|
|
||||||
@BeforeAll
|
|
||||||
static void prepare() {
|
|
||||||
validator = new AdultValidator();
|
|
||||||
validatorContext = Mockito.mock(ConstraintValidatorContext.class);
|
|
||||||
Mockito.when(validatorContext.getClockProvider()).thenReturn(DefaultClockProvider.INSTANCE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("No digits in PESEL")
|
@DisplayName("No digits in PESEL")
|
||||||
void invalidPatternTest() {
|
void invalidPatternTest() {
|
||||||
assertThat(call("notAPesel")).isFalse();
|
assertThatValidation("notAPesel").isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Not an adult")
|
@DisplayName("Not an adult")
|
||||||
void notAnAdultTest() {
|
void notAnAdultTest() {
|
||||||
assertThat(call("24242400000")).isFalse();
|
assertThatValidation("24242400000").isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Adult")
|
@DisplayName("Adult")
|
||||||
void adultTest() {
|
void adultTest() {
|
||||||
assertThat(call("88010100000")).isTrue();
|
assertThatValidation("88010100000").isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Elderly person")
|
@DisplayName("Elderly person")
|
||||||
void seniorTest() {
|
void seniorTest() {
|
||||||
assertThat(call("00010100000")).isTrue();
|
assertThatValidation("00010100000").isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Invalid date")
|
@DisplayName("Invalid date")
|
||||||
void notAValidDateTest() {
|
void notAValidDateTest() {
|
||||||
assertThat(call("00919100000")).isFalse();
|
assertThatValidation("00919100000").isFalse();
|
||||||
}
|
|
||||||
|
|
||||||
private boolean call(String value) {
|
|
||||||
return validator.isValid(value, validatorContext);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
package eu.ztsh.wymiana.validation;
|
||||||
|
|
||||||
|
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeRequestValidator, CurrencyExchangeRequest> {
|
||||||
|
|
||||||
|
protected ValidExchangeRequestValidatorTest() {
|
||||||
|
super(new ValidExchangeRequestValidator());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Valid request with buy value specified")
|
||||||
|
void validRequestWithBuyTest() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Valid request with sell value specified")
|
||||||
|
void validRequestWithSellTest() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Invalid PESEL value")
|
||||||
|
void invalidPeselTest() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("From and To have same value")
|
||||||
|
void sameFromToTest() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Empty amounts")
|
||||||
|
void emptyBuySellTest() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Both Buy and Sell params filled in")
|
||||||
|
void bothFilledBuySellTest() {
|
||||||
|
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
@DisplayName("Negative amount value")
|
||||||
|
void negativeAmountTest() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
31
src/test/java/eu/ztsh/wymiana/validation/ValidatorTest.java
Normal file
31
src/test/java/eu/ztsh/wymiana/validation/ValidatorTest.java
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package eu.ztsh.wymiana.validation;
|
||||||
|
|
||||||
|
import jakarta.validation.ConstraintValidator;
|
||||||
|
import jakarta.validation.ConstraintValidatorContext;
|
||||||
|
import org.assertj.core.api.AbstractBooleanAssert;
|
||||||
|
import org.hibernate.validator.internal.engine.DefaultClockProvider;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public abstract class ValidatorTest<V extends ConstraintValidator<?, C>, C> {
|
||||||
|
|
||||||
|
private final V validator;
|
||||||
|
private static ConstraintValidatorContext validatorContext;
|
||||||
|
|
||||||
|
protected ValidatorTest(V validator) {
|
||||||
|
this.validator = validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void prepare() {
|
||||||
|
validatorContext = Mockito.mock(ConstraintValidatorContext.class);
|
||||||
|
Mockito.when(validatorContext.getClockProvider()).thenReturn(DefaultClockProvider.INSTANCE);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected AbstractBooleanAssert<?> assertThatValidation(C value) {
|
||||||
|
return assertThat(validator.isValid(value, validatorContext));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue