Skip to content

Commit 0582048

Browse files
author
Alvin Reyes
authored
Merge pull request #45 from securitiesindustrydataexchange/StatusCode-Issue-35
Status code is now an enum
2 parents f8902e3 + 2b2953b commit 0582048

File tree

10 files changed

+141
-42
lines changed

10 files changed

+141
-42
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,13 @@
7676
<dependency>
7777
<groupId>com.google.code.gson</groupId>
7878
<artifactId>gson</artifactId>
79-
<version>2.7</version>
79+
<version>2.8.2</version>
8080
</dependency>
81+
<!--
82+
You must install java-crypto-conditions-2.0.0-SNAPSHOT.jar in your local Maven repository with the following
83+
84+
mvn install:install-file -Dfile=<absolute_path_to_repo>\libs\java-crypto-conditions-2.0.0-SNAPSHOT.jar -DgroupId=org.interledger -DartifactId=java-crypto-conditions -Dversion=2.0.0-SNAPSHOT -Dpackaging=jar
85+
-->
8186
<dependency>
8287
<groupId>org.interledger</groupId>
8388
<artifactId>java-crypto-conditions</artifactId>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.authenteq.api;
2+
3+
/**
4+
* Any http errors when checking the status of Transactions or Blocks
5+
*/
6+
public class StatusException extends Exception
7+
{
8+
private final int httpResponseCode;
9+
10+
/**
11+
* Constructor
12+
*
13+
* @param httpResponseCode the http error
14+
* @param msg the error message
15+
*/
16+
public StatusException( int httpResponseCode, String msg )
17+
{
18+
super( msg );
19+
this.httpResponseCode = httpResponseCode;
20+
}
21+
22+
/**
23+
* Get the augmented error message with the HTTP Response code
24+
*
25+
* @return the message
26+
*/
27+
@Override
28+
public String getMessage()
29+
{
30+
return "HTTP Response code: " + httpResponseCode + " Message: "+ super.getMessage();
31+
}
32+
}

src/main/java/com/authenteq/api/StatusesApi.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import java.io.IOException;
1111
import java.util.logging.Logger;
1212

