From a848c66fb982540796980e2c22d239d782ec08a0 Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Mon, 17 Apr 2017 16:07:05 -0500 Subject: [PATCH] Introducing the BaseApiTest class and making some test classes extend it --- .../graphenej/api/BaseApiTest.java | 33 ++++++++ .../graphenej/api/GetKeyReferencesTest.java | 81 +++++++++++++++++++ .../graphenej/api/GetLimitOrdersTest.java | 23 +----- 3 files changed, 115 insertions(+), 22 deletions(-) create mode 100644 graphenej/src/test/java/de/bitsharesmunich/graphenej/api/BaseApiTest.java create mode 100644 graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetKeyReferencesTest.java diff --git a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/BaseApiTest.java b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/BaseApiTest.java new file mode 100644 index 0000000..c6d02b5 --- /dev/null +++ b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/BaseApiTest.java @@ -0,0 +1,33 @@ +package de.bitsharesmunich.graphenej.api; + +import com.neovisionaries.ws.client.WebSocket; +import com.neovisionaries.ws.client.WebSocketFactory; + +import org.junit.Before; + +import javax.net.ssl.SSLContext; + +import de.bitsharesmunich.graphenej.test.NaiveSSLContext; + +/** + * Created by nelson on 4/14/17. + */ + +public class BaseApiTest { + protected String BLOCK_PAY_DE = System.getenv("BLOCKPAY_DE"); + + protected SSLContext context; + protected WebSocket mWebSocket; + + @Before + public void setUp() throws Exception { + context = NaiveSSLContext.getInstance("TLS"); + WebSocketFactory factory = new WebSocketFactory(); + + // Set the custom SSL context. + factory.setSSLContext(context); + + mWebSocket = factory.createSocket(BLOCK_PAY_DE); + } + +} diff --git a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetKeyReferencesTest.java b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetKeyReferencesTest.java new file mode 100644 index 0000000..c774973 --- /dev/null +++ b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetKeyReferencesTest.java @@ -0,0 +1,81 @@ +package de.bitsharesmunich.graphenej.api; + +import com.neovisionaries.ws.client.WebSocketException; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import de.bitsharesmunich.graphenej.Address; +import de.bitsharesmunich.graphenej.UserAccount; +import de.bitsharesmunich.graphenej.errors.MalformedAddressException; +import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; +import de.bitsharesmunich.graphenej.models.BaseResponse; +import de.bitsharesmunich.graphenej.models.WitnessResponse; + +/** + * Created by nelson on 4/14/17. + */ +public class GetKeyReferencesTest extends BaseApiTest { + + private String[] publicKeys = new String[] { + "BTS8DuGHXpHYedq7qhT65BEEdQPvLT8nxZ862Hf8NgvSZUMuwUFkn", + "BTS53ehf9Qoeg9o4E1KuxdZRXCVg3Z9ApbEDHVdQhERDJDEFkPkGs" + }; + + @Test + public void testGetKeyReferences(){ + ArrayList
addresses = new ArrayList<>(); + for(String addr : publicKeys){ + try { + Address address = new Address(addr); + addresses.add(address); + } catch (MalformedAddressException e) { + System.out.println("MalformedAddressException. Msg: "+e.getMessage()); + } + } + mWebSocket.addListener(new GetKeyReferences(addresses, new WitnessResponseListener() { + @Override + public void onSuccess(WitnessResponse response) { + System.out.println("onSuccess"); + int counter = 0; + List> accountListList = (List>) response.result; + for(List accountList : accountListList){ + for(UserAccount userAccount : accountList){ + System.out.println("User account: "+userAccount.getObjectId()); + } + if(accountList.size() > 1){ + System.out.println("Key with address: "+publicKeys[counter]+" controls more than one role in account: "+accountList.get(0).getObjectId()); + }else if(accountList.size() == 1){ + System.out.println("Key with address: "+publicKeys[counter]+" controls just one role in account: "+accountList.get(0).getObjectId()); + } + counter++; + } + synchronized (GetKeyReferencesTest.this){ + GetKeyReferencesTest.this.notifyAll(); + } + } + + @Override + public void onError(BaseResponse.Error error) { + System.out.println("onError. Msg: "+error.message); + + synchronized (GetKeyReferencesTest.this){ + GetKeyReferencesTest.this.notifyAll(); + } + } + })); + + try{ + mWebSocket.connect(); + synchronized (this){ + wait(); + } + } catch (WebSocketException e) { + System.out.println("WebSocketException. Msg: " + e.getMessage()); + } catch (InterruptedException e) { + System.out.println("InterruptedException. Msg: "+e.getMessage()); + } + } +} \ No newline at end of file diff --git a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetLimitOrdersTest.java b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetLimitOrdersTest.java index ffa2665..74b9764 100644 --- a/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetLimitOrdersTest.java +++ b/graphenej/src/test/java/de/bitsharesmunich/graphenej/api/GetLimitOrdersTest.java @@ -1,19 +1,14 @@ package de.bitsharesmunich.graphenej.api; import com.google.common.primitives.UnsignedLong; -import com.neovisionaries.ws.client.WebSocket; import com.neovisionaries.ws.client.WebSocketException; -import com.neovisionaries.ws.client.WebSocketFactory; import org.junit.After; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import java.util.List; -import javax.net.ssl.SSLContext; - import de.bitsharesmunich.graphenej.Asset; import de.bitsharesmunich.graphenej.AssetAmount; import de.bitsharesmunich.graphenej.Converter; @@ -24,33 +19,17 @@ import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener; import de.bitsharesmunich.graphenej.models.BaseResponse; import de.bitsharesmunich.graphenej.models.WitnessResponse; import de.bitsharesmunich.graphenej.operations.LimitOrderCreateOperation; -import de.bitsharesmunich.graphenej.test.NaiveSSLContext; import static org.hamcrest.CoreMatchers.is; /** * Created by nelson on 3/24/17. */ -public class GetLimitOrdersTest { - private String BLOCK_PAY_DE = System.getenv("BLOCKPAY_DE"); +public class GetLimitOrdersTest extends BaseApiTest { private UserAccount seller = new UserAccount("1.2.143563"); private final Asset base = new Asset("1.3.121", "USD", 4); private final Asset quote = new Asset("1.3.0", "BTS", 5); - private SSLContext context; - private WebSocket mWebSocket; - - @Before - public void setUp() throws Exception { - context = NaiveSSLContext.getInstance("TLS"); - WebSocketFactory factory = new WebSocketFactory(); - - // Set the custom SSL context. - factory.setSSLContext(context); - - mWebSocket = factory.createSocket(BLOCK_PAY_DE); - } - @Test public void testGetLimitOrders(){ try {