---
tree test done, but dot work together
This commit is contained in:
psemenkov 2022-09-15 16:45:02 +03:00
parent 45fa3ddc02
commit 702a107be6
3 changed files with 199 additions and 31 deletions

View file

@ -72,7 +72,10 @@ public class BankAccountService extends QueueConsumer implements InitializingBea
bankAccount.setDestination(req.getDestination());
bankAccount.setTaxpayerIdentificationNumber(req.getTaxpayerIdentificationNumber());
bankAccount.setTaxRegistrationReasonCode(req.getTaxRegistrationReasonCode());
bankAccount.setAccount(req.getAccount());
bankAccountMap.update(bankAccount);
log.debug("successfully update, existing bankAccount with id {}", bankAccount.getId());
}
private void bankAccountDelete(BaseRequest<CommonDeleteRequest> userRequest) {

View file

@ -39,13 +39,6 @@ public class HazelcastServiceTestConfiguration {
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);
HazelcastHelper.otcSystem_setStorageState(true, hazelcastInstance);

View file

@ -7,6 +7,7 @@ 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.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -22,6 +23,8 @@ 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.clearing.platform.messaging.domain.cud.account.BankAccountUpdateRequest;
import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest;
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
import java.util.Collections;
@ -35,18 +38,24 @@ import static ru.spcex.clearing.account.utils.MatcherFactory.usingIgnoringFields
public class BankAccountServiceTest {
public static final Matcher<BankAccount> BANK_ACCOUNT_MATCHER = usingIgnoringFieldsComparator("id");
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<>();
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_DELETE = Consts.DESTINATION_BANK_ACCOUNT_DELETE;
@Autowired
@Qualifier("hazelcastServiceTest")
private HazelcastService hazelcastServiceTest;
private MockConsumer<String, Object> mockConsumer;
{
@BeforeEach
void setUp() {
mockConsumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST);
}
@Test
public void bankAccountNew() throws InterruptedException {
//arrange
BankAccountNewRequest bankAccountNewRequest = new BankAccountNewRequest();
bankAccountNewRequest.setBankName("ooo tinkoff");
bankAccountNewRequest.setBankIdentificationCode("99999");
bankAccountNewRequest.setCorrespondentAccount("9294189285498598598");
@ -57,24 +66,17 @@ public class BankAccountServiceTest {
bankAccountNewRequest.setTaxRegistrationReasonCode("886886");
bankAccountNewRequest.setAccount("123456789123");
baseRequest.setRequestPayload(bankAccountNewRequest);
baseRequest.setId(0L);
baseRequest.setActionType(ActionType.NEW);
BaseRequest<BankAccountNewRequest> baseNewRequest = new BaseRequest<>();
baseNewRequest.setRequestPayload(bankAccountNewRequest);
baseNewRequest.setId(0L);
baseNewRequest.setActionType(ActionType.NEW);
String jsonBaseNewRequest;
ObjectMapper objectMapper = new ObjectMapper();
try {
jsonBaseRequest = objectMapper.writeValueAsString(baseRequest);
jsonBaseNewRequest = objectMapper.writeValueAsString(baseNewRequest);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
@BeforeEach
void setUp() {
mockConsumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST);
}
@Test
public void bankAccountNew() throws InterruptedException {
//arrange
BankAccount predictableResult = new BankAccount();
predictableResult.setBankName("ooo tinkoff");
predictableResult.setBankIdentificationCode("99999");
@ -88,13 +90,13 @@ public class BankAccountServiceTest {
predictableResult.setId(0L);
//KAFKA
mockConsumer.schedulePollTask(() -> {
mockConsumer.rebalance(Collections.singletonList(new TopicPartition(TOPIC, PARTITION)));
mockConsumer.addRecord(new ConsumerRecord<>(TOPIC, PARTITION, 0, "key", jsonBaseRequest));
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, PARTITION);
TopicPartition tp = new TopicPartition(TOPIC_ACCOUNT_NEW, PARTITION);
startOffsets.put(tp, 0L);
mockConsumer.updateBeginningOffsets(startOffsets);
@ -110,7 +112,177 @@ public class BankAccountServiceTest {
//ASSERT
IMap<Long, BankAccount> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_BankAccount);
BankAccount result = iMap.get(0L);
iMap.clear();
BANK_ACCOUNT_MATCHER.assertMatch(result, predictableResult);
}
@Test
void bankAccountUpdate() throws InterruptedException {
//arrange
BankAccountNewRequest bankAccountNewRequest = new BankAccountNewRequest();
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<BankAccountNewRequest> baseNewRequest = new BaseRequest<>();
baseNewRequest.setRequestPayload(bankAccountNewRequest);
baseNewRequest.setId(0L);
baseNewRequest.setActionType(ActionType.NEW);
String jsonBaseNewRequest;
ObjectMapper objectMapper = new ObjectMapper();
try {
jsonBaseNewRequest = objectMapper.writeValueAsString(baseNewRequest);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
BankAccountUpdateRequest bankAccountUpdateRequest = new BankAccountUpdateRequest();
bankAccountUpdateRequest.setId(0L);
bankAccountUpdateRequest.setBankName("NEW BUNK NAME");
bankAccountUpdateRequest.setBankIdentificationCode("88888");
bankAccountUpdateRequest.setCorrespondentAccount("894984646541316");
bankAccountUpdateRequest.setCorrespondentAccountName("BIK OF NEW BUNK");
bankAccountUpdateRequest.setCurrency("EU");
bankAccountUpdateRequest.setDestination("OOO NEW BUNK");
bankAccountUpdateRequest.setTaxpayerIdentificationNumber("65468461321");
bankAccountUpdateRequest.setTaxRegistrationReasonCode("532137");
bankAccountUpdateRequest.setAccount("326984656514");
BaseRequest<BankAccountUpdateRequest> baseUpdateRequest = new BaseRequest<>();
baseUpdateRequest.setRequestPayload(bankAccountUpdateRequest);
baseUpdateRequest.setId(0L);
baseUpdateRequest.setActionType(ActionType.UPDATE);
String jsonBaseUpdateRequest;
try {
jsonBaseUpdateRequest = objectMapper.writeValueAsString(baseUpdateRequest);
} catch (JsonProcessingException 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.setAccount("326984656514");
predictableUpdateResult.setId(0L);
//KAFKA
mockConsumer.schedulePollTask(() -> {
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);
//ACT
//service set up
BankAccountService bankAccountService = new BankAccountService(mockConsumer, hazelcastServiceTest);
Thread.sleep(10000);
//callbacks set up
bankAccountService.afterPropertiesSet();
Thread.sleep(10000);
//KAFKA
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);
//ASSERT
Thread.sleep(10000);
IMap<Long, BankAccount> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_BankAccount);
BankAccount resultUpdating = iMap.get(0L);
iMap.clear();
BANK_ACCOUNT_MATCHER.assertMatch(resultUpdating, predictableUpdateResult);
}
@Test
void bankAccountDelete() throws InterruptedException {
//arrange
BankAccountNewRequest bankAccountNewRequest = new BankAccountNewRequest();
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<BankAccountNewRequest> baseNewRequest = new BaseRequest<>();
baseNewRequest.setRequestPayload(bankAccountNewRequest);
baseNewRequest.setId(0L);
baseNewRequest.setActionType(ActionType.NEW);
String jsonBaseNewRequest;
ObjectMapper objectMapper = new ObjectMapper();
try {
jsonBaseNewRequest = objectMapper.writeValueAsString(baseNewRequest);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
CommonDeleteRequest commonDeleteRequest = new CommonDeleteRequest();
commonDeleteRequest.setId(0L);
BaseRequest<CommonDeleteRequest> baseDeleteRequest = new BaseRequest<>();
baseDeleteRequest.setRequestPayload(commonDeleteRequest);
baseDeleteRequest.setId(0L);
baseDeleteRequest.setActionType(ActionType.DELETE);
String jsonDeleteNewRequest;
try {
jsonDeleteNewRequest = objectMapper.writeValueAsString(baseDeleteRequest);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
//KAFKA
mockConsumer.schedulePollTask(() -> {
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);
//ACT
//service set up
BankAccountService bankAccountService = new BankAccountService(mockConsumer, hazelcastServiceTest);
Thread.sleep(10000);
//callbacks set up
bankAccountService.afterPropertiesSet();
Thread.sleep(10000);
//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);
//ASSERT
Thread.sleep(10000);
IMap<Long, BankAccount> iMap = hazelcastServiceTest.getHazelcast().getMap(IMDGDistributedNames.Map_BankAccount);
Assertions.assertEquals(0, iMap.size());
}
}