Skip to content

Commit

Permalink
[resolves wildfly-extras#2917] XChange cannot check API connection be…
Browse files Browse the repository at this point in the history
…fore start
  • Loading branch information
tdiesler committed Nov 27, 2019
1 parent 98c8c93 commit 7197409
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* #%L
* Wildfly Camel :: Testsuite
* %%
* Copyright (C) 2013 - 2017 RedHat
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.wildfly.camel.test.xchange;

import java.io.IOException;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.camel.CamelContext;
import org.apache.camel.component.xchange.XChangeComponent;

abstract class AbstractXChangeIntegrationTest {

boolean checkAPIConnection() throws IOException {
try {
URL url = new URL("https://api.binance.com");
URLConnection con = url.openConnection();
con.connect();
return true;
} catch (ConnectException ex) {
// If the connection to the Binance API endpoint is refused we skip all further testing
// e.g. Redhat Jenkins Env
if (ex.getMessage().contains("Connection refused"))
return false;
else
throw ex;
} catch (IOException ex) {
throw ex;
}
}

boolean hasAPICredentials(CamelContext camelctx) {
XChangeComponent component = camelctx.getComponent("xchange", XChangeComponent.class);
return component.getXChange().getExchangeSpecification().getApiKey() != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.xchange.XChangeComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
Expand All @@ -41,11 +40,13 @@

@CamelAware
@RunWith(Arquillian.class)
public class XChangeAccountIntegrationTest {
public class XChangeAccountIntegrationTest extends AbstractXChangeIntegrationTest {

@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class, "camel-xchange-account-tests");
JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "camel-xchange-account-tests");
archive.addClasses(AbstractXChangeIntegrationTest.class);
return archive;
}

@Test
Expand All @@ -57,13 +58,14 @@ public void testBalances() throws Exception {

camelctx.start();
try {
Assume.assumeTrue(checkAPIConnection());
Assume.assumeTrue(hasAPICredentials(camelctx));
ProducerTemplate template = camelctx.createProducerTemplate();
List<Balance> balances = template.requestBody("direct:balances", null, List.class);
Assert.assertNotNull("Balances not null", balances);
balances.forEach(b -> System.out.println(b));
} finally {
camelctx.close();
camelctx.stop();
}
}

Expand All @@ -76,12 +78,13 @@ public void testWallets() throws Exception {

camelctx.start();
try {
Assume.assumeTrue(checkAPIConnection());
Assume.assumeTrue(hasAPICredentials(camelctx));
ProducerTemplate template = camelctx.createProducerTemplate();
List<Wallet> wallets = template.requestBody("direct:wallets", null, List.class);
Assert.assertNotNull("Wallets not null", wallets);
} finally {
camelctx.close();
camelctx.stop();
}
}

Expand All @@ -94,20 +97,16 @@ public void testFundingHistory() throws Exception {

camelctx.start();
try {
Assume.assumeTrue(checkAPIConnection());
Assume.assumeTrue(hasAPICredentials(camelctx));
ProducerTemplate template = camelctx.createProducerTemplate();
List<FundingRecord> records = template.requestBody("direct:fundingHistory", null, List.class);
Assert.assertNotNull("Funding records not null", records);
} finally {
camelctx.close();
camelctx.stop();
}
}

private boolean hasAPICredentials(CamelContext context) {
XChangeComponent component = context.getComponent("xchange", XChangeComponent.class);
return component.getXChange().getExchangeSpecification().getApiKey() != null;
}

private RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.knowm.xchange.currency.CurrencyPair;
Expand All @@ -38,11 +39,13 @@

@CamelAware
@RunWith(Arquillian.class)
public class XChangeMarketIntegrationTest {
public class XChangeMarketIntegrationTest extends AbstractXChangeIntegrationTest {

@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class, "camel-xchange-market-tests");
JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "camel-xchange-market-tests");
archive.addClasses(AbstractXChangeIntegrationTest.class);
return archive;
}

@Test
Expand All @@ -53,6 +56,7 @@ public void testTicker() throws Exception {

camelctx.start();
try {
Assume.assumeTrue(checkAPIConnection());
ProducerTemplate template = camelctx.createProducerTemplate();
Ticker ticker = template.requestBody("direct:ticker", CurrencyPair.EOS_ETH, Ticker.class);
Assert.assertNotNull("Ticker not null", ticker);
Expand All @@ -62,7 +66,7 @@ public void testTicker() throws Exception {
Assert.assertNotNull("Ticker not null", ticker);
System.out.println(ticker);
} finally {
camelctx.close();
camelctx.stop();
}
}

Expand All @@ -74,12 +78,13 @@ public void testTickerBTCUSDT() throws Exception {

camelctx.start();
try {
Assume.assumeTrue(checkAPIConnection());
ProducerTemplate template = camelctx.createProducerTemplate();
Ticker ticker = template.requestBody("direct:tickerBTCUSDT", null, Ticker.class);
Assert.assertNotNull("Ticker not null", ticker);
System.out.println(ticker);
} finally {
camelctx.close();
camelctx.stop();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.knowm.xchange.currency.Currency;
Expand All @@ -43,11 +44,13 @@

@CamelAware
@RunWith(Arquillian.class)
public class XChangeMetadataIntegrationTest {
public class XChangeMetadataIntegrationTest extends AbstractXChangeIntegrationTest {

@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class, "camel-xchange-metadata-tests");
JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "camel-xchange-metadata-tests");
archive.addClasses(AbstractXChangeIntegrationTest.class);
return archive;
}

@Test
Expand All @@ -59,12 +62,13 @@ public void testCurrencies() throws Exception {

camelctx.start();
try {
Assume.assumeTrue(checkAPIConnection());
ProducerTemplate template = camelctx.createProducerTemplate();
List<Currency> currencies = template.requestBody("direct:currencies", null, List.class);
Assert.assertNotNull("Currencies not null", currencies);
Assert.assertTrue("Contains ETH", currencies.contains(Currency.ETH));
} finally {
camelctx.close();
camelctx.stop();
}
}

Expand All @@ -76,14 +80,15 @@ public void testCurrencyMetaData() throws Exception {

camelctx.start();
try {
Assume.assumeTrue(checkAPIConnection());
ProducerTemplate template = camelctx.createProducerTemplate();
CurrencyMetaData metadata = template.requestBody("direct:currencyMetaData", Currency.ETH, CurrencyMetaData.class);
Assert.assertNotNull("CurrencyMetaData not null", metadata);

metadata = template.requestBodyAndHeader("direct:currencyMetaData", null, HEADER_CURRENCY, Currency.ETH, CurrencyMetaData.class);
Assert.assertNotNull("CurrencyMetaData not null", metadata);
} finally {
camelctx.close();
camelctx.stop();
}
}

Expand All @@ -96,12 +101,13 @@ public void testCurrencyPairs() throws Exception {

camelctx.start();
try {
Assume.assumeTrue(checkAPIConnection());
ProducerTemplate template = camelctx.createProducerTemplate();
List<CurrencyPair> pairs = template.requestBody("direct:currencyPairs", null, List.class);
Assert.assertNotNull("Pairs not null", pairs);
Assert.assertTrue("Contains EOS/ETH", pairs.contains(CurrencyPair.EOS_ETH));
} finally {
camelctx.close();
camelctx.stop();
}
}

Expand All @@ -113,14 +119,15 @@ public void testCurrencyPairMetaData() throws Exception {

camelctx.start();
try {
Assume.assumeTrue(checkAPIConnection());
ProducerTemplate template = camelctx.createProducerTemplate();
CurrencyPairMetaData metadata = template.requestBody("direct:currencyPairMetaData", CurrencyPair.EOS_ETH, CurrencyPairMetaData.class);
Assert.assertNotNull("CurrencyPairMetaData not null", metadata);

metadata = template.requestBodyAndHeader("direct:currencyPairMetaData", null, HEADER_CURRENCY_PAIR, CurrencyPair.EOS_ETH, CurrencyPairMetaData.class);
Assert.assertNotNull("CurrencyPairMetaData not null", metadata);
} finally {
camelctx.close();
camelctx.stop();
}
}

Expand Down

0 comments on commit 7197409

Please sign in to comment.