--- adding hazelcast client
This commit is contained in:
parent
7e82916010
commit
b6780affc0
4 changed files with 194 additions and 19 deletions
|
|
@ -37,6 +37,11 @@
|
||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- TEST -->
|
<!-- TEST -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mockito</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>mockito-core</artifactId>
|
<artifactId>mockito-core</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
package ru.spcex.clearing.account.config;
|
||||||
|
|
||||||
|
import com.hazelcast.config.Config;
|
||||||
|
import com.hazelcast.config.JoinConfig;
|
||||||
|
import com.hazelcast.config.MulticastConfig;
|
||||||
|
import com.hazelcast.config.NetworkConfig;
|
||||||
|
import com.hazelcast.core.Hazelcast;
|
||||||
|
import com.hazelcast.core.HazelcastInstance;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class HazelcastInstanceTestConfiguration {
|
||||||
|
|
||||||
|
@Bean(name = "hazelcastInstance")
|
||||||
|
public HazelcastInstance hazelcastInstance() {
|
||||||
|
Config cfg = new Config();
|
||||||
|
cfg.setInstanceName("unittest_test_" + new Random().nextInt());
|
||||||
|
NetworkConfig networkConfig = new NetworkConfig();
|
||||||
|
JoinConfig joinConfig = new JoinConfig();
|
||||||
|
joinConfig.setMulticastConfig(new MulticastConfig().setEnabled(false));
|
||||||
|
networkConfig.setJoin(joinConfig);
|
||||||
|
cfg.setNetworkConfig(networkConfig);
|
||||||
|
return Hazelcast.newHazelcastInstance(cfg);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,73 @@
|
||||||
|
package ru.spcex.clearing.account.config;
|
||||||
|
|
||||||
|
import com.hazelcast.config.*;
|
||||||
|
import com.hazelcast.core.Hazelcast;
|
||||||
|
import com.hazelcast.core.HazelcastInstance;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
import ru.spcex.platform.imdg.iml.hazelcast.config.HazelcastClientParams;
|
||||||
|
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class HazelcastServiceTestConfiguration {
|
||||||
|
private HazelcastInstance hazelcastInstance;
|
||||||
|
|
||||||
|
private static ThreadPoolTaskExecutor createThreadPoolTaskExecutor(int maxPoolSz, boolean waitForCompletion) {
|
||||||
|
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
|
||||||
|
if (maxPoolSz > 2) {
|
||||||
|
pool.setKeepAliveSeconds(60);
|
||||||
|
pool.setAllowCoreThreadTimeOut(true);
|
||||||
|
}
|
||||||
|
pool.setCorePoolSize(maxPoolSz);
|
||||||
|
pool.setWaitForTasksToCompleteOnShutdown(waitForCompletion);
|
||||||
|
return pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "hazelcastServiceTest")
|
||||||
|
public HazelcastService hazelcastService(@Qualifier("taskExecutorHazelcastClientInitializer") ThreadPoolTaskExecutor taskExecutorHazelcastClientInitializer, @Qualifier("taskExecutorIdGeneratorAwaiter") ThreadPoolTaskExecutor taskExecutorIdGeneratorAwaiter, HazelcastClientParams params) {
|
||||||
|
Config cfg = new Config();
|
||||||
|
cfg.setInstanceName("localhost");
|
||||||
|
|
||||||
|
NetworkConfig networkConfig = new NetworkConfig();
|
||||||
|
JoinConfig joinConfig = new JoinConfig();
|
||||||
|
joinConfig.setMulticastConfig(new MulticastConfig().setEnabled(false));
|
||||||
|
joinConfig.setTcpIpConfig(new TcpIpConfig().setEnabled(true).setMembers(List.of("127.0.0.1")));
|
||||||
|
networkConfig.setJoin(joinConfig);
|
||||||
|
// .setPort(configRoot.getHazelcast().getListenPort())
|
||||||
|
// .setJoin(new JoinConfig()
|
||||||
|
// .setMulticastConfig(new MulticastConfig()
|
||||||
|
// .setEnabled(false))
|
||||||
|
// .setTcpIpConfig(new TcpIpConfig()
|
||||||
|
// .setEnabled(true).setMembers(hzSettings.getClusterMembers())
|
||||||
|
// )
|
||||||
|
cfg.setNetworkConfig(networkConfig);
|
||||||
|
hazelcastInstance = Hazelcast.newHazelcastInstance(cfg);
|
||||||
|
return new HazelcastService(taskExecutorHazelcastClientInitializer, taskExecutorIdGeneratorAwaiter, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "taskExecutorHazelcastClientInitializer")
|
||||||
|
public ThreadPoolTaskExecutor taskExecutorHazelcastClientInitializer() {
|
||||||
|
return createThreadPoolTaskExecutor(1, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "taskExecutorIdGeneratorAwaiter")
|
||||||
|
public ThreadPoolTaskExecutor taskExecutorIdGeneratorAwaiter() {
|
||||||
|
return createThreadPoolTaskExecutor(1, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "hazelcastClientParams")
|
||||||
|
public HazelcastClientParams getHazelcastClientParams() {
|
||||||
|
HazelcastClientParams params = new HazelcastClientParams();
|
||||||
|
params.setLogin("dev");
|
||||||
|
params.setPassword("dev-pass");
|
||||||
|
params.setClusterMembers("127.0.0.1");
|
||||||
|
params.setInstanceName("localhost");
|
||||||
|
params.setNearCacheConfig(new NearCacheConfig());
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,41 +1,81 @@
|
||||||
package ru.spcex.clearing.account.service;
|
package ru.spcex.clearing.account.service;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.hazelcast.core.IMap;
|
||||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
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.consumer.OffsetResetStrategy;
|
||||||
import org.apache.kafka.common.TopicPartition;
|
import org.apache.kafka.common.TopicPartition;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
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.clearing.classes.statics.data.account.BankAccount;
|
import ru.clearing.classes.statics.data.account.BankAccount;
|
||||||
|
import ru.spcex.clearing.account.config.HazelcastServiceTestConfiguration;
|
||||||
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.BaseRequest;
|
||||||
import ru.spcex.clearing.platform.messaging.domain.Consts;
|
import ru.spcex.clearing.platform.messaging.domain.Consts;
|
||||||
|
import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountNewRequest;
|
||||||
import ru.spcex.platform.imdg.api.Imdg;
|
import ru.spcex.platform.imdg.api.Imdg;
|
||||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
|
||||||
import ru.spcex.platform.imdg.iml.hazelcast.adapter.ImdgHazelcast;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import static org.mockito.Mockito.doReturn;
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@ContextConfiguration(classes = {
|
||||||
class BankAccountServiceTest {
|
HazelcastServiceTestConfiguration.class})
|
||||||
|
public class BankAccountServiceTest {
|
||||||
|
|
||||||
private static final int PARTITION = 0;
|
private static final int PARTITION = 0;
|
||||||
private static final String TOPIC = Consts.DESTINATION_BANK_ACCOUNT_NEW;
|
private static final String TOPIC = Consts.DESTINATION_BANK_ACCOUNT_NEW;
|
||||||
|
private final String jsonBaseRequest;
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
private final BankAccountNewRequest bankAccountNewRequest = new BankAccountNewRequest();
|
||||||
|
private final BaseRequest<BankAccountNewRequest> baseRequest = new BaseRequest<>();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("hazelcastServiceTest")
|
||||||
|
private HazelcastService hazelcastServiceTest;
|
||||||
private MockConsumer<String, Object> mockConsumer;
|
private MockConsumer<String, Object> mockConsumer;
|
||||||
|
|
||||||
|
{
|
||||||
|
bankAccountNewRequest.setBankName("ooo tinkoff");
|
||||||
|
bankAccountNewRequest.setBankIdentificationCode("99999");
|
||||||
|
bankAccountNewRequest.setCorrespondentAccount("9294189285498598598");
|
||||||
|
bankAccountNewRequest.setCorrespondentAccountName("BIK OF TINKOFF");
|
||||||
|
bankAccountNewRequest.setCurrency("RUB");
|
||||||
|
bankAccountNewRequest.setDestination("OOO ROGA I KOPITA");
|
||||||
|
bankAccountNewRequest.setTaxpayerIdentificationNumber("848484848484");
|
||||||
|
bankAccountNewRequest.setTaxRegistrationReasonCode("886886");
|
||||||
|
bankAccountNewRequest.setAccount("123456789123");
|
||||||
|
|
||||||
|
baseRequest.setRequestPayload(bankAccountNewRequest);
|
||||||
|
baseRequest.setId(0L);
|
||||||
|
baseRequest.setActionType(ActionType.NEW);
|
||||||
|
try {
|
||||||
|
jsonBaseRequest = objectMapper.writeValueAsString(baseRequest);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
mockConsumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST);
|
mockConsumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void bankAccountNew() {
|
public void bankAccountNew() throws InterruptedException {
|
||||||
//arrange
|
//arrange
|
||||||
mockConsumer.schedulePollTask(() -> {
|
mockConsumer.schedulePollTask(() -> {
|
||||||
mockConsumer.rebalance(Collections.singletonList(new TopicPartition(TOPIC, PARTITION)));
|
mockConsumer.rebalance(Collections.singletonList(new TopicPartition(TOPIC, PARTITION)));
|
||||||
mockConsumer.addRecord(new ConsumerRecord<>(TOPIC, PARTITION, 0, "key", "test-value"));
|
mockConsumer.addRecord(new ConsumerRecord<>(TOPIC, PARTITION, 0, "key", jsonBaseRequest));
|
||||||
});
|
});
|
||||||
mockConsumer.schedulePollTask(() -> mockConsumer.wakeup());
|
mockConsumer.schedulePollTask(() -> mockConsumer.wakeup());
|
||||||
|
|
||||||
|
|
@ -43,24 +83,52 @@ class BankAccountServiceTest {
|
||||||
TopicPartition tp = new TopicPartition(TOPIC, PARTITION);
|
TopicPartition tp = new TopicPartition(TOPIC, PARTITION);
|
||||||
startOffsets.put(tp, 0L);
|
startOffsets.put(tp, 0L);
|
||||||
mockConsumer.updateBeginningOffsets(startOffsets);
|
mockConsumer.updateBeginningOffsets(startOffsets);
|
||||||
ImdgProvider imdgProvider = Mockito.mock(ImdgProvider.class);
|
|
||||||
Imdg<BankAccount> bankAccountMap = new ImdgHazelcast();
|
|
||||||
doReturn(bankAccountMap).when(imdgProvider).getImdg(IMDGDistributedNames.Map_BankAccount, BankAccount.class);
|
|
||||||
|
|
||||||
BankAccountService bankAccountService = new BankAccountService(mockConsumer, imdgProvider);
|
|
||||||
// Map<TopicPartition, List<ConsumerRecord<Integer, String>>> records = new LinkedHashMap<>();
|
// ImdgProvider imdgProvider = Mockito.mock(ImdgProvider.class);
|
||||||
//
|
//
|
||||||
// String topic = Consts.DESTINATION_BANK_ACCOUNT_NEW;
|
// ImdgHazelcast bankAccountMap = Mockito.mock(ImdgHazelcast.class);
|
||||||
// ConsumerRecord<Integer, String> record1 = new ConsumerRecord<>(topic, 1, 0, 0L, TimestampType.CREATE_TIME, 0L, 0, 0, 1, "value1");
|
//
|
||||||
// records.put(new TopicPartition(topic, 0), Arrays.asList(record1));
|
// ArgumentCaptor<BankAccount> bankAccountCaptor = ArgumentCaptor.forClass(BankAccount.class);
|
||||||
|
//
|
||||||
//doReturn(records).when(kafkaQueue).poll(Duration.of(10, ChronoUnit.SECONDS));
|
// doReturn(bankAccountMap).when(imdgProvider).getImdg(IMDGDistributedNames.Map_BankAccount, BankAccount.class);
|
||||||
|
|
||||||
|
|
||||||
//act
|
try {
|
||||||
|
hazelcastServiceTest.init();
|
||||||
|
Thread.sleep(10000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
//мапа которую проверял Илья
|
||||||
|
IMap<Object, Object> map = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_KeyRate);
|
||||||
|
Imdg<BankAccount> imdg = hazelcastServiceTest.getImdg(IMDGDistributedNames.Map_BankAccount, BankAccount.class);
|
||||||
|
// IMap<Long, BankAccount> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_BankAccount);
|
||||||
|
BankAccountService bankAccountService = new BankAccountService(mockConsumer, hazelcastServiceTest);
|
||||||
bankAccountService.afterPropertiesSet();
|
bankAccountService.afterPropertiesSet();
|
||||||
|
|
||||||
|
|
||||||
|
Thread.sleep(10000);
|
||||||
|
IMap<Long, BankAccount> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_BankAccount);
|
||||||
|
|
||||||
|
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.setAccount("123456789123");
|
||||||
|
|
||||||
|
|
||||||
|
// Mockito.verify(bankAccountMap).insert(bankAccountCaptor.capture());
|
||||||
|
|
||||||
|
// BankAccount bankAccount = bankAccountCaptor.getValue();
|
||||||
//assert
|
//assert
|
||||||
|
|
||||||
|
|
||||||
|
// assertThat(bankAccount).isEqualTo(predictableResult);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue