Add Mock kafka producer. Fixing test in account-service.
This commit is contained in:
parent
a915121c3f
commit
201f74de86
11 changed files with 301 additions and 228 deletions
|
|
@ -60,6 +60,11 @@
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ public class HazelcastServiceTestConfiguration {
|
||||||
joinConfig.setTcpIpConfig(new TcpIpConfig().setEnabled(true).setMembers(List.of("127.0.0.1")));
|
joinConfig.setTcpIpConfig(new TcpIpConfig().setEnabled(true).setMembers(List.of("127.0.0.1")));
|
||||||
networkConfig.setJoin(joinConfig);
|
networkConfig.setJoin(joinConfig);
|
||||||
cfg.setNetworkConfig(networkConfig);
|
cfg.setNetworkConfig(networkConfig);
|
||||||
hazelcastInstance = Hazelcast.newHazelcastInstance(cfg);
|
hazelcastInstance = Hazelcast.getOrCreateHazelcastInstance(cfg);
|
||||||
HazelcastHelper.otcSystem_setStorageState(true, hazelcastInstance);
|
HazelcastHelper.otcSystem_setStorageState(true, hazelcastInstance);
|
||||||
return new HazelcastService(taskExecutorHazelcastClientInitializer, taskExecutorIdGeneratorAwaiter, params);
|
return new HazelcastService(taskExecutorHazelcastClientInitializer, taskExecutorIdGeneratorAwaiter, params);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,14 @@ package ru.spcex.clearing.account.config;
|
||||||
|
|
||||||
import org.apache.kafka.clients.consumer.MockConsumer;
|
import org.apache.kafka.clients.consumer.MockConsumer;
|
||||||
import org.apache.kafka.clients.consumer.OffsetResetStrategy;
|
import org.apache.kafka.clients.consumer.OffsetResetStrategy;
|
||||||
import org.apache.kafka.clients.producer.MockProducer;
|
|
||||||
import org.apache.kafka.clients.producer.Producer;
|
import org.apache.kafka.clients.producer.Producer;
|
||||||
import org.apache.kafka.common.serialization.StringSerializer;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Scope;
|
||||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||||
import ru.spcex.clearing.platform.messaging.serialization.JsonSerializer;
|
|
||||||
import ru.spcex.clearing.platform.messaging.service.RequestInfo;
|
import ru.spcex.clearing.platform.messaging.service.RequestInfo;
|
||||||
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
|
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
|
||||||
import ru.spcex.platform.imdg.api.Imdg;
|
import ru.spcex.platform.imdg.api.Imdg;
|
||||||
|
|
@ -23,7 +22,7 @@ public class KafkaConfigTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@Bean(name = "kafkaSenderTest")
|
@Bean(name = "kafkaSenderTest")
|
||||||
public KafkaSender kafkaSender(@Qualifier("mockProducerTest") Producer<String, Object> kafkaProducer, @Qualifier("hazelcastServiceTest") ImdgProvider imdgProvider) {
|
public KafkaSender kafkaSender(Producer<String, Object> kafkaProducer, @Qualifier("hazelcastServiceTest") ImdgProvider imdgProvider) {
|
||||||
ImdgId imdgIdGenerator = imdgProvider.getImdgIdGenerator();
|
ImdgId imdgIdGenerator = imdgProvider.getImdgIdGenerator();
|
||||||
return KafkaSender
|
return KafkaSender
|
||||||
.setup()
|
.setup()
|
||||||
|
|
@ -36,13 +35,10 @@ public class KafkaConfigTest {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||||
@Bean(name = "mockConsumerTest")
|
@Bean(name = "mockConsumerTest")
|
||||||
public MockConsumer<String, Object> createConsumer() {
|
public MockConsumer<String, Object> createConsumer() {
|
||||||
return new MockConsumer<>(OffsetResetStrategy.EARLIEST);
|
return new MockConsumer<>(OffsetResetStrategy.EARLIEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean(name = "mockProducerTest")
|
|
||||||
public MockProducer<String, Object> createProducer() {
|
|
||||||
return new MockProducer<>(true, new StringSerializer(), new JsonSerializer());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,21 @@ package ru.spcex.clearing.account.service;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.hazelcast.core.IMap;
|
|
||||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
|
||||||
import org.apache.kafka.clients.consumer.MockConsumer;
|
import org.apache.kafka.clients.consumer.MockConsumer;
|
||||||
import org.apache.kafka.common.TopicPartition;
|
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.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
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.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
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.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
import ru.clearing.classes.statics.data.account.Account;
|
import ru.clearing.classes.statics.data.account.Account;
|
||||||
import ru.spcex.clearing.account.config.HazelcastServiceTestConfiguration;
|
import ru.spcex.clearing.account.config.HazelcastServiceTestConfiguration;
|
||||||
import ru.spcex.clearing.account.config.KafkaConfigTest;
|
import ru.spcex.clearing.account.config.KafkaConfigTest;
|
||||||
import ru.spcex.clearing.account.utils.ImapEvent;
|
|
||||||
import ru.spcex.clearing.account.utils.MatcherFactory;
|
import ru.spcex.clearing.account.utils.MatcherFactory;
|
||||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||||
import ru.spcex.clearing.platform.messaging.domain.ActionType;
|
import ru.spcex.clearing.platform.messaging.domain.ActionType;
|
||||||
|
|
@ -27,18 +28,22 @@ import ru.spcex.clearing.platform.messaging.domain.cud.balance.AccountSdfToState
|
||||||
import ru.spcex.clearing.platform.messaging.domain.cud.balance.StatementRequest;
|
import ru.spcex.clearing.platform.messaging.domain.cud.balance.StatementRequest;
|
||||||
import ru.spcex.clearing.platform.messaging.service.RequestInfo;
|
import ru.spcex.clearing.platform.messaging.service.RequestInfo;
|
||||||
import ru.spcex.clearing.platform.messaging.service.Status;
|
import ru.spcex.clearing.platform.messaging.service.Status;
|
||||||
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
|
import ru.spcex.platform.imdg.iml.hazelcast.adapter.ImdgHazelcast;
|
||||||
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
|
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.mockito.Mockito.timeout;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static ru.spcex.clearing.account.config.HazelcastServiceTestConfiguration.currentID;
|
import static ru.spcex.clearing.account.config.HazelcastServiceTestConfiguration.currentID;
|
||||||
import static ru.spcex.clearing.account.utils.MatcherFactory.usingIgnoringFieldsComparator;
|
import static ru.spcex.clearing.account.utils.MatcherFactory.usingIgnoringFieldsComparator;
|
||||||
|
import static ru.spcex.clearing.account.utils.MockKafkaUtils.addRecordToKafka;
|
||||||
|
|
||||||
@ExtendWith(SpringExtension.class)
|
@ExtendWith(SpringExtension.class)
|
||||||
@ContextConfiguration(classes = {
|
@ContextConfiguration(classes = {
|
||||||
|
AccountService.class,
|
||||||
HazelcastServiceTestConfiguration.class,
|
HazelcastServiceTestConfiguration.class,
|
||||||
KafkaConfigTest.class})
|
KafkaConfigTest.class})
|
||||||
class AccountServiceTest {
|
class AccountServiceTest {
|
||||||
|
|
@ -46,18 +51,17 @@ class AccountServiceTest {
|
||||||
public static final MatcherFactory.Matcher<RequestInfo> REQUEST_INFO_MATCHER_MATCHER = usingIgnoringFieldsComparator("created");
|
public static final MatcherFactory.Matcher<RequestInfo> REQUEST_INFO_MATCHER_MATCHER = usingIgnoringFieldsComparator("created");
|
||||||
private static final int PARTITION = 0;
|
private static final int PARTITION = 0;
|
||||||
private static final String TOPIC_ACCOUNT_NEW = Consts.ACCOUNT_NEW;
|
private static final String TOPIC_ACCOUNT_NEW = Consts.ACCOUNT_NEW;
|
||||||
|
private static final String account = "123456789123";
|
||||||
|
@Autowired
|
||||||
|
AccountService accountService;
|
||||||
@Autowired
|
@Autowired
|
||||||
@Qualifier("hazelcastServiceTest")
|
@Qualifier("hazelcastServiceTest")
|
||||||
private HazelcastService hazelcastServiceTest;
|
private HazelcastService hazelcastServiceTest;
|
||||||
|
|
||||||
@Autowired
|
@Captor
|
||||||
@Qualifier("kafkaSenderTest")
|
private ArgumentCaptor<ProducerRecord> producerRecord;
|
||||||
private KafkaSender kafkaSender;
|
@SpyBean
|
||||||
|
private MockProducer<String, Object> producer;
|
||||||
@Autowired
|
|
||||||
@Qualifier("mockConsumerTest")
|
|
||||||
private MockConsumer<String, Object> mockConsumer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link AccountService#accountNew(BaseRequest)}<br>
|
* {@link AccountService#accountNew(BaseRequest)}<br>
|
||||||
|
|
@ -71,12 +75,12 @@ class AccountServiceTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void accountNew() throws InterruptedException {
|
void accountNew() throws InterruptedException {
|
||||||
//arrange
|
//ARRANGE
|
||||||
Long firstID = currentID.getAndIncrement();
|
Long firstID = currentID.getAndIncrement();
|
||||||
Long secondID = currentID.getAndIncrement();
|
Long secondID = currentID.getAndIncrement();
|
||||||
AccountSdfRequestPart accountSdfRequestPart = new AccountSdfRequestPart();
|
AccountSdfRequestPart accountSdfRequestPart = new AccountSdfRequestPart();
|
||||||
accountSdfRequestPart.setSdfId(firstID);
|
accountSdfRequestPart.setSdfId(firstID);
|
||||||
accountSdfRequestPart.setAccount("123456789123");
|
accountSdfRequestPart.setAccount(account);
|
||||||
accountSdfRequestPart.setCompanyId(firstID);
|
accountSdfRequestPart.setCompanyId(firstID);
|
||||||
AccountSdf01Request accountSdf01Request = new AccountSdf01Request();
|
AccountSdf01Request accountSdf01Request = new AccountSdf01Request();
|
||||||
accountSdf01Request.setGroupingSdf01Id(firstID);
|
accountSdf01Request.setGroupingSdf01Id(firstID);
|
||||||
|
|
@ -109,41 +113,33 @@ class AccountServiceTest {
|
||||||
baseRequest.setRequestPayload(statementRequest);
|
baseRequest.setRequestPayload(statementRequest);
|
||||||
|
|
||||||
Account predictableAccount = new Account();
|
Account predictableAccount = new Account();
|
||||||
predictableAccount.setAccount("123456789123");
|
predictableAccount.setAccount(account);
|
||||||
predictableAccount.setId(firstID);
|
predictableAccount.setId(firstID);
|
||||||
|
predictableAccount.setCompanyId(firstID);
|
||||||
|
|
||||||
RequestInfo predictableRequestInfo = new RequestInfo();
|
RequestInfo predictableRequestInfo = new RequestInfo();
|
||||||
predictableRequestInfo.setId(secondID);
|
predictableRequestInfo.setId(secondID);
|
||||||
predictableRequestInfo.setStatus(Status.Processing);
|
predictableRequestInfo.setStatus(Status.Processing);
|
||||||
|
|
||||||
|
|
||||||
//ACT
|
//ACT
|
||||||
|
|
||||||
hazelcastServiceTest.waitTillReadyState();
|
hazelcastServiceTest.waitTillReadyState();
|
||||||
//service set up
|
|
||||||
AccountService accountService = new AccountService(mockConsumer, hazelcastServiceTest, kafkaSender);
|
|
||||||
//callbacks set up
|
|
||||||
accountService.afterPropertiesSet();
|
|
||||||
|
|
||||||
//KAFKA
|
//KAFKA
|
||||||
mockConsumer.schedulePollTask(() -> {
|
addRecordToKafka((MockConsumer) accountService.getConsumer(), TOPIC_ACCOUNT_NEW, PARTITION, 0, jsonBaseNewRequest);
|
||||||
mockConsumer.rebalance(Collections.singletonList(new TopicPartition(TOPIC_ACCOUNT_NEW, PARTITION)));
|
|
||||||
mockConsumer.addRecord(new ConsumerRecord<>(TOPIC_ACCOUNT_NEW, PARTITION, 0, "key", jsonBaseNewRequest));
|
|
||||||
});
|
|
||||||
HashMap<TopicPartition, Long> startOffsets = new HashMap<>();
|
|
||||||
TopicPartition tp = new TopicPartition(TOPIC_ACCOUNT_NEW, PARTITION);
|
|
||||||
startOffsets.put(tp, 0L);
|
|
||||||
mockConsumer.updateBeginningOffsets(startOffsets);
|
|
||||||
|
|
||||||
IMap<Long, Account> accountIMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_Account);
|
//waiting for kafka producer send message (finale event)
|
||||||
IMap<Long, RequestInfo> requestInfoIMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_RequestInfo);
|
verify(producer, timeout(30_000L).times(1))
|
||||||
|
.send(producerRecord.capture());
|
||||||
|
BaseRequest<Object> baseRequestObject = (BaseRequest<Object>) producerRecord.getValue().value();
|
||||||
|
|
||||||
|
ImdgHazelcast<Account> accountImdg = (ImdgHazelcast<Account>) hazelcastServiceTest.getImdg(IMDGDistributedNames.Map_Account, Account.class);
|
||||||
|
ImdgHazelcast<RequestInfo> requestInfoImdg = (ImdgHazelcast<RequestInfo>) hazelcastServiceTest.getImdg(IMDGDistributedNames.Map_RequestInfo, RequestInfo.class);
|
||||||
|
|
||||||
ImapEvent imapEvent = new ImapEvent(requestInfoIMap);
|
|
||||||
imapEvent.waitWhenHappened();
|
|
||||||
//ASSERT
|
//ASSERT
|
||||||
Account accountResult = accountIMap.get(firstID);
|
Account accountResult = accountImdg.getSingleObjectBySQL(String.format("account = %s", account));
|
||||||
RequestInfo requestInfoResult = requestInfoIMap.get(secondID);
|
RequestInfo requestInfoResult = requestInfoImdg.getSingleObjectByID(baseRequestObject.getId());
|
||||||
|
|
||||||
|
assertEquals(Consts.STATEMENT_PROCESS, producerRecord.getValue().topic());
|
||||||
ACCOUNT_MATCHER.assertMatch(accountResult, predictableAccount);
|
ACCOUNT_MATCHER.assertMatch(accountResult, predictableAccount);
|
||||||
REQUEST_INFO_MATCHER_MATCHER.assertMatch(requestInfoResult, predictableRequestInfo);
|
REQUEST_INFO_MATCHER_MATCHER.assertMatch(requestInfoResult, predictableRequestInfo);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,24 +2,26 @@ package ru.spcex.clearing.account.service;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.hazelcast.core.IMap;
|
|
||||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
|
||||||
import org.apache.kafka.clients.consumer.MockConsumer;
|
import org.apache.kafka.clients.consumer.MockConsumer;
|
||||||
import org.apache.kafka.clients.consumer.OffsetResetStrategy;
|
|
||||||
import org.apache.kafka.clients.producer.MockProducer;
|
import org.apache.kafka.clients.producer.MockProducer;
|
||||||
import org.apache.kafka.common.TopicPartition;
|
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
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.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
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.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import ru.clearing.classes.statics.data.account.Account;
|
||||||
import ru.clearing.classes.statics.data.account.BankAccount;
|
import ru.clearing.classes.statics.data.account.BankAccount;
|
||||||
|
import ru.clearing.classes.statics.data.company.Company;
|
||||||
|
import ru.spcex.clearing.account.config.ErrorResolverConfig;
|
||||||
import ru.spcex.clearing.account.config.HazelcastServiceTestConfiguration;
|
import ru.spcex.clearing.account.config.HazelcastServiceTestConfiguration;
|
||||||
import ru.spcex.clearing.account.config.KafkaConfigTest;
|
import ru.spcex.clearing.account.config.KafkaConfigTest;
|
||||||
import ru.spcex.clearing.account.utils.ImapEvent;
|
import ru.spcex.clearing.account.config.ValidationConfig;
|
||||||
import ru.spcex.clearing.account.utils.MatcherFactory.Matcher;
|
import ru.spcex.clearing.account.utils.MatcherFactory.Matcher;
|
||||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||||
import ru.spcex.clearing.platform.messaging.domain.ActionType;
|
import ru.spcex.clearing.platform.messaging.domain.ActionType;
|
||||||
|
|
@ -28,46 +30,64 @@ import ru.spcex.clearing.platform.messaging.domain.Consts;
|
||||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountNewRequest;
|
import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountNewRequest;
|
||||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountUpdateRequest;
|
import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountUpdateRequest;
|
||||||
import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest;
|
import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest;
|
||||||
|
import ru.spcex.clearing.platform.messaging.service.RequestInfoUpdate;
|
||||||
|
import ru.spcex.platform.enumeration.AccountType;
|
||||||
|
import ru.spcex.platform.enumeration.Allowed;
|
||||||
|
import ru.spcex.platform.enumeration.Status;
|
||||||
|
import ru.spcex.platform.imdg.api.Imdg;
|
||||||
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
|
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
|
||||||
import ru.spcex.platform.utils.enumeration.IMessageResolver;
|
|
||||||
import ru.spcex.platform.utils.validation.IValidator;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
import javax.annotation.PostConstruct;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.mockito.Mockito.timeout;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static ru.spcex.clearing.account.utils.MatcherFactory.usingIgnoringFieldsComparator;
|
import static ru.spcex.clearing.account.utils.MatcherFactory.usingIgnoringFieldsComparator;
|
||||||
|
import static ru.spcex.clearing.account.utils.MockKafkaUtils.addRecordToKafka;
|
||||||
|
import static ru.spcex.clearing.platform.messaging.service.Status.Success;
|
||||||
|
|
||||||
@ExtendWith(SpringExtension.class)
|
@ExtendWith(SpringExtension.class)
|
||||||
@ContextConfiguration(classes = {
|
@ContextConfiguration(classes = {
|
||||||
|
ErrorResolverConfig.class,
|
||||||
|
BankAccountService.class,
|
||||||
|
ValidationConfig.class,
|
||||||
HazelcastServiceTestConfiguration.class,
|
HazelcastServiceTestConfiguration.class,
|
||||||
KafkaConfigTest.class})
|
KafkaConfigTest.class})
|
||||||
public class BankAccountServiceTest {
|
public class BankAccountServiceTest {
|
||||||
public static final Matcher<BankAccount> BANK_ACCOUNT_MATCHER = usingIgnoringFieldsComparator();
|
public static final Matcher<BankAccount> BANK_ACCOUNT_MATCHER = usingIgnoringFieldsComparator();
|
||||||
|
public static final Matcher<Account> ACCOUNT_MATCHER = usingIgnoringFieldsComparator();
|
||||||
|
public static final Matcher<BaseRequest<Object>> BASE_REQUEST_MATCHER = usingIgnoringFieldsComparator();
|
||||||
private static final int PARTITION = 0;
|
private static final int PARTITION = 0;
|
||||||
private static final String TOPIC_ACCOUNT_NEW = Consts.DESTINATION_BANK_ACCOUNT_NEW;
|
private static final String TOPIC_ACCOUNT_NEW = Consts.DESTINATION_BANK_ACCOUNT_NEW;
|
||||||
private static final String TOPIC_ACCOUNT_UPDATE = Consts.DESTINATION_BANK_ACCOUNT_UPDATE;
|
private static final String TOPIC_ACCOUNT_UPDATE = Consts.DESTINATION_BANK_ACCOUNT_UPDATE;
|
||||||
private static final String TOPIC_ACCOUNT_DELETE = Consts.DESTINATION_BANK_ACCOUNT_DELETE;
|
private static final String TOPIC_ACCOUNT_DELETE = Consts.DESTINATION_BANK_ACCOUNT_DELETE;
|
||||||
private static final Long ID = 0L;
|
private static final Long ID = 0L;
|
||||||
|
private static final Long accountId = 12L;
|
||||||
|
protected final Long addresseeIdNew = 2L;
|
||||||
|
protected final String deal = "111111111";
|
||||||
|
private final String acc = "0123456789";
|
||||||
|
protected Imdg<Company> companyImdg;
|
||||||
|
private Imdg<BankAccount> bankAccountImdg;
|
||||||
|
private Imdg<Account> accountImdg;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@Qualifier("hazelcastServiceTest")
|
@Qualifier("hazelcastServiceTest")
|
||||||
private HazelcastService hazelcastServiceTest;
|
private HazelcastService hazelcastServiceTest;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private IMessageResolver messageResolver;
|
private BankAccountService bankAccountService;
|
||||||
|
|
||||||
@Autowired
|
@Captor
|
||||||
private Function<BankAccountNewRequest, IValidator> bankAccountNewRequestValidator;
|
private ArgumentCaptor<ProducerRecord> producerRecord;
|
||||||
|
@SpyBean
|
||||||
|
private MockProducer<String, Object> producer;
|
||||||
|
|
||||||
private MockConsumer<String, Object> mockConsumer;
|
@PostConstruct
|
||||||
private MockProducer<String, Object> mockProducer;
|
private void init() {
|
||||||
|
hazelcastServiceTest.waitAvailable();
|
||||||
@BeforeEach
|
companyImdg = hazelcastServiceTest.getImdg(IMDGDistributedNames.Map_Company, Company.class);
|
||||||
void setUp() {
|
bankAccountImdg = hazelcastServiceTest.getImdg(IMDGDistributedNames.Map_BankAccount, BankAccount.class);
|
||||||
mockConsumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST);
|
accountImdg = hazelcastServiceTest.getImdg(IMDGDistributedNames.Map_Account, Account.class);
|
||||||
mockProducer = new MockProducer<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -84,19 +104,32 @@ public class BankAccountServiceTest {
|
||||||
* {@link BankAccountNewRequest#taxRegistrationReasonCode} - 886886<br>
|
* {@link BankAccountNewRequest#taxRegistrationReasonCode} - 886886<br>
|
||||||
* {@link BankAccountNewRequest#account} - 123456789123<br>
|
* {@link BankAccountNewRequest#account} - 123456789123<br>
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
public void bankAccountNew() throws InterruptedException {
|
public void bankAccountNew() throws InterruptedException {
|
||||||
//arrange
|
//ARRANGE
|
||||||
|
BaseRequest<Object> predictableBaseRequest = new BaseRequest<>();
|
||||||
|
predictableBaseRequest.setId(ID);
|
||||||
|
predictableBaseRequest.setActionType(ActionType.SYSTEM);
|
||||||
|
RequestInfoUpdate requestInfoUpdate = new RequestInfoUpdate();
|
||||||
|
requestInfoUpdate.setId(ID);
|
||||||
|
requestInfoUpdate.setStatus(Success);
|
||||||
|
predictableBaseRequest.setRequestPayload(requestInfoUpdate);
|
||||||
|
|
||||||
|
BankAccount predictableBankAccount = getBankAccount();
|
||||||
|
Company company = getTestCompany();
|
||||||
|
companyImdg.insert(company);
|
||||||
|
Account predictableAccount = getTestAccount(accountId, acc);
|
||||||
BankAccountNewRequest bankAccountNewRequest = new BankAccountNewRequest();
|
BankAccountNewRequest bankAccountNewRequest = new BankAccountNewRequest();
|
||||||
bankAccountNewRequest.setBankName("ooo tinkoff");
|
bankAccountNewRequest.setBankName(predictableBankAccount.getBankName());
|
||||||
bankAccountNewRequest.setBankIdentificationCode("99999");
|
bankAccountNewRequest.setBankIdentificationCode(predictableBankAccount.getBankIdentificationCode());
|
||||||
bankAccountNewRequest.setCorrespondentAccount("9294189285498598598");
|
bankAccountNewRequest.setCorrespondentAccount(predictableBankAccount.getCorrespondentAccount());
|
||||||
bankAccountNewRequest.setCorrespondentAccountName("BIK OF TINKOFF");
|
bankAccountNewRequest.setCorrespondentAccountName(predictableBankAccount.getCorrespondentAccountName());
|
||||||
bankAccountNewRequest.setCurrency("RUB");
|
bankAccountNewRequest.setCurrency(predictableBankAccount.getCurrency());
|
||||||
bankAccountNewRequest.setDestination("OOO ROGA I KOPITA");
|
bankAccountNewRequest.setDestination(predictableBankAccount.getDestination());
|
||||||
bankAccountNewRequest.setTaxpayerIdentificationNumber("848484848484");
|
bankAccountNewRequest.setTaxpayerIdentificationNumber(predictableBankAccount.getTaxpayerIdentificationNumber());
|
||||||
bankAccountNewRequest.setTaxRegistrationReasonCode("886886");
|
bankAccountNewRequest.setTaxRegistrationReasonCode(predictableBankAccount.getTaxRegistrationReasonCode());
|
||||||
bankAccountNewRequest.setAccount("123456789123");
|
bankAccountNewRequest.setAccount(predictableBankAccount.getAccount());
|
||||||
|
bankAccountNewRequest.setCompanyId(predictableBankAccount.getCompanyId());
|
||||||
|
|
||||||
BaseRequest<BankAccountNewRequest> baseNewRequest = new BaseRequest<>();
|
BaseRequest<BankAccountNewRequest> baseNewRequest = new BaseRequest<>();
|
||||||
baseNewRequest.setRequestPayload(bankAccountNewRequest);
|
baseNewRequest.setRequestPayload(bankAccountNewRequest);
|
||||||
|
|
@ -109,47 +142,26 @@ public class BankAccountServiceTest {
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
BankAccount predictableResult = new BankAccount();
|
|
||||||
predictableResult.setBankName("ooo tinkoff");
|
|
||||||
predictableResult.setBankIdentificationCode("99999");
|
|
||||||
predictableResult.setCorrespondentAccount("9294189285498598598");
|
|
||||||
predictableResult.setCorrespondentAccountName("BIK OF TINKOFF");
|
|
||||||
predictableResult.setCurrency("RUB");
|
|
||||||
predictableResult.setDestination("OOO ROGA I KOPITA");
|
|
||||||
predictableResult.setTaxpayerIdentificationNumber("848484848484");
|
|
||||||
predictableResult.setTaxRegistrationReasonCode("886886");
|
|
||||||
predictableResult.setId(ID);
|
|
||||||
|
|
||||||
//ACT
|
//ACT
|
||||||
hazelcastServiceTest.waitTillReadyState();
|
addRecordToKafka((MockConsumer) bankAccountService.getConsumer(), TOPIC_ACCOUNT_NEW, PARTITION, 0, jsonBaseNewRequest);
|
||||||
//service set up
|
|
||||||
BankAccountService bankAccountService = new BankAccountService(mockConsumer, mockProducer, hazelcastServiceTest, messageResolver, bankAccountNewRequestValidator);
|
|
||||||
//callbacks set up
|
|
||||||
bankAccountService.afterPropertiesSet();
|
|
||||||
|
|
||||||
//KAFKA
|
//waiting for kafka producer send message (finale event)
|
||||||
mockConsumer.schedulePollTask(() -> {
|
verify(producer, timeout(30_000L).times(1))
|
||||||
mockConsumer.rebalance(Collections.singletonList(new TopicPartition(TOPIC_ACCOUNT_NEW, PARTITION)));
|
.send(producerRecord.capture());
|
||||||
mockConsumer.addRecord(new ConsumerRecord<>(TOPIC_ACCOUNT_NEW, PARTITION, 0, "key", jsonBaseNewRequest));
|
BaseRequest<Object> baseRequestResult = (BaseRequest<Object>) producerRecord.getValue().value();
|
||||||
});
|
|
||||||
|
|
||||||
|
Account accountResult = accountImdg.getSingleObjectBySQL(String.format("account = %s", acc));
|
||||||
HashMap<TopicPartition, Long> startOffsets = new HashMap<>();
|
BankAccount bankAccountResult = bankAccountImdg.getSingleObjectBySQL(String.format("account = %s or companyId = %s", acc, addresseeIdNew));
|
||||||
TopicPartition tp = new TopicPartition(TOPIC_ACCOUNT_NEW, PARTITION);
|
predictableBankAccount.setAccountId(accountResult.getId());
|
||||||
startOffsets.put(tp, 0L);
|
predictableBankAccount.setId(bankAccountResult.getId());
|
||||||
mockConsumer.updateBeginningOffsets(startOffsets);
|
setSameValueToField(accountResult, predictableAccount);
|
||||||
|
|
||||||
IMap<Long, BankAccount> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_BankAccount);
|
|
||||||
|
|
||||||
//waiting for hazelcast map item updates
|
|
||||||
ImapEvent imapEvent = new ImapEvent(iMap);
|
|
||||||
imapEvent.waitWhenHappened();
|
|
||||||
|
|
||||||
//ASSERT
|
//ASSERT
|
||||||
BankAccount result = iMap.get(ID);
|
assertEquals(Consts.REQUEST_INFO_UPDATE, producerRecord.getValue().topic());
|
||||||
|
BANK_ACCOUNT_MATCHER.assertMatch(bankAccountResult, predictableBankAccount);
|
||||||
BANK_ACCOUNT_MATCHER.assertMatch(result, predictableResult);
|
ACCOUNT_MATCHER.assertMatch(accountResult, predictableAccount);
|
||||||
assertTrue(mockProducer.history().get(0).topic().equalsIgnoreCase(Consts.REQUEST_INFO_UPDATE));
|
BASE_REQUEST_MATCHER.assertMatch(baseRequestResult, predictableBaseRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -166,31 +178,48 @@ public class BankAccountServiceTest {
|
||||||
* {@link BankAccountUpdateRequest#taxRegistrationReasonCode} - 532137<br>
|
* {@link BankAccountUpdateRequest#taxRegistrationReasonCode} - 532137<br>
|
||||||
* {@link BankAccountUpdateRequest#account} - 326984656514<br>
|
* {@link BankAccountUpdateRequest#account} - 326984656514<br>
|
||||||
*/
|
*/
|
||||||
// @Test
|
@Test
|
||||||
void bankAccountUpdate() throws InterruptedException {
|
void bankAccountUpdate() throws InterruptedException {
|
||||||
//arrange
|
//ARRANGE
|
||||||
BankAccount bankAccountExists = new BankAccount();
|
//ARRANGE
|
||||||
bankAccountExists.setId(ID);
|
BaseRequest<Object> predictableBaseRequest = new BaseRequest<>();
|
||||||
bankAccountExists.setBankName("ooo tinkoff");
|
predictableBaseRequest.setId(ID);
|
||||||
bankAccountExists.setBankIdentificationCode("99999");
|
predictableBaseRequest.setActionType(ActionType.SYSTEM);
|
||||||
bankAccountExists.setCorrespondentAccount("9294189285498598598");
|
RequestInfoUpdate requestInfoUpdate = new RequestInfoUpdate();
|
||||||
bankAccountExists.setCorrespondentAccountName("BIK OF TINKOFF");
|
requestInfoUpdate.setId(ID);
|
||||||
bankAccountExists.setCurrency("RUB");
|
requestInfoUpdate.setStatus(Success);
|
||||||
bankAccountExists.setDestination("OOO ROGA I KOPITA");
|
predictableBaseRequest.setRequestPayload(requestInfoUpdate);
|
||||||
bankAccountExists.setTaxpayerIdentificationNumber("848484848484");
|
|
||||||
bankAccountExists.setTaxRegistrationReasonCode("886886");
|
// Company company = getTestCompany();
|
||||||
|
// companyImdg.insert(company);
|
||||||
|
Account predictableAccount = getTestAccount(accountId, acc);
|
||||||
|
accountImdg.insert(predictableAccount);
|
||||||
|
|
||||||
|
BankAccount bankAccountExists = getBankAccount();
|
||||||
|
bankAccountImdg.insert(bankAccountExists);
|
||||||
|
BankAccount predictableUpdateBankAccount = new BankAccount();
|
||||||
|
predictableUpdateBankAccount.setBankName("NEW BUNK NAME");
|
||||||
|
predictableUpdateBankAccount.setBankIdentificationCode("88888");
|
||||||
|
predictableUpdateBankAccount.setCorrespondentAccount("894984646541316");
|
||||||
|
predictableUpdateBankAccount.setCorrespondentAccountName("BIK OF NEW BUNK");
|
||||||
|
predictableUpdateBankAccount.setCurrency("EU");
|
||||||
|
predictableUpdateBankAccount.setDestination("OOO NEW BUNK");
|
||||||
|
predictableUpdateBankAccount.setTaxpayerIdentificationNumber("65468461321");
|
||||||
|
predictableUpdateBankAccount.setTaxRegistrationReasonCode("532137");
|
||||||
|
predictableUpdateBankAccount.setAccount(acc);
|
||||||
|
predictableUpdateBankAccount.setId(ID);
|
||||||
|
|
||||||
BankAccountUpdateRequest bankAccountUpdateRequest = new BankAccountUpdateRequest();
|
BankAccountUpdateRequest bankAccountUpdateRequest = new BankAccountUpdateRequest();
|
||||||
bankAccountUpdateRequest.setId(ID);
|
bankAccountUpdateRequest.setId(predictableUpdateBankAccount.getId());
|
||||||
bankAccountUpdateRequest.setBankName("NEW BUNK NAME");
|
bankAccountUpdateRequest.setBankName(predictableUpdateBankAccount.getBankName());
|
||||||
bankAccountUpdateRequest.setBankIdentificationCode("88888");
|
bankAccountUpdateRequest.setBankIdentificationCode(predictableUpdateBankAccount.getBankIdentificationCode());
|
||||||
bankAccountUpdateRequest.setCorrespondentAccount("894984646541316");
|
bankAccountUpdateRequest.setCorrespondentAccount(predictableUpdateBankAccount.getCorrespondentAccount());
|
||||||
bankAccountUpdateRequest.setCorrespondentAccountName("BIK OF NEW BUNK");
|
bankAccountUpdateRequest.setCorrespondentAccountName(predictableUpdateBankAccount.getCorrespondentAccountName());
|
||||||
bankAccountUpdateRequest.setCurrency("EU");
|
bankAccountUpdateRequest.setCurrency(predictableUpdateBankAccount.getCurrency());
|
||||||
bankAccountUpdateRequest.setDestination("OOO NEW BUNK");
|
bankAccountUpdateRequest.setDestination(predictableUpdateBankAccount.getDestination());
|
||||||
bankAccountUpdateRequest.setTaxpayerIdentificationNumber("65468461321");
|
bankAccountUpdateRequest.setTaxpayerIdentificationNumber(predictableUpdateBankAccount.getTaxpayerIdentificationNumber());
|
||||||
bankAccountUpdateRequest.setTaxRegistrationReasonCode("532137");
|
bankAccountUpdateRequest.setTaxRegistrationReasonCode(predictableUpdateBankAccount.getTaxRegistrationReasonCode());
|
||||||
bankAccountUpdateRequest.setAccount("326984656514");
|
bankAccountUpdateRequest.setAccount(predictableUpdateBankAccount.getAccount());
|
||||||
|
|
||||||
BaseRequest<BankAccountUpdateRequest> baseUpdateRequest = new BaseRequest<>();
|
BaseRequest<BankAccountUpdateRequest> baseUpdateRequest = new BaseRequest<>();
|
||||||
baseUpdateRequest.setRequestPayload(bankAccountUpdateRequest);
|
baseUpdateRequest.setRequestPayload(bankAccountUpdateRequest);
|
||||||
|
|
@ -203,47 +232,29 @@ public class BankAccountServiceTest {
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
BankAccount predictableUpdateResult = new BankAccount();
|
|
||||||
predictableUpdateResult.setBankName("NEW BUNK NAME");
|
|
||||||
predictableUpdateResult.setBankIdentificationCode("88888");
|
|
||||||
predictableUpdateResult.setCorrespondentAccount("894984646541316");
|
|
||||||
predictableUpdateResult.setCorrespondentAccountName("BIK OF NEW BUNK");
|
|
||||||
predictableUpdateResult.setCurrency("EU");
|
|
||||||
predictableUpdateResult.setDestination("OOO NEW BUNK");
|
|
||||||
predictableUpdateResult.setTaxpayerIdentificationNumber("65468461321");
|
|
||||||
predictableUpdateResult.setTaxRegistrationReasonCode("532137");
|
|
||||||
predictableUpdateResult.setId(ID);
|
|
||||||
|
|
||||||
//ACT
|
//ACT
|
||||||
|
addRecordToKafka((MockConsumer) bankAccountService.getConsumer(), TOPIC_ACCOUNT_UPDATE, PARTITION, 0, jsonBaseUpdateRequest);
|
||||||
|
|
||||||
hazelcastServiceTest.waitTillReadyState();
|
//waiting for kafka producer send message (finale event)
|
||||||
//service set up
|
verify(producer, timeout(30_000L).times(1))
|
||||||
BankAccountService bankAccountService = new BankAccountService(mockConsumer, mockProducer, hazelcastServiceTest, messageResolver, bankAccountNewRequestValidator);
|
.send(producerRecord.capture());
|
||||||
//callbacks set up
|
BaseRequest<Object> baseRequestResult = (BaseRequest<Object>) producerRecord.getValue().value();
|
||||||
bankAccountService.afterPropertiesSet();
|
|
||||||
|
|
||||||
IMap<Long, BankAccount> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_BankAccount);
|
Account accountResult = accountImdg.getSingleObjectBySQL(String.format("account = %s", acc));
|
||||||
iMap.put(ID, bankAccountExists);
|
BankAccount resultUpdating = bankAccountImdg.getSingleObjectBySQL(String.format("account = %s or companyId = %s", acc, addresseeIdNew));
|
||||||
ImapEvent imapEvent = new ImapEvent(iMap);
|
predictableUpdateBankAccount.setAccountId(resultUpdating.getAccountId());
|
||||||
|
predictableUpdateBankAccount.setCompanyId(resultUpdating.getCompanyId());
|
||||||
//KAFKA
|
predictableAccount.setUpdated(accountResult.getUpdated());
|
||||||
mockConsumer.schedulePollTask(() -> {
|
|
||||||
mockConsumer.rebalance(Collections.singletonList(new TopicPartition(TOPIC_ACCOUNT_UPDATE, PARTITION)));
|
|
||||||
mockConsumer.addRecord(new ConsumerRecord<>(TOPIC_ACCOUNT_UPDATE, PARTITION, 0, "key", jsonBaseUpdateRequest));
|
|
||||||
});
|
|
||||||
HashMap<TopicPartition, Long> startOffsetsUpdating = new HashMap<>();
|
|
||||||
TopicPartition tpUpdating = new TopicPartition(TOPIC_ACCOUNT_UPDATE, PARTITION);
|
|
||||||
startOffsetsUpdating.put(tpUpdating, 0L);
|
|
||||||
mockConsumer.updateBeginningOffsets(startOffsetsUpdating);
|
|
||||||
|
|
||||||
//waiting for hazelcast map item updates
|
|
||||||
imapEvent.waitWhenHappened();
|
|
||||||
|
|
||||||
//ASSERT
|
//ASSERT
|
||||||
BankAccount resultUpdating = iMap.get(ID);
|
assertEquals(Consts.REQUEST_INFO_UPDATE, producerRecord.getValue().topic());
|
||||||
BANK_ACCOUNT_MATCHER.assertMatch(resultUpdating, predictableUpdateResult);
|
|
||||||
|
|
||||||
assertTrue(mockProducer.history().get(0).topic().equalsIgnoreCase(Consts.REQUEST_INFO_UPDATE));
|
//ASSERT
|
||||||
|
assertEquals(Consts.REQUEST_INFO_UPDATE, producerRecord.getValue().topic());
|
||||||
|
BANK_ACCOUNT_MATCHER.assertMatch(resultUpdating, predictableUpdateBankAccount);
|
||||||
|
ACCOUNT_MATCHER.assertMatch(accountResult, predictableAccount);
|
||||||
|
BASE_REQUEST_MATCHER.assertMatch(baseRequestResult, predictableBaseRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -254,17 +265,12 @@ public class BankAccountServiceTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void bankAccountDelete() throws InterruptedException {
|
void bankAccountDelete() throws InterruptedException {
|
||||||
//arrange
|
//ARRANGE
|
||||||
BankAccount bankAccountExists = new BankAccount();
|
BankAccount bankAccountExists = getBankAccount();
|
||||||
bankAccountExists.setId(ID);
|
bankAccountImdg.insert(bankAccountExists);
|
||||||
bankAccountExists.setBankName("ooo tinkoff");
|
Account account = new Account();
|
||||||
bankAccountExists.setBankIdentificationCode("99999");
|
account.setId(accountId);
|
||||||
bankAccountExists.setCorrespondentAccount("9294189285498598598");
|
accountImdg.insert(account);
|
||||||
bankAccountExists.setCorrespondentAccountName("BIK OF TINKOFF");
|
|
||||||
bankAccountExists.setCurrency("RUB");
|
|
||||||
bankAccountExists.setDestination("OOO ROGA I KOPITA");
|
|
||||||
bankAccountExists.setTaxpayerIdentificationNumber("848484848484");
|
|
||||||
bankAccountExists.setTaxRegistrationReasonCode("886886");
|
|
||||||
|
|
||||||
CommonDeleteRequest commonDeleteRequest = new CommonDeleteRequest();
|
CommonDeleteRequest commonDeleteRequest = new CommonDeleteRequest();
|
||||||
commonDeleteRequest.setId(ID);
|
commonDeleteRequest.setId(ID);
|
||||||
|
|
@ -281,32 +287,58 @@ public class BankAccountServiceTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
//ACT
|
//ACT
|
||||||
//service set up
|
addRecordToKafka((MockConsumer) bankAccountService.getConsumer(), TOPIC_ACCOUNT_DELETE, PARTITION, 0, jsonDeleteNewRequest);
|
||||||
hazelcastServiceTest.waitTillReadyState();
|
|
||||||
BankAccountService bankAccountService = new BankAccountService(mockConsumer, mockProducer, hazelcastServiceTest, messageResolver, bankAccountNewRequestValidator);
|
|
||||||
//callbacks set up
|
|
||||||
bankAccountService.afterPropertiesSet();
|
|
||||||
|
|
||||||
IMap<Long, BankAccount> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_BankAccount);
|
//waiting for kafka producer send message (finale event)
|
||||||
iMap.put(ID, bankAccountExists);
|
verify(producer, timeout(30_000L).times(1))
|
||||||
ImapEvent imapEvent = new ImapEvent(iMap);
|
.send(producerRecord.capture());
|
||||||
|
|
||||||
//KAFKA
|
|
||||||
mockConsumer.schedulePollTask(() -> {
|
|
||||||
mockConsumer.rebalance(Collections.singletonList(new TopicPartition(TOPIC_ACCOUNT_DELETE, PARTITION)));
|
|
||||||
mockConsumer.addRecord(new ConsumerRecord<>(TOPIC_ACCOUNT_DELETE, PARTITION, 0, "key", jsonDeleteNewRequest));
|
|
||||||
});
|
|
||||||
HashMap<TopicPartition, Long> startOffsetsDeleting = new HashMap<>();
|
|
||||||
TopicPartition tpDeleting = new TopicPartition(TOPIC_ACCOUNT_DELETE, PARTITION);
|
|
||||||
startOffsetsDeleting.put(tpDeleting, 0L);
|
|
||||||
mockConsumer.updateBeginningOffsets(startOffsetsDeleting);
|
|
||||||
|
|
||||||
//waiting for hazelcast map item updates
|
|
||||||
imapEvent.waitWhenHappened();
|
|
||||||
|
|
||||||
//ASSERT
|
//ASSERT
|
||||||
Assertions.assertEquals(0, iMap.size());
|
BankAccount bankAccount = bankAccountImdg.getSingleObjectByID(baseDeleteRequest.getId());
|
||||||
|
Assertions.assertNull(bankAccount);
|
||||||
|
assertEquals(Consts.REQUEST_INFO_UPDATE, producerRecord.getValue().topic());
|
||||||
|
}
|
||||||
|
|
||||||
assertTrue(mockProducer.history().get(0).topic().equalsIgnoreCase(Consts.REQUEST_INFO_UPDATE));
|
private Company getTestCompany() {
|
||||||
|
Company company = new Company();
|
||||||
|
company.setId(addresseeIdNew);
|
||||||
|
company.setWorkflowStatus(Status.Active.getKey());
|
||||||
|
company.setTradingCode(deal);
|
||||||
|
company.setShortName("ShortName");
|
||||||
|
company.setFullName("FullName");
|
||||||
|
return company;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Account getTestAccount(Long id, String acc) {
|
||||||
|
Account account = new Account();
|
||||||
|
account.setId(id);
|
||||||
|
account.setAccount(acc);
|
||||||
|
account.setAccountType(AccountType.Bank.getKey());
|
||||||
|
account.setAccountStatus(Status.Active.getKey());
|
||||||
|
account.setProcessingSign(Allowed.ALLOWED.getKey());
|
||||||
|
return account;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setSameValueToField(Account from, Account to) {
|
||||||
|
to.setCreated(from.getCreated());
|
||||||
|
to.setUpdated(from.getUpdated());
|
||||||
|
to.setId(from.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
private BankAccount getBankAccount() {
|
||||||
|
BankAccount bankAccount = new BankAccount();
|
||||||
|
bankAccount.setAccountId(accountId);
|
||||||
|
bankAccount.setAccount(acc);
|
||||||
|
bankAccount.setCompanyId(addresseeIdNew);
|
||||||
|
bankAccount.setId(ID);
|
||||||
|
bankAccount.setBankName("ooo tinkoff");
|
||||||
|
bankAccount.setBankIdentificationCode("99999");
|
||||||
|
bankAccount.setCorrespondentAccount("9294189285498598598");
|
||||||
|
bankAccount.setCorrespondentAccountName("BIK OF TINKOFF");
|
||||||
|
bankAccount.setCurrency("RUB");
|
||||||
|
bankAccount.setDestination("OOO ROGA I KOPITA");
|
||||||
|
bankAccount.setTaxpayerIdentificationNumber("848484848484");
|
||||||
|
bankAccount.setTaxRegistrationReasonCode("886886");
|
||||||
|
return bankAccount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -49,7 +49,7 @@ public class ImapEvent<T> {
|
||||||
checkEventHappened.set(secondRan);//если что-то пойдет не так не тормозить основной поток
|
checkEventHappened.set(secondRan);//если что-то пойдет не так не тормозить основной поток
|
||||||
secondRan = true;
|
secondRan = true;
|
||||||
}
|
}
|
||||||
}, 0, 60 * 1000);
|
}, 0, 30 * 1000);
|
||||||
synchronized (checkEventHappened) {
|
synchronized (checkEventHappened) {
|
||||||
while (!checkEventHappened.get()) {
|
while (!checkEventHappened.get()) {
|
||||||
checkEventHappened.wait(100);
|
checkEventHappened.wait(100);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
package ru.spcex.clearing.account.utils;
|
||||||
|
|
||||||
|
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||||
|
import org.apache.kafka.clients.consumer.MockConsumer;
|
||||||
|
import org.apache.kafka.clients.producer.RecordMetadata;
|
||||||
|
import org.apache.kafka.common.TopicPartition;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
public class MockKafkaUtils {
|
||||||
|
public static void addRecordToKafka(MockConsumer mockConsumer, String topic, int partition, long offset, String jsonValue) {
|
||||||
|
TopicPartition tp = new TopicPartition(topic, partition);
|
||||||
|
HashMap<TopicPartition, Long> startOffsets = new HashMap<>();
|
||||||
|
startOffsets.put(tp, 0L);
|
||||||
|
mockConsumer.updateBeginningOffsets(startOffsets);
|
||||||
|
mockConsumer.schedulePollTask(() -> {
|
||||||
|
mockConsumer.rebalance(Collections.singletonList(tp));
|
||||||
|
mockConsumer.addRecord(new ConsumerRecord<>(topic, partition, offset, "key", jsonValue));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class FutureRecordMetadata implements Future<RecordMetadata> {
|
||||||
|
@Override
|
||||||
|
public boolean cancel(boolean mayInterruptIfRunning) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCancelled() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDone() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RecordMetadata get() throws InterruptedException, ExecutionException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RecordMetadata get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -62,6 +62,7 @@ public abstract class AbstractServiceTest {
|
||||||
protected static ImdgHazelcast<SDf17> sdf17Imdg;
|
protected static ImdgHazelcast<SDf17> sdf17Imdg;
|
||||||
protected static ImdgHazelcast<AccountBalance> accountBalanceImdg;
|
protected static ImdgHazelcast<AccountBalance> accountBalanceImdg;
|
||||||
protected static ImdgHazelcast<Company> companyImdg;
|
protected static ImdgHazelcast<Company> companyImdg;
|
||||||
|
protected static ImdgHazelcast<Account> accountImdg;
|
||||||
protected final Long accountIdNew = 10L;
|
protected final Long accountIdNew = 10L;
|
||||||
protected final Long addresseeIdNew = 2L;
|
protected final Long addresseeIdNew = 2L;
|
||||||
protected final String deal = "111111111";
|
protected final String deal = "111111111";
|
||||||
|
|
@ -77,7 +78,7 @@ public abstract class AbstractServiceTest {
|
||||||
hazelcast.waitAvailable();
|
hazelcast.waitAvailable();
|
||||||
companyImdg = (ImdgHazelcast<Company>) hazelcast.getImdg(IMDGDistributedNames.Map_Company, Company.class);
|
companyImdg = (ImdgHazelcast<Company>) hazelcast.getImdg(IMDGDistributedNames.Map_Company, Company.class);
|
||||||
ImdgHazelcast<CompanySymbols> companySymbolsImdg = (ImdgHazelcast<CompanySymbols>) hazelcast.getImdg(IMDGDistributedNames.Map_CompanySymbols, CompanySymbols.class);
|
ImdgHazelcast<CompanySymbols> companySymbolsImdg = (ImdgHazelcast<CompanySymbols>) hazelcast.getImdg(IMDGDistributedNames.Map_CompanySymbols, CompanySymbols.class);
|
||||||
ImdgHazelcast<Account> accountImdg = (ImdgHazelcast<Account>) hazelcast.getImdg(IMDGDistributedNames.Map_Account, Account.class);
|
accountImdg = (ImdgHazelcast<Account>) hazelcast.getImdg(IMDGDistributedNames.Map_Account, Account.class);
|
||||||
requestInfoImdg = (ImdgHazelcast<RequestInfo>) hazelcast.getImdg(IMDGDistributedNames.Map_RequestInfo, RequestInfo.class);
|
requestInfoImdg = (ImdgHazelcast<RequestInfo>) hazelcast.getImdg(IMDGDistributedNames.Map_RequestInfo, RequestInfo.class);
|
||||||
statementImdg = (ImdgHazelcast<Statement>) hazelcast.getImdg(IMDGDistributedNames.Map_Statement, Statement.class);
|
statementImdg = (ImdgHazelcast<Statement>) hazelcast.getImdg(IMDGDistributedNames.Map_Statement, Statement.class);
|
||||||
sdf01Imdg = (ImdgHazelcast<SDf01>) hazelcast.getImdg(IMDGDistributedNames.Map_SDf01, SDf01.class);
|
sdf01Imdg = (ImdgHazelcast<SDf01>) hazelcast.getImdg(IMDGDistributedNames.Map_SDf01, SDf01.class);
|
||||||
|
|
|
||||||
|
|
@ -52,9 +52,9 @@ class AccountBalanceServiceTest extends AbstractServiceTest {
|
||||||
@Test
|
@Test
|
||||||
void createAccountBalance() {
|
void createAccountBalance() {
|
||||||
Company company = getTestCompany();
|
Company company = getTestCompany();
|
||||||
companyMap.put(addresseeIdNew, company);
|
companyImdg.insert(company);
|
||||||
Account account = getTestAccount(accountIdNew, acc);
|
Account account = getTestAccount(accountIdNew, acc);
|
||||||
accountMap.put(accountIdNew, account);
|
accountImdg.insert(account);
|
||||||
|
|
||||||
AccountResult predictableNewResult = getTestAccountResult(accountIdNew, account, company);
|
AccountResult predictableNewResult = getTestAccountResult(accountIdNew, account, company);
|
||||||
AccountResult resultNew = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCode);
|
AccountResult resultNew = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCode);
|
||||||
|
|
@ -96,7 +96,8 @@ class AccountBalanceServiceTest extends AbstractServiceTest {
|
||||||
//CompanyNotFound
|
//CompanyNotFound
|
||||||
AccountResult predictableResult;
|
AccountResult predictableResult;
|
||||||
predictableResult = new AccountResult(new EnumMessage(BalanceError.CompanyNotFound));
|
predictableResult = new AccountResult(new EnumMessage(BalanceError.CompanyNotFound));
|
||||||
companyMap.delete(addresseeIdNew);
|
Company company = getTestCompany();
|
||||||
|
companyImdg.delete(company);
|
||||||
|
|
||||||
AccountResult result = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCode);
|
AccountResult result = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCode);
|
||||||
ACCOUNT_BALANCE_MATCHER.assertMatch(result, predictableResult);
|
ACCOUNT_BALANCE_MATCHER.assertMatch(result, predictableResult);
|
||||||
|
|
@ -105,11 +106,11 @@ class AccountBalanceServiceTest extends AbstractServiceTest {
|
||||||
ACCOUNT_BALANCE_MATCHER.assertMatch(result, predictableResult);
|
ACCOUNT_BALANCE_MATCHER.assertMatch(result, predictableResult);
|
||||||
|
|
||||||
//AccountNotPresent accountType=null
|
//AccountNotPresent accountType=null
|
||||||
companyMap.put(addresseeIdNew, getTestCompany());
|
companyImdg.insert(company);
|
||||||
Account account = new Account();
|
Account account = new Account();
|
||||||
account.setId(accountIdNew);
|
account.setId(accountIdNew);
|
||||||
account.setAccountStatus(Status.Active.getKey());
|
account.setAccountStatus(Status.Active.getKey());
|
||||||
accountMap.put(accountIdNew, account);
|
accountImdg.insert(account);
|
||||||
predictableResult = new AccountResult(new EnumMessage(BalanceError.AccountNotPresent));
|
predictableResult = new AccountResult(new EnumMessage(BalanceError.AccountNotPresent));
|
||||||
|
|
||||||
result = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCode);
|
result = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCode);
|
||||||
|
|
@ -119,7 +120,7 @@ class AccountBalanceServiceTest extends AbstractServiceTest {
|
||||||
account.setId(null);
|
account.setId(null);
|
||||||
account.setAccountType(AccountType.Clrn.getKey());
|
account.setAccountType(AccountType.Clrn.getKey());
|
||||||
account.setAccountStatus(Status.Active.getKey());
|
account.setAccountStatus(Status.Active.getKey());
|
||||||
accountMap.put(accountIdNew, account);
|
accountImdg.insert(account);
|
||||||
predictableResult = new AccountResult(new EnumMessage(BalanceError.AccountNotPresent));
|
predictableResult = new AccountResult(new EnumMessage(BalanceError.AccountNotPresent));
|
||||||
|
|
||||||
result = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCode);
|
result = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCode);
|
||||||
|
|
@ -129,14 +130,14 @@ class AccountBalanceServiceTest extends AbstractServiceTest {
|
||||||
account.setId(accountIdNew);
|
account.setId(accountIdNew);
|
||||||
account.setAccountType(AccountType.Clrn.getKey());
|
account.setAccountType(AccountType.Clrn.getKey());
|
||||||
account.setAccountStatus(null);
|
account.setAccountStatus(null);
|
||||||
accountMap.put(accountIdNew, account);
|
accountImdg.insert(account);
|
||||||
predictableResult = new AccountResult(new EnumMessage(BalanceError.AccountNotActive, account.getId()));
|
predictableResult = new AccountResult(new EnumMessage(BalanceError.AccountNotActive, account.getId()));
|
||||||
|
|
||||||
result = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCode);
|
result = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCode);
|
||||||
ACCOUNT_BALANCE_MATCHER.assertMatch(result, predictableResult);
|
ACCOUNT_BALANCE_MATCHER.assertMatch(result, predictableResult);
|
||||||
|
|
||||||
account.setAccountStatus(Status.Active.getKey());
|
account.setAccountStatus(Status.Active.getKey());
|
||||||
accountMap.put(accountIdNew, account);
|
accountImdg.insert(account);
|
||||||
result = accountBalanceService.createAccountBalance(addresseeIdNew, null, amountNew, cashMovementCurrencyCode);
|
result = accountBalanceService.createAccountBalance(addresseeIdNew, null, amountNew, cashMovementCurrencyCode);
|
||||||
predictableResult = new AccountResult(new EnumMessage(BalanceError.AccountNotPresent));
|
predictableResult = new AccountResult(new EnumMessage(BalanceError.AccountNotPresent));
|
||||||
ACCOUNT_BALANCE_MATCHER.assertMatch(result, predictableResult);
|
ACCOUNT_BALANCE_MATCHER.assertMatch(result, predictableResult);
|
||||||
|
|
|
||||||
|
|
@ -36,14 +36,11 @@ class StatementServiceServiceTest extends AbstractServiceTest {
|
||||||
private final Long ID = 11L;
|
private final Long ID = 11L;
|
||||||
@Autowired
|
@Autowired
|
||||||
StatementService statementService;
|
StatementService statementService;
|
||||||
|
|
||||||
@Captor
|
@Captor
|
||||||
ArgumentCaptor<ProducerRecord> producerRecord;
|
ArgumentCaptor<ProducerRecord> producerRecord;
|
||||||
|
|
||||||
private MockConsumer<String, Object> mockConsumer;
|
private MockConsumer<String, Object> mockConsumer;
|
||||||
private SDf01 sDf01;
|
private SDf01 sDf01;
|
||||||
private Company company;
|
private Company company;
|
||||||
|
|
||||||
@SpyBean
|
@SpyBean
|
||||||
private MockProducer<String, Object> producer;
|
private MockProducer<String, Object> producer;
|
||||||
|
|
||||||
|
|
@ -89,14 +86,10 @@ class StatementServiceServiceTest extends AbstractServiceTest {
|
||||||
verify(producer, timeout(30_000L).times(1))
|
verify(producer, timeout(30_000L).times(1))
|
||||||
.send(producerRecord.capture());
|
.send(producerRecord.capture());
|
||||||
|
|
||||||
// Collection<RequestInfo> resultsRequestInfo = requestInfoImdg.getCollectionObjectsByFieldValues(Map.of("status", Status.Processing));
|
|
||||||
// RequestInfo requestInfo = resultsRequestInfo.stream().max((entry1, entry2) -> entry1.getId() > entry2.getId() ? 1 : -1).get();
|
|
||||||
|
|
||||||
assertEquals(Consts.EXPORT_PROCESS, producerRecord.getValue().topic());
|
|
||||||
BaseRequest<Object> baseRequest = (BaseRequest<Object>) producerRecord.getValue().value();
|
BaseRequest<Object> baseRequest = (BaseRequest<Object>) producerRecord.getValue().value();
|
||||||
|
|
||||||
RequestInfo resultRequestInfo = requestInfoImdg.getSingleObjectByID(baseRequest.getId());
|
RequestInfo resultRequestInfo = requestInfoImdg.getSingleObjectByID(baseRequest.getId());
|
||||||
|
|
||||||
|
assertEquals(Consts.EXPORT_PROCESS, producerRecord.getValue().topic());
|
||||||
assertNotNull(baseRequest);
|
assertNotNull(baseRequest);
|
||||||
assertNotNull(resultRequestInfo);
|
assertNotNull(resultRequestInfo);
|
||||||
}
|
}
|
||||||
|
|
@ -134,14 +127,10 @@ class StatementServiceServiceTest extends AbstractServiceTest {
|
||||||
verify(producer, timeout(30_000L).times(1))
|
verify(producer, timeout(30_000L).times(1))
|
||||||
.send(producerRecord.capture());
|
.send(producerRecord.capture());
|
||||||
|
|
||||||
// Collection<RequestInfo> resultsRequestInfo = requestInfoImdg.getCollectionObjectsByFieldValues(Map.of("status", Status.Processing));
|
|
||||||
// RequestInfo requestInfo = resultsRequestInfo.stream().max((entry1, entry2) -> entry1.getId() > entry2.getId() ? 1 : -1).get();
|
|
||||||
|
|
||||||
assertEquals(Consts.ACCOUNT_NEW, producerRecord.getValue().topic());
|
|
||||||
BaseRequest<Object> baseRequest = (BaseRequest<Object>) producerRecord.getValue().value();
|
BaseRequest<Object> baseRequest = (BaseRequest<Object>) producerRecord.getValue().value();
|
||||||
|
|
||||||
RequestInfo resultRequestInfo = requestInfoImdg.getSingleObjectByID(baseRequest.getId());
|
RequestInfo resultRequestInfo = requestInfoImdg.getSingleObjectByID(baseRequest.getId());
|
||||||
|
|
||||||
|
assertEquals(Consts.ACCOUNT_NEW, producerRecord.getValue().topic());
|
||||||
assertNotNull(baseRequest);
|
assertNotNull(baseRequest);
|
||||||
assertNotNull(resultRequestInfo);
|
assertNotNull(resultRequestInfo);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -97,7 +97,7 @@ public class QueueConsumer implements AutoCloseable {
|
||||||
}
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
log.error(ExceptionUtils.getStackTrace(e));
|
log.error(ExceptionUtils.getStackTrace(e));
|
||||||
if (producer != null) {
|
if (producer != null && o != null) {
|
||||||
sendErrorResponse((BaseRequest<?>) o);
|
sendErrorResponse((BaseRequest<?>) o);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue