Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#341 added automatic string split to max 64 bytes and added test for string splitting #362

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.bloxbean.cardano.client.metadata.MetadataList;
import com.bloxbean.cardano.client.metadata.MetadataMap;
import com.bloxbean.cardano.client.util.JsonUtil;
import com.bloxbean.cardano.client.util.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
Expand Down Expand Up @@ -36,16 +37,26 @@ public CBORMetadataList addNegative(BigInteger value) {

@Override
public CBORMetadataList add(String value) {
checkLength(value);
array.add(new UnicodeString(value));
if (checkLength(value) > 64) {
CBORMetadataList cborMetadataList = new CBORMetadataList();
cborMetadataList.addAll(StringUtils.splitStringEveryNCharacters(value, 64));
array.add(cborMetadataList.getArray());
} else {
array.add(new UnicodeString(value));
}
return this;
}

@Override
public CBORMetadataList addAll(String[] value) {
for (String str : value) {
checkLength(str);
array.add(new UnicodeString(str));
if (checkLength(str) > 64) {
CBORMetadataList cborMetadataList = new CBORMetadataList();
cborMetadataList.addAll(StringUtils.splitStringEveryNCharacters(str, 64));
array.add(cborMetadataList.getArray());
} else {
array.add(new UnicodeString(str));
}
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ public CBORMetadataMap put(BigInteger key, byte[] value) {

@Override
public CBORMetadataMap put(BigInteger key, String value) {
checkLength(value);
map.put(new UnsignedInteger(key), new UnicodeString(value));
if (checkLength(value) > 64) {
CBORMetadataList cborMetadataList = new CBORMetadataList();
cborMetadataList.addAll(StringUtils.splitStringEveryNCharacters(value, 64));
map.put(new UnsignedInteger(key), cborMetadataList.getArray());
} else {
map.put(new UnsignedInteger(key), new UnicodeString(value));
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.math.BigInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

class CBORMetadataListTest {

Expand All @@ -21,13 +22,16 @@ void testRemove() {
list.add("value5");
list.add("value6");
list.add("value7");
list.add("Extra Long string of more than 64 Bytes................................");
System.out.println(list.toJson());
assertThat(list.size()).isEqualTo(7);
assertThat(list.size()).isEqualTo(8);

assertTrue(list.getValueAt(7) instanceof CBORMetadataList);

list.removeItem("value5");
list.removeItem(BigInteger.valueOf(4));

assertThat(list.size()).isEqualTo(5);
assertThat(list.size()).isEqualTo(6);
assertThat(list.getValueAt(4)).isNotEqualTo("value5");
assertThat(list.getValueAt(4)).isEqualTo("value7");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.math.BigInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

class CBORMetadataMapTest {

Expand All @@ -17,13 +18,16 @@ void testRemove() throws CborException {
metadataMap.put("key1", "value1");
metadataMap.put("key2", BigInteger.valueOf(123));
metadataMap.put("key3", "value3");
metadataMap.put("key4", "Extra Long string of more than 64 Bytes................................");
metadataMap.put(new BigInteger("123456"), "value4");

assertThat(metadataMap.keys()).hasSize(4);
assertTrue(metadataMap.get("key4") instanceof CBORMetadataList);

assertThat(metadataMap.keys()).hasSize(5);
metadataMap.remove("key1");
metadataMap.remove(new BigInteger("123456"));

assertThat(metadataMap.keys()).hasSize(2);
assertThat(metadataMap.keys()).hasSize(3);
assertThat(metadataMap.get("key2")).isEqualTo(BigInteger.valueOf(123));
assertThat(metadataMap.get("key3")).isEqualTo("value3");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void getAuxiliaryDataHash_whenPlutusScriptAndMetadata() throws CborSerial
byte[] auxHashBytes = auxiliaryData.getAuxiliaryDataHash();
String auxHash = HexUtil.encodeHexString(auxHashBytes);

assertThat(auxHash).isEqualTo("591ad666282de3400e798f7a78957410624b0bb7bcbc004325eafc869818f142");
assertThat(auxHash).isEqualTo("9754d68a3d8bdc75150c415eba8a0b0623e10db17ee4a7a33883efc05f9cdc0b");
}

@Test
Expand Down