InstantaneousRewardsServiceImpl.java

1
package org.cardanofoundation.explorer.api.service.impl;
2
3
import java.util.Comparator;
4
import java.util.List;
5
import java.util.Map;
6
import java.util.Set;
7
import java.util.function.Function;
8
import java.util.stream.Collectors;
9
10
import lombok.RequiredArgsConstructor;
11
12
import org.springframework.data.domain.Page;
13
import org.springframework.data.domain.PageImpl;
14
import org.springframework.data.domain.Pageable;
15
import org.springframework.data.domain.Sort;
16
import org.springframework.stereotype.Service;
17
18
import org.cardanofoundation.explorer.api.model.response.BaseFilterResponse;
19
import org.cardanofoundation.explorer.api.model.response.InstantaneousRewardsResponse;
20
import org.cardanofoundation.explorer.api.projection.InstantaneousRewardsProjection;
21
import org.cardanofoundation.explorer.api.projection.TxIOProjection;
22
import org.cardanofoundation.explorer.api.repository.ledgersync.ReserveRepository;
23
import org.cardanofoundation.explorer.api.repository.ledgersync.TreasuryRepository;
24
import org.cardanofoundation.explorer.api.repository.ledgersync.TxRepository;
25
import org.cardanofoundation.explorer.api.service.InstantaneousRewardsService;
26
27
@Service
28
@RequiredArgsConstructor
29
public class InstantaneousRewardsServiceImpl implements InstantaneousRewardsService {
30
31
  private final ReserveRepository reserveRepository;
32
  private final TreasuryRepository treasuryRepository;
33
  private final TxRepository txRepository;
34
  private static final String TX_ID = "txId";
35
  private static final String NUMBER_OF_STAKES = "numberOfStakes";
36
  private static final String REWARDS = "rewards";
37
38
  @Override
39
  public BaseFilterResponse<InstantaneousRewardsResponse> getAll(Pageable pageable) {
40
    var instantaneousRewards = reserveRepository.findAllTx();
41
    instantaneousRewards.addAll(treasuryRepository.findAllTx());
42 1 1. getAll : removed call to org/cardanofoundation/explorer/api/service/impl/InstantaneousRewardsServiceImpl::sortInstantaneousRewards → NO_COVERAGE
    sortInstantaneousRewards(pageable.getSort(), instantaneousRewards);
43
    int totalElements = instantaneousRewards.size();
44
    final int start = (int) pageable.getOffset();
45 1 1. getAll : Replaced integer addition with subtraction → NO_COVERAGE
    final int end = Math.min((start + pageable.getPageSize()), totalElements);
46
    instantaneousRewards = instantaneousRewards.subList(start, end);
47
    Set<Long> txIds =
48
        instantaneousRewards.stream()
49
            .map(InstantaneousRewardsProjection::getTxId)
50
            .collect(Collectors.toSet());
51
    List<TxIOProjection> txs = txRepository.findTxIn(txIds);
52
    Map<Long, TxIOProjection> txMap =
53
        txs.stream().collect(Collectors.toMap(TxIOProjection::getId, Function.identity()));
54
    List<InstantaneousRewardsResponse> response =
55
        instantaneousRewards.stream()
56
            .map(
57
                item ->
58 1 1. lambda$getAll$0 : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/InstantaneousRewardsServiceImpl::lambda$getAll$0 → NO_COVERAGE
                    InstantaneousRewardsResponse.builder()
59
                        .txHash(txMap.get(item.getTxId()).getHash())
60
                        .blockNo(txMap.get(item.getTxId()).getBlockNo())
61
                        .epochNo(txMap.get(item.getTxId()).getEpochNo())
62
                        .epochSlotNo(txMap.get(item.getTxId()).getEpochSlotNo())
63
                        .time(txMap.get(item.getTxId()).getTime())
64
                        .epochSlotNo(txMap.get(item.getTxId()).getEpochSlotNo())
65
                        .slotNo(txMap.get(item.getTxId()).getSlot())
66
                        .numberOfStakes(item.getNumberOfStakes())
67
                        .rewards(item.getRewards())
68
                        .build())
69
            .collect(Collectors.toList());
70
    Page<InstantaneousRewardsResponse> page = new PageImpl<>(response, pageable, totalElements);
71 1 1. getAll : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/InstantaneousRewardsServiceImpl::getAll → NO_COVERAGE
    return new BaseFilterResponse<>(page);
72
  }
73
74
  /**
75
   * Sorts the list of instantaneous rewards based on the sortable parameter
76
   *
77
   * @param sortable the sortable parameter
78
   * @param instantaneousRewards the list of instantaneous rewards
79
   */
80
  private void sortInstantaneousRewards(
81
      Sort sortable, List<InstantaneousRewardsProjection> instantaneousRewards) {
82
    String sortField = sortable.stream().findFirst().map(Sort.Order::getProperty).orElse(TX_ID);
83
    String sortOrder =
84
        sortable.stream()
85
            .findFirst()
86
            .map(Sort.Order::getDirection)
87
            .map(Enum::name)
88
            .orElse(Sort.Direction.DESC.name());
89
    switch (sortField) {
90
      case NUMBER_OF_STAKES -> {
91 1 1. sortInstantaneousRewards : negated conditional → NO_COVERAGE
        if (sortOrder.equals(Sort.Direction.ASC.name()))
92 1 1. sortInstantaneousRewards : removed call to java/util/List::sort → NO_COVERAGE
          instantaneousRewards.sort(
93
              Comparator.comparing(InstantaneousRewardsProjection::getNumberOfStakes));
94
        else
95 1 1. sortInstantaneousRewards : removed call to java/util/List::sort → NO_COVERAGE
          instantaneousRewards.sort(
96
              Comparator.comparing(InstantaneousRewardsProjection::getNumberOfStakes).reversed());
97
      }
98
      case REWARDS -> {
99 1 1. sortInstantaneousRewards : negated conditional → NO_COVERAGE
        if (sortOrder.equals(Sort.Direction.ASC.name()))
100 1 1. sortInstantaneousRewards : removed call to java/util/List::sort → NO_COVERAGE
          instantaneousRewards.sort(
101
              Comparator.comparing(InstantaneousRewardsProjection::getRewards));
102
        else
103 1 1. sortInstantaneousRewards : removed call to java/util/List::sort → NO_COVERAGE
          instantaneousRewards.sort(
104
              Comparator.comparing(InstantaneousRewardsProjection::getRewards).reversed());
105
      }
106
      default -> {
107 1 1. sortInstantaneousRewards : negated conditional → NO_COVERAGE
        if (sortOrder.equals(Sort.Direction.ASC.name()))
108 1 1. sortInstantaneousRewards : removed call to java/util/List::sort → NO_COVERAGE
          instantaneousRewards.sort(Comparator.comparing(InstantaneousRewardsProjection::getTxId));
109
        else
110 1 1. sortInstantaneousRewards : removed call to java/util/List::sort → NO_COVERAGE
          instantaneousRewards.sort(
111
              Comparator.comparing(InstantaneousRewardsProjection::getTxId).reversed());
112
      }
113
    }
114
  }
115
}

