@Test public void getFundsForRequestGroupedByFunder() { final long requestId = 45L; final List<Fund> funds = new ArrayList<>(); final List<Refund> refunds = new ArrayList<>(); final String funder1UserId = "46534-gjh"; final String funder2UserId = "hgfh-676"; final String funder1Address = "0x5346547"; final String funder2Address = "0xabcdefg"; final List<UserFundsDto> userFunds = Arrays.asList(UserFundsDto.builder() .funderAddress(funder1Address) .funderUserId(funder1UserId) .fndFunds(TokenValueDtoMother.FND().totalAmount(new BigDecimal("30000000000000000000")).build()) .otherFunds(TokenValueDtoMother.ZRX().totalAmount(new BigDecimal("15000000000000000000")).build()) .fndRefunds(null) .otherRefunds(null) .build(), UserFundsDto.builder() .funderAddress(funder2Address) .funderUserId(funder2UserId) .fndFunds(TokenValueDtoMother.FND().totalAmount(new BigDecimal("40000000000000000000")).build()) .otherFunds(TokenValueDtoMother.ZRX().totalAmount(new BigDecimal("21000000000000000000")).build()) .fndRefunds(TokenValueDtoMother.FND().totalAmount(new BigDecimal("-20000000000000000000")).build()) .otherRefunds(TokenValueDtoMother.ZRX().totalAmount(new BigDecimal("-21000000000000000000")).build()) .build(), UserFundsDto.builder() .funderAddress("0xd645") .fndFunds(TokenValueDtoMother.FND().totalAmount(new BigDecimal("30000000000000000000")).build()) .fndRefunds(TokenValueDtoMother.FND().totalAmount(new BigDecimal("-15000000000000000000")).build()) .build(), UserFundsDto.builder() .funderAddress("0x645f") .funderUserId("dfgh-766dgfh") .otherFunds(TokenValueDtoMother.ZRX().totalAmount(new BigDecimal("50000000000000000000")).build()) .otherRefunds(TokenValueDtoMother.ZRX().totalAmount(new BigDecimal("-50000000000000000000")).build()) .build()); final List<FundsByFunderDto> fundsByFunder = Arrays.asList(FundsByFunderDto.builder().funderUserId(funder1UserId).build(), FundsByFunderDto.builder().funderUserId(funder2UserId).build()); final List<FundsByFunderDto> refundsByFunder = Collections.singletonList(FundsByFunderDto.builder().funderUserId(funder2UserId).build()); when(fundRepository.findAllByRequestId(requestId)).thenReturn(funds); when(refundRepository.findAllByRequestId(requestId)).thenReturn(refunds); when(fundFundsByFunderAggregator.aggregate(same(funds))).thenReturn(fundsByFunder); when(refundFundsByFunderAggregator.aggregate(same(refunds))).thenReturn(refundsByFunder); when(fundsAndRefundsAggregator.aggregate(Stream.concat(fundsByFunder.stream(), refundsByFunder.stream()).collect(Collectors.toList()))).thenReturn(userFunds); final FundsForRequestDto result = fundService.getFundsForRequestGroupedByFunder(requestId); assertThat(result.getUserFunds()).contains(UserFundsDto.builder() .funderAddress(funder1Address) .funderUserId(funder1UserId) .fndFunds(TokenValueDtoMother.FND().totalAmount(new BigDecimal("30000000000000000000")).build()) .otherFunds(TokenValueDtoMother.ZRX().totalAmount(new BigDecimal("15000000000000000000")).build()) .fndRefunds(null) .otherRefunds(null) .build(), UserFundsDto.builder() .funderAddress(funder2Address) .funderUserId(funder2UserId) .fndFunds(TokenValueDtoMother.FND().totalAmount(new BigDecimal("40000000000000000000")).build()) .otherFunds(TokenValueDtoMother.ZRX().totalAmount(new BigDecimal("21000000000000000000")).build()) .fndRefunds(TokenValueDtoMother.FND().totalAmount(new BigDecimal("-20000000000000000000")).build()) .otherRefunds(TokenValueDtoMother.ZRX().totalAmount(new BigDecimal("-21000000000000000000")).build()) .build(), UserFundsDto.builder() .funderAddress("0xd645") .fndFunds(TokenValueDtoMother.FND().totalAmount(new BigDecimal("30000000000000000000")).build()) .otherFunds(TokenValueDtoMother.ZRX().totalAmount(BigDecimal.ZERO).build()) .fndRefunds(TokenValueDtoMother.FND().totalAmount(new BigDecimal("-15000000000000000000")).build()) .otherRefunds(TokenValueDtoMother.ZRX().totalAmount(BigDecimal.ZERO).build()) .build(), UserFundsDto.builder() .funderAddress("0x645f") .funderUserId("dfgh-766dgfh") .fndFunds(TokenValueDtoMother.FND().totalAmount(BigDecimal.ZERO).build()) .otherFunds(TokenValueDtoMother.ZRX().totalAmount(new BigDecimal("50000000000000000000")).build()) .fndRefunds(TokenValueDtoMother.FND().totalAmount(BigDecimal.ZERO).build()) .otherRefunds(TokenValueDtoMother.ZRX().totalAmount(new BigDecimal("-50000000000000000000")).build()) .build() ); assertThat(result.getFndFunds().getTokenSymbol()).isEqualTo(TokenValueDtoMother.FND().build().getTokenSymbol()); assertThat(result.getFndFunds().getTokenAddress()).isEqualTo(TokenValueDtoMother.FND().build().getTokenAddress()); assertThat(result.getFndFunds().getTotalAmount()).isEqualTo(new BigDecimal("65000000000000000000")); assertThat(result.getOtherFunds().getTokenSymbol()).isEqualTo(TokenValueDtoMother.ZRX().build().getTokenSymbol()); assertThat(result.getOtherFunds().getTokenAddress()).isEqualTo(TokenValueDtoMother.ZRX().build().getTokenAddress()); assertThat(result.getOtherFunds().getTotalAmount()).isEqualTo(new BigDecimal("15000000000000000000")); }
@Override @Transactional(readOnly = true) public FundsForRequestDto getFundsForRequestGroupedByFunder(final Long requestId) { final List<UserFundsDto> userFunds = getFundsAndRefundsFor(requestId); enrichFundsWithZeroValues(userFunds); final TokenValueDto fndFunds = totalFunds(userFunds, UserFundsDto::getFndFunds, UserFundsDto::getFndRefunds); final TokenValueDto otherFunds = totalFunds(userFunds, UserFundsDto::getOtherFunds, UserFundsDto::getOtherRefunds); return FundsForRequestDto.builder() .userFunds(userFunds) .fndFunds(fndFunds) .otherFunds(otherFunds) .usdFunds(fiatService.getUsdPrice(fndFunds, otherFunds)) .build(); }
FundServiceImpl implements FundService { @Override @Transactional(readOnly = true) public FundsForRequestDto getFundsForRequestGroupedByFunder(final Long requestId) { final List<UserFundsDto> userFunds = getFundsAndRefundsFor(requestId); enrichFundsWithZeroValues(userFunds); final TokenValueDto fndFunds = totalFunds(userFunds, UserFundsDto::getFndFunds, UserFundsDto::getFndRefunds); final TokenValueDto otherFunds = totalFunds(userFunds, UserFundsDto::getOtherFunds, UserFundsDto::getOtherRefunds); return FundsForRequestDto.builder() .userFunds(userFunds) .fndFunds(fndFunds) .otherFunds(otherFunds) .usdFunds(fiatService.getUsdPrice(fndFunds, otherFunds)) .build(); } }
FundServiceImpl implements FundService { @Override @Transactional(readOnly = true) public FundsForRequestDto getFundsForRequestGroupedByFunder(final Long requestId) { final List<UserFundsDto> userFunds = getFundsAndRefundsFor(requestId); enrichFundsWithZeroValues(userFunds); final TokenValueDto fndFunds = totalFunds(userFunds, UserFundsDto::getFndFunds, UserFundsDto::getFndRefunds); final TokenValueDto otherFunds = totalFunds(userFunds, UserFundsDto::getOtherFunds, UserFundsDto::getOtherRefunds); return FundsForRequestDto.builder() .userFunds(userFunds) .fndFunds(fndFunds) .otherFunds(otherFunds) .usdFunds(fiatService.getUsdPrice(fndFunds, otherFunds)) .build(); } @Autowired  FundServiceImpl(final FundRepository fundRepository,
                           final RefundRepository refundRepository,
                           final PendingFundRepository pendingFundRepository,
                           final RequestRepository requestRepository,
                           final Mappers mappers,
                           final ApplicationEventPublisher eventPublisher,
                           final CacheManager cacheManager,
                           final FundRequestContractsService fundRequestContractsService,
                           final FiatService fiatService,
                           final TokenValueMapper tokenValueMapper,
                           final FundFundsByFunderAggregator fundFundsByFunderAggregator,
                           final RefundFundsByFunderAggregator refundFundsByFunderAggregator,
                           final FundsAndRefundsAggregator fundsAndRefundsAggregator); }
