Skip to content

Commit

Permalink
Fix sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
faystmax committed Sep 25, 2020
1 parent cc4501f commit 413765f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.faystmax.binance.api.client.exception;

public class HmacSHA256SignException extends RuntimeException {
public HmacSHA256SignException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Response intercept(Chain chain) throws IOException {

final String payload = original.url().query();
if (!StringUtils.isEmpty(payload)) {
String signature = HmacSHA256Signer.sign(payload, secret);
String signature = HmacSHA256SignHelper.sign(payload, secret);
HttpUrl signedUrl = original.url().newBuilder().addQueryParameter("signature", signature).build();
newRequestBuilder.url(signedUrl);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.faystmax.binance.api.client.security;

import com.faystmax.binance.api.client.exception.HmacSHA256SignException;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.Mac;
Expand All @@ -8,7 +9,7 @@
/**
* Utility class to sign messages using HMAC-SHA256
*/
public class HmacSHA256Signer {
public class HmacSHA256SignHelper {
/**
* Sign the given message using the given secret
*
Expand All @@ -23,7 +24,7 @@ public static String sign(String message, String secret) {
sha256_HMAC.init(secretKeySpec);
return new String(Hex.encodeHex(sha256_HMAC.doFinal(message.getBytes())));
} catch (Exception e) {
throw new RuntimeException("Unable to sign message.", e);
throw new HmacSHA256SignException("Unable to sign message.", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.faystmax.binance.api.client;

import com.faystmax.binance.api.client.exception.BinanceApiException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static com.faystmax.binance.api.client.BinanceApiClientTestConstant.SYMBOL;
import static com.faystmax.binance.api.client.BinanceApiClientTestConstants.SYMBOL;
import static org.junit.jupiter.api.Assertions.assertThrows;

class BinanceApiClientErrorTest {
@Test
void testApiError() {
var apiClient = BinanceApiClientFactory.create("test", "test");
Assertions.assertThrows(BinanceApiException.class, () -> {
apiClient.getMyTrades(SYMBOL);
});
assertThrows(BinanceApiException.class, () -> apiClient.getMyTrades(SYMBOL));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import java.math.BigDecimal;
import java.util.List;

import static com.faystmax.binance.api.client.BinanceApiClientTestConstant.SYMBOL;
import static com.faystmax.binance.api.client.BinanceApiClientTestConstants.SYMBOL;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
Expand All @@ -21,36 +22,36 @@
*/
@Disabled
class BinanceApiClientPrivateRoutesTest {
public static final String API_KEY = ""; // Place Api key here
public static final String SECRET = ""; // Place Secret key here

private static BinanceApiClient apiClient;

@BeforeAll
public static void setUp() {
apiClient = BinanceApiClientFactory.create(
"", // place Api key here
"" // place Secret key here
);
apiClient = BinanceApiClientFactory.create(API_KEY, SECRET);
}

@Test
void getMyTrades_ok() {
void getMyTradesReturnNotNullTrades() {
List<Trade> trades = apiClient.getMyTrades(SYMBOL);
assertNotNull(trades);
}

@Test
void getAccount_ok() {
void getAccountReturnNotNullAccount() {
Account account = apiClient.getAccount();
assertNotNull(account);
}

@Test
void newOrderTest_ok() {
void newOrderTest() {
NewOrder order = NewOrder.marketSell(SYMBOL, new BigDecimal("0.5"));
apiClient.newOrderTest(order);
assertDoesNotThrow(() -> apiClient.newOrderTest(order));
}

@Test
void getAllOrders_ok() {
void getAllOrdersReturnNotNullOrders() {
List<Order> allOrders = apiClient.getAllOrders(new AllOrdersRequest(SYMBOL));
assertNotNull(allOrders);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.math.BigDecimal;

import static com.faystmax.binance.api.client.BinanceApiClientTestConstant.SYMBOL;
import static com.faystmax.binance.api.client.BinanceApiClientTestConstants.SYMBOL;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -24,30 +24,30 @@ public static void setUp() {
}

@Test
void ping_ok() {
void ping() {
apiClient.ping();
}

@Test
void getServerTime_ok() {
void getServerTimeReturnPositiveValue() {
Long serverTime = apiClient.getServerTime();
assertTrue(serverTime > 0);
}

@Test
void getExchangeInfo_ok() {
void getExchangeInfoReturnNotNullInfo() {
ExchangeInfo exchangeInfo = apiClient.getExchangeInfo();
assertNotNull(exchangeInfo);
}

@Test
void get24HrPriceStatistics_ok() {
void get24HrPriceStatisticsReturnPositiveLastPrice() {
TickerStatistics statistics = apiClient.get24HrPriceStatistics(SYMBOL);
assertTrue(statistics.getLastPrice().compareTo(BigDecimal.ZERO) > 0);
}

@Test
void getLatestPrice_ok() {
void getLatestPriceReturnPositiveValue() {
TickerPrice price = apiClient.getLatestPrice(SYMBOL);
assertTrue(price.getPrice().compareTo(BigDecimal.ZERO) > 0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.faystmax.binance.api.client;

class BinanceApiClientTestConstant {
class BinanceApiClientTestConstants {
public static final String SYMBOL = "ETHUSDT";
}

0 comments on commit 413765f

Please sign in to comment.