13-
14-
1513
/**
1614
* The Class StatusesApi.
1715
*/
@@ -26,12 +24,16 @@ public class StatusesApi {
2624
* @return the transaction status
2725
* @throws IOException Signals that an I/O exception has occurred.
2826
*/
29-
public static Status getTransactionStatus(String transactionId) throws IOException {
27+
public static Status getTransactionStatus(String transactionId) throws StatusException, IOException
28+
{
3029
LOGGER.info("getTransactionStatus Call :" + transactionId);
31-
Response response = NetworkUtils.sendGetRequest(BigChainDBGlobals.getBaseUrl() + BigchainDbApi.STATUSES + "?transaction_id="+ transactionId);
30+
try( Response response = NetworkUtils.sendGetRequest(BigChainDBGlobals.getBaseUrl() + BigchainDbApi.STATUSES + "?transaction_id="+ transactionId) ) {
31+
if( response.code() == 200 ) {
3232
String body = response.body().string();
33-
response.close();
3433
return JsonUtils.fromJson(body, Status.class);
34+
}
35+
throw new StatusException( response.code(), response.body() != null ? response.body().toString() : "Error in response, body is empty" );
36+
}
3537
}
3638

3739
/**
@@ -41,11 +43,15 @@ public static Status getTransactionStatus(String transactionId) throws IOExcepti
4143
* @return the block status
4244
* @throws IOException Signals that an I/O exception has occurred.
4345
*/
44-
public static Status getBlockStatus(String blockId) throws IOException {
46+
public static Status getBlockStatus(String blockId) throws StatusException, IOException {
4547
LOGGER.info("getBlockStatus Call :" + blockId);
46-
Response response = NetworkUtils.sendGetRequest(BigChainDBGlobals.getBaseUrl() + BigchainDbApi.STATUSES + "?block_id="+ blockId);
48+
try( Response response = NetworkUtils.sendGetRequest(BigChainDBGlobals.getBaseUrl() + BigchainDbApi.STATUSES + "?block_id="+ blockId) ) {
49+
if( response.code() == 200 ) {
4750
String body = response.body().string();
48-
response.close();
4951
return JsonUtils.fromJson(body, Status.class);
52+
}
53+
throw new StatusException( response.code(), response.body() != null ? response.body().toString() : "Error in response, body is empty" );
54+
}
5055
}
5156
}
57+

src/main/java/com/authenteq/model/Status.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
/**
88
* The Class Status.
99
*/
10-
public class Status {
11-
10+
public class Status
11+
{
1212
/** The status. */
13-
@SerializedName("status")
14-
private String status;
13+
private StatusCode status;
1514

1615
/**
1716
* Gets the status.
1817
*
1918
* @return the status
2019
*/
21-
public String getStatus() {
20+
public StatusCode getStatus()
21+
{
2222
return status;
2323
}
2424

@@ -27,18 +27,8 @@ public String getStatus() {
2727
*
2828
* @param status the new status
2929
*/
30-
public void setStatus(String status) {
31-
this.status = status;
32-
}
33-
34-
/**
35-
* toString
36-
*
37-
* @return status
38-
*/
39-
@Override
40-
public String toString()
30+
public void setStatus( StatusCode statusCode )
4131
{
42-
return status;
32+
this.status = statusCode;
4333
}
4434
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.authenteq.model;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public enum StatusCode
6+
{
7+
@SerializedName( "valid" )
8+
VALID( "valid" ),
9+
@SerializedName( "backlog" )
10+
BACKLOG( "backlog" ),
11+
@SerializedName( "undecided" )
12+
UNDECIDED( "undecided" );
13+
14+
private final String statusCode;
15+
16+
/**
17+
* Status code initializer
18+
*
19+
* @param status the status code
20+
*/
21+
StatusCode( final String status )
22+
{
23+
this.statusCode = status;
24+
}
25+
26+
/**
27+
* get the status
28+
*
29+
* @return the status
30+
*/
31+
public String statusCode()
32+
{
33+
return this.statusCode;
34+
}
35+
36+
/**
37+
* toString
38+
*
39+
* @return status
40+
*/
41+
@Override
42+
public String toString()
43+
{
44+
return this.statusCode;
45+
}
46+
}

src/main/java/com/authenteq/util/JsonUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.authenteq.model.Outputs;
77
import com.authenteq.model.Transactions;
88
import com.authenteq.model.Votes;
9+
import com.google.gson.FieldNamingPolicy;
910
import com.google.gson.Gson;
1011
import com.google.gson.GsonBuilder;
1112

@@ -33,7 +34,10 @@ public static Gson getGson() {
3334
if (gson == null) {
3435
GsonBuilder builder = new GsonBuilder();
3536

36-
gson = builder.setPrettyPrinting().serializeNulls().disableHtmlEscaping().setPrettyPrinting()
37+
gson = builder.setPrettyPrinting()
38+
.serializeNulls()
39+
.disableHtmlEscaping()
40+
.setPrettyPrinting()
3741
.registerTypeAdapter(Transactions.class, new TransactionsDeserializer())
3842
.registerTypeAdapter(Assets.class, new AssetsDeserializer())
3943
.registerTypeAdapter(Outputs.class, new OutputsDeserializer())

src/test/java/com/authenteq/AbstractTest.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.authenteq;
22

3+
import com.authenteq.api.StatusException;
34
import com.authenteq.api.StatusesApi;
45
import com.authenteq.model.Status;
6+
import com.authenteq.model.StatusCode;
57
import com.authenteq.model.Transaction;
68

79
import java.io.FileInputStream;
@@ -131,7 +133,7 @@ protected static String getEnv( final String envKey, final String otherwise )
131133
* @return the status
132134
* @throws IOException network error
133135
*/
134-
protected Status getStatus( final Transaction transaction ) throws IOException
136+
protected Status getStatus( final Transaction transaction ) throws IOException, StatusException
135137
{
136138
return getStatus( transaction, getInt( "test.status.retries", 60 ) );
137139
}
@@ -144,17 +146,30 @@ protected Status getStatus( final Transaction transaction ) throws IOException
144146
* @return the status
145147
* @throws IOException network error
146148
*/
147-
protected Status getStatus( final Transaction transaction, final int attempts ) throws IOException
149+
protected Status getStatus( final Transaction transaction, final int attempts ) throws IOException, StatusException
148150
{
151+
return getStatus( transaction, attempts, StatusCode.VALID );
152+
}
153+
154+
/**
155+
* Poll for transaction status
156+
*
157+
* @param transaction transaction to check the status of
158+
* @param attempts how many tries while waiting for validation
159+
* @return the status
160+
* @throws IOException network error
161+
*/
162+
protected Status getStatus( final Transaction transaction, final int attempts, final StatusCode expectedStatus ) throws IOException, StatusException
163+
{
164+
Status status = null;
149165
for( int idx = 0; idx < attempts; idx++ ) {
150-
Status status = StatusesApi.getTransactionStatus( transaction.getId() );
151-
if( status.getStatus().equalsIgnoreCase( "valid" ) )
166+
status = StatusesApi.getTransactionStatus( transaction.getId() );
167+
System.err.println( " status " + status );
168+
if( status.getStatus().equals( expectedStatus ) )
152169
return status;
153170
}
154171

155-
Status invalid = new Status();
156-
invalid.setStatus( "timeout" );
157-
return invalid;
172+
throw new RuntimeException( "Could not get valid status, last status " + status + " after " + attempts + " attempts ");
158173
}
159174

160175
/**

src/test/java/com/authenteq/api/AssetsApiTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88
import com.authenteq.AbstractTest;
99
import com.authenteq.builders.BigchainDbTransactionBuilder;
1010
import com.authenteq.model.Assets;
11+
import com.authenteq.model.StatusCode;
1112
import com.authenteq.model.Transaction;
1213
import net.i2p.crypto.eddsa.EdDSAPrivateKey;
1314
import net.i2p.crypto.eddsa.EdDSAPublicKey;
1415
import org.junit.BeforeClass;
1516
import org.junit.Test;
1617

17-
import com.authenteq.api.AssetsApi;
1818
import com.authenteq.builders.BigchainDbConfigBuilder;
1919

2020
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertTrue;
2221

2322
/**
2423
* The Class AssetsApiTest.
@@ -42,7 +41,8 @@ public static void init() {
4241
* Test asset search.
4342
*/
4443
@Test
45-
public void testAssetSearch() {
44+
public void testAssetSearch()
45+
{
4646
String uuid = getUUID();
4747
System.err.println( "AssetApiTest.testAssetSearch.uuid " + uuid );
4848
try {
@@ -55,11 +55,11 @@ public void testAssetSearch() {
5555
.addAsset( "uuid", uuid )
5656
.buildAndSign( (EdDSAPublicKey) alice.getPublic(), (EdDSAPrivateKey) alice.getPrivate() )
5757
.sendTransaction();
58-
assertEquals( "valid", getStatus( transaction ).toString()); // wait for the transaction to be marked valid
58+
assertEquals( StatusCode.VALID, getStatus( transaction ).getStatus() ); // wait for the transaction to be marked VALID
5959

6060
Assets assets = AssetsApi.getAssets( asQuoted( uuid ) );
6161
assertTrue( assets.size() == 1 ); // there should be one and only one
62-
} catch (IOException e) {
62+
} catch (IOException | StatusException e) {
6363
e.printStackTrace();
6464
}
6565
}

src/test/java/com/authenteq/api/BlocksApiTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
*/
2929
public class BlocksApiTest extends AbstractTest
3030
{
31+
private String publicKey = "302a300506032b657003210033c43dc2180936a2a9138a05f06c892d2fb1cfda4562cbc35373bf13cd8ed373";
32+
private String privateKey = "302e020100300506032b6570042204206f6b0cd095f1e83fc5f08bffb79c7c8a30e77a3ab65f4bc659026b76394fcea8";
33+
3134
/**
3235
* Inits the.
3336
*/

src/test/java/com/authenteq/api/StatusesApiTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
import org.junit.BeforeClass;
77
import org.junit.Test;
88

9-
import com.authenteq.api.AssetsApi;
109
import com.authenteq.builders.BigchainDbConfigBuilder;
1110

12-
1311
/**
1412
* The Class StatusesApiTest.
1513
*/
@@ -35,7 +33,7 @@ public static void init() {
3533
public void testStatusTransaction() {
3634
try {
3735
System.out.println(StatusesApi.getTransactionStatus("437ce30de5cf1c3ad199fa983aded47d0db43567befa92e3a36b38a5784e4d3a").getStatus());
38-
} catch (IOException e) {
36+
} catch (IOException | StatusException e) {
3937
// TODO Auto-generated catch block
4038
e.printStackTrace();
4139
}

0 commit comments

Comments
 (0)