Mutations

42

1.1
Location : getAll
Killed by : none
removed call to org/cardanofoundation/explorer/api/service/impl/InstantaneousRewardsServiceImpl::sortInstantaneousRewards → NO_COVERAGE

45

1.1
Location : getAll
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

58

1.1
Location : lambda$getAll$0
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/service/impl/InstantaneousRewardsServiceImpl::lambda$getAll$0 → NO_COVERAGE

71

1.1
Location : getAll
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/service/impl/InstantaneousRewardsServiceImpl::getAll → NO_COVERAGE

91

1.1
Location : sortInstantaneousRewards
Killed by : none
negated conditional → NO_COVERAGE

92

1.1
Location : sortInstantaneousRewards
Killed by : none
removed call to java/util/List::sort → NO_COVERAGE

95

1.1
Location : sortInstantaneousRewards
Killed by : none
removed call to java/util/List::sort → NO_COVERAGE

99

1.1
Location : sortInstantaneousRewards
Killed by : none
negated conditional → NO_COVERAGE

100

1.1
Location : sortInstantaneousRewards
Killed by : none
removed call to java/util/List::sort → NO_COVERAGE

103

1.1
Location : sortInstantaneousRewards
Killed by : none
removed call to java/util/List::sort → NO_COVERAGE

107

1.1
Location : sortInstantaneousRewards
Killed by : none
negated conditional → NO_COVERAGE

108

1.1
Location : sortInstantaneousRewards
Killed by : none
removed call to java/util/List::sort → NO_COVERAGE

110

1.1
Location : sortInstantaneousRewards
Killed by : none
removed call to java/util/List::sort → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.14.2