backend-api http://jira.mfd.msk:8088/browse/CLS-331 валидация рексестов, поправил реквесты, добавил тестирвоание и исключение (todo)
This commit is contained in:
parent
c1d70b5990
commit
ca30893ed2
3 changed files with 78 additions and 7 deletions
|
|
@ -159,4 +159,12 @@ public class BankAccountNewAction implements IAction<BankAccountNewRequest> {
|
||||||
public void setAccount(String account) {
|
public void setAccount(String account) {
|
||||||
this.account = account;
|
this.account = account;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getCompanyId() {
|
||||||
|
return companyId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCompanyId(Long companyId) {
|
||||||
|
this.companyId = companyId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
package ru.spcex.clearing.backendapi.service.validation;
|
package ru.spcex.clearing.backendapi.service.validation;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import ru.spcex.clearing.backendapi.controller.request.cud.securities.MoneyMarketSecurityUpdateAction;
|
||||||
import ru.spcex.clearing.backendapi.domain.actions.IAction;
|
import ru.spcex.clearing.backendapi.domain.actions.IAction;
|
||||||
import ru.spcex.clearing.backendapi.errors.BackEndError;
|
import ru.spcex.clearing.backendapi.errors.BackEndError;
|
||||||
import ru.spcex.clearing.backendapi.meta.FieldExtracted;
|
import ru.spcex.clearing.backendapi.meta.FieldExtracted;
|
||||||
|
|
@ -28,8 +30,8 @@ import java.util.function.Function;
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class ActionMetaValidation implements InitializingBean {
|
public class ActionMetaValidation implements InitializingBean {
|
||||||
protected final Logger log = LoggerFactory.getLogger(getClass());
|
protected static final Logger log = LoggerFactory.getLogger(ActionMetaValidation.class);
|
||||||
private final Map<String, Function<Object, IValidator>> validators;
|
protected final Map<String, Function<Object, IValidator>> validators;
|
||||||
protected final MetaServer meta;
|
protected final MetaServer meta;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|
@ -67,6 +69,26 @@ public class ActionMetaValidation implements InitializingBean {
|
||||||
// нет обязательных полей для валидации
|
// нет обязательных полей для валидации
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
// Тестирование getter
|
||||||
|
{
|
||||||
|
Object object;
|
||||||
|
try {
|
||||||
|
object = metaAction.getClazz().getDeclaredConstructor().newInstance();
|
||||||
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||||
|
throw new RuntimeException("Error self-test validator on class " + metaAction.getClazz(), e);
|
||||||
|
}
|
||||||
|
for (FieldExtracted field : metaAction.getFields()) {
|
||||||
|
try {
|
||||||
|
if (field.getField().isRequired() != null && field.getField().isRequired()) {
|
||||||
|
Object value = field.extractValue(object);
|
||||||
|
}
|
||||||
|
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||||
|
log.warn("Error self-test validator on class {} and field {}: {}",
|
||||||
|
metaAction.getClazz(), field.getMemberName(), ExceptionUtils.getStackTrace(e));
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
final MetaValidatorRule metaValidatorRule = new MetaValidatorRule(metaAction);
|
final MetaValidatorRule metaValidatorRule = new MetaValidatorRule(metaAction);
|
||||||
return iAcc -> {
|
return iAcc -> {
|
||||||
ImdgValidationContext<T> ctx = new ImdgValidationContext<>();
|
ImdgValidationContext<T> ctx = new ImdgValidationContext<>();
|
||||||
|
|
@ -88,8 +110,8 @@ public class ActionMetaValidation implements InitializingBean {
|
||||||
@Override
|
@Override
|
||||||
public Optional<EnumMessage> validate(ImdgValidationContext<IAction> context) {
|
public Optional<EnumMessage> validate(ImdgValidationContext<IAction> context) {
|
||||||
IAction object = context.getValidatedObject();
|
IAction object = context.getValidatedObject();
|
||||||
try {
|
for (FieldExtracted field : metaAction.getFields()) {
|
||||||
for (FieldExtracted field : metaAction.getFields()) {
|
try {
|
||||||
if (field.getField().isRequired() != null && field.getField().isRequired()) {
|
if (field.getField().isRequired() != null && field.getField().isRequired()) {
|
||||||
Object value = field.extractValue(object);
|
Object value = field.extractValue(object);
|
||||||
if (value == null)
|
if (value == null)
|
||||||
|
|
@ -97,10 +119,16 @@ public class ActionMetaValidation implements InitializingBean {
|
||||||
// if (value instanceof String && ((String)value).isEmpty()) // пустое поле, но не null
|
// if (value instanceof String && ((String)value).isEmpty()) // пустое поле, но не null
|
||||||
// return of(BackEndError.ValidationError, field.getMemberName());
|
// return of(BackEndError.ValidationError, field.getMemberName());
|
||||||
}
|
}
|
||||||
|
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
|
||||||
|
if (object instanceof MoneyMarketSecurityUpdateAction) {
|
||||||
|
//todo в мете для moneyMarketSecurity / actions / put / для поля lotSize field="securityId" исключение - там надо оставить field, требуется для frontend
|
||||||
|
if (((MoneyMarketSecurityUpdateAction) object).getLotSize() == null)
|
||||||
|
return of(BackEndError.ValidationError, "LotSize");
|
||||||
|
} else {
|
||||||
|
log.warn("Error apply meta-validator {} for {} : {}",
|
||||||
|
metaAction, object, e.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
|
|
||||||
throw new RuntimeException("Validator error verify " + object + " by " + metaAction);
|
|
||||||
}
|
}
|
||||||
return empty();
|
return empty();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import ru.spcex.clearing.backendapi.controller.request.cud.account.BankAccountNewAction;
|
||||||
import ru.spcex.clearing.backendapi.controller.request.cud.common.CommonDeleteAction;
|
import ru.spcex.clearing.backendapi.controller.request.cud.common.CommonDeleteAction;
|
||||||
import ru.spcex.clearing.backendapi.controller.request.cud.company.ListingNewAction;
|
import ru.spcex.clearing.backendapi.controller.request.cud.company.ListingNewAction;
|
||||||
import ru.spcex.clearing.backendapi.domain.actions.IAction;
|
import ru.spcex.clearing.backendapi.domain.actions.IAction;
|
||||||
|
|
@ -56,4 +57,38 @@ class ActionMetaValidationTest {
|
||||||
assertEquals("[]", vResult.toString());
|
assertEquals("[]", vResult.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testForBankAccountNewAction() {
|
||||||
|
ActionMetaValidation metaValidator = new ActionMetaValidation(meta);
|
||||||
|
metaValidator.afterPropertiesSet();
|
||||||
|
|
||||||
|
{
|
||||||
|
BankAccountNewAction action = new BankAccountNewAction();
|
||||||
|
assertTrue(action.validate().isEmpty());
|
||||||
|
IValidator v = metaValidator.getValidator(null, action);
|
||||||
|
assertNotNull(v);
|
||||||
|
Collection<EnumMessage> vResult = v.validateAll();
|
||||||
|
assertEquals("[EnumMessage{subject=ValidationError, args: [currency]}]", vResult.toString());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
BankAccountNewAction action = new BankAccountNewAction();
|
||||||
|
action.setBankIdentificationCode("Hello world");
|
||||||
|
action.setBankName("Hello world");
|
||||||
|
action.setCorrespondentAccount("Hello world");
|
||||||
|
action.setCorrespondentAccountName("Hello world");
|
||||||
|
action.setCurrency("Hello world");
|
||||||
|
action.setDestination("Hello world");
|
||||||
|
action.setTaxpayerIdentificationNumber("Hello world");
|
||||||
|
action.setTaxRegistrationReasonCode("Hello world");
|
||||||
|
action.setAccount("Hello world");
|
||||||
|
action.setCompanyId(123L);
|
||||||
|
|
||||||
|
IValidator v = metaValidator.getValidator(null, action);
|
||||||
|
assertNotNull(v);
|
||||||
|
Collection<EnumMessage> vResult = v.validateAll();
|
||||||
|
assertEquals("[]", vResult.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue