test: ValidExchangeRequestValidatorTest more test cases & validator implementation

TODO: move disabled tests to future service / endpoint suite
This commit is contained in:
Piotr Dec 2024-05-23 18:22:25 +02:00
parent 8d722e3ce4
commit b5237c49bc
Signed by: stawros
GPG key ID: F89F27AD8F881A91
2 changed files with 46 additions and 12 deletions

View file

@ -4,12 +4,21 @@ import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext; import jakarta.validation.ConstraintValidatorContext;
public class ValidExchangeRequestValidator implements ConstraintValidator<ValidExchangeRequest, CurrencyExchangeRequest> { public class ValidExchangeRequestValidator implements
ConstraintValidator<ValidExchangeRequest, CurrencyExchangeRequest> {
@Override @Override
public boolean isValid(CurrencyExchangeRequest currencyExchangeRequest, public boolean isValid(CurrencyExchangeRequest request,
ConstraintValidatorContext constraintValidatorContext) { ConstraintValidatorContext constraintValidatorContext) {
return false; if (request == null) {
return false;
}
return !request.from().equals(request.to())
&& !((request.toBuy() == null && request.toSell() == null)
|| (request.toBuy() != null && request.toSell() != null))
&& ((request.toBuy() != null && request.toBuy() >= 0)
|| (request.toSell() != null && request.toSell() >= 0));
} }
} }

View file

@ -2,8 +2,13 @@ package eu.ztsh.wymiana.validation;
import eu.ztsh.wymiana.EntityCreator; import eu.ztsh.wymiana.EntityCreator;
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest; import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
import org.junit.jupiter.api.Disabled;
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.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
import static eu.ztsh.wymiana.EntityCreator.Constants.*; import static eu.ztsh.wymiana.EntityCreator.Constants.*;
@ -33,17 +38,13 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
.build()).isTrue(); .build()).isTrue();
} }
@Test @Disabled("Already validated (has field annotation)")
@DisplayName("Invalid PESEL value") @DisplayName("Invalid PESEL value")
void invalidPeselTest() { @ParameterizedTest
@MethodSource
void invalidPeselTest(String pesel) {
assertThatValidation(EntityCreator.exchangeRequest() assertThatValidation(EntityCreator.exchangeRequest()
.pesel("INVALID") .pesel(pesel)
.from(PLN_SYMBOL)
.to(USD_SYMBOL)
.toSell(USD_SELL)
.build()).isFalse();
assertThatValidation(EntityCreator.exchangeRequest()
.pesel(PESEL.replace('6', '7'))
.from(PLN_SYMBOL) .from(PLN_SYMBOL)
.to(USD_SYMBOL) .to(USD_SYMBOL)
.toSell(USD_SELL) .toSell(USD_SELL)
@ -69,6 +70,26 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
.build()).isFalse(); .build()).isFalse();
} }
@Disabled("Already validated (has field annotation)")
@Test
@DisplayName("Empty 'from' value")
void emptyFromTest() {
assertThatValidation(EntityCreator.exchangeRequest()
.to(USD_SYMBOL)
.toSell(USD_SELL)
.build()).isFalse();
}
@Disabled("Already validated (has field annotation)")
@Test
@DisplayName("Empty 'to' value")
void emptyToTest() {
assertThatValidation(EntityCreator.exchangeRequest()
.from(PLN_SYMBOL)
.toSell(USD_SELL)
.build()).isFalse();
}
@Test @Test
@DisplayName("Both Buy and Sell params filled in") @DisplayName("Both Buy and Sell params filled in")
void bothFilledBuySellTest() { void bothFilledBuySellTest() {
@ -100,4 +121,8 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
.build()).isFalse(); .build()).isFalse();
} }
private static Stream<String> invalidPeselTest() {
return Stream.of("INVALID", PESEL.replace('6', '7'));
}
} }