---
adding hazelcast client
This commit is contained in:
psemenkov 2022-09-14 15:20:28 +03:00
parent 7e82916010
commit b6780affc0
4 changed files with 194 additions and 19 deletions

View file

@ -37,6 +37,11 @@
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- TEST -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>

View file

@ -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);
}
}

View file

@ -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;
}
}

View file

@ -1,41 +1,81 @@
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.MockConsumer;
import org.apache.kafka.clients.consumer.OffsetResetStrategy;
import org.apache.kafka.common.TopicPartition;
import org.junit.jupiter.api.BeforeEach;
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.spcex.clearing.account.config.HazelcastServiceTestConfiguration;
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.cud.account.BankAccountNewRequest;
import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgProvider;
import ru.spcex.platform.imdg.iml.hazelcast.adapter.ImdgHazelcast;
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
import java.util.Collections;
import java.util.HashMap;
import static org.mockito.Mockito.doReturn;
class BankAccountServiceTest {
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {
HazelcastServiceTestConfiguration.class})
public class BankAccountServiceTest {
private static final int PARTITION = 0;
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;
{
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
void setUp() {
mockConsumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST);
}
@Test
public void bankAccountNew() {
public void bankAccountNew() throws InterruptedException {
//arrange
mockConsumer.schedulePollTask(() -> {
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());
@ -43,24 +83,52 @@ class BankAccountServiceTest {
TopicPartition tp = new TopicPartition(TOPIC, PARTITION);
startOffsets.put(tp, 0L);
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;
// 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));
//doReturn(records).when(kafkaQueue).poll(Duration.of(10, ChronoUnit.SECONDS));
// ImdgHazelcast bankAccountMap = Mockito.mock(ImdgHazelcast.class);
//
// ArgumentCaptor<BankAccount> bankAccountCaptor = ArgumentCaptor.forClass(BankAccount.class);
//
// 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();
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
// assertThat(bankAccount).isEqualTo(predictableResult);
}
}