Browse Source

Code Climate Updates (#3)

master
Taylor Bockman 7 years ago committed by GitHub
parent
commit
92543177dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      .codeclimate.yml
  2. 11
      CONTRIBUTING.md
  3. 1
      README.md
  4. 2
      build.gradle
  5. 24
      src/main/java/com/sigmaflare/binancej/GeneralUtilities.java
  6. 28
      src/main/java/com/sigmaflare/binancej/Helpers.java
  7. 62
      src/main/java/com/sigmaflare/binancej/MarketData.java
  8. 2
      src/main/java/com/sigmaflare/binancej/entities/OrderBookDepth.java
  9. 40
      src/main/java/com/sigmaflare/binancej/entities/transform/OrderBookDepthResponseDeserializer.java
  10. 45
      src/test/java/com/sigmaflare/binancej/CandlestickMethodTests.java
  11. 10
      src/test/java/com/sigmaflare/binancej/ExchangeInfoMethodTests.java
  12. 33
      src/test/java/com/sigmaflare/binancej/OrderBookDepthTests.java
  13. 8
      src/test/java/com/sigmaflare/binancej/PingMethodTests.java
  14. 8
      src/test/java/com/sigmaflare/binancej/TickerPriceTests.java
  15. 10
      src/test/java/com/sigmaflare/binancej/TimeMethodTests.java

5
.codeclimate.yml

@ -0,0 +1,5 @@
version: "2"
checks:
argument-count:
config:
threshold: 5 # Miller's law

11
CONTRIBUTING.md

@ -8,6 +8,7 @@ The goal of this project is to provide the de-facto standard Binance API in Java
#### Sections
> * [Getting Started](#getting-started)
> * [Branch Naming](#branch-naming)
> * [Testing](#testing)
> * [Code Standards](#code-standards)
> * [Versioning](#versioning)
@ -35,6 +36,16 @@ cd binancej
gradle clean && gradle build
```
## Branch Naming
An often overlooked aspect of repository management is naming of branches. In order to make branches easier to prune,
maintain, and track please use the format `<name-id>-<ticket-or-feature-name>`.
* `<name-id>`: I could use either `ag` (for "angrygoats") or `tb` (for "Taylor Bockman"). Both allow someone to quickly know who owns the branch.
* `<ticket-or-feature-name>`: If the ticket/feature has an associated issue/ticket number please include it in the branch name. Otherwise an accurate description of the feature is sufficient.
## Testing
We operate a strict TDD shop here. As a result, any PR submitted without tests for anything but _the most trivial_ of

1
README.md

@ -2,7 +2,6 @@
[![CircleCI](https://circleci.com/gh/angrygoats/binancej/tree/master.svg?style=svg&circle-token=ec4614038357b8ff4bcc4773bedd4a264a1947b7)](https://circleci.com/gh/angrygoats/binancej/tree/master)
[![Maintainability](https://api.codeclimate.com/v1/badges/78e89cca82f04486b11e/maintainability)](https://codeclimate.com/github/angrygoats/binancej/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/78e89cca82f04486b11e/test_coverage)](https://codeclimate.com/github/angrygoats/binancej/test_coverage)
A Java 8 implementation of the [Binance API Specification](https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md).

2
build.gradle

@ -4,7 +4,7 @@ plugins {
id 'com.github.ethankhall.semantic-versioning' version "1.1.0" apply true
}
project.version.with { major = 1; minor= 0; patch = 0 }
project.version.with { major = 1; minor= 0; patch = 1 }
sourceCompatibility = 1.8

24
src/main/java/com/sigmaflare/binancej/GeneralUtilities.java

@ -17,9 +17,9 @@ import org.apache.http.util.EntityUtils;
import java.io.IOException;
import static com.sigmaflare.binancej.Constant.BASE_ENDPOINT;
import static com.sigmaflare.binancej.Constant.NO_RESPONSE_TEXT;
import static com.sigmaflare.binancej.Constant.NO_RESPONSE_TEXT_FORMATTED;
import static com.sigmaflare.binancej.Helpers.buildGetRequestFromEndpoint;
/**
* GeneralUtilities is a container class for methods that interact with the infrastructure
@ -40,14 +40,14 @@ public class GeneralUtilities extends BaseBinanceApi {
super(apiKey, secretKey, closeableHttpClient);
}
/**
* Hits the ping endpoint to check if the service is alive.
*
* @return empty Ping object if it returned 200, otherwise ServiceError
*/
public Either<ServiceError, Ping> ping() throws BinanceServiceUnreachableException {
final String url = String.format("%s%s", BASE_ENDPOINT, PING_URL);
final HttpGet request = Helpers.getBuilder(url, apiKey);
final HttpGet request = buildGetRequestFromEndpoint(PING_URL, apiKey);
try {
try (CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(request)) {
@ -56,9 +56,9 @@ public class GeneralUtilities extends BaseBinanceApi {
HttpEntity httpEntity = closeableHttpResponse.getEntity();
if (httpEntity == null) {
log.error(NO_RESPONSE_TEXT, url);
log.error(NO_RESPONSE_TEXT, request.getURI().toASCIIString());
throw new BinanceServiceUnreachableException(
String.format(NO_RESPONSE_TEXT_FORMATTED, url), null);
String.format(NO_RESPONSE_TEXT_FORMATTED, request.getURI().toASCIIString()), null);
}
String response = EntityUtils.toString(httpEntity);
@ -81,8 +81,7 @@ public class GeneralUtilities extends BaseBinanceApi {
* @throws BinanceServiceUnreachableException Throws when the request fails
*/
public Either<ServiceError, Time> getServerTime() throws BinanceServiceUnreachableException {
final String url = String.format("%s%s", BASE_ENDPOINT, TIME_URL);
final HttpGet request = Helpers.getBuilder(url, apiKey);
final HttpGet request = buildGetRequestFromEndpoint(TIME_URL, apiKey);
try {
try (CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(request)) {
@ -91,9 +90,9 @@ public class GeneralUtilities extends BaseBinanceApi {
HttpEntity httpEntity = closeableHttpResponse.getEntity();
if (httpEntity == null) {
log.error(NO_RESPONSE_TEXT, url);
log.error(NO_RESPONSE_TEXT, request.getURI().toASCIIString());
throw new BinanceServiceUnreachableException(
String.format(NO_RESPONSE_TEXT_FORMATTED, url), null);
String.format(NO_RESPONSE_TEXT_FORMATTED, request.getURI().toASCIIString()), null);
}
String response = EntityUtils.toString(httpEntity);
@ -116,8 +115,7 @@ public class GeneralUtilities extends BaseBinanceApi {
* @throws BinanceServiceUnreachableException If the service cannot be reached
*/
public Either<ServiceError, ExchangeInfo> getExchangeInfo() throws BinanceServiceUnreachableException {
final String url = String.format("%s%s", BASE_ENDPOINT, EXCHANGE_INFO_URL);
final HttpGet request = Helpers.getBuilder(url, apiKey);
final HttpGet request = buildGetRequestFromEndpoint(EXCHANGE_INFO_URL, apiKey);
try {
try (CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(request)) {
@ -126,9 +124,9 @@ public class GeneralUtilities extends BaseBinanceApi {
HttpEntity httpEntity = closeableHttpResponse.getEntity();
if (httpEntity == null) {
log.error(NO_RESPONSE_TEXT, url);
log.error(NO_RESPONSE_TEXT, request.getURI().toASCIIString());
throw new BinanceServiceUnreachableException(
String.format(NO_RESPONSE_TEXT_FORMATTED, url), null);
String.format(NO_RESPONSE_TEXT_FORMATTED, request.getURI().toASCIIString()), null);
}
String response = EntityUtils.toString(httpEntity);

28
src/main/java/com/sigmaflare/binancej/Helpers.java

@ -9,6 +9,8 @@ import org.apache.http.client.methods.HttpPost;
import java.util.function.Function;
import static com.sigmaflare.binancej.Constant.BASE_ENDPOINT;
public final class Helpers {
private Helpers() {
}
@ -25,13 +27,35 @@ public final class Helpers {
/**
* Utilizes getBuilder to build a fully functional HttpGet request
* @param endpoint The endpoint AFTER the base endpoint (e.g. /api/v1/xyz)
* @param apiKey The API key to use
* @return A configured HttpGet object
*/
public static HttpGet buildGetRequestFromEndpoint(String endpoint, String apiKey) {
final String url = String.format("%s%s", BASE_ENDPOINT, endpoint);
return Helpers.getBuilder(url, apiKey);
}
/**
* Utilizes postBuilder to build a fully functional HttpPost request
* @param endpoint The endpoint AFTER the base endpoint (e.g. /api/v1/xyz)
* @param apiKey The API key to use
* @return A configured HttpPost object
*/
public static HttpPost buildPostRequestFromEndpoint(String endpoint, String apiKey) {
final String url = String.format("%s%s", BASE_ENDPOINT, endpoint);
return Helpers.postBuilder(url, apiKey);
}
/**
* Builds a configured HttpGet
*
* @param url The full URL to GET to
* @param apiKey The API key
* @return A configured HttpGet object
*/
public static HttpGet getBuilder(String url, String apiKey) {
private static HttpGet getBuilder(String url, String apiKey) {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("X-MBX-APIKEY", apiKey);
return httpGet;
@ -44,7 +68,7 @@ public final class Helpers {
* @param apiKey The API key
* @return A configured HttpPost object
*/
public static HttpPost postBuilder(String url, String apiKey) {
private static HttpPost postBuilder(String url, String apiKey) {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("X-MBX-APIKEY", apiKey);
return httpPost;

62
src/main/java/com/sigmaflare/binancej/MarketData.java

@ -20,10 +20,10 @@ import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.List;
import static com.sigmaflare.binancej.Constant.BASE_ENDPOINT;
import static com.sigmaflare.binancej.Constant.NO_RESPONSE_TEXT;
import static com.sigmaflare.binancej.Constant.NO_RESPONSE_TEXT_FORMATTED;
import static com.sigmaflare.binancej.Constant.SYMBOL_AND_INTERVAL_MUST_BE_SUPPLIED;
import static com.sigmaflare.binancej.Helpers.buildGetRequestFromEndpoint;
@Slf4j
public class MarketData extends BaseBinanceApi {
@ -62,9 +62,8 @@ public class MarketData extends BaseBinanceApi {
*/
public Either<ServiceError, OrderBookDepth> getOrderBookDepth(String symbol, int limit)
throws BinanceServiceUnreachableException {
String url = String.format("%s%s?symbol=%s&limit=%d", BASE_ENDPOINT, ORDER_BOOK_URL, symbol, limit);
final HttpGet request = Helpers.getBuilder(url, apiKey);
String urlWithParams = String.format("%s?symbol=%s&limit=%d", ORDER_BOOK_URL, symbol, limit);
final HttpGet request = buildGetRequestFromEndpoint(urlWithParams, apiKey);
try {
try (CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(request)) {
@ -73,8 +72,9 @@ public class MarketData extends BaseBinanceApi {
HttpEntity httpEntity = closeableHttpResponse.getEntity();
if (httpEntity == null) {
log.error(NO_RESPONSE_TEXT, url);
throw new BinanceServiceUnreachableException(String.format(NO_RESPONSE_TEXT_FORMATTED, url), null);
log.error(NO_RESPONSE_TEXT, request.getURI().toASCIIString());
throw new BinanceServiceUnreachableException(
String.format(NO_RESPONSE_TEXT_FORMATTED, request.getURI().toASCIIString()), null);
}
String response = EntityUtils.toString(httpEntity);
@ -107,8 +107,7 @@ public class MarketData extends BaseBinanceApi {
}
String url = String.format(
"%s%s?symbol=%s&interval=%s",
BASE_ENDPOINT,
"%s?symbol=%s&interval=%s",
CANDLESTICK_URL,
symbol,
interval.toString());
@ -134,8 +133,7 @@ public class MarketData extends BaseBinanceApi {
}
String url = String.format(
"%s%s?symbol=%s&interval=%s&limit=%d",
BASE_ENDPOINT,
"%s?symbol=%s&interval=%s&limit=%d",
CANDLESTICK_URL,
symbol,
interval.toString(),
@ -167,16 +165,14 @@ public class MarketData extends BaseBinanceApi {
if (isStartTime) {
url = String.format(
"%s%s?symbol=%s&interval=%s&startTime=%d",
BASE_ENDPOINT,
"%s?symbol=%s&interval=%s&startTime=%d",
CANDLESTICK_URL,
symbol,
interval.toString(),
time);
} else {
url = String.format(
"%s%s?symbol=%s&interval=%s&endTime=%d",
BASE_ENDPOINT,
"%s?symbol=%s&interval=%s&endTime=%d",
CANDLESTICK_URL,
symbol,
interval.toString(),
@ -210,8 +206,7 @@ public class MarketData extends BaseBinanceApi {
if (isStartTime) {
url = String.format(
"%s%s?symbol=%s&interval=%s&limit=%d&startTime=%d",
BASE_ENDPOINT,
"%s?symbol=%s&interval=%s&limit=%d&startTime=%d",
CANDLESTICK_URL,
symbol,
interval.toString(),
@ -219,8 +214,7 @@ public class MarketData extends BaseBinanceApi {
time);
} else {
url = String.format(
"%s%s?symbol=%s&interval=%s&limit=%d&endTime=%d",
BASE_ENDPOINT,
"%s?symbol=%s&interval=%s&limit=%d&endTime=%d",
CANDLESTICK_URL,
symbol,
interval.toString(),
@ -254,8 +248,7 @@ public class MarketData extends BaseBinanceApi {
String url;
url = String.format(
"%s%s?symbol=%s&interval=%s&limit=%d&startTime=%d&endTime=%d",
BASE_ENDPOINT,
"%s?symbol=%s&interval=%s&limit=%d&startTime=%d&endTime=%d",
CANDLESTICK_URL,
symbol,
interval.toString(),
@ -278,9 +271,9 @@ public class MarketData extends BaseBinanceApi {
if (symbol == null) {
throw new IllegalArgumentException("Symbol must not be null");
}
String url = String.format("%s%s?symbol=%s", BASE_ENDPOINT, TICKER_PRICE_URL, symbol);
final HttpGet request = Helpers.getBuilder(url, apiKey);
String urlWithParams = String.format("%s?symbol=%s", TICKER_PRICE_URL, symbol);
final HttpGet request = buildGetRequestFromEndpoint(urlWithParams, apiKey);
try {
try (CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(request)) {
@ -289,8 +282,9 @@ public class MarketData extends BaseBinanceApi {
HttpEntity httpEntity = closeableHttpResponse.getEntity();
if (httpEntity == null) {
log.error(NO_RESPONSE_TEXT, url);
throw new BinanceServiceUnreachableException(String.format(NO_RESPONSE_TEXT_FORMATTED, url), null);
log.error(NO_RESPONSE_TEXT, request.getURI().toASCIIString());
throw new BinanceServiceUnreachableException(
String.format(NO_RESPONSE_TEXT_FORMATTED, request.getURI().toASCIIString()), null);
}
String response = EntityUtils.toString(httpEntity);
@ -314,9 +308,7 @@ public class MarketData extends BaseBinanceApi {
* @throws BinanceServiceUnreachableException If the service is unreachable
*/
public Either<ServiceError, List<TickerPrice>> getTickerPrices() throws BinanceServiceUnreachableException {
String url = String.format("%s%s", BASE_ENDPOINT, TICKER_PRICE_URL);
final HttpGet request = Helpers.getBuilder(url, apiKey);
final HttpGet request = buildGetRequestFromEndpoint(TICKER_PRICE_URL, apiKey);
try {
try (CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(request)) {
@ -325,8 +317,9 @@ public class MarketData extends BaseBinanceApi {
HttpEntity httpEntity = closeableHttpResponse.getEntity();
if (httpEntity == null) {
log.error(NO_RESPONSE_TEXT, url);
throw new BinanceServiceUnreachableException(String.format(NO_RESPONSE_TEXT_FORMATTED, url), null);
log.error(NO_RESPONSE_TEXT, request.getURI().toASCIIString());
throw new BinanceServiceUnreachableException(
String.format(NO_RESPONSE_TEXT_FORMATTED, request.getURI().toASCIIString()), null);
}
String response = EntityUtils.toString(httpEntity);
@ -347,13 +340,13 @@ public class MarketData extends BaseBinanceApi {
* Gets candlestick data from the provided URL, allowing all of the getCandleStickData functions to act
* as glorified URL builders.
*
* @param url The URL to use
* @param endpoint The endpoint to use that has the associated parameters populated
* @return A list of candlesticks if successful, otherwise an ServiceError
* @throws BinanceServiceUnreachableException If the service is unreachable
*/
private Either<ServiceError, List<Candlestick>>
getCandleStickDataFromUrl(String url) throws BinanceServiceUnreachableException {
final HttpGet request = Helpers.getBuilder(url, apiKey);
getCandleStickDataFromUrl(String endpoint) throws BinanceServiceUnreachableException {
final HttpGet request = buildGetRequestFromEndpoint(endpoint, apiKey);
try {
try (CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(request)) {
@ -362,8 +355,9 @@ public class MarketData extends BaseBinanceApi {
HttpEntity httpEntity = closeableHttpResponse.getEntity();
if (httpEntity == null) {
log.error(NO_RESPONSE_TEXT, url);
throw new BinanceServiceUnreachableException(String.format(NO_RESPONSE_TEXT_FORMATTED, url), null);
log.error(NO_RESPONSE_TEXT, request.getURI().toASCIIString());
throw new BinanceServiceUnreachableException(
String.format(NO_RESPONSE_TEXT_FORMATTED, request.getURI().toASCIIString()), null);
}
String response = EntityUtils.toString(httpEntity);

2
src/main/java/com/sigmaflare/binancej/entities/OrderBookDepth.java

@ -21,7 +21,7 @@ import java.util.List;
@NoArgsConstructor
@JsonDeserialize(using = OrderBookDepthResponseDeserializer.class)
public class OrderBookDepth {
private Long lastUpdateId;
private long lastUpdateId;
private List<OrderBookPricing> bids;

40
src/main/java/com/sigmaflare/binancej/entities/transform/OrderBookDepthResponseDeserializer.java

@ -27,19 +27,17 @@ public class OrderBookDepthResponseDeserializer extends StdDeserializer<OrderBoo
super(vc);
}
@Override
public OrderBookDepth deserialize(JsonParser jp, DeserializationContext context) throws IOException {
JsonNode node = jp.getCodec().readTree(jp);
Long lastUpdateId = node.get("lastUpdateId").asLong();
List<OrderBookPricing> bids = new ArrayList<>();
List<OrderBookPricing> asks = new ArrayList<>();
/**
* Generates a List of OrderBookPricing from the JSON supplied
* @param node The JsonNode given from the deserialize method
* @param nodeName The name of the node to transform
* @return A list of OrderBookPricing
*/
private List<OrderBookPricing> createOrderBookPricingListFromNode(JsonNode node, String nodeName) {
// NOTE: Jackson nodes don't seem to support conversion to streams
for (JsonNode n : node.get("bids")) {
bids.add(OrderBookPricing
List<OrderBookPricing> data = new ArrayList<>();
for (JsonNode n : node.get(nodeName)) {
data.add(OrderBookPricing
.builder()
.price(new BigDecimal(n.get(0).asText()))
.quantity(new BigDecimal(n.get(1).asText()))
@ -47,20 +45,20 @@ public class OrderBookDepthResponseDeserializer extends StdDeserializer<OrderBoo
);
}
for (JsonNode n : node.get("asks")) {
asks.add(OrderBookPricing
.builder()
.price(new BigDecimal(n.get(0).asText()))
.quantity(new BigDecimal(n.get(1).asText()))
.build()
);
return data;
}
@Override
public OrderBookDepth deserialize(JsonParser jp, DeserializationContext context) throws IOException {
JsonNode node = jp.getCodec().readTree(jp);
Long lastUpdateId = node.get("lastUpdateId").asLong();
return OrderBookDepth
.builder()
.lastUpdateId(lastUpdateId)
.bids(bids)
.asks(asks)
.bids(createOrderBookPricingListFromNode(node, "bids"))
.asks(createOrderBookPricingListFromNode(node, "asks"))
.build();
}
}

45
src/test/java/com/sigmaflare/binancej/CandlestickMethodTests.java

@ -37,6 +37,7 @@ public class CandlestickMethodTests {
private final String urlWithLimit = String.format("%s%s", url, "&limit=1000");
private final String urlWithStartTime = String.format("%s%s", url, "&startTime=1234");
private final String urlWithEndTime = String.format("%s%s", url, "&endTime=1234");
private final String urlWithAllParameters = String.format("%s%s", urlWithLimit, "&startTime=1234&endTime=1234");
private final String urlWithLimitAndStartTime = String.format("%s%s", urlWithLimit, "&startTime=1234");
private final String urlWithLimitAndEndTime = String.format("%s%s", urlWithLimit, "&endTime=1234");
@ -103,7 +104,7 @@ public class CandlestickMethodTests {
marketData.getCandleStickData("TEST", Interval.EIGHT_HOURS);
assertTrue(res.isRight());
assertEquals(Helpers.extractEitherValueSafely(res.right()), candlesticks);
assertEquals(candlesticks, Helpers.extractEitherValueSafely(res.right()));
}
@Test
@ -130,7 +131,7 @@ public class CandlestickMethodTests {
marketData.getCandleStickData("TEST", Interval.EIGHT_HOURS, 1000);
assertTrue(res.isRight());
assertEquals(Helpers.extractEitherValueSafely(res.right()), candlesticks);
assertEquals(candlesticks, Helpers.extractEitherValueSafely(res.right()));
}
@Test
@ -157,7 +158,7 @@ public class CandlestickMethodTests {
marketData.getCandleStickData("TEST", Interval.EIGHT_HOURS, 1000, 1234L,true);
assertTrue(res.isRight());
assertEquals(Helpers.extractEitherValueSafely(res.right()), candlesticks);
assertEquals(candlesticks, Helpers.extractEitherValueSafely(res.right()));
}
@Test
@ -184,7 +185,35 @@ public class CandlestickMethodTests {
marketData.getCandleStickData("TEST", Interval.EIGHT_HOURS, 1000, 1234L,false);
assertTrue(res.isRight());
assertEquals(Helpers.extractEitherValueSafely(res.right()), candlesticks);
assertEquals(candlesticks, Helpers.extractEitherValueSafely(res.right()));
}
@Test
public void testCandlestickServiceWithAllParametersReturnsSuccessfully()
throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
when(mockedCloseableHttpResponse.getStatusLine())
.thenReturn(new BasicStatusLine(new ProtocolVersion("TEST", 1, 0),
200, "test"));
when(mockedCloseableHttpClient.execute(argThat(new GetMatcher(urlWithAllParameters, "1234"))))
.thenReturn(mockedCloseableHttpResponse);
BasicHttpEntity httpEntity = new BasicHttpEntity();
httpEntity.setContent(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)));
when(mockedCloseableHttpResponse.getEntity()).thenReturn(httpEntity);
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient);
Either<ServiceError, List<Candlestick>> res =
marketData.getCandleStickData("TEST", Interval.EIGHT_HOURS,
1000, 1234L,1234L);
assertTrue(res.isRight());
assertEquals(candlesticks, Helpers.extractEitherValueSafely(res.right()));
}
@Test
@ -211,7 +240,7 @@ public class CandlestickMethodTests {
marketData.getCandleStickData("TEST", Interval.EIGHT_HOURS, 1234L,true);
assertTrue(res.isRight());
assertEquals(Helpers.extractEitherValueSafely(res.right()), candlesticks);
assertEquals(candlesticks, Helpers.extractEitherValueSafely(res.right()));
}
@Test
@ -238,7 +267,7 @@ public class CandlestickMethodTests {
marketData.getCandleStickData("TEST", Interval.EIGHT_HOURS, 1234L,false);
assertTrue(res.isRight());
assertEquals(Helpers.extractEitherValueSafely(res.right()), candlesticks);
assertEquals(candlesticks, Helpers.extractEitherValueSafely(res.right()));
}
@Test(expected = BinanceServiceUnreachableException.class)
@ -259,7 +288,7 @@ public class CandlestickMethodTests {
}
@Test
public void TestCandlestickServiceBadApiKeyReturns400() throws IOException, BinanceServiceUnreachableException {
public void testCandlestickServiceBadApiKeyReturns400() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -284,6 +313,6 @@ public class CandlestickMethodTests {
marketData.getCandleStickData("TEST", Interval.EIGHT_HOURS);
assertTrue(res.isLeft());
assertEquals(Helpers.extractEitherValueSafely(res.left()), serviceError);
assertEquals(serviceError, Helpers.extractEitherValueSafely(res.left()));
}
}

10
src/test/java/com/sigmaflare/binancej/ExchangeInfoMethodTests.java

@ -44,7 +44,7 @@ public class ExchangeInfoMethodTests {
}
@Test
public void TestExchangeInfoServiceReturnsSuccessfully() throws IOException, BinanceServiceUnreachableException {
public void testExchangeInfoServiceReturnsSuccessfully() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -68,11 +68,11 @@ public class ExchangeInfoMethodTests {
Either<ServiceError, ExchangeInfo> res = generalUtilities.getExchangeInfo();
assertTrue(res.isRight());
assertEquals(Helpers.extractEitherValueSafely(res.right()), exchangeInfo);
assertEquals(exchangeInfo, Helpers.extractEitherValueSafely(res.right()));
}
@Test(expected = BinanceServiceUnreachableException.class)
public void TestExchangeInfoServiceWithNoHttpEntityFails() throws IOException, BinanceServiceUnreachableException {
public void testExchangeInfoServiceWithNoHttpEntityFails() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -90,7 +90,7 @@ public class ExchangeInfoMethodTests {
}
@Test
public void TestExchangeInfoServiceBadApiKeyReturns400() throws IOException, BinanceServiceUnreachableException {
public void testExchangeInfoServiceBadApiKeyReturns400() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -115,6 +115,6 @@ public class ExchangeInfoMethodTests {
Either<ServiceError, ExchangeInfo> res = generalUtilities.getExchangeInfo();
assertTrue(res.isLeft());
assertEquals(Helpers.extractEitherValueSafely(res.left()), serviceError);
assertEquals(serviceError, Helpers.extractEitherValueSafely(res.left()));
}
}

33
src/test/java/com/sigmaflare/binancej/OrderBookDepthTests.java

@ -3,6 +3,7 @@ package com.sigmaflare.binancej;
import com.codepoetics.ambivalence.Either;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sigmaflare.binancej.entities.OrderBookDepth;
import com.sigmaflare.binancej.entities.OrderBookPricing;
import com.sigmaflare.binancej.entities.ServiceError;
import com.sigmaflare.binancej.exceptions.BinanceServiceUnreachableException;
import com.sigmaflare.binancej.matchers.GetMatcher;
@ -18,6 +19,7 @@ import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import static com.sigmaflare.binancej.Constant.BASE_ENDPOINT;
@ -46,8 +48,29 @@ public class OrderBookDepthTests {
mapper.readValue(orderBookDepthJson, OrderBookDepth.class);
}
// This test is only seen in some test suites - typically ones with custom deserializers
@Test
public void TestOrderBookDepthReturnsSuccessfully() throws IOException, BinanceServiceUnreachableException {
public void testValuesAreDeserializedCorrectly() {
OrderBookPricing bidPricing = OrderBookPricing
.builder()
.price(new BigDecimal("4.00000000"))
.quantity(new BigDecimal("431.00000000"))
.build();
OrderBookPricing askPricing = OrderBookPricing
.builder()
.price(new BigDecimal("4.00000200"))
.quantity(new BigDecimal("12.00000000"))
.build();
assertEquals(1027024, orderBookDepth.getLastUpdateId());
assertEquals(bidPricing, orderBookDepth.getBids().get(0));
assertEquals(askPricing, orderBookDepth.getAsks().get(0));
}
@Test
public void testOrderBookDepthReturnsSuccessfully() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -72,12 +95,12 @@ public class OrderBookDepthTests {
Either<ServiceError, OrderBookDepth> res = marketData.getOrderBookDepth("ETHBTC");
assertTrue(res.isRight());
assertEquals(Helpers.extractEitherValueSafely(res.right()), orderBookDepth);
assertEquals(orderBookDepth, Helpers.extractEitherValueSafely(res.right()));
}
@Test(expected = BinanceServiceUnreachableException.class)
public void TestOrderBookDepthWithNoHttpEntityFails() throws IOException, BinanceServiceUnreachableException {
public void testOrderBookDepthWithNoHttpEntityFails() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -94,7 +117,7 @@ public class OrderBookDepthTests {
}
@Test
public void TestOrderBookDepthWithBadApiKeyReturns400() throws IOException, BinanceServiceUnreachableException {
public void testOrderBookDepthWithBadApiKeyReturns400() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -119,6 +142,6 @@ public class OrderBookDepthTests {
Either<ServiceError, OrderBookDepth> res = marketData.getOrderBookDepth("ETHBTC", 1000);
assertTrue(res.isLeft());
assertEquals(Helpers.extractEitherValueSafely(res.left()), serviceError);
assertEquals(serviceError, Helpers.extractEitherValueSafely(res.left()));
}
}

8
src/test/java/com/sigmaflare/binancej/PingMethodTests.java

@ -30,7 +30,7 @@ public class PingMethodTests {
private final String url = String.format("%s%s", BASE_ENDPOINT, "/api/v1/ping");
@Test
public void TestServiceAliveReturnsSuccess() throws IOException, BinanceServiceUnreachableException {
public void testServiceAliveReturnsSuccess() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -57,7 +57,7 @@ public class PingMethodTests {
}
@Test(expected = BinanceServiceUnreachableException.class)
public void TestServiceFailsToReturnHttpEntity() throws IOException, BinanceServiceUnreachableException {
public void testServiceFailsToReturnHttpEntity() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -75,7 +75,7 @@ public class PingMethodTests {
}
@Test
public void TestServiceBadApiKeyReturns400() throws IOException, BinanceServiceUnreachableException {
public void testServiceBadApiKeyReturns400() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -100,6 +100,6 @@ public class PingMethodTests {
Either<ServiceError, Ping> res = generalUtilities.ping();
assertTrue(res.isLeft());
assertEquals(Helpers.extractEitherValueSafely(res.left()), serviceError);
assertEquals(serviceError, Helpers.extractEitherValueSafely(res.left()));
}
}

8
src/test/java/com/sigmaflare/binancej/TickerPriceTests.java

@ -83,7 +83,7 @@ public class TickerPriceTests {
Either<ServiceError, TickerPrice> res = marketData.getTickerPriceForSymbol("TEST");
assertTrue(res.isRight());
assertEquals(Helpers.extractEitherValueSafely(res.right()), tickerPrice);
assertEquals(tickerPrice, Helpers.extractEitherValueSafely(res.right()));
}
@Test
@ -109,7 +109,7 @@ public class TickerPriceTests {
Either<ServiceError, List<TickerPrice>> res = marketData.getTickerPrices();
assertTrue(res.isRight());
assertEquals(Helpers.extractEitherValueSafely(res.right()), tickerPrices);
assertEquals(tickerPrices, Helpers.extractEitherValueSafely(res.right()));
}
@Test(expected = BinanceServiceUnreachableException.class)
@ -174,7 +174,7 @@ public class TickerPriceTests {
Either<ServiceError, TickerPrice> res = marketData.getTickerPriceForSymbol("TEST");
assertTrue(res.isLeft());
assertEquals(Helpers.extractEitherValueSafely(res.left()), serviceError);
assertEquals(serviceError, Helpers.extractEitherValueSafely(res.left()));
}
@Test
@ -202,6 +202,6 @@ public class TickerPriceTests {
Either<ServiceError, List<TickerPrice>> res = marketData.getTickerPrices();
assertTrue(res.isLeft());
assertEquals(Helpers.extractEitherValueSafely(res.left()), serviceError);
assertEquals(serviceError, Helpers.extractEitherValueSafely(res.left()));
}
}

10
src/test/java/com/sigmaflare/binancej/TimeMethodTests.java

@ -29,7 +29,7 @@ public class TimeMethodTests {
private final String url = String.format("%s%s", BASE_ENDPOINT, "/api/v1/time");
@Test
public void TestTimeReturnsSuccessfully() throws IOException, BinanceServiceUnreachableException {
public void testTimeReturnsSuccessfully() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -55,11 +55,11 @@ public class TimeMethodTests {
Either<ServiceError, Time> res = generalUtilities.getServerTime();
assertTrue(res.isRight());
assertEquals(Helpers.extractEitherValueSafely(res.right()), time);
assertEquals(time, Helpers.extractEitherValueSafely(res.right()));
}
@Test(expected = BinanceServiceUnreachableException.class)
public void TestTimeNoEntityReturnedFails() throws IOException, BinanceServiceUnreachableException {
public void testTimeNoEntityReturnedFails() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -77,7 +77,7 @@ public class TimeMethodTests {
}
@Test
public void TestTimeBadApiKeyReturns400() throws IOException, BinanceServiceUnreachableException {
public void testTimeBadApiKeyReturns400() throws IOException, BinanceServiceUnreachableException {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
@ -103,6 +103,6 @@ public class TimeMethodTests {
Either<ServiceError, Time> res = generalUtilities.getServerTime();
assertTrue(res.isLeft());
assertEquals(Helpers.extractEitherValueSafely(res.left()), serviceError);
assertEquals(serviceError, Helpers.extractEitherValueSafely(res.left()));
}
}

Loading…
Cancel
Save