IMP-123: rename TerminalBalance -> AccountBalance (#59)

* IMP-123: implement terminal balance service

* IMP-123: add tests

* IMP-123: fix checkstyle violations

* IMP-123: add NPE test

* IMP-123: add getTerminalBalancesTest

* IMP-123: add getTerminalBalancesTest

* IMP-123: bump scrooge-proto, set balance as optional

* IMP-123: fix TerminalBalanceMapper

* IMP-123: rename TerminalBalance -> AccountBalance
This commit is contained in:
Fedor Shimich 2024-04-17 16:07:00 +03:00 committed by GitHub
parent 5497f51247
commit 21e20c518a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 92 additions and 85 deletions

View File

@ -30,7 +30,7 @@
<db.user>postgres</db.user>
<db.password>postgres</db.password>
<db.schema>scrooge</db.schema>
<scrooge.proto.version>1.23-aa3bf23</scrooge.proto.version>
<scrooge.proto.version>1.25-9ce7fc6</scrooge.proto.version>
<testcontainers.annotations.version>1.4.3</testcontainers.annotations.version>
<fistful-proto.version>1.134-cfaaf3d</fistful-proto.version>
<machinegun-proto.version>1.24-f533965</machinegun-proto.version>

View File

@ -0,0 +1,9 @@
package dev.vality.scrooge.dao;
import dev.vality.scrooge.account.AccountBalance;
import java.util.List;
public interface AccountBalanceDao {
List<AccountBalance> getAllAccountBalances();
}

View File

@ -1,7 +1,7 @@
package dev.vality.scrooge.dao;
import dev.vality.scrooge.dao.mapper.TerminalBalanceMapper;
import dev.vality.scrooge.terminal.balance.TerminalBalance;
import dev.vality.scrooge.dao.mapper.AccountBalanceMapper;
import dev.vality.scrooge.account.AccountBalance;
import org.jooq.Query;
import org.springframework.stereotype.Component;
@ -12,21 +12,21 @@ import static dev.vality.scrooge.dao.domain.Tables.*;
@Component
public class TerminalBalanceDaoImpl extends AbstractDao implements TerminalBalanceDao {
public class AccountBalanceDaoImpl extends AbstractDao implements AccountBalanceDao {
public static final String ACCOUNT_ID = "ACCOUNT_ID";
public static final String TERMINAL_ID = "TERMINAL_ID";
public static final String TERMINAL_NAME = "TERMINAL_NAME";
private final TerminalBalanceMapper terminalBalanceMapper;
private final AccountBalanceMapper accountBalanceMapper;
public TerminalBalanceDaoImpl(DataSource dataSource, TerminalBalanceMapper terminalBalanceMapper) {
public AccountBalanceDaoImpl(DataSource dataSource, AccountBalanceMapper accountBalanceMapper) {
super(dataSource);
this.terminalBalanceMapper = terminalBalanceMapper;
this.accountBalanceMapper = accountBalanceMapper;
}
@Override
public List<TerminalBalance> getAllTerminalBalances() {
public List<AccountBalance> getAllAccountBalances() {
Query query = getDslContext().select(PROVIDER.ID, PROVIDER.NAME, ACCOUNT.ID.as(ACCOUNT_ID), ACCOUNT.CURRENCY,
ACCOUNT.NUMBER, TERMINAL.ID.as(TERMINAL_ID), TERMINAL.NAME.as(TERMINAL_NAME),
BALANCE.VALUE, BALANCE.TIMESTAMP)
@ -36,6 +36,6 @@ public class TerminalBalanceDaoImpl extends AbstractDao implements TerminalBalan
.leftJoin(BALANCE).on(ACCOUNT.ID.eq(BALANCE.ACCOUNT_ID)))
.where(PROVIDER.ID.isNotNull().and(ACCOUNT.ID.isNotNull()))
.orderBy(PROVIDER.ID.desc());
return fetch(query, terminalBalanceMapper);
return fetch(query, accountBalanceMapper);
}
}

View File

@ -1,9 +0,0 @@
package dev.vality.scrooge.dao;
import dev.vality.scrooge.terminal.balance.TerminalBalance;
import java.util.List;
public interface TerminalBalanceDao {
List<TerminalBalance> getAllTerminalBalances();
}

View File

@ -1,10 +1,10 @@
package dev.vality.scrooge.dao.mapper;
import dev.vality.geck.common.util.TypeUtil;
import dev.vality.scrooge.terminal.balance.Balance;
import dev.vality.scrooge.terminal.balance.Provider;
import dev.vality.scrooge.terminal.balance.Terminal;
import dev.vality.scrooge.terminal.balance.TerminalBalance;
import dev.vality.scrooge.account.AccountBalance;
import dev.vality.scrooge.account.Balance;
import dev.vality.scrooge.account.Provider;
import dev.vality.scrooge.account.Terminal;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
@ -13,14 +13,14 @@ import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Optional;
import static dev.vality.scrooge.dao.TerminalBalanceDaoImpl.TERMINAL_ID;
import static dev.vality.scrooge.dao.TerminalBalanceDaoImpl.TERMINAL_NAME;
import static dev.vality.scrooge.dao.AccountBalanceDaoImpl.TERMINAL_ID;
import static dev.vality.scrooge.dao.AccountBalanceDaoImpl.TERMINAL_NAME;
import static dev.vality.scrooge.dao.domain.Tables.*;
@Component
public class TerminalBalanceMapper implements RowMapper<TerminalBalance> {
public class AccountBalanceMapper implements RowMapper<AccountBalance> {
@Override
public TerminalBalance mapRow(ResultSet rs, int rowNum) throws SQLException {
public AccountBalance mapRow(ResultSet rs, int rowNum) throws SQLException {
var terminal = new Terminal()
.setId(rs.getString(TERMINAL_ID))
.setName(rs.getString(TERMINAL_NAME));
@ -30,10 +30,10 @@ public class TerminalBalanceMapper implements RowMapper<TerminalBalance> {
var currencyCode = rs.getString(ACCOUNT.CURRENCY.getName());
var balance = Optional.ofNullable(rs.getString(BALANCE.VALUE.getName())).map(amount ->
new Balance().setAmount(amount).setCurrencyCode(currencyCode)).orElse(null);
new Balance().setAmount(getAmount(amount)).setCurrencyCode(currencyCode)).orElse(null);
return new TerminalBalance()
return new AccountBalance()
.setAccountId(rs.getString(ACCOUNT.NUMBER.getName()))
.setLastUpdated(
Optional.ofNullable(rs.getObject(BALANCE.TIMESTAMP.getName(), LocalDateTime.class))
@ -43,4 +43,12 @@ public class TerminalBalanceMapper implements RowMapper<TerminalBalance> {
.setBalance(balance)
.setProvider(provider);
}
private long getAmount(String str) {
try {
return Long.parseLong(str);
} catch (NumberFormatException e) {
return -1;
}
}
}

View File

@ -1,6 +1,6 @@
package dev.vality.scrooge.endpoint;
import dev.vality.scrooge.terminal.balance.TerminalServiceSrv;
import dev.vality.scrooge.account.AccountServiceSrv;
import dev.vality.woody.thrift.impl.http.THServiceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
@ -14,12 +14,12 @@ public class TerminalBalanceServlet extends GenericServlet {
private Servlet thriftServlet;
@Autowired
private TerminalServiceSrv.Iface requestHandler;
private AccountServiceSrv.Iface requestHandler;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
thriftServlet = new THServiceBuilder().build(TerminalServiceSrv.Iface.class, requestHandler);
thriftServlet = new THServiceBuilder().build(AccountServiceSrv.Iface.class, requestHandler);
}
@Override

View File

@ -0,0 +1,34 @@
package dev.vality.scrooge.service;
import dev.vality.fistful.fistful_stat.InvalidRequest;
import dev.vality.scrooge.dao.AccountBalanceDao;
import dev.vality.scrooge.account.AccountBalanceResponse;
import dev.vality.scrooge.account.AccountServiceSrv;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;
import java.util.List;
@Slf4j
@Service
@RequiredArgsConstructor
public class AccountBalanceHandler implements AccountServiceSrv.Iface {
private final AccountBalanceDao accountBalanceDao;
@Override
public AccountBalanceResponse getAccountBalances() throws TException {
log.info("New account balance request");
try {
var balances = accountBalanceDao.getAllAccountBalances();
var response = new AccountBalanceResponse(balances);
log.debug("Account balance response: {}", response);
return response;
} catch (Exception e) {
log.error("Failed to process stat request", e);
throw new InvalidRequest(List.of(e.getMessage()));
}
}
}

View File

@ -1,34 +0,0 @@
package dev.vality.scrooge.service;
import dev.vality.fistful.fistful_stat.InvalidRequest;
import dev.vality.scrooge.dao.TerminalBalanceDao;
import dev.vality.scrooge.terminal.balance.TerminalBalanceResponse;
import dev.vality.scrooge.terminal.balance.TerminalServiceSrv;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;
import java.util.List;
@Slf4j
@Service
@RequiredArgsConstructor
public class TerminalBalanceHandler implements TerminalServiceSrv.Iface {
private final TerminalBalanceDao terminalBalanceDao;
@Override
public TerminalBalanceResponse getTerminalBalances() throws TException {
log.info("New terminal balance request");
try {
var balances = terminalBalanceDao.getAllTerminalBalances();
var response = new TerminalBalanceResponse(balances);
log.debug("Terminal balance response: {}", response);
return response;
} catch (Exception e) {
log.error("Failed to process stat request", e);
throw new InvalidRequest(List.of(e.getMessage()));
}
}
}

View File

@ -57,7 +57,7 @@ public abstract class TestObjectFactory {
public static dev.vality.scrooge.dao.domain.tables.pojos.Balance testBalance(Long accountId) {
var balance = new dev.vality.scrooge.dao.domain.tables.pojos.Balance();
balance.setValue(randomString());
balance.setValue(randomLong().toString());
balance.setTimestamp(LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS));
balance.setAccountId(accountId);
return balance;

View File

@ -6,7 +6,7 @@ import dev.vality.scrooge.dao.domain.tables.pojos.Account;
import dev.vality.scrooge.dao.domain.tables.pojos.Balance;
import dev.vality.scrooge.dao.domain.tables.pojos.Provider;
import dev.vality.scrooge.dao.domain.tables.records.TerminalRecord;
import dev.vality.scrooge.dao.mapper.TerminalBalanceMapper;
import dev.vality.scrooge.dao.mapper.AccountBalanceMapper;
import org.jooq.DSLContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -23,9 +23,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
@PostgresqlJooqTest
@ContextConfiguration(classes = {
ProviderDaoImpl.class, TerminalDaoImpl.class, AccountDaoImpl.class,
BalanceDaoImpl.class, TerminalBalanceDaoImpl.class, TerminalBalanceMapper.class
BalanceDaoImpl.class, AccountBalanceDaoImpl.class, AccountBalanceMapper.class
})
class TerminalBalanceDaoImplTest {
class AccountBalanceDaoImplTest {
@Autowired
private ProviderDao providerDao;
@ -40,7 +40,7 @@ class TerminalBalanceDaoImplTest {
private BalanceDao balanceDao;
@Autowired
private TerminalBalanceDao terminalBalanceDao;
private AccountBalanceDao accountBalanceDao;
@Autowired
private DSLContext dslContext;
@ -61,7 +61,7 @@ class TerminalBalanceDaoImplTest {
final var account = createAccount(provider.getId());
final var balance = createBalance(account.getId());
var result = terminalBalanceDao.getAllTerminalBalances();
var result = accountBalanceDao.getAllAccountBalances();
assertEquals(1, result.size());
var terminalBalance = result.iterator().next();
@ -70,7 +70,7 @@ class TerminalBalanceDaoImplTest {
assertEquals(terminal.getName(), terminalBalance.getTerminal().getName());
assertEquals(provider.getId().toString(), terminalBalance.getProvider().getId());
assertEquals(provider.getName(), terminalBalance.getProvider().getName());
assertEquals(balance.getValue(), terminalBalance.getBalance().getAmount());
assertEquals(balance.getValue(), String.valueOf(terminalBalance.getBalance().getAmount()));
assertEquals(account.getCurrency(), terminalBalance.getBalance().getCurrencyCode());
}
@ -79,7 +79,7 @@ class TerminalBalanceDaoImplTest {
createTestRow();
createTestRow();
var result = terminalBalanceDao.getAllTerminalBalances();
var result = accountBalanceDao.getAllAccountBalances();
assertEquals(2, result.size());
var iterator = result.iterator();
@ -96,7 +96,7 @@ class TerminalBalanceDaoImplTest {
var account = createAccount(provider.getId());
var balance = createBalance(account.getId());
var result = terminalBalanceDao.getAllTerminalBalances();
var result = accountBalanceDao.getAllAccountBalances();
assertEquals(1, result.size());
}
@ -106,7 +106,7 @@ class TerminalBalanceDaoImplTest {
createTerminal(provider.getId());
createAccount(provider.getId());
var result = terminalBalanceDao.getAllTerminalBalances();
var result = accountBalanceDao.getAllAccountBalances();
assertEquals(1, result.size());
}

View File

@ -1,9 +1,8 @@
package dev.vality.scrooge.service.impl;
import dev.vality.scrooge.dao.TerminalBalanceDao;
import dev.vality.scrooge.service.TerminalBalanceHandler;
import dev.vality.scrooge.terminal.balance.TerminalBalance;
import dev.vality.scrooge.terminal.balance.TerminalServiceSrv;
import dev.vality.scrooge.dao.AccountBalanceDao;
import dev.vality.scrooge.service.AccountBalanceHandler;
import dev.vality.scrooge.account.AccountBalance;
import org.apache.thrift.TException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -19,19 +18,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {TerminalBalanceHandler.class})
public class TerminalBalanceHandlerTest {
@ContextConfiguration(classes = {AccountBalanceHandler.class})
public class AccountBalanceHandlerTest {
@Autowired
private TerminalBalanceHandler requestHandler;
private AccountBalanceHandler requestHandler;
@MockBean
private TerminalBalanceDao terminalBalanceDao;
private AccountBalanceDao accountBalanceDao;
@Test
void getTerminalBalancesTest() throws TException {
Mockito.when(terminalBalanceDao.getAllTerminalBalances()).thenReturn(List.of(new TerminalBalance()));
var result = requestHandler.getTerminalBalances();
Mockito.when(accountBalanceDao.getAllAccountBalances()).thenReturn(List.of(new AccountBalance()));
var result = requestHandler.getAccountBalances();
assertNotNull(result);
assertNotNull(result.getBalances());
assertEquals(1, result.getBalances().size());