@Transactional(readOnly = true) @Override public List<FundDto> findAll() { return mappers.mapList(Fund.class, FundDto.class, fundRepository.findAll()); }
private List<UserFundsDto> getFundsAndRefundsFor(final Long requestId) { final List<Fund> fundsForRequest = fundRepository.findAllByRequestId(requestId); final List<Refund> refundsForRequest = refundRepository.findAllByRequestId(requestId); return fundsAndRefundsAggregator.aggregate(Stream.concat(fundFundsByFunderAggregator.aggregate(fundsForRequest).stream(), refundFundsByFunderAggregator.aggregate(refundsForRequest).stream()) .collect(Collectors.toList())); }
@Transactional(readOnly = true) @Override @Cacheable(value = "statistics", key = "'all'") public StatisticsDto getStatistics() { double totalAvailableFunding = fundRepository.getAmountPerTokenWhereRequestHasStatusFunded() .stream() .mapToDouble(this::mapToUsd) .sum(); String mostFundedProject = getMostFunded(fundRepository.getAmountPerTokenPerProjectWhereRequestHasStatusFunded()); String mostFundedTechnology = getMostFunded(fundRepository.getAmountPerTokenPerTechnologyWhereRequestHasStatusFunded()); return StatisticsDto.builder() .totalAvailableFunding(totalAvailableFunding) .mostFundedProject(mostFundedProject) .mostFundedTechnology(mostFundedTechnology) .build(); }
fundRequestContractsService = mock(FundRequestContractsService.class, RETURNS_DEEP_STUBS); fiatService = mock(FiatService.class); when(fundRepository.saveAndFlush(any(Fund.class))).then(returnsFirstArg()); funder = UserProfileMother.davy()::getId; tokenValueMapper = mock(TokenValueMapper.class); fundsAndRefundsAggregator); when(fundRepository.saveAndFlush(any(Fund.class))).then(returnsFirstArg());
final Request request3 = requestRepository.saveAndFlush(RequestMother.freeCodeCampNoUserStories().withStatus(RequestStatus.CLAIMED).build()); fundRepository.save(Arrays.asList(FundMother.zrxFundFunderKnown() .requestId(request1.getId()) .tokenValue(ZRX().amountInWei(new BigDecimal("10")).build()) .build())); final List<Object[]> result = fundRepository.getAmountPerTokenPerProjectWhereRequestHasStatusFunded();
Request request3 = requestRepository.saveAndFlush(RequestMother.freeCodeCampNoUserStories().withStatus(RequestStatus.CLAIMED).build()); fundRepository.save(Arrays.asList(FundMother.zrxFundFunderKnown() .requestId(request.getId()) .tokenValue(ZRX().amountInWei(new BigDecimal("10")).build()) .build())); List<Object[]> result = fundRepository.getAmountPerTokenPerTechnologyWhereRequestHasStatusFunded();
@Test public void statisticsReturnsTotalAvailableFunding() { TokenAmountDto tokenAmount = new TokenAmountDto(fnd.getAddress(), EthUtil.toWei(BigDecimal.TEN, fnd.getDecimals())); TokenAmountDto tokenAmount2 = new TokenAmountDto(zrx.getAddress(), EthUtil.toWei(BigDecimal.ONE, zrx.getDecimals())); when(fundRepository.getAmountPerTokenWhereRequestHasStatusFunded()).thenReturn(Arrays.asList(tokenAmount, tokenAmount2)); when(fiatService.getUsdPrice(TokenValueDto.builder() .tokenSymbol(fnd.getSymbol()) .tokenAddress(fnd.getAddress()) .totalAmount(EthUtil.fromWei(tokenAmount.getTotalAmount(), fnd.getDecimals())) .build())).thenReturn(10.0); when(fiatService.getUsdPrice(TokenValueDto.builder() .tokenSymbol(zrx.getSymbol()) .tokenAddress(zrx.getAddress()) .totalAmount(EthUtil.fromWei(tokenAmount2.getTotalAmount(), zrx.getDecimals())) .build())).thenReturn(20.0); StatisticsDto result = statisticsService.getStatistics(); assertThat(result.getTotalAvailableFunding()).isEqualTo(30.0); }
@Test public void statisticsReturnsMostFundedProject() { TokenAmountDto tokenAmount = new TokenAmountDto(fnd.getAddress(), EthUtil.toWei(BigDecimal.TEN, fnd.getDecimals())); TokenAmountDto tokenAmount2 = new TokenAmountDto(zrx.getAddress(), EthUtil.toWei(BigDecimal.ONE, zrx.getDecimals())); List<Object[]> requests = Arrays.asList( createObject("FundRequest", fnd.getAddress(), tokenAmount.getTotalAmount()), createObject("Dock", zrx.getAddress(), tokenAmount2.getTotalAmount()) ); when(fundRepository.getAmountPerTokenPerProjectWhereRequestHasStatusFunded()).thenReturn(requests); when(fiatService.getUsdPrice(TokenValueDto.builder() .tokenSymbol(fnd.getSymbol()) .tokenAddress(fnd.getAddress()) .totalAmount(EthUtil.fromWei(tokenAmount.getTotalAmount(), fnd.getDecimals())) .build())).thenReturn(10.0); when(fiatService.getUsdPrice(TokenValueDto.builder() .tokenSymbol(zrx.getSymbol()) .tokenAddress(zrx.getAddress()) .totalAmount(EthUtil.fromWei(tokenAmount2.getTotalAmount(), zrx.getDecimals())) .build())).thenReturn(20.0); StatisticsDto result = statisticsService.getStatistics(); assertThat(result.getMostFundedProject()).isEqualTo("Dock"); }
@Test public void statisticsReturnsMostFundedTechnology() { TokenAmountDto tokenAmount = new TokenAmountDto(fnd.getAddress(), EthUtil.toWei(BigDecimal.TEN, fnd.getDecimals())); TokenAmountDto tokenAmount2 = new TokenAmountDto(zrx.getAddress(), EthUtil.toWei(BigDecimal.ONE, zrx.getDecimals())); List<Object[]> requests = Arrays.asList( createObject("Java", fnd.getAddress(), tokenAmount.getTotalAmount()), createObject("Python", zrx.getAddress(), tokenAmount2.getTotalAmount()) ); when(fundRepository.getAmountPerTokenPerTechnologyWhereRequestHasStatusFunded()).thenReturn(requests); when(fiatService.getUsdPrice(TokenValueDto.builder() .tokenSymbol(fnd.getSymbol()) .tokenAddress(fnd.getAddress()) .totalAmount(EthUtil.fromWei(tokenAmount.getTotalAmount(), fnd.getDecimals())) .build())).thenReturn(10.0); when(fiatService.getUsdPrice(TokenValueDto.builder() .tokenSymbol(zrx.getSymbol()) .tokenAddress(zrx.getAddress()) .totalAmount(EthUtil.fromWei(tokenAmount2.getTotalAmount(), zrx.getDecimals())) .build())).thenReturn(20.0); StatisticsDto result = statisticsService.getStatistics(); assertThat(result.getMostFundedTechnology()).isEqualTo("Python"); }
@Override @Transactional(readOnly = true) public FundDto findOne(Long id) { return mappers.map(Fund.class, FundDto.class, fundRepository.findOne(id).orElseThrow(ResourceNotFoundException::new)); }
private void verifyFundsSaved(final FundsAddedCommand command, final Principal funder) { ArgumentCaptor<Fund> fundArgumentCaptor = ArgumentCaptor.forClass(Fund.class); verify(fundRepository).saveAndFlush(fundArgumentCaptor.capture()); assertThat(fundArgumentCaptor.getValue().getRequestId()).isEqualTo(command.getRequestId()); assertThat(fundArgumentCaptor.getValue().getTokenValue()).isEqualTo(TokenValue.builder().amountInWei(command.getAmountInWei()).tokenAddress(command.getToken()).build()); assertThat(fundArgumentCaptor.getValue().getFunderUserId()).isEqualTo(funder.getName()); assertThat(fundArgumentCaptor.getValue().getBlockchainEventId()).isEqualTo(command.getBlockchainEventId()); } }
@Test public void findRequestsUserHasFunded() throws Exception { Request request = RequestMother .freeCodeCampNoUserStories() .build(); request = requestRepository.saveAndFlush(request); Fund fund = FundMother.fndFundFunderKnown().requestId(request.getId()).build(); fundRepository.saveAndFlush(fund); List<Request> requests = requestRepository.findRequestsUserHasFunded("wrong", Collections.singletonList(fund.getFunderAddress())); assertThat(requests).containsExactly(request); }
@Transactional(readOnly = true) @Override public List<FundDto> findAll(Iterable<Long> ids) { return mappers.mapList(Fund.class, FundDto.class, fundRepository.findAll(ids)); }
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);
@Override @Transactional public void addFunds(final FundsAddedCommand command) { final Fund.FundBuilder fundBuilder = Fund.builder() .tokenValue(TokenValue.builder() .amountInWei(command.getAmountInWei()) .tokenAddress(command.getToken()) .build()) .requestId(command.getRequestId()) .timestamp(command.getTimestamp()) .funderAddress(command.getFunderAddress()) .blockchainEventId(command.getBlockchainEventId()); final Optional<PendingFund> pendingFund = pendingFundRepository.findByTransactionHash(command.getTransactionHash()); if (pendingFund.isPresent()) { fundBuilder.funderUserId(pendingFund.get().getUserId()); } final Fund fund = fundRepository.saveAndFlush(fundBuilder.build()); cacheManager.getCache("funds").evict(fund.getRequestId()); eventPublisher.publishEvent(RequestFundedEvent.builder() .fundDto(mappers.map(Fund.class, FundDto.class, fund)) .requestId(command.getRequestId()) .timestamp(command.getTimestamp()) .build()); }
@Test public void findAllByIterable() { List<Fund> funds = singletonList(FundMother.fndFundFunderKnown().build()); Set<Long> ids = funds.stream().map(Fund::getId).collect(Collectors.toSet()); when(fundRepository.findAll(ids)).thenReturn(funds); List<FundDto> expecedFunds = singletonList(FundDtoMother.aFundDto().build()); when(mappers.mapList(Fund.class, FundDto.class, funds)).thenReturn(expecedFunds); List<FundDto> result = fundService.findAll(ids); assertThat(result).isEqualTo(expecedFunds); }
@Test public void save() throws Exception { Request request = requestRepository.saveAndFlush(RequestMother.freeCodeCampNoUserStories().build()); final Fund fund = FundMother.fndFundFunderKnown() .tokenValue(FND().amountInWei((new BigDecimal("30"))).build()) .requestId(request.getId()) .build(); fundRepository.saveAndFlush(fund); }
@Test public void findAll() { List<Fund> funds = singletonList(FundMother.fndFundFunderKnown().build()); when(fundRepository.findAll()).thenReturn(funds); List<FundDto> expecedFunds = singletonList(FundDtoMother.aFundDto().build()); when(mappers.mapList(Fund.class, FundDto.class, funds)).thenReturn(expecedFunds); List<FundDto> result = fundService.findAll(); assertThat(result).isEqualTo(expecedFunds); }