-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ignoreUtxosIfDatumHash flag
- Loading branch information
Showing
6 changed files
with
180 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...bloxbean/cardano/client/backend/api/helper/impl/DefaultUtxoSelectionStrategyImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.bloxbean.cardano.client.backend.api.helper.impl; | ||
|
||
import com.bloxbean.cardano.client.backend.api.UtxoService; | ||
import com.bloxbean.cardano.client.backend.exception.ApiException; | ||
import com.bloxbean.cardano.client.backend.model.Result; | ||
import com.bloxbean.cardano.client.backend.model.Utxo; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.io.IOException; | ||
import java.math.BigInteger; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.bloxbean.cardano.client.common.CardanoConstants.LOVELACE; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class DefaultUtxoSelectionStrategyImplTest { | ||
|
||
private static final String LIST_1 = "list1"; | ||
@Mock | ||
UtxoService utxoService; | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
String dataFile = "utxos-selection-strategy.json"; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
private List<Utxo> loadUtxos(String key) throws IOException { | ||
TypeReference<HashMap<String, List<Utxo>>> typeRef | ||
= new TypeReference<HashMap<String, List<Utxo>>>() {}; | ||
Map<String, List<Utxo>> map = objectMapper.readValue(this.getClass().getClassLoader().getResourceAsStream(dataFile), typeRef); | ||
return map.getOrDefault(key, Collections.emptyList()); | ||
} | ||
|
||
@Test | ||
void selectUtxos_returnUtxosWithoutDataHashWhenIgnoreUtxoDataHashIsTrue() throws IOException, ApiException { | ||
String address = "addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82"; | ||
|
||
List<Utxo> utxos = loadUtxos(LIST_1); | ||
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200)); | ||
|
||
DefaultUtxoSelectionStrategyImpl selectionStrategy = new DefaultUtxoSelectionStrategyImpl(utxoService); | ||
List<Utxo> selectedUtxos = selectionStrategy.selectUtxos(address, LOVELACE, new BigInteger("5000"), Collections.EMPTY_SET); | ||
|
||
List<String> txnHashList = selectedUtxos.stream().map(utxo -> utxo.getTxHash()).collect(Collectors.toList()); | ||
|
||
assertThat(txnHashList).doesNotContain("88c014d348bf1919c78a5cb87a5beed87729ff3f8a2019be040117a41a83e82e"); | ||
assertThat(txnHashList).hasSize(3); | ||
} | ||
|
||
@Test | ||
void selectUtxos_returnUtxosWithoutDataHashWhenIgnoreUtxoDataHashIsFalse() throws IOException, ApiException { | ||
String address = "addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82"; | ||
|
||
List<Utxo> utxos = loadUtxos(LIST_1); | ||
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200)); | ||
|
||
DefaultUtxoSelectionStrategyImpl selectionStrategy = new DefaultUtxoSelectionStrategyImpl(utxoService); | ||
selectionStrategy.setIgnoreUtxosWithDatumHash(false); | ||
|
||
List<Utxo> selectedUtxos = selectionStrategy.selectUtxos(address, LOVELACE, new BigInteger("5000"), Collections.EMPTY_SET); | ||
|
||
List<String> txnHashList = selectedUtxos.stream().map(utxo -> utxo.getTxHash()).collect(Collectors.toList()); | ||
|
||
assertThat(txnHashList).contains("88c014d348bf1919c78a5cb87a5beed87729ff3f8a2019be040117a41a83e82e"); | ||
assertThat(txnHashList).hasSize(3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"list1": [ | ||
{ | ||
"tx_hash": "735262c68b5fa220dee2b447d0d1dd44e0800ba6212dcea7955c561f365fb0e9", | ||
"tx_index": 0, | ||
"output_index": 0, | ||
"data_hash": null, | ||
"amount": [ | ||
{ | ||
"unit": "lovelace", | ||
"quantity": "1000" | ||
}, | ||
{ | ||
"unit": "6b8d07d69639e9413dd637a1a815a7323c69c86abbafb66dbfdb1aa7", | ||
"quantity": "2" | ||
} | ||
], | ||
"block": "656f65cb141fa8a37b50c60da1e9f28a2e45a1ea80e13ac4f9e1323a2ec07557" | ||
}, | ||
{ | ||
"tx_hash": "88c014d348bf1919c78a5cb87a5beed87729ff3f8a2019be040117a41a83e82e", | ||
"tx_index": 1, | ||
"output_index": 1, | ||
"data_hash": "somehashxxxxdfksdjfsdfsldfjslfjslfjslfjslfsdlfsdf", | ||
"amount": [ | ||
{ | ||
"unit": "lovelace", | ||
"quantity": "2000" | ||
} | ||
], | ||
"block": "1a06a27cfcb9316dfc3a498e96b80a3a9427ad03170077e3a80fd41a43ec3d7b" | ||
}, | ||
{ | ||
"tx_hash": "6674e44f0f03915b1611ce58aaff5f2e52054e1911fbcd0f17dbc205f44763b6", | ||
"tx_index": 0, | ||
"output_index": 0, | ||
"amount": [ | ||
{ | ||
"unit": "lovelace", | ||
"quantity": "3000" | ||
} | ||
], | ||
"block": "eb2043df94b34d1746cb47303446237e6dca7d051100c8f4229795be8c1f5e63" | ||
}, | ||
{ | ||
"tx_hash": "a712906ae823ecefe6cab76e5cfd427bd0b6144df10c6f89a56fbdf30fa807f4", | ||
"tx_index": 1, | ||
"output_index": 1, | ||
"amount": [ | ||
{ | ||
"unit": "lovelace", | ||
"quantity": "4000" | ||
} | ||
], | ||
"block": "c2a5408a54596d52e3b0d7aed546ceb7e4d74a06ce80dca13664acd3afdea3e5" | ||
} | ||
] | ||
} |