Search in sources :

Example 1 with UserService

use of tech.cassandre.trading.bot.service.UserService in project cassandre-trading-bot by cassandre-tech.

the class BasicCassandreStrategyTestMock method userService.

@SuppressWarnings("unchecked")
@Bean
@Primary
public UserService userService() {
    Set<BalanceDTO> balances = new LinkedHashSet<>();
    final Map<String, AccountDTO> accounts = new LinkedHashMap<>();
    UserService userService = mock(UserService.class);
    // =============================================================================================================
    // Account retrieved by configuration - Empty, just allowing the getTradeAccount() in configuration to work.
    AccountDTO tempAccount = AccountDTO.builder().accountId("03").name("trade").build();
    accounts.put("03", tempAccount);
    UserDTO tempUser = UserDTO.builder().accounts(accounts).build();
    accounts.clear();
    // =============================================================================================================
    // Creating the response to the first call of the AccountFlux.
    // User has three accounts:
    // - Account 01 (No name with 1 BTC).
    // - Account 02 (No name with 1 ETH).
    // - Account 03 (Trade account with 2 BTC, 10 ETH, 2 000 USDT).
    // User service response 01.
    BalanceDTO account01Balance1 = BalanceDTO.builder().currency(BTC).available(new BigDecimal("1")).build();
    balances.add(account01Balance1);
    AccountDTO account01 = AccountDTO.builder().accountId("01").balances(balances).build();
    accounts.put("01", account01);
    UserDTO userResponse01 = UserDTO.builder().accounts(accounts).build();
    balances.clear();
    accounts.clear();
    // User service response 02.
    BalanceDTO account02Balance1 = BalanceDTO.builder().currency(ETH).available(new BigDecimal("1")).build();
    balances.add(account02Balance1);
    AccountDTO account02 = AccountDTO.builder().accountId("02").balances(balances).build();
    accounts.put("02", account02);
    UserDTO userResponse02 = UserDTO.builder().accounts(accounts).build();
    balances.clear();
    accounts.clear();
    // User service response 03.
    balances.add(BalanceDTO.builder().currency(BTC).available(new BigDecimal("2")).build());
    balances.add(BalanceDTO.builder().currency(ETH).available(new BigDecimal("10")).build());
    balances.add(BalanceDTO.builder().currency(EUR).available(new BigDecimal("2000")).build());
    AccountDTO account03 = AccountDTO.builder().accountId("03").name("trade").balances(balances).build();
    accounts.put("03", account03);
    UserDTO userResponse03 = UserDTO.builder().accounts(accounts).build();
    balances.clear();
    accounts.clear();
    // User service response 04.
    balances.add(BalanceDTO.builder().currency(BTC).available(new BigDecimal("1")).build());
    balances.add(BalanceDTO.builder().currency(ETH).available(new BigDecimal("10")).build());
    balances.add(BalanceDTO.builder().currency(USDT).available(new BigDecimal("100")).build());
    AccountDTO account04 = AccountDTO.builder().accountId("03").name("trade").balances(balances).build();
    accounts.put("03", account04);
    UserDTO userResponse04 = UserDTO.builder().accounts(accounts).build();
    balances.clear();
    accounts.clear();
    // We have two different mock replies.
    // StrategiesAutoConfiguration calls userService.getUser().
    given(userService.getUser()).willReturn(Optional.of(tempUser));
    // AccountFlux calls userService.getAccounts()
    given(userService.getAccounts()).willReturn(userResponse01.getAccounts(), userResponse02.getAccounts(), userResponse03.getAccounts(), userResponse04.getAccounts());
    return userService;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UserService(tech.cassandre.trading.bot.service.UserService) UserDTO(tech.cassandre.trading.bot.dto.user.UserDTO) BalanceDTO(tech.cassandre.trading.bot.dto.user.BalanceDTO) AccountDTO(tech.cassandre.trading.bot.dto.user.AccountDTO) BigDecimal(java.math.BigDecimal) LinkedHashMap(java.util.LinkedHashMap) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 2 with UserService

use of tech.cassandre.trading.bot.service.UserService in project cassandre-trading-bot by cassandre-tech.

the class BasicTa4jCassandreStrategyTestMock method userService.

@SuppressWarnings("unchecked")
@Bean
@Primary
public UserService userService() {
    Set<BalanceDTO> balances = new LinkedHashSet<>();
    final Map<String, AccountDTO> accounts = new LinkedHashMap<>();
    UserService userService = mock(UserService.class);
    // Returns three updates.
    // =============================================================================================================
    // Account retrieved by configuration.
    AccountDTO tempAccount = AccountDTO.builder().accountId("03").name("trade").build();
    accounts.put("trade", tempAccount);
    accounts.clear();
    // Account 01.
    BalanceDTO account01Balance1 = BalanceDTO.builder().available(new BigDecimal("1")).build();
    balances.add(account01Balance1);
    AccountDTO account01 = AccountDTO.builder().accountId("01").name("trade").balances(balances).build();
    accounts.put("01", account01);
    UserDTO user01 = UserDTO.builder().accounts(accounts).build();
    balances.clear();
    accounts.clear();
    // Account 02.
    BalanceDTO account02Balance1 = BalanceDTO.builder().available(new BigDecimal("1")).build();
    balances.add(account02Balance1);
    AccountDTO account02 = AccountDTO.builder().accountId("02").balances(balances).build();
    accounts.put("02", account02);
    UserDTO user02 = UserDTO.builder().accounts(accounts).build();
    balances.clear();
    accounts.clear();
    // Account 03.
    BalanceDTO account03Balance1 = BalanceDTO.builder().currency(BTC).available(new BigDecimal("1")).build();
    balances.add(account03Balance1);
    BalanceDTO account03Balance2 = BalanceDTO.builder().currency(USDT).available(new BigDecimal("150")).build();
    balances.add(account03Balance2);
    AccountDTO account03 = AccountDTO.builder().accountId("03").name("trade").balances(balances).build();
    accounts.put("03", account03);
    UserDTO user03 = UserDTO.builder().accounts(accounts).build();
    balances.clear();
    accounts.clear();
    // Mock replies.
    given(userService.getUser()).willReturn(Optional.of(user01), Optional.of(user02), Optional.of(user03));
    return userService;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UserService(tech.cassandre.trading.bot.service.UserService) UserDTO(tech.cassandre.trading.bot.dto.user.UserDTO) BalanceDTO(tech.cassandre.trading.bot.dto.user.BalanceDTO) AccountDTO(tech.cassandre.trading.bot.dto.user.AccountDTO) BigDecimal(java.math.BigDecimal) LinkedHashMap(java.util.LinkedHashMap) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Aggregations

BigDecimal (java.math.BigDecimal)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 Bean (org.springframework.context.annotation.Bean)2 Primary (org.springframework.context.annotation.Primary)2 AccountDTO (tech.cassandre.trading.bot.dto.user.AccountDTO)2 BalanceDTO (tech.cassandre.trading.bot.dto.user.BalanceDTO)2 UserDTO (tech.cassandre.trading.bot.dto.user.UserDTO)2 UserService (tech.cassandre.trading.bot.service.UserService)2