1 | package org.cardanofoundation.explorer.api.mapper; | |
2 | ||
3 | import java.sql.Timestamp; | |
4 | import java.time.LocalDateTime; | |
5 | import java.util.Objects; | |
6 | ||
7 | import org.springframework.beans.factory.annotation.Value; | |
8 | ||
9 | import org.apache.commons.lang3.StringUtils; | |
10 | import org.mapstruct.Mapper; | |
11 | import org.mapstruct.Mapping; | |
12 | import org.mapstruct.Named; | |
13 | ||
14 | import org.cardanofoundation.explorer.api.common.enumeration.AddressType; | |
15 | import org.cardanofoundation.explorer.api.model.response.token.TokenAddressResponse; | |
16 | import org.cardanofoundation.explorer.api.model.response.token.TokenFilterResponse; | |
17 | import org.cardanofoundation.explorer.api.model.response.token.TokenMetadataResponse; | |
18 | import org.cardanofoundation.explorer.api.model.response.token.TokenResponse; | |
19 | import org.cardanofoundation.explorer.api.projection.AddressTokenProjection; | |
20 | import org.cardanofoundation.explorer.api.util.HexUtils; | |
21 | import org.cardanofoundation.explorer.common.entity.ledgersync.MultiAsset; | |
22 | import org.cardanofoundation.explorer.common.entity.ledgersyncsagg.AddressTxAmount; | |
23 | ||
24 | @Mapper( | |
25 | componentModel = "spring", | |
26 | imports = {HexUtils.class}, | |
27 | uses = {AssetMetadataMapper.class}) | |
28 | public abstract class TokenMapper { | |
29 | ||
30 | @Value("${application.token-logo-endpoint}") | |
31 | protected String tokenLogoEndpoint; | |
32 | ||
33 | @Mapping( | |
34 | target = "displayName", | |
35 | expression = "java(getDisplayName(multiAsset.getNameView(), multiAsset.getFingerprint()))") | |
36 | @Mapping(target = "createdOn", source = "time") | |
37 | public abstract TokenFilterResponse fromMultiAssetToFilterResponse(MultiAsset multiAsset); | |
38 | ||
39 | @Mapping( | |
40 | target = "displayName", | |
41 | expression = "java(HexUtils.fromHex(multiAsset.getName(), multiAsset.getFingerprint()))") | |
42 | @Mapping(target = "createdOn", source = "time") | |
43 | public abstract TokenResponse fromMultiAssetToResponse(MultiAsset multiAsset); | |
44 | ||
45 | @Mapping( | |
46 | target = "displayName", | |
47 | expression = "java(HexUtils.fromHex(projection.getTokenName(), projection.getFingerprint()))") | |
48 | @Mapping(target = "name", source = "tokenName") | |
49 | @Mapping(target = "metadata", expression = "java(getMetadata(projection))") | |
50 | @Mapping(target = "addressType", expression = "java(getAddressType(projection.getHolder()))") | |
51 | @Mapping( | |
52 | target = "address", | |
53 | expression = "java(getAddress(projection.getAddress(), projection.getHolder()))") | |
54 | public abstract TokenAddressResponse fromAddressTokenProjection( | |
55 | AddressTokenProjection projection); | |
56 | ||
57 | @Mapping( | |
58 | target = "displayName", | |
59 | expression = "java(HexUtils.fromHex(multiAsset.getName(), multiAsset.getFingerprint()))") | |
60 | @Mapping(target = "name", source = "multiAsset.name") | |
61 | @Mapping(target = "metadata", expression = "java(getMetadata(projection))") | |
62 | @Mapping(target = "addressType", expression = "java(getAddressType(projection.getAddress()))") | |
63 | @Mapping(target = "policy", source = "multiAsset.policy") | |
64 | @Mapping(target = "fingerprint", source = "multiAsset.fingerprint") | |
65 | public abstract TokenAddressResponse fromAddressTokenProjectionAndMultiAsset( | |
66 | AddressTokenProjection projection, MultiAsset multiAsset); | |
67 | ||
68 | @Mapping( | |
69 | target = "displayName", | |
70 | expression = "java(HexUtils.fromHex(multiAsset.getName(), multiAsset.getFingerprint()))") | |
71 | @Mapping(target = "policy", source = "multiAsset.policy") | |
72 | @Mapping(target = "fingerprint", source = "multiAsset.fingerprint") | |
73 | @Mapping(target = "quantity", source = "addressTxAmount.quantity") | |
74 | @Mapping(target = "address", ignore = true) | |
75 | @Mapping(target = "addressId", ignore = true) | |
76 | public abstract TokenAddressResponse fromMultiAssetAndAddressToken( | |
77 | MultiAsset multiAsset, AddressTxAmount addressTxAmount); | |
78 | ||
79 | LocalDateTime fromTimestamp(Timestamp timestamp) { | |
80 |
2
1. fromTimestamp : replaced return value with null for org/cardanofoundation/explorer/api/mapper/TokenMapper::fromTimestamp → NO_COVERAGE 2. fromTimestamp : negated conditional → NO_COVERAGE |
return timestamp == null ? null : timestamp.toLocalDateTime(); |
81 | } | |
82 | ||
83 | String getDisplayName(String nameView, String fingerprint) { | |
84 |
1
1. getDisplayName : negated conditional → NO_COVERAGE |
if (!StringUtils.isEmpty(nameView)) { |
85 |
1
1. getDisplayName : replaced return value with "" for org/cardanofoundation/explorer/api/mapper/TokenMapper::getDisplayName → NO_COVERAGE |
return nameView; |
86 |
1
1. getDisplayName : replaced return value with "" for org/cardanofoundation/explorer/api/mapper/TokenMapper::getDisplayName → NO_COVERAGE |
} else return fingerprint; |
87 | } | |
88 | ||
89 | @Named("getTokenLogoURL") | |
90 | String getTokenLogoEndpoint(String logo) { | |
91 |
2
1. getTokenLogoEndpoint : replaced return value with "" for org/cardanofoundation/explorer/api/mapper/TokenMapper::getTokenLogoEndpoint → NO_COVERAGE 2. getTokenLogoEndpoint : negated conditional → NO_COVERAGE |
return Objects.isNull(logo) ? null : (tokenLogoEndpoint + logo); |
92 | } | |
93 | ||
94 | String getAddress(String address, String holder) { | |
95 |
2
1. getAddress : negated conditional → SURVIVED 2. getAddress : replaced return value with "" for org/cardanofoundation/explorer/api/mapper/TokenMapper::getAddress → SURVIVED |
return StringUtils.isEmpty(address) ? holder : address; |
96 | } | |
97 | ||
98 | AddressType getAddressType(String address) { | |
99 |
1
1. getAddressType : negated conditional → KILLED |
if (StringUtils.isEmpty(address)) { |
100 | return null; | |
101 | } | |
102 |
2
1. getAddressType : replaced return value with null for org/cardanofoundation/explorer/api/mapper/TokenMapper::getAddressType → SURVIVED 2. getAddressType : negated conditional → SURVIVED |
return address.startsWith("stake") ? AddressType.STAKE_ADDRESS : AddressType.PAYMENT_ADDRESS; |
103 | } | |
104 | ||
105 | TokenMetadataResponse getMetadata(AddressTokenProjection projection) { | |
106 |
1
1. getMetadata : negated conditional → SURVIVED |
if (StringUtils.isEmpty(projection.getSubject())) { |
107 | return null; | |
108 | } | |
109 | TokenMetadataResponse tokenMetadataResponse = new TokenMetadataResponse(); | |
110 |
1
1. getMetadata : removed call to org/cardanofoundation/explorer/api/model/response/token/TokenMetadataResponse::setUrl → NO_COVERAGE |
tokenMetadataResponse.setUrl(projection.getUrl()); |
111 |
1
1. getMetadata : removed call to org/cardanofoundation/explorer/api/model/response/token/TokenMetadataResponse::setTicker → NO_COVERAGE |
tokenMetadataResponse.setTicker(projection.getTicker()); |
112 |
1
1. getMetadata : removed call to org/cardanofoundation/explorer/api/model/response/token/TokenMetadataResponse::setLogo → NO_COVERAGE |
tokenMetadataResponse.setLogo(getTokenLogoEndpoint(projection.getLogo())); |
113 |
1
1. getMetadata : removed call to org/cardanofoundation/explorer/api/model/response/token/TokenMetadataResponse::setDecimals → NO_COVERAGE |
tokenMetadataResponse.setDecimals(projection.getDecimals()); |
114 |
1
1. getMetadata : removed call to org/cardanofoundation/explorer/api/model/response/token/TokenMetadataResponse::setDescription → NO_COVERAGE |
tokenMetadataResponse.setDescription(projection.getDescription()); |
115 |
1
1. getMetadata : replaced return value with null for org/cardanofoundation/explorer/api/mapper/TokenMapper::getMetadata → NO_COVERAGE |
return tokenMetadataResponse; |
116 | } | |
117 | } | |
Mutations | ||
80 |
1.1 2.2 |
|
84 |
1.1 |
|
85 |
1.1 |
|
86 |
1.1 |
|
91 |
1.1 2.2 |
|
95 |
1.1 2.2 |
|
99 |
1.1 |
|
102 |
1.1 2.2 |
|
106 |
1.1 |
|
110 |
1.1 |
|
111 |
1.1 |
|
112 |
1.1 |
|
113 |
1.1 |
|
114 |
1.1 |
|
115 |
1.1 |