Skip to content

Commit

Permalink
ManageData operation
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekn committed Sep 9, 2016
1 parent a75e5cc commit c5faa15
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/main/java/org/stellar/sdk/ManageDataOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.stellar.sdk;

import org.stellar.sdk.xdr.DataValue;
import org.stellar.sdk.xdr.ManageDataOp;
import org.stellar.sdk.xdr.OperationType;
import org.stellar.sdk.xdr.String64;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Represents <a href="https://www.stellar.org/developers/learn/concepts/list-of-operations.html#manage-data" target="_blank">ManageData</a> operation.
* @see <a href="https://www.stellar.org/developers/learn/concepts/list-of-operations.html" target="_blank">List of Operations</a>
*/
public class ManageDataOperation extends Operation {
private final String name;
private final byte[] value;

private ManageDataOperation(String name, byte[] value) {
this.name = checkNotNull(name, "name cannot be null");
this.value = value;
}

/**
* The name of the data value
*/
public String getName() {
return name;
}

/**
* Data value
*/
public byte[] getValue() {
return value;
}

@Override
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
ManageDataOp op = new ManageDataOp();
String64 name = new String64();
name.setString64(this.name);
op.setDataName(name);

if (value != null) {
DataValue dataValue = new DataValue();
dataValue.setDataValue(this.value);
op.setDataValue(dataValue);
}

org.stellar.sdk.xdr.Operation.OperationBody body = new org.stellar.sdk.xdr.Operation.OperationBody();
body.setDiscriminant(OperationType.MANAGE_DATA);
body.setManageDataOp(op);

return body;
}

