MiCARServiceImpl.java

1
package org.cardanofoundation.explorer.api.service.impl;
2
3
import java.nio.charset.StandardCharsets;
4
import java.util.Objects;
5
import java.util.Optional;
6
7
import lombok.RequiredArgsConstructor;
8
9
import org.springframework.beans.factory.annotation.Value;
10
import org.springframework.stereotype.Service;
11
import org.springframework.web.reactive.function.client.WebClient;
12
13
import reactor.core.publisher.Mono;
14
15
import org.cardanofoundation.explorer.api.common.constant.CommonConstant;
16
import org.cardanofoundation.explorer.api.common.enumeration.TypeTokenGson;
17
import org.cardanofoundation.explorer.api.config.aop.singletoncache.SingletonCall;
18
import org.cardanofoundation.explorer.api.exception.BusinessCode;
19
import org.cardanofoundation.explorer.api.model.response.micar.AddressCarbonEmissionResponse;
20
import org.cardanofoundation.explorer.api.repository.ledgersync.StakeAddressRepository;
21
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.AddressRepository;
22
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.AddressTxCountRepository;
23
import org.cardanofoundation.explorer.api.repository.ledgersyncagg.StakeAddressTxCountRepository;
24
import org.cardanofoundation.explorer.api.service.MiCARService;
25
import org.cardanofoundation.explorer.api.util.AddressUtils;
26
import org.cardanofoundation.explorer.common.entity.ledgersync.StakeAddress;
27
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.Address;
28
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.AddressTxCount;
29
import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.StakeAddressTxCount;
30
import org.cardanofoundation.explorer.common.exception.BusinessException;
31
32
@Service
33
@RequiredArgsConstructor
34
public class MiCARServiceImpl implements MiCARService {
35
  private final StakeAddressTxCountRepository stakeAddressTxCountRepository;
36
  private final AddressTxCountRepository addressTxCountRepository;
37
  private final StakeAddressRepository stakeAddressRepository;
38
  private final WebClient webClient;
39
40
  @Value("${application.api.micar.overview}")
41
  private String apiMicarOverviewUrl;
42
43
  @Value("${application.api.micar.historical}")
44
  private String apiMicarHistoricalUrl;
45
46
  @Value("${application.api.micar.public-key}")
47
  private String micarPublicKey;
48
49
  private final AddressRepository addressRepository;
50
51
  @Override
52
  public AddressCarbonEmissionResponse getCarbonEmissionsByAddressAndPool(String address) {
53 1 1. getCarbonEmissionsByAddressAndPool : negated conditional → KILLED
    if (Objects.isNull(address)) {
54 1 1. getCarbonEmissionsByAddressAndPool : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::getCarbonEmissionsByAddressAndPool → NO_COVERAGE
      return AddressCarbonEmissionResponse.builder().build();
55
    }
56 1 1. getCarbonEmissionsByAddressAndPool : negated conditional → KILLED
    if (address.startsWith(CommonConstant.PREFIXED_STAKE_KEY)) {
57
      Optional<StakeAddress> stakeAddress = stakeAddressRepository.findByView(address);
58 1 1. getCarbonEmissionsByAddressAndPool : negated conditional → KILLED
      if (stakeAddress.isEmpty()) {
59 1 1. getCarbonEmissionsByAddressAndPool : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::getCarbonEmissionsByAddressAndPool → KILLED
        return AddressCarbonEmissionResponse.builder().build();
60
      }
61
      Optional<StakeAddressTxCount> stakeAddressTxCount =
62
          stakeAddressTxCountRepository.findByStakeAddress(stakeAddress.get().getView());
63 1 1. getCarbonEmissionsByAddressAndPool : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::getCarbonEmissionsByAddressAndPool → KILLED
      return AddressCarbonEmissionResponse.builder()
64
          .stakeAddress(address)
65
          .txCount(stakeAddressTxCount.orElseGet(StakeAddressTxCount::new).getTxCount())
66
          .carbonEmissionPerTx(CommonConstant.MiCAR.CO2_EMISSION_PER_TX)
67
          .build();
68
    } else {
69
      try {
70
        AddressUtils.checkStakeAddress(address);
71
        Optional<Address> addr = addressRepository.findFirstByAddress(address);
72 1 1. getCarbonEmissionsByAddressAndPool : negated conditional → KILLED
        if (addr.isEmpty()) {
73 1 1. getCarbonEmissionsByAddressAndPool : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::getCarbonEmissionsByAddressAndPool → NO_COVERAGE
          return AddressCarbonEmissionResponse.builder().build();
74
        }
75
        Optional<AddressTxCount> addressTxCount = addressTxCountRepository.findByAddress(address);
76 1 1. getCarbonEmissionsByAddressAndPool : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::getCarbonEmissionsByAddressAndPool → KILLED
        return AddressCarbonEmissionResponse.builder()
77
            .address(address)
78
            .txCount(addressTxCount.orElseGet(AddressTxCount::new).getTxCount())
79
            .carbonEmissionPerTx(CommonConstant.MiCAR.CO2_EMISSION_PER_TX)
80
            .build();
81
      } catch (Exception e) {
82 1 1. getCarbonEmissionsByAddressAndPool : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::getCarbonEmissionsByAddressAndPool → KILLED
        return AddressCarbonEmissionResponse.builder().build();
83
      }
84
    }
85
  }
86
87
  @SingletonCall(typeToken = TypeTokenGson.MICAR, expireAfterSeconds = 200)
88
  public Object getCarbonEmissionsOverview() {
89 1 1. getCarbonEmissionsOverview : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::getCarbonEmissionsOverview → NO_COVERAGE
    return webClient
90
        .get()
91
        .uri(apiMicarOverviewUrl, micarPublicKey)
92
        .acceptCharset(StandardCharsets.UTF_8)
93
        .retrieve()
94
        .onStatus(
95 3 1. lambda$getCarbonEmissionsOverview$0 : negated conditional → NO_COVERAGE
2. lambda$getCarbonEmissionsOverview$0 : replaced boolean return with true for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::lambda$getCarbonEmissionsOverview$0 → NO_COVERAGE
3. lambda$getCarbonEmissionsOverview$0 : negated conditional → NO_COVERAGE
            status -> status.is4xxClientError() || status.is5xxServerError(),
96
            clientResponse ->
97 1 1. lambda$getCarbonEmissionsOverview$1 : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::lambda$getCarbonEmissionsOverview$1 → NO_COVERAGE
                Mono.error(new BusinessException(BusinessCode.EXTERNAL_API_IS_NOT_AVAILABLE)))
98
        .bodyToMono(Object.class)
99
        .block();
100
  }
101
102
  @SingletonCall(typeToken = TypeTokenGson.MICAR, expireAfterSeconds = 200)
103
  public Object getCarbonEmissionsHistorical() {
104 1 1. getCarbonEmissionsHistorical : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::getCarbonEmissionsHistorical → NO_COVERAGE
    return webClient
105
        .get()
106
        .uri(apiMicarHistoricalUrl, micarPublicKey)
107
        .acceptCharset(StandardCharsets.UTF_8)
108
        .retrieve()
109
        .onStatus(
110 3 1. lambda$getCarbonEmissionsHistorical$2 : negated conditional → NO_COVERAGE
2. lambda$getCarbonEmissionsHistorical$2 : negated conditional → NO_COVERAGE
3. lambda$getCarbonEmissionsHistorical$2 : replaced boolean return with true for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::lambda$getCarbonEmissionsHistorical$2 → NO_COVERAGE
            status -> status.is4xxClientError() || status.is5xxServerError(),
111
            clientResponse ->
112 1 1. lambda$getCarbonEmissionsHistorical$3 : replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::lambda$getCarbonEmissionsHistorical$3 → NO_COVERAGE
                Mono.error(new BusinessException(BusinessCode.EXTERNAL_API_IS_NOT_AVAILABLE)))
113
        .bodyToMono(Object.class)
114
        .block();
115
  }
116
}