FundServiceImpl implements FundService { @Override @Transactional(readOnly = true) public FundsForRequestDto getFundsForRequestGroupedByFunder(final Long requestId) { final List<UserFundsDto> userFunds = getFundsAndRefundsFor(requestId); enrichFundsWithZeroValues(userFunds); final TokenValueDto fndFunds = totalFunds(userFunds, UserFundsDto::getFndFunds, UserFundsDto::getFndRefunds); final TokenValueDto otherFunds = totalFunds(userFunds, UserFundsDto::getOtherFunds, UserFundsDto::getOtherRefunds); return FundsForRequestDto.builder() .userFunds(userFunds) .fndFunds(fndFunds) .otherFunds(otherFunds) .usdFunds(fiatService.getUsdPrice(fndFunds, otherFunds)) .build(); } @Autowired  FundServiceImpl(final FundRepository fundRepository,
                           final RefundRepository refundRepository,
                           final PendingFundRepository pendingFundRepository,
                           final RequestRepository requestRepository,
                           final Mappers mappers,
                           final ApplicationEventPublisher eventPublisher,
                           final CacheManager cacheManager,
                           final FundRequestContractsService fundRequestContractsService,
                           final FiatService fiatService,
                           final TokenValueMapper tokenValueMapper,
                           final FundFundsByFunderAggregator fundFundsByFunderAggregator,
                           final RefundFundsByFunderAggregator refundFundsByFunderAggregator,
                           final FundsAndRefundsAggregator fundsAndRefundsAggregator); @Transactional(readOnly = true) @Override List<FundDto> findAll(); @Transactional(readOnly = true) @Override List<FundDto> findAll(Iterable<Long> ids); @Override @Transactional(readOnly = true) FundDto findOne(Long id); @Override @Transactional(readOnly = true) @Cacheable(value = "funds", key = "#requestId", unless = "#result.isEmpty()") List<TokenValueDto> getTotalFundsForRequest(Long requestId); @Override @Transactional(readOnly = true) FundsForRequestDto getFundsForRequestGroupedByFunder(final Long requestId); @Override @CacheEvict(value = "funds", key = "#requestId") void clearTotalFundsCache(Long requestId); @Override @Transactional void addFunds(final FundsAddedCommand command); @Override Optional<TokenValueDto> getFundsFor(final Long requestId, final String funderAddress, final String tokenAddress); }