public static class Builder {
private final String name;
private final byte[] value;

private KeyPair mSourceAccount;

/**
* Construct a new ManageOffer builder from a ManageDataOp XDR.
* @param op {@link ManageDataOp}
*/
Builder(ManageDataOp op) {
name = op.getDataName().getString64();
if (op.getDataValue() != null) {
value = op.getDataValue().getDataValue();
} else {
value = null;
}
}

/**
* Creates a new ManageData builder. If you want to delete data entry pass null as a <code>value</code> param.
* @param name The name of data entry
* @param value The value of data entry. <code>null</code>null will delete data entry.
*/
public Builder(String name, byte[] value) {
this.name = checkNotNull(name, "name cannot be null");
this.value = value;
}

/**
* Sets the source account for this operation.
* @param sourceAccount The operation's source account.
* @return Builder object so you can chain methods.
*/
public Builder setSourceAccount(KeyPair sourceAccount) {
mSourceAccount = checkNotNull(sourceAccount, "sourceAccount cannot be null");
return this;
}

/**
* Builds an operation
*/
public ManageDataOperation build() {
ManageDataOperation operation = new ManageDataOperation(name, value);
if (mSourceAccount != null) {
operation.setSourceAccount(mSourceAccount);
}
return operation;
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/org/stellar/sdk/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public static Operation fromXdr(org.stellar.sdk.xdr.Operation xdr) {
case ACCOUNT_MERGE:
operation = new AccountMergeOperation.Builder(body).build();
break;
case MANAGE_DATA:
operation = new ManageDataOperation.Builder(body.getManageDataOp()).build();
break;
default:
throw new RuntimeException("Unknown operation body " + body.getDiscriminant());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.gson.JsonParseException;

import org.stellar.sdk.KeyPair;
import org.stellar.sdk.responses.operations.ManageDataOperationResponse;
import org.stellar.sdk.responses.operations.OperationResponse;
import org.stellar.sdk.responses.operations.CreateAccountOperationResponse;
import org.stellar.sdk.responses.operations.PaymentOperationResponse;
Expand Down Expand Up @@ -52,6 +53,8 @@ public OperationResponse deserialize(JsonElement json, Type typeOfT, JsonDeseria
return gson.fromJson(json, AccountMergeOperationResponse.class);
case 9:
return gson.fromJson(json, InflationOperationResponse.class);
case 10:
return gson.fromJson(json, ManageDataOperationResponse.class);
default:
throw new RuntimeException("Invalid operation type");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.stellar.sdk.responses.operations;

import com.google.gson.annotations.SerializedName;

/**
* Represents ManageDataoperation response.
* @see <a href="https://www.stellar.org/developers/horizon/reference/resources/operation.html" target="_blank">Operation documentation</a>
* @see org.stellar.sdk.requests.OperationsRequestBuilder
* @see org.stellar.sdk.Server#operations()
*/
public class ManageDataOperationResponse extends OperationResponse {
@SerializedName("name")
protected final String name;
@SerializedName("value")
protected final String value;

ManageDataOperationResponse(String name, String value) {
this.name = name;
this.value = value;
}

public String getName() {
return name;
}

public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public String getPagingToken() {
* <li>path_payment</li>
* <li>create_passive_offer</li>
* <li>inflation</li>
* <li>manage_data</li>
* </ul>
*/
public String getType() {
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/org/stellar/sdk/OperationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -377,6 +378,48 @@ public void testAccountMergeOperation() throws IOException, FormatException {
operation.toXdrBase64());
}

@Test
public void testManageDataOperation() throws IOException, FormatException {
// GC5SIC4E3V56VOHJ3OZAX5SJDTWY52JYI2AFK6PUGSXFVRJQYQXXZBZF
KeyPair source = KeyPair.fromSecretSeed("SC4CGETADVYTCR5HEAVZRB3DZQY5Y4J7RFNJTRA6ESMHIPEZUSTE2QDK");

ManageDataOperation operation = new ManageDataOperation.Builder("test", new byte[]{0, 1, 2, 3, 4})
.setSourceAccount(source)
.build();

org.stellar.sdk.xdr.Operation xdr = operation.toXdr();

ManageDataOperation parsedOperation = (ManageDataOperation) Operation.fromXdr(xdr);

assertEquals("test", parsedOperation.getName());
assertTrue(Arrays.equals(new byte[]{0, 1, 2, 3, 4}, parsedOperation.getValue()));

assertEquals(
"AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAoAAAAEdGVzdAAAAAEAAAAFAAECAwQ=",
operation.toXdrBase64());
}

@Test
public void testManageDataOperationEmptyValue() throws IOException, FormatException {
// GC5SIC4E3V56VOHJ3OZAX5SJDTWY52JYI2AFK6PUGSXFVRJQYQXXZBZF
KeyPair source = KeyPair.fromSecretSeed("SC4CGETADVYTCR5HEAVZRB3DZQY5Y4J7RFNJTRA6ESMHIPEZUSTE2QDK");

ManageDataOperation operation = new ManageDataOperation.Builder("test", null)
.setSourceAccount(source)
.build();

org.stellar.sdk.xdr.Operation xdr = operation.toXdr();

ManageDataOperation parsedOperation = (ManageDataOperation) Operation.fromXdr(xdr);

assertEquals("test", parsedOperation.getName());
assertEquals(null, parsedOperation.getValue());

assertEquals(
"AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAoAAAAEdGVzdAAAAAA=",
operation.toXdrBase64());
}

@Test
public void testToXdrAmount() {
assertEquals(0L, Operation.toXdrAmount("0"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.stellar.sdk.responses.operations.CreateAccountOperationResponse;
import org.stellar.sdk.responses.operations.CreatePassiveOfferOperationResponse;
import org.stellar.sdk.responses.operations.InflationOperationResponse;
import org.stellar.sdk.responses.operations.ManageDataOperationResponse;
import org.stellar.sdk.responses.operations.ManageOfferOperationResponse;
import org.stellar.sdk.responses.operations.OperationResponse;
import org.stellar.sdk.responses.operations.PathPaymentOperationResponse;
Expand Down Expand Up @@ -486,4 +487,74 @@ public void testDeserializeInflationOperation() {

assertEquals(operation.getId(), new Long(12884914177L));
}

@Test
public void testDeserializeManageDataOperation() {
String json = "{\n" +
" \"_links\": {\n" +
" \"self\": {\n" +
" \"href\": \"https://horizon-testnet.stellar.org/operations/14336188517191688\"\n" +
" },\n" +
" \"transaction\": {\n" +
" \"href\": \"https://horizon-testnet.stellar.org/transactions/ad99999615f653528e67d8c8783e14044519c3e5233b73633aa88050c5856103\"\n" +
" },\n" +
" \"effects\": {\n" +
" \"href\": \"https://horizon-testnet.stellar.org/operations/14336188517191688/effects\"\n" +
" },\n" +
" \"succeeds\": {\n" +
" \"href\": \"https://horizon-testnet.stellar.org/effects?order=desc\\u0026cursor=14336188517191688\"\n" +
" },\n" +
" \"precedes\": {\n" +
" \"href\": \"https://horizon-testnet.stellar.org/effects?order=asc\\u0026cursor=14336188517191688\"\n" +
" }\n" +
" },\n" +
" \"id\": \"14336188517191688\",\n" +
" \"paging_token\": \"14336188517191688\",\n" +
" \"source_account\": \"GAC3U2W4KPZJX7GX5AZPOEWBJVUTJQ4TZNO74YN24ETTFIJVY7EMMANP\",\n" +
" \"type\": \"manage_data\",\n" +
" \"type_i\": 10,\n" +
" \"name\": \"CollateralValue\",\n" +
" \"value\": \"MjAwMA==\"\n" +
"}";

ManageDataOperationResponse operation = (ManageDataOperationResponse) GsonSingleton.getInstance().fromJson(json, OperationResponse.class);

assertEquals(operation.getId(), new Long(14336188517191688L));
assertEquals(operation.getName(), "CollateralValue");
assertEquals(operation.getValue(), "MjAwMA==");
}

@Test
public void testDeserializeManageDataOperationValueEmpty() {
String json = "{\n" +
" \"_links\": {\n" +
" \"self\": {\n" +
" \"href\": \"https://horizon-testnet.stellar.org/operations/14336188517191688\"\n" +
" },\n" +
" \"transaction\": {\n" +
" \"href\": \"https://horizon-testnet.stellar.org/transactions/ad99999615f653528e67d8c8783e14044519c3e5233b73633aa88050c5856103\"\n" +
" },\n" +
" \"effects\": {\n" +
" \"href\": \"https://horizon-testnet.stellar.org/operations/14336188517191688/effects\"\n" +
" },\n" +
" \"succeeds\": {\n" +
" \"href\": \"https://horizon-testnet.stellar.org/effects?order=desc\\u0026cursor=14336188517191688\"\n" +
" },\n" +
" \"precedes\": {\n" +
" \"href\": \"https://horizon-testnet.stellar.org/effects?order=asc\\u0026cursor=14336188517191688\"\n" +
" }\n" +
" },\n" +
" \"id\": \"14336188517191688\",\n" +
" \"paging_token\": \"14336188517191688\",\n" +
" \"source_account\": \"GAC3U2W4KPZJX7GX5AZPOEWBJVUTJQ4TZNO74YN24ETTFIJVY7EMMANP\",\n" +
" \"type\": \"manage_data\",\n" +
" \"type_i\": 10,\n" +
" \"name\": \"CollateralValue\",\n" +
" \"value\": null\n" +
"}";

ManageDataOperationResponse operation = (ManageDataOperationResponse) GsonSingleton.getInstance().fromJson(json, OperationResponse.class);

assertEquals(operation.getValue(), null);
}
}

0 comments on commit c5faa15

Please sign in to comment.