Mutations

53

1.1
Location : getCarbonEmissionsByAddressAndPool
Killed by : org.cardanofoundation.explorer.api.service.MiCARServiceTest.[engine:junit-jupiter]/[class:org.cardanofoundation.explorer.api.service.MiCARServiceTest]/[method:testGetCarbonEmissionsByAddress_matchingAddress()]
negated conditional → KILLED

54

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

56

1.1
Location : getCarbonEmissionsByAddressAndPool
Killed by : org.cardanofoundation.explorer.api.service.MiCARServiceTest.[engine:junit-jupiter]/[class:org.cardanofoundation.explorer.api.service.MiCARServiceTest]/[method:testGetCarbonEmissionsByAddress_matchingAddress()]
negated conditional → KILLED

58

1.1
Location : getCarbonEmissionsByAddressAndPool
Killed by : org.cardanofoundation.explorer.api.service.MiCARServiceTest.[engine:junit-jupiter]/[class:org.cardanofoundation.explorer.api.service.MiCARServiceTest]/[method:testGetCarbonEmissionsByAddress_matchingStakeAddress()]
negated conditional → KILLED

59

1.1
Location : getCarbonEmissionsByAddressAndPool
Killed by : org.cardanofoundation.explorer.api.service.MiCARServiceTest.[engine:junit-jupiter]/[class:org.cardanofoundation.explorer.api.service.MiCARServiceTest]/[method:testGetCarbonEmissionsByAddress_StakeAddressNotExist()]
replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::getCarbonEmissionsByAddressAndPool → KILLED

