Adding AccountBalanceServiceTest.
This commit is contained in:
parent
f5afbd5c9f
commit
ec0a5edd67
7 changed files with 327 additions and 4 deletions
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>clearing-parent</artifactId>
|
||||
|
|
@ -40,6 +40,22 @@
|
|||
<groupId>ru.spcex.platform</groupId>
|
||||
<artifactId>platform-enum</artifactId>
|
||||
</dependency>
|
||||
<!-- TEST -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
@ -67,6 +83,23 @@
|
|||
<finalName>${project.artifactId}</finalName>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.21.0</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>1.2.0-M1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.2.0-M1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
|
@ -23,7 +23,7 @@ public enum AccountBalanceValidationRule implements IValidationRule<ImdgValidati
|
|||
Imdg<Company> companyImdg = context.obtainMap(IMDGDistributedNames.Map_Company, Company.class);
|
||||
Company company = companyImdg.getSingleObjectByID(validatedObject.addresseeId());
|
||||
if (company == null) {
|
||||
return of( BalanceError.CompanyNotFound);
|
||||
return of(BalanceError.CompanyNotFound);
|
||||
}
|
||||
context.storeObject(ValidationStored.Company, company);
|
||||
return empty();
|
||||
|
|
@ -50,6 +50,6 @@ public enum AccountBalanceValidationRule implements IValidationRule<ImdgValidati
|
|||
|
||||
@Override
|
||||
public String ruleName() {
|
||||
return "Sdf01ValidationRule." + name();
|
||||
return "AccountBalanceValidationRule." + name();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,75 @@
|
|||
package ru.spcex.clearing.balance.config;
|
||||
|
||||
import com.hazelcast.config.*;
|
||||
import com.hazelcast.core.Hazelcast;
|
||||
import com.hazelcast.core.HazelcastInstance;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.api.ImdgProvider;
|
||||
import ru.spcex.platform.imdg.iml.hazelcast.config.HazelcastClientParams;
|
||||
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
|
||||
import ru.spcex.platform.imdg.iml.hazelcast.util.HazelcastHelper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
@Configuration
|
||||
public class BalanceImdgTestConfig {
|
||||
|
||||
private HazelcastInstance hazelcastInstance;
|
||||
|
||||
private static ThreadPoolTaskExecutor createThreadPoolTestTaskExecutor(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 = "taskExecutorHazelcastTestClientInitializer")
|
||||
public ThreadPoolTaskExecutor taskExecutorHazelcastTestClientInitializer() {
|
||||
return createThreadPoolTestTaskExecutor(1, true);
|
||||
}
|
||||
|
||||
@Bean(name = "taskExecutorTestIdGeneratorAwaiter")
|
||||
public ThreadPoolTaskExecutor taskExecutorTestIdGeneratorAwaiter() {
|
||||
return createThreadPoolTestTaskExecutor(1, false);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
@Bean(name = "hazelcastServiceTest")
|
||||
public ImdgProvider imdgTestProvider(
|
||||
@Qualifier("taskExecutorHazelcastTestClientInitializer") ThreadPoolTaskExecutor taskExecutorHazelcastClientInitializer,
|
||||
@Qualifier("taskExecutorTestIdGeneratorAwaiter") 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);
|
||||
cfg.setNetworkConfig(networkConfig);
|
||||
hazelcastInstance = Hazelcast.newHazelcastInstance(cfg);
|
||||
HazelcastHelper.otcSystem_setStorageState(true, hazelcastInstance);
|
||||
return new HazelcastService(taskExecutorHazelcastClientInitializer, taskExecutorIdGeneratorAwaiter, params);
|
||||
}
|
||||
|
||||
@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("hzTestClient" + new Random().nextInt());
|
||||
params.setNearCacheConfig(new NearCacheConfig());
|
||||
return params;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package ru.spcex.clearing.balance.config;
|
||||
|
||||
import org.apache.kafka.clients.consumer.Consumer;
|
||||
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.Producer;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
@Configuration
|
||||
public class KafkaTestConfig {
|
||||
|
||||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
@Bean
|
||||
public Consumer<String, Object> createTestConsumer() {
|
||||
return new MockConsumer<>(OffsetResetStrategy.EARLIEST);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Producer<String, Object> createTestProducer() {
|
||||
return new MockProducer<>();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package ru.spcex.clearing.balance.service;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import ru.spcex.clearing.balance.config.BalanceImdgTestConfig;
|
||||
import ru.spcex.clearing.balance.config.KafkaSenderConfig;
|
||||
import ru.spcex.clearing.balance.config.KafkaTestConfig;
|
||||
import ru.spcex.clearing.balance.config.ValidationConfig;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = {
|
||||
AccountBalanceService.class,
|
||||
ValidationConfig.class,
|
||||
BalanceImdgTestConfig.class,
|
||||
KafkaSenderConfig.class,
|
||||
KafkaTestConfig.class})
|
||||
public abstract class AbstractServiceTest {
|
||||
}
|
||||
|
|
@ -0,0 +1,132 @@
|
|||
package ru.spcex.clearing.balance.service;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import ru.clearing.classes.statics.data.account.Account;
|
||||
import ru.clearing.classes.statics.data.account.AccountBalance;
|
||||
import ru.clearing.classes.statics.data.company.Company;
|
||||
import ru.spcex.clearing.balance.errors.BalanceError;
|
||||
import ru.spcex.clearing.balance.utils.MatcherFactory.Matcher;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
import ru.spcex.platform.enumeration.AccountType;
|
||||
import ru.spcex.platform.enumeration.BalanceAccountType;
|
||||
import ru.spcex.platform.enumeration.Status;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import static ru.spcex.clearing.balance.utils.MatcherFactory.usingIgnoringFieldsComparator;
|
||||
|
||||
|
||||
class AccountBalanceServiceTest extends AbstractServiceTest {
|
||||
|
||||
public static final Matcher<AccountResult> ACCOUNT_BALANCE_MATCHER = usingIgnoringFieldsComparator("account.created", "account.updated", "account.clearingDate");
|
||||
private final Long id = 0L;
|
||||
private final Long accountIdNew = 0L;
|
||||
private final Long addresseeIdNew = 0L;
|
||||
private final BigDecimal amountNew = new BigDecimal(1000);
|
||||
private final String cashMovementCurrencyCodeNew = "cash";
|
||||
private final Long accountIdUpdate = accountIdNew;
|
||||
private final Long addresseeIdUpdate = addresseeIdNew;
|
||||
private final BigDecimal amountUpdate = new BigDecimal(100000);
|
||||
private final String cashMovementCurrencyCodeUpdate = "cash";
|
||||
@Autowired
|
||||
AccountBalanceService accountBalanceService;
|
||||
@Autowired
|
||||
@Qualifier("hazelcastServiceTest")
|
||||
private ImdgProvider hazelcast;
|
||||
|
||||
private Imdg<Company> companyImdg;
|
||||
private Imdg<Account> accountImdg;
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
companyImdg = hazelcast.getImdg(IMDGDistributedNames.Map_Company, Company.class);
|
||||
accountImdg = hazelcast.getImdg(IMDGDistributedNames.Map_Account, Account.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAccountBalance() {
|
||||
hazelcast.waitAvailable();
|
||||
Company company = new Company();
|
||||
company.setId(addresseeIdNew);
|
||||
company.setTradingCode("code");
|
||||
company.setShortName("ShortName");
|
||||
company.setFullName("FullName");
|
||||
companyImdg.insert(company);
|
||||
Account account = new Account();
|
||||
account.setId(accountIdNew);
|
||||
account.setAccount("123456789");
|
||||
account.setAccountType(AccountType.Clrn.getKey());
|
||||
account.setAccountStatus(Status.Active.getKey());
|
||||
accountImdg.insert(account);
|
||||
|
||||
AccountBalance accountBalanceNew = new AccountBalance();
|
||||
accountBalanceNew.setId(id);
|
||||
accountBalanceNew.setCompanyId(addresseeIdNew);
|
||||
accountBalanceNew.setAccountId(accountIdNew);
|
||||
accountBalanceNew.setAccountType(account.getAccountType());
|
||||
accountBalanceNew.setAccount(account.getAccount());
|
||||
accountBalanceNew.setOpenBalanceAmount(amountNew);
|
||||
accountBalanceNew.setFreeBalanceAmount(amountNew);
|
||||
accountBalanceNew.setBalanceAmount(amountNew);
|
||||
accountBalanceNew.setBalanceAccountType(BalanceAccountType.Active.getKey());
|
||||
accountBalanceNew.setCurrencyCode(cashMovementCurrencyCodeNew);
|
||||
accountBalanceNew.setTradingCode(company.getTradingCode());
|
||||
accountBalanceNew.setShortName(company.getShortName());
|
||||
accountBalanceNew.setFullName(company.getFullName());
|
||||
AccountResult predictableNewResult = new AccountResult(accountBalanceNew);
|
||||
AccountResult resultNew = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCodeNew);
|
||||
resultNew.getAccount().setId(id);
|
||||
|
||||
AccountBalance accountBalanceUpdate = new AccountBalance();
|
||||
accountBalanceUpdate.setId(id);
|
||||
accountBalanceUpdate.setCompanyId(addresseeIdUpdate);
|
||||
accountBalanceUpdate.setAccountId(accountIdUpdate);
|
||||
accountBalanceUpdate.setAccountType(account.getAccountType());
|
||||
accountBalanceUpdate.setAccount(account.getAccount());
|
||||
accountBalanceUpdate.setOpenBalanceAmount(amountUpdate);
|
||||
accountBalanceUpdate.setFreeBalanceAmount(amountUpdate);
|
||||
accountBalanceUpdate.setBalanceAmount(amountUpdate);
|
||||
accountBalanceUpdate.setBalanceAccountType(BalanceAccountType.Active.getKey());
|
||||
accountBalanceUpdate.setCurrencyCode(cashMovementCurrencyCodeUpdate);
|
||||
accountBalanceUpdate.setTradingCode(company.getTradingCode());
|
||||
accountBalanceUpdate.setShortName(company.getShortName());
|
||||
accountBalanceUpdate.setFullName(company.getFullName());
|
||||
AccountResult resultUpdate = accountBalanceService.createAccountBalance(addresseeIdUpdate, accountIdUpdate, amountUpdate, cashMovementCurrencyCodeUpdate);
|
||||
resultUpdate.getAccount().setId(id);
|
||||
AccountResult predictableUpdateResult = new AccountResult(accountBalanceUpdate);
|
||||
|
||||
ACCOUNT_BALANCE_MATCHER.assertMatch(resultNew, predictableNewResult);
|
||||
ACCOUNT_BALANCE_MATCHER.assertMatch(resultUpdate, predictableUpdateResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatedCreateAccountBalance() {
|
||||
AccountResult predictableResult;
|
||||
hazelcast.waitAvailable();
|
||||
predictableResult = new AccountResult(new EnumMessage(BalanceError.CompanyNotFound));
|
||||
|
||||
AccountResult result = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCodeNew);
|
||||
ACCOUNT_BALANCE_MATCHER.assertMatch(result, predictableResult);
|
||||
|
||||
Company company = new Company();
|
||||
company.setId(addresseeIdNew);
|
||||
companyImdg.insert(company);
|
||||
|
||||
Account account = new Account();
|
||||
// account.setId(accountIdNew);
|
||||
account.setAccountType(AccountType.Clrn.getKey());
|
||||
account.setAccountStatus(Status.Active.getKey());
|
||||
accountImdg.insert(account);
|
||||
predictableResult = new AccountResult(new EnumMessage(BalanceError.AccountNotPresent));
|
||||
|
||||
result = accountBalanceService.createAccountBalance(addresseeIdNew, accountIdNew, amountNew, cashMovementCurrencyCodeNew);
|
||||
ACCOUNT_BALANCE_MATCHER.assertMatch(result, predictableResult);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package ru.spcex.clearing.balance.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Factory for creating test matchers.
|
||||
* <p>
|
||||
* Comparing actual and expected objects via AssertJ
|
||||
*/
|
||||
public class MatcherFactory {
|
||||
|
||||
public static <T> Matcher<T> usingIgnoringFieldsComparator(String... fieldsToIgnore) {
|
||||
return new Matcher<>(fieldsToIgnore);
|
||||
}
|
||||
|
||||
public static class Matcher<T> {
|
||||
private final String[] fieldsToIgnore;
|
||||
|
||||
private Matcher(String... fieldsToIgnore) {
|
||||
this.fieldsToIgnore = fieldsToIgnore;
|
||||
}
|
||||
|
||||
public void assertMatch(T actual, T expected) {
|
||||
assertThat(actual).usingRecursiveComparison().ignoringFields(fieldsToIgnore).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public final void assertMatch(Iterable<T> actual, T... expected) {
|
||||
assertMatch(actual, Arrays.asList(expected));
|
||||
}
|
||||
|
||||
public void assertMatch(Iterable<T> actual, Iterable<T> expected) {
|
||||
assertThat(actual).usingRecursiveFieldByFieldElementComparatorIgnoringFields(fieldsToIgnore).isEqualTo(expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue