Skip to content

Commit 46577d5

Browse files
authored
Enhancement: Add UNKNOWN enum type to HTTP client enums. (#351)
1 parent c792c63 commit 46577d5

File tree

2 files changed

+123
-6
lines changed

2 files changed

+123
-6
lines changed

src/main/java/com/algorand/algosdk/v2/client/model/Enums.java

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.algorand.algosdk.v2.client.model;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
34
import com.fasterxml.jackson.annotation.JsonProperty;
45

56
public class Enums {
@@ -10,7 +11,8 @@ public class Enums {
1011
public enum AddressRole {
1112
@JsonProperty("sender") SENDER("sender"),
1213
@JsonProperty("receiver") RECEIVER("receiver"),
13-
@JsonProperty("freeze-target") FREEZETARGET("freeze-target");
14+
@JsonProperty("freeze-target") FREEZETARGET("freeze-target"),
15+
@JsonProperty("") UNKNOWN("");
1416

1517
final String serializedName;
1618
AddressRole(String name) {
@@ -21,6 +23,17 @@ public enum AddressRole {
2123
public String toString() {
2224
return this.serializedName;
2325
}
26+
27+
@JsonCreator
28+
public static AddressRole forValue(String value) {
29+
for (AddressRole t : values()) {
30+
if (t.serializedName.equalsIgnoreCase(value)) {
31+
return t;
32+
}
33+
}
34+
return UNKNOWN;
35+
}
36+
2437
}
2538

2639
/**
@@ -34,7 +47,8 @@ public enum Exclude {
3447
@JsonProperty("created-assets") CREATEDASSETS("created-assets"),
3548
@JsonProperty("apps-local-state") APPSLOCALSTATE("apps-local-state"),
3649
@JsonProperty("created-apps") CREATEDAPPS("created-apps"),
37-
@JsonProperty("none") NONE("none");
50+
@JsonProperty("none") NONE("none"),
51+
@JsonProperty("") UNKNOWN("");
3852

3953
final String serializedName;
4054
Exclude(String name) {
@@ -45,6 +59,17 @@ public enum Exclude {
4559
public String toString() {
4660
return this.serializedName;
4761
}
62+
63+
@JsonCreator
64+
public static Exclude forValue(String value) {
65+
for (Exclude t : values()) {
66+
if (t.serializedName.equalsIgnoreCase(value)) {
67+
return t;
68+
}
69+
}
70+
return UNKNOWN;
71+
}
72+
4873
}
4974

5075
/**
@@ -54,7 +79,8 @@ public String toString() {
5479
*/
5580
public enum Hashtype {
5681
@JsonProperty("sha512_256") SHA512_256("sha512_256"),
57-
@JsonProperty("sha256") SHA256("sha256");
82+
@JsonProperty("sha256") SHA256("sha256"),
83+
@JsonProperty("") UNKNOWN("");
5884

5985
final String serializedName;
6086
Hashtype(String name) {
@@ -65,6 +91,17 @@ public enum Hashtype {
6591
public String toString() {
6692
return this.serializedName;
6793
}
94+
95+
@JsonCreator
96+
public static Hashtype forValue(String value) {
97+
for (Hashtype t : values()) {
98+
if (t.serializedName.equalsIgnoreCase(value)) {
99+
return t;
100+
}
101+
}
102+
return UNKNOWN;
103+
}
104+
68105
}
69106

70107
/**
@@ -84,7 +121,8 @@ public enum OnCompletion {
84121
@JsonProperty("closeout") CLOSEOUT("closeout"),
85122
@JsonProperty("clear") CLEAR("clear"),
86123
@JsonProperty("update") UPDATE("update"),
87-
@JsonProperty("delete") DELETE("delete");
124+
@JsonProperty("delete") DELETE("delete"),
125+
@JsonProperty("") UNKNOWN("");
88126

89127
final String serializedName;
90128
OnCompletion(String name) {
@@ -95,6 +133,17 @@ public enum OnCompletion {
95133
public String toString() {
96134
return this.serializedName;
97135
}
136+
137+
@JsonCreator
138+
public static OnCompletion forValue(String value) {
139+
for (OnCompletion t : values()) {
140+
if (t.serializedName.equalsIgnoreCase(value)) {
141+
return t;
142+
}
143+
}
144+
return UNKNOWN;
145+
}
146+
98147
}
99148

100149
/**
@@ -107,7 +156,8 @@ public String toString() {
107156
public enum SigType {
108157
@JsonProperty("sig") SIG("sig"),
109158
@JsonProperty("msig") MSIG("msig"),
110-
@JsonProperty("lsig") LSIG("lsig");
159+
@JsonProperty("lsig") LSIG("lsig"),
160+
@JsonProperty("") UNKNOWN("");
111161

112162
final String serializedName;
113163
SigType(String name) {
@@ -118,6 +168,17 @@ public enum SigType {
118168
public String toString() {
119169
return this.serializedName;
120170
}
171+
172+
@JsonCreator
173+
public static SigType forValue(String value) {
174+
for (SigType t : values()) {
175+
if (t.serializedName.equalsIgnoreCase(value)) {
176+
return t;
177+
}
178+
}
179+
return UNKNOWN;
180+
}
181+
121182
}
122183

123184
/**
@@ -137,7 +198,8 @@ public enum TxType {
137198
@JsonProperty("acfg") ACFG("acfg"),
138199
@JsonProperty("axfer") AXFER("axfer"),
139200
@JsonProperty("afrz") AFRZ("afrz"),
140-
@JsonProperty("appl") APPL("appl");
201+
@JsonProperty("appl") APPL("appl"),
202+
@JsonProperty("") UNKNOWN("");
141203

142204
final String serializedName;
143205
TxType(String name) {
@@ -148,6 +210,17 @@ public enum TxType {
148210
public String toString() {
149211
return this.serializedName;
150212
}
213+
214+
@JsonCreator
215+
public static TxType forValue(String value) {
216+
for (TxType t : values()) {
217+
if (t.serializedName.equalsIgnoreCase(value)) {
218+
return t;
219+
}
220+
}
221+
return UNKNOWN;
222+
}
223+
151224
}
152225

153226
}

src/test/java/com/algorand/algosdk/util/TestEncoder.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package com.algorand.algosdk.util;
22

3+
import com.algorand.algosdk.algod.client.model.PaymentTransactionType;
4+
import com.algorand.algosdk.transaction.Transaction;
5+
import com.algorand.algosdk.v2.client.model.Account;
6+
import com.algorand.algosdk.v2.client.model.Enums;
37
import org.apache.commons.lang3.StringUtils;
48
import org.junit.jupiter.api.Assertions;
59
import org.junit.jupiter.api.Test;
610
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.CsvSource;
712
import org.junit.jupiter.params.provider.ValueSource;
813

14+
import java.io.IOException;
915
import java.math.BigInteger;
1016

1117
import static org.assertj.core.api.Assertions.*;
@@ -131,4 +137,42 @@ public void testDecodeUint64() {
131137
.isInstanceOf(IllegalArgumentException.class)
132138
.hasMessage("Length of byte array is invalid");
133139
}
140+
141+
@ParameterizedTest
142+
@CsvSource({
143+
"pay, pay",
144+
"blah, ",
145+
"blah, ''"
146+
})
147+
public void testTransactionEnum(String type, String expected) throws IOException {
148+
String txnString = "{\"type\": \"" + type + "\"}";
149+
Transaction txn = Encoder.decodeFromJson(txnString, Transaction.class);
150+
if (expected != null) {
151+
Transaction.Type enumValue = Transaction.Type.forValue(expected);
152+
assertThat(enumValue).isEqualTo(txn.type);
153+
} else {
154+
assertThat(Transaction.Type.Default).isEqualTo(txn.type);
155+
}
156+
157+
TestUtil.serializeDeserializeCheck(txn);
158+
}
159+
160+
@ParameterizedTest
161+
@CsvSource({
162+
"sig, sig",
163+
"blah, ",
164+
"blah, ''"
165+
})
166+
public void testHttpModelEnum(String type, String expected) throws IOException {
167+
String acctString = "{\"sig-type\": \"" + type + "\"}";
168+
Account acct = Encoder.decodeFromJson(acctString, Account.class);
169+
if (expected != null) {
170+
Enums.SigType enumValue = Enums.SigType.forValue(expected);
171+
assertThat(enumValue).isEqualTo(acct.sigType);
172+
} else {
173+
assertThat(Enums.SigType.UNKNOWN).isEqualTo(acct.sigType);
174+
}
175+
176+
TestUtil.serializeDeserializeCheck(acct);
177+
}
134178
}

0 commit comments

Comments
 (0)