63

1.1
Location : getCarbonEmissionsByAddressAndPool
Killed by : org.cardanofoundation.explorer.api.service.MiCARServiceTest.[engine:junit-jupiter]/[class:org.cardanofoundation.explorer.api.service.MiCARServiceTest]/[method:testGetCarbonEmissionsByAddress_matchingStakeAddress()]
replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::getCarbonEmissionsByAddressAndPool → KILLED

72

1.1
Location : getCarbonEmissionsByAddressAndPool
Killed by : org.cardanofoundation.explorer.api.service.MiCARServiceTest.[engine:junit-jupiter]/[class:org.cardanofoundation.explorer.api.service.MiCARServiceTest]/[method:testGetCarbonEmissionsByAddress_matchingAddress()]
negated conditional → KILLED

73

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

76

1.1
Location : getCarbonEmissionsByAddressAndPool
Killed by : org.cardanofoundation.explorer.api.service.MiCARServiceTest.[engine:junit-jupiter]/[class:org.cardanofoundation.explorer.api.service.MiCARServiceTest]/[method:testGetCarbonEmissionsByAddress_matchingAddress()]
replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::getCarbonEmissionsByAddressAndPool → KILLED

82

1.1
Location : getCarbonEmissionsByAddressAndPool
Killed by : org.cardanofoundation.explorer.api.service.MiCARServiceTest.[engine:junit-jupiter]/[class:org.cardanofoundation.explorer.api.service.MiCARServiceTest]/[method:testGetCarbonEmissionsByAddress_AddressNotValid()]
replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::getCarbonEmissionsByAddressAndPool → KILLED

89

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

95

1.1
Location : lambda$getCarbonEmissionsOverview$0
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : lambda$getCarbonEmissionsOverview$0
Killed by : none
replaced boolean return with true for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::lambda$getCarbonEmissionsOverview$0 → NO_COVERAGE

3.3
Location : lambda$getCarbonEmissionsOverview$0
Killed by : none
negated conditional → NO_COVERAGE

97

1.1
Location : lambda$getCarbonEmissionsOverview$1
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::lambda$getCarbonEmissionsOverview$1 → NO_COVERAGE

104

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

110

1.1
Location : lambda$getCarbonEmissionsHistorical$2
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : lambda$getCarbonEmissionsHistorical$2
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : lambda$getCarbonEmissionsHistorical$2
Killed by : none
replaced boolean return with true for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::lambda$getCarbonEmissionsHistorical$2 → NO_COVERAGE

112

1.1
Location : lambda$getCarbonEmissionsHistorical$3
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/service/impl/MiCARServiceImpl::lambda$getCarbonEmissionsHistorical$3 → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.14.2