Fixing NPE and DateTimeParseException in Sdf01ValidationRule. Adding validation test in Sdf01ExecutorTest.validatedExecute.
This commit is contained in:
parent
b5d92c6cee
commit
77de16d8f2
3 changed files with 201 additions and 36 deletions
|
|
@ -13,6 +13,7 @@ import ru.spcex.platform.utils.validation.IValidationRule;
|
|||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
|
|
@ -21,10 +22,13 @@ public enum Sdf01ValidationRule implements IValidationRule<ImdgValidationContext
|
|||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<SDf01> context) {
|
||||
SDf01 sdf01 = context.getValidatedObject();
|
||||
if (sdf01.getDeal() == null) {
|
||||
return of(BalanceError.CompanyNotFound);
|
||||
}
|
||||
Imdg<Company> companyImdg = context.obtainMap(IMDGDistributedNames.Map_Company, Company.class);
|
||||
Company company = companyImdg.getSingleObjectByFieldValues(Map.of("tradingCode", sdf01.getDeal()));
|
||||
if (company == null) {
|
||||
return of( BalanceError.CompanyNotFound);
|
||||
return of(BalanceError.CompanyNotFound);
|
||||
}
|
||||
context.storeObject(ValidationStored.Company, company);
|
||||
return empty();
|
||||
|
|
@ -34,6 +38,9 @@ public enum Sdf01ValidationRule implements IValidationRule<ImdgValidationContext
|
|||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<SDf01> context) {
|
||||
SDf01 sdf01 = context.getValidatedObject();
|
||||
if (sdf01.getAccount() == null) {
|
||||
return of(BalanceError.AccountNotPresent);
|
||||
}
|
||||
Imdg<Account> accountImdg = context.obtainMap(IMDGDistributedNames.Map_Account, Account.class);
|
||||
Account account = accountImdg.getSingleObjectByFieldValues(Map.of("account", sdf01.getAccount(),
|
||||
"accountType", AccountType.Clrn.getKey()));
|
||||
|
|
@ -58,7 +65,16 @@ public enum Sdf01ValidationRule implements IValidationRule<ImdgValidationContext
|
|||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<SDf01> context) {
|
||||
SDf01 sdf01 = context.getValidatedObject();
|
||||
if (!LocalDate.now().equals(LocalDate.parse(sdf01.getDat(), datFormatter))) {
|
||||
if (sdf01.getDat() == null) {
|
||||
return of(BalanceError.CurrentDateOnly);
|
||||
}
|
||||
LocalDate date;
|
||||
try {
|
||||
date = LocalDate.parse(sdf01.getDat(), datFormatter);
|
||||
} catch (DateTimeParseException e) {
|
||||
return of(BalanceError.CurrentDateOnly);
|
||||
}
|
||||
if (!LocalDate.now().equals(date)) {
|
||||
return of(BalanceError.CurrentDateOnly);
|
||||
}
|
||||
return empty();
|
||||
|
|
|
|||
|
|
@ -123,25 +123,4 @@ public abstract class AbstractServiceTest {
|
|||
return statement;
|
||||
}
|
||||
|
||||
protected SDf02 getTestSdf02(Long id, SDf01 sdf01, Long generationIdForGroup) {
|
||||
SDf02 sDf02 = new SDf02();
|
||||
sDf02.setId(id);
|
||||
sDf02.setCurr_code(sdf01.getCurr_code());
|
||||
sDf02.setAccount(sdf01.getAccount());
|
||||
sDf02.setRemainder(sdf01.getRemainder());
|
||||
sDf02.setDeal(sdf01.getDeal());
|
||||
sDf02.setAcc_code(sdf01.getAcc_code());
|
||||
sDf02.setDat(sdf01.getDat());
|
||||
sDf02.setMarket(sdf01.getMarket());
|
||||
sDf02.setAcc_name(sdf01.getAcc_name());
|
||||
sDf02.setAcc_type(sdf01.getAcc_type());
|
||||
sDf02.setSumengage(sdf01.getSumengage());
|
||||
sDf02.setSumunblock(sdf01.getSumunblock());
|
||||
sDf02.setFile_type(sdf01.getFile_type());
|
||||
sDf02.setInSDf01Id(sdf01.getId());
|
||||
sDf02.setGenerationId(generationIdForGroup);
|
||||
sDf02.setGenerationTime(Instant.now());
|
||||
sDf02.setResult("OK!");
|
||||
return sDf02;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,10 +8,17 @@ import ru.clearing.classes.statics.data.company.Company;
|
|||
import ru.clearing.classes.statics.data.sdf.SDf01;
|
||||
import ru.clearing.classes.statics.data.sdf.SDf02;
|
||||
import ru.clearing.classes.statics.data.statement.Statement;
|
||||
import ru.spcex.clearing.balance.errors.BalanceError;
|
||||
import ru.spcex.clearing.balance.utils.MatcherFactory;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.sdf01.AccountSdfRequestPart;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.balance.StatementRequest;
|
||||
import ru.spcex.platform.enumeration.AccountType;
|
||||
import ru.spcex.platform.enumeration.OperationStatus;
|
||||
import ru.spcex.platform.enumeration.Status;
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Collection;
|
||||
|
|
@ -22,15 +29,24 @@ import static ru.spcex.clearing.balance.utils.MatcherFactory.usingIgnoringFields
|
|||
|
||||
class Sdf01ExecutorTest extends AbstractServiceTest {
|
||||
|
||||
private static final MatcherFactory.Matcher<Result> RESULT_MATCHER = usingIgnoringFieldsComparator("account.created", "account.updated", "account.clearingDate");
|
||||
private static final MatcherFactory.Matcher<Result> RESULT_MATCHER = usingIgnoringFieldsComparator("account.created", "account.updated", "account.clearingDate", "generationId");
|
||||
private static final MatcherFactory.Matcher<Statement> STATEMENT_MATCHER = usingIgnoringFieldsComparator("created", "comment", "outSDfId");
|
||||
private static final MatcherFactory.Matcher<SDf02> SDF_02_MATCHER = usingIgnoringFieldsComparator("created", "comment", "outSDfId", "generationTime");
|
||||
private static final MatcherFactory.Matcher<SDf02> SDF_02_MATCHER = usingIgnoringFieldsComparator("created", "comment", "outSDfId", "generationTime", "generationId", "id");
|
||||
private static final MatcherFactory.Matcher<AccountBalance> ACCOUNT_BALANCE_MATCHER = usingIgnoringFieldsComparator("created", "updated", "clearingDate");
|
||||
|
||||
private final static DateTimeFormatter datFormatter = DateTimeFormatter.ofPattern("dd.MM.yy");
|
||||
private final Long id = currentId.getAndIncrement();
|
||||
@Autowired
|
||||
Sdf01Executor sdf01Executor;
|
||||
private SDf01 sdf01;
|
||||
private StatementRequest statementRequest;
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
super.init();
|
||||
sdf01 = getTestSdf01();
|
||||
statementRequest = new StatementRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link Sdf01Executor#execute(Collection collection, StatementRequest statementRequest)}<br>
|
||||
|
|
@ -50,23 +66,12 @@ class Sdf01ExecutorTest extends AbstractServiceTest {
|
|||
@Test
|
||||
void execute() {
|
||||
//check create statement, SDf02 and AccountBalance
|
||||
SDf01 sdf01 = new SDf01();
|
||||
sdf01.setMarket("U");
|
||||
sdf01.setDeal(deal);
|
||||
sdf01.setAccount(acc);
|
||||
sdf01.setCurr_code("RUR");
|
||||
sdf01.setDat(LocalDate.now().format(datFormatter));
|
||||
sdf01.setAcc_type("A");
|
||||
sdf01.setRemainder(amountNew.toString());
|
||||
|
||||
Company company = getTestCompany();
|
||||
companyMap.put(addresseeIdNew, company);
|
||||
|
||||
Account account = getTestAccount(id);
|
||||
accountMap.put(accountIdNew, account);
|
||||
|
||||
StatementRequest statementRequest = new StatementRequest();
|
||||
|
||||
Result predictableResult = new Result();
|
||||
predictableResult.setGenerationId(id);
|
||||
Statement predictableStatement = getTestStatement(currentId.getAndIncrement(), company, account, sdf01);
|
||||
|
|
@ -85,4 +90,169 @@ class Sdf01ExecutorTest extends AbstractServiceTest {
|
|||
SDF_02_MATCHER.assertMatch(resultSdf02, predictableSdf02);
|
||||
ACCOUNT_BALANCE_MATCHER.assertMatch(resultAccountBalance, predictableNewResult.getAccount());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link Sdf01Executor#execute(Collection collection, StatementRequest statementRequest)}<br>
|
||||
* Тест проверяет валидацию<br>
|
||||
* Входные параметры:<br>
|
||||
* accountId - {@link StatementRequest}: new StatementRequest()<br>
|
||||
* addresseeId - {@link Collection<SDf01>}<br>
|
||||
* addresseeId - {@link SDf01}<br>
|
||||
* {@link SDf01#market} - "U"<br>
|
||||
* {@link SDf01#deal} - "111111111"<br>
|
||||
* {@link SDf01#account} - "123456789"<br>
|
||||
* {@link SDf01#curr_code} - "RUR"<br>
|
||||
* {@link SDf01#dat} - текущая дата<br>
|
||||
* {@link SDf01#acc_type} - "A"<br>
|
||||
* {@link SDf01#remainder} - "1000"<br>
|
||||
*/
|
||||
@Test
|
||||
void validatedExecute() {
|
||||
//CompanyNotFound
|
||||
companyMap.delete(addresseeIdNew);
|
||||
checkError(new EnumMessage(BalanceError.CompanyNotFound));
|
||||
|
||||
sdf01.setDeal(null);
|
||||
checkError(new EnumMessage(BalanceError.CompanyNotFound));
|
||||
|
||||
//AccountNotPresent accountType=null
|
||||
sdf01.setDeal(deal);
|
||||
Company company = getTestCompany();
|
||||
companyMap.put(addresseeIdNew, company);
|
||||
Account account = new Account();
|
||||
account.setAccount(acc);
|
||||
account.setAccountStatus(Status.Active.getKey());
|
||||
accountMap.put(accountIdNew, account);
|
||||
checkErrorAccountNotPresent(company);
|
||||
|
||||
//AccountNotPresent account=null
|
||||
account.setAccount(null);
|
||||
account.setAccountType(AccountType.Clrn.getKey());
|
||||
account.setAccountStatus(Status.Active.getKey());
|
||||
accountMap.put(accountIdNew, account);
|
||||
checkErrorAccountNotPresent(company);
|
||||
|
||||
//AccountNotPresent sdf01.account==null
|
||||
sdf01.setAccount(null);
|
||||
checkErrorAccountNotPresent(company);
|
||||
|
||||
//CurrencyNotFound sdf01.curr_code =! "RUR"
|
||||
accountMap.put(accountIdNew, getTestAccount(id));
|
||||
sdf01.setAccount(acc);
|
||||
sdf01.setCurr_code("RUB");
|
||||
checkError(new EnumMessage(BalanceError.CurrencyNotFound));
|
||||
|
||||
//CurrencyNotFound sdf01.curr_code =! "RUR"
|
||||
sdf01.setCurr_code(null);
|
||||
checkError(new EnumMessage(BalanceError.CurrencyNotFound));
|
||||
|
||||
//CurrentDateOnly
|
||||
sdf01.setCurr_code("RUR");
|
||||
sdf01.setDat("19.01.23");
|
||||
checkError(new EnumMessage(BalanceError.CurrentDateOnly));
|
||||
|
||||
//CurrentDateOnly
|
||||
sdf01.setDat("19.01.2023");
|
||||
checkError(new EnumMessage(BalanceError.CurrentDateOnly));
|
||||
|
||||
//CurrentDateOnly
|
||||
sdf01.setDat(null);
|
||||
checkError(new EnumMessage(BalanceError.CurrentDateOnly));
|
||||
|
||||
//WrongAccount
|
||||
sdf01.setDat(LocalDate.now().format(datFormatter));
|
||||
sdf01.setAcc_type(null);
|
||||
checkError(new EnumMessage(BalanceError.WrongAccount));
|
||||
|
||||
//WrongMarket
|
||||
sdf01.setAcc_type("A");
|
||||
sdf01.setMarket(null);
|
||||
checkError(new EnumMessage(BalanceError.WrongMarket));
|
||||
|
||||
sdf01.setMarket("U");
|
||||
}
|
||||
|
||||
private void checkError(EnumMessage enumMessage) {
|
||||
// SDf01 sdf01 = getTestSdf01();
|
||||
SDf02 predictableSdf02 = getTestErrorSdf02(sdf01, enumMessage, id);
|
||||
Result result = sdf01Executor.execute(Collections.singletonList(sdf01), statementRequest);
|
||||
Collection<SDf02> resultsSdf02 = sdf02Imdg.getCollectionObjectsByFieldValues(Map.of("account", acc));
|
||||
SDf02 resultSdf02 = resultsSdf02.stream().max((entry1, entry2) -> entry1.getId() > entry2.getId() ? 1 : -1).get();
|
||||
SDF_02_MATCHER.assertMatch(resultSdf02, predictableSdf02);
|
||||
}
|
||||
|
||||
private void checkErrorAccountNotPresent(Company company) {
|
||||
Result predictableResult = new Result();
|
||||
predictableResult.getAccountRequests().add(createAccountRequestPart(sdf01.getId(), sdf01.getAccount(), company.getId()));
|
||||
Result result = sdf01Executor.execute(Collections.singletonList(sdf01), statementRequest);
|
||||
RESULT_MATCHER.assertMatch(result, predictableResult);
|
||||
|
||||
}
|
||||
|
||||
private SDf02 getTestSdf02(Long id, SDf01 sdf01, Long generationIdForGroup) {
|
||||
SDf02 sDf02 = new SDf02();
|
||||
sDf02.setId(id);
|
||||
sDf02.setCurr_code(sdf01.getCurr_code());
|
||||
sDf02.setAccount(sdf01.getAccount());
|
||||
sDf02.setRemainder(sdf01.getRemainder());
|
||||
sDf02.setDeal(sdf01.getDeal());
|
||||
sDf02.setAcc_code(sdf01.getAcc_code());
|
||||
sDf02.setDat(sdf01.getDat());
|
||||
sDf02.setMarket(sdf01.getMarket());
|
||||
sDf02.setAcc_name(sdf01.getAcc_name());
|
||||
sDf02.setAcc_type(sdf01.getAcc_type());
|
||||
sDf02.setSumengage(sdf01.getSumengage());
|
||||
sDf02.setSumunblock(sdf01.getSumunblock());
|
||||
sDf02.setFile_type(sdf01.getFile_type());
|
||||
sDf02.setInSDf01Id(sdf01.getId());
|
||||
sDf02.setGenerationId(generationIdForGroup);
|
||||
sDf02.setGenerationTime(Instant.now());
|
||||
sDf02.setResult("OK!");
|
||||
return sDf02;
|
||||
}
|
||||
|
||||
private SDf01 getTestSdf01() {
|
||||
SDf01 sdf01 = new SDf01();
|
||||
sdf01.setId(id);
|
||||
sdf01.setMarket("U");
|
||||
sdf01.setDeal(deal);
|
||||
sdf01.setAccount(acc);
|
||||
sdf01.setCurr_code("RUR");
|
||||
sdf01.setDat(LocalDate.now().format(datFormatter));
|
||||
sdf01.setAcc_type("A");
|
||||
sdf01.setRemainder(amountNew.toString());
|
||||
return sdf01;
|
||||
}
|
||||
|
||||
private SDf02 getTestErrorSdf02(SDf01 sdf01, EnumMessage error, Long generationIdForGroup) {
|
||||
SDf02 sDf02 = new SDf02();
|
||||
sDf02.setId(id);
|
||||
sDf02.setCurr_code(sdf01.getCurr_code());
|
||||
sDf02.setAccount(sdf01.getAccount());
|
||||
sDf02.setRemainder(sdf01.getRemainder());
|
||||
sDf02.setDeal(sdf01.getDeal());
|
||||
sDf02.setAcc_code(sdf01.getAcc_code());
|
||||
sDf02.setDat(sdf01.getDat());
|
||||
sDf02.setMarket(sdf01.getMarket());
|
||||
sDf02.setAcc_name(sdf01.getAcc_name());
|
||||
sDf02.setAcc_type(sdf01.getAcc_type());
|
||||
sDf02.setSumengage(sdf01.getSumengage());
|
||||
sDf02.setSumunblock(sdf01.getSumunblock());
|
||||
sDf02.setFile_type(sdf01.getFile_type());
|
||||
sDf02.setInSDf01Id(sdf01.getId());
|
||||
String errorId = error.getSubject().getId().toString();
|
||||
sDf02.setResult(errorId.substring(errorId.length() - 3));
|
||||
sDf02.setGenerationId(generationIdForGroup);
|
||||
sDf02.setGenerationTime(Instant.now());
|
||||
return sDf02;
|
||||
}
|
||||
|
||||
private AccountSdfRequestPart createAccountRequestPart(Long sdf01Id, String account, Long companyId) {
|
||||
AccountSdfRequestPart req = new AccountSdfRequestPart();
|
||||
req.setAccount(account);
|
||||
req.setCompanyId(companyId);
|
||||
req.setAccountType(AccountType.Clrn.getKey());
|
||||
req.setSdfId(sdf01Id);
|
||||
return req;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue