Introducing the HTLC object instance

This commit is contained in:
Nelson R. Perez 2019-07-30 17:05:21 -05:00
parent 39bcf78add
commit 6ba8379b92
2 changed files with 69 additions and 0 deletions

View file

@ -0,0 +1,43 @@
package cy.agorise.graphenej;
import com.google.gson.JsonElement;
import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import cy.agorise.graphenej.interfaces.ByteSerializable;
import cy.agorise.graphenej.interfaces.JsonSerializable;
/**
* Class used to represent an existing HTLC contract.
*/
public class Htlc extends GrapheneObject implements ByteSerializable, JsonSerializable {
public Htlc(String id) {
super(id);
}
@Override
public byte[] toBytes() {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutput out = new DataOutputStream(byteArrayOutputStream);
try {
Varint.writeUnsignedVarLong(this.instance, out);
} catch (IOException e) {
e.printStackTrace();
}
return byteArrayOutputStream.toByteArray();
}
@Override
public String toJsonString() {
return this.getObjectId();
}
@Override
public JsonElement toJsonObject() {
return null;
}
}

View file

@ -0,0 +1,26 @@
package cy.agorise.graphenej;
import org.junit.Assert;
import org.junit.Test;
public class HtlcTest {
private final Htlc htlc = new Htlc("1.16.124");
@Test
public void testByteSerialization(){
Htlc htlc1 = new Htlc("1.16.1");
Htlc htlc2 = new Htlc("1.16.100");
Htlc htlc3 = new Htlc("1.16.500");
Htlc htlc4 = new Htlc("1.16.1000");
byte[] expected_1 = Util.hexToBytes("01");
byte[] expected_2 = Util.hexToBytes("64");
byte[] expected_3 = Util.hexToBytes("f403");
byte[] expected_4 = Util.hexToBytes("e807");
Assert.assertArrayEquals(expected_1, htlc1.toBytes());
Assert.assertArrayEquals(expected_2, htlc2.toBytes());
Assert.assertArrayEquals(expected_3, htlc3.toBytes());
Assert.assertArrayEquals(expected_4, htlc4.toBytes());
}
}