test: ValidExchangeRequestValidatorTest more test cases & validator implementation
TODO: move disabled tests to future service / endpoint suite
This commit is contained in:
parent
8d722e3ce4
commit
b5237c49bc
2 changed files with 46 additions and 12 deletions
|
@ -4,12 +4,21 @@ import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
|||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
|
||||
public class ValidExchangeRequestValidator implements ConstraintValidator<ValidExchangeRequest, CurrencyExchangeRequest> {
|
||||
public class ValidExchangeRequestValidator implements
|
||||
ConstraintValidator<ValidExchangeRequest, CurrencyExchangeRequest> {
|
||||
|
||||
@Override
|
||||
public boolean isValid(CurrencyExchangeRequest currencyExchangeRequest,
|
||||
public boolean isValid(CurrencyExchangeRequest request,
|
||||
ConstraintValidatorContext constraintValidatorContext) {
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,8 +2,13 @@ package eu.ztsh.wymiana.validation;
|
|||
|
||||
import eu.ztsh.wymiana.EntityCreator;
|
||||
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
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.*;
|
||||
|
||||
|
@ -33,17 +38,13 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
|||
.build()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("Already validated (has field annotation)")
|
||||
@DisplayName("Invalid PESEL value")
|
||||
void invalidPeselTest() {
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void invalidPeselTest(String pesel) {
|
||||
assertThatValidation(EntityCreator.exchangeRequest()
|
||||
.pesel("INVALID")
|
||||
.from(PLN_SYMBOL)
|
||||
.to(USD_SYMBOL)
|
||||
.toSell(USD_SELL)
|
||||
.build()).isFalse();
|
||||
assertThatValidation(EntityCreator.exchangeRequest()
|
||||
.pesel(PESEL.replace('6', '7'))
|
||||
.pesel(pesel)
|
||||
.from(PLN_SYMBOL)
|
||||
.to(USD_SYMBOL)
|
||||
.toSell(USD_SELL)
|
||||
|
@ -69,6 +70,26 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
|||
.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
|
||||
@DisplayName("Both Buy and Sell params filled in")
|
||||
void bothFilledBuySellTest() {
|
||||
|
@ -100,4 +121,8 @@ class ValidExchangeRequestValidatorTest extends ValidatorTest<ValidExchangeReque
|
|||
.build()).isFalse();
|
||||
}
|
||||
|
||||
private static Stream<String> invalidPeselTest() {
|
||||
return Stream.of("INVALID", PESEL.replace('6', '7'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue