From 804b6592272ec0421da516e550c372792ad7cbd4 Mon Sep 17 00:00:00 2001 From: akulikov Date: Tue, 2 May 2023 16:41:39 +0300 Subject: [PATCH] depoAccountNew test --- .../DepoAccountValidationConfig.java | 3 +- .../account/service/DepoAccountService.java | 1 + .../service/DepoAccountServiceTest.java | 180 ++++++++++++++++++ 3 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 clearing-parent/account-service/src/test/java/ru/spcex/clearing/account/service/DepoAccountServiceTest.java diff --git a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/config/validation/DepoAccountValidationConfig.java b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/config/validation/DepoAccountValidationConfig.java index 9f41be340..10277912b 100644 --- a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/config/validation/DepoAccountValidationConfig.java +++ b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/config/validation/DepoAccountValidationConfig.java @@ -37,6 +37,7 @@ public class DepoAccountValidationConfig { context.setValidatedObject(depoAccountNewRequest); Consumer addImdg = (s) -> context.addImdg(s, imdgForValidation.get(s)); addImdg.accept(IMDGDistributedNames.Map_Company); + addImdg.accept(IMDGDistributedNames.Map_Account); addImdg.accept(IMDGDistributedNames.Map_DepoAccount); return new ValidatorImpl<>(context, IdPresentRule.instance("companyId", @@ -61,7 +62,7 @@ public class DepoAccountValidationConfig { accountStatusPredicate, accountTypePredicate); Collection accounts = accountImdg.getCollectionObjectsByPredicate(finalPredicate); - if (!accounts.isEmpty()) return null; + if (accounts.isEmpty()) return null; return AccountError.AccountAlreadyExist; }), FieldRequiredRule.instance("depoAccountType", diff --git a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/DepoAccountService.java b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/DepoAccountService.java index dcffe05f9..625e909f5 100644 --- a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/DepoAccountService.java +++ b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/DepoAccountService.java @@ -92,6 +92,7 @@ public class DepoAccountService extends QueueConsumer implements InitializingBea DepoAccount depoAccount = new DepoAccount(); depoAccount.setCompanyId(req.getCompanyId()); depoAccount.setAccountId(accountId); + depoAccount.setDepoAccountType(req.getDepoAccountType()); depoAccountId = depoAccountImdg.insert(depoAccount); txOk = true; } finally { diff --git a/clearing-parent/account-service/src/test/java/ru/spcex/clearing/account/service/DepoAccountServiceTest.java b/clearing-parent/account-service/src/test/java/ru/spcex/clearing/account/service/DepoAccountServiceTest.java new file mode 100644 index 000000000..0d6ea0bcb --- /dev/null +++ b/clearing-parent/account-service/src/test/java/ru/spcex/clearing/account/service/DepoAccountServiceTest.java @@ -0,0 +1,180 @@ +package ru.spcex.clearing.account.service; + +import org.apache.kafka.clients.consumer.MockConsumer; +import org.apache.kafka.clients.producer.MockProducer; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import ru.clearing.classes.statics.data.account.Account; +import ru.clearing.classes.statics.data.account.DepoAccount; +import ru.clearing.classes.statics.data.company.ClearingMemberCategory; +import ru.clearing.classes.statics.data.company.Company; +import ru.clearing.classes.statics.data.company.relation.Relation; +import ru.clearing.platform.dictionary.AccountTypeDictionary; +import ru.clearing.platform.dictionary.DepoAccountTypeDictionary; +import ru.spcex.clearing.account.config.BeanConfiguration; +import ru.spcex.clearing.account.config.HazelcastServiceTestConfiguration; +import ru.spcex.clearing.account.config.KafkaConfigTest; +import ru.spcex.clearing.account.config.validation.AccountValidationConfig; +import ru.spcex.clearing.account.config.validation.DepoAccountValidationConfig; +import ru.spcex.clearing.account.config.validation.ValidationConfig; +import ru.spcex.clearing.account.utils.MatcherFactory; +import ru.spcex.clearing.imdg.IMDGDistributedNames; +import ru.spcex.clearing.platform.messaging.domain.Consts; +import ru.spcex.clearing.platform.messaging.domain.cud.account.DepoAccountNewRequest; +import ru.spcex.platform.enumeration.*; +import ru.spcex.platform.imdg.api.Imdg; +import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService; + +import javax.annotation.PostConstruct; +import java.util.Map; + +import static ru.spcex.clearing.account.utils.MatcherFactory.usingIgnoringFieldsComparator; +import static ru.spcex.clearing.account.utils.TestUtils.*; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = { + BeanConfiguration.class, + ValidationConfig.class, + DepoAccountValidationConfig.class, + AccountValidationConfig.class, + AccountService.class, + DepoAccountService.class, + HazelcastServiceTestConfiguration.class, + KafkaConfigTest.class}) +class DepoAccountServiceTest { + public static final MatcherFactory.Matcher CLEARING_ACCOUNT_MATCHER = usingIgnoringFieldsComparator(); + public static final MatcherFactory.Matcher ACCOUNT_MATCHER = usingIgnoringFieldsComparator("created", "updated"); + private static final int PARTITION = 0; + private static final Long companyId = 0L; + private static final Long relationId = 0L; + private static final String DEPO_ACCOUNT_TYPE_DICT = "A"; + private static final String ACCOUNT_VALUE = "account-value"; + private static final String TRADING_CODE = "trading-code"; + + @Autowired + DepoAccountService depoAccountService; + + @Autowired + @Qualifier("hazelcastServiceTest") + private HazelcastService hazelcastServiceTest; + + @Captor + private ArgumentCaptor producerRecord; + @SpyBean + private MockProducer producer; + + private Imdg depoAccountImdg; + private Imdg accountImdg; + private Imdg companyImdg; + private Imdg depoAccountTypeDictionaryImdg; + private Imdg accountTypeDictionaryImdg; + private Imdg clearingMemberCategoryImdg; + private Imdg relationImdg; + + + @PostConstruct + private void init() { + hazelcastServiceTest.waitAvailable(); + depoAccountImdg = hazelcastServiceTest.getImdg( + IMDGDistributedNames.Map_DepoAccount, DepoAccount.class + ); + accountImdg = hazelcastServiceTest.getImdg( + IMDGDistributedNames.Map_Account, Account.class + ); + + companyImdg = hazelcastServiceTest.getImdg( + IMDGDistributedNames.Map_Company, Company.class + ); + accountTypeDictionaryImdg = hazelcastServiceTest.getImdg( + IMDGDistributedNames.Map_AccountTypeDictionary, AccountTypeDictionary.class + ); + depoAccountTypeDictionaryImdg = hazelcastServiceTest.getImdg( + IMDGDistributedNames.Map_DepoAccountTypeDictionary, DepoAccountTypeDictionary.class + ); + clearingMemberCategoryImdg = hazelcastServiceTest.getImdg( + IMDGDistributedNames.Map_ClearingMemberCategory, ClearingMemberCategory.class + ); + relationImdg = hazelcastServiceTest.getImdg( + IMDGDistributedNames.Map_Relation, Relation.class + ); + + Company company = new Company(); + company.setId(companyId); + company.setWorkflowStatus(WorkflowStatus.Active.getKey()); + company.setTradingCode(TRADING_CODE); + companyImdg.insert(company); + + AccountTypeDictionary accountTypeDictionary = new AccountTypeDictionary(); + accountTypeDictionary.setCode(AccountType.Depo.getKey()); + accountTypeDictionary.setName(AccountType.Depo.getKey()); + accountTypeDictionaryImdg.insert(accountTypeDictionary); + + DepoAccountTypeDictionary depoAccountTypeDictionary = new DepoAccountTypeDictionary(); + depoAccountTypeDictionary.setName(DEPO_ACCOUNT_TYPE_DICT); + depoAccountTypeDictionary.setCode(DEPO_ACCOUNT_TYPE_DICT); + depoAccountTypeDictionaryImdg.insert(depoAccountTypeDictionary); + + ClearingMemberCategory clearingMemberCategory = new ClearingMemberCategory(); + clearingMemberCategory.setClearingMemberCategory(ClearingCategory.B.getKey()); + clearingMemberCategory.setCompanyId(companyId); + clearingMemberCategoryImdg.insert(clearingMemberCategory); + + Relation relation = new Relation(); + relation.setId(relationId); + relation.setConsumerId(companyId); + relation.setService(Service.MKR.getKey()); + relationImdg.insert(relation); + + } + + @Test + void depoAccountNew() { + DepoAccountNewRequest depoAccountNewRequest = new DepoAccountNewRequest(); + depoAccountNewRequest.setAccount(ACCOUNT_VALUE); + depoAccountNewRequest.setCompanyId(companyId); + depoAccountNewRequest.setDepoAccountType(DEPO_ACCOUNT_TYPE_DICT); + + String jsonString = getJsonStringForNew(depoAccountNewRequest, 0L); + + addRecordToKafka((MockConsumer) depoAccountService.getConsumer(), + Consts.DESTINATION_DEPO_ACCOUNT_NEW, + PARTITION, + 0, + jsonString); + + waitingWhenAddedRecordAndCheckIt(0L, producer, producerRecord); + + Account predictableAccount = new Account(); + predictableAccount.setAccount(ACCOUNT_VALUE); + predictableAccount.setAccountType(AccountType.Depo.getKey()); + predictableAccount.setStatus(AccountStatus.ACTIVE.getKey()); + predictableAccount.setRelationId(relationId); + predictableAccount.setCompanyId(companyId); + + DepoAccount predictableDepoAccount = new DepoAccount(); + predictableDepoAccount.setCompanyId(companyId); + predictableDepoAccount.setDepoAccountType(DEPO_ACCOUNT_TYPE_DICT); + + Account resultAccountNew = accountImdg.getSingleObjectByFieldValues(Map.of("accountType", AccountType.Depo.getKey())); + DepoAccount resultDepoAccountNew = depoAccountImdg.getSingleObjectByFieldValues(Map.of("accountId", resultAccountNew.getId())); + + predictableAccount.setId(resultAccountNew.getId()); + predictableDepoAccount.setAccountId(resultAccountNew.getId()); + predictableDepoAccount.setId(resultDepoAccountNew.getId()); + + ACCOUNT_MATCHER.assertMatch(resultAccountNew, predictableAccount); + CLEARING_ACCOUNT_MATCHER.assertMatch(resultDepoAccountNew, predictableDepoAccount); + + depoAccountImdg.delete(resultDepoAccountNew); + accountImdg.delete(resultAccountNew); + } + +} \ No newline at end of file