backend-api http://jira.mfd.msk:8088/browse/CLS-331 валидация рексестов, поправил реквесты, добавил тестирвоание и исключение (todo)

This commit is contained in:
AKurakin 2023-05-25 19:33:31 +03:00
parent c1d70b5990
commit ca30893ed2
3 changed files with 78 additions and 7 deletions

View file

@ -159,4 +159,12 @@ public class BankAccountNewAction implements IAction<BankAccountNewRequest> {
public void setAccount(String account) {
this.account = account;
}
public Long getCompanyId() {
return companyId;
}
public void setCompanyId(Long companyId) {
this.companyId = companyId;
}
}

View file

@ -1,11 +1,13 @@
package ru.spcex.clearing.backendapi.service.validation;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
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.errors.BackEndError;
import ru.spcex.clearing.backendapi.meta.FieldExtracted;
@ -28,8 +30,8 @@ import java.util.function.Function;
*/
@Component
public class ActionMetaValidation implements InitializingBean {
protected final Logger log = LoggerFactory.getLogger(getClass());
private final Map<String, Function<Object, IValidator>> validators;
protected static final Logger log = LoggerFactory.getLogger(ActionMetaValidation.class);
protected final Map<String, Function<Object, IValidator>> validators;
protected final MetaServer meta;
@Autowired
@ -67,6 +69,26 @@ public class ActionMetaValidation implements InitializingBean {
// нет обязательных полей для валидации
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);
return iAcc -> {
ImdgValidationContext<T> ctx = new ImdgValidationContext<>();
@ -88,8 +110,8 @@ public class ActionMetaValidation implements InitializingBean {
@Override
public Optional<EnumMessage> validate(ImdgValidationContext<IAction> context) {
IAction object = context.getValidatedObject();
try {
for (FieldExtracted field : metaAction.getFields()) {
for (FieldExtracted field : metaAction.getFields()) {
try {
if (field.getField().isRequired() != null && field.getField().isRequired()) {
Object value = field.extractValue(object);
if (value == null)
@ -97,10 +119,16 @@ public class ActionMetaValidation implements InitializingBean {
// if (value instanceof String && ((String)value).isEmpty()) // пустое поле, но не null
// 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();
}

View file

@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
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.company.ListingNewAction;
import ru.spcex.clearing.backendapi.domain.actions.IAction;
@ -56,4 +57,38 @@ class ActionMetaValidationTest {
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());
}
}
}