FundServiceImpl implements FundService { @Override @Transactional(readOnly = true) public FundsForRequestDto getFundsForRequestGroupedByFunder(final Long requestId) { final List<UserFundsDto> userFunds = getFundsAndRefundsFor(requestId); enrichFundsWithZeroValues(userFunds); final TokenValueDto fndFunds = totalFunds(userFunds, UserFundsDto::getFndFunds, UserFundsDto::getFndRefunds); final TokenValueDto otherFunds = totalFunds(userFunds, UserFundsDto::getOtherFunds, UserFundsDto::getOtherRefunds); return FundsForRequestDto.builder() .userFunds(userFunds) .fndFunds(fndFunds) .otherFunds(otherFunds) .usdFunds(fiatService.getUsdPrice(fndFunds, otherFunds)) .build(); } @Autowired  FundServiceImpl(final FundRepository fundRepository,
                           final RefundRepository refundRepository,
                           final PendingFundRepository pendingFundRepository,
                           final RequestRepository requestRepository,
                           final Mappers mappers,
                           final ApplicationEventPublisher eventPublisher,
                           final CacheManager cacheManager,
                           final FundRequestContractsService fundRequestContractsService,
                           final FiatService fiatService,
                           final TokenValueMapper tokenValueMapper,
                           final FundFundsByFunderAggregator fundFundsByFunderAggregator,
                           final RefundFundsByFunderAggregator refundFundsByFunderAggregator,
                           final FundsAndRefundsAggregator fundsAndRefundsAggregator); @Transactional(readOnly = true) @Override List<FundDto> findAll(); @Transactional(readOnly = true) @Override List<FundDto> findAll(Iterable<Long> ids); @Override @Transactional(readOnly = true) FundDto findOne(Long id); @Override @Transactional(readOnly = true) @Cacheable(value = "funds", key = "#requestId", unless = "#result.isEmpty()") List<TokenValueDto> getTotalFundsForRequest(Long requestId); @Override @Transactional(readOnly = true) FundsForRequestDto getFundsForRequestGroupedByFunder(final Long requestId); @Override @CacheEvict(value = "funds", key = "#requestId") void clearTotalFundsCache(Long requestId); @Override @Transactional void addFunds(final FundsAddedCommand command); @Override Optional<TokenValueDto> getFundsFor(final Long requestId, final String funderAddress, final String tokenAddress);  }