You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2022/10/13 09:37:43 UTC
[camel-quarkus] 01/07: Fallback to mocked responses if XChange APIs are not available
This is an automated email from the ASF dual-hosted git repository.
jamesnetherton pushed a commit to branch 2.13.x
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit d35e6fdccbe9e4ade04a28c5a9615ed1b443b001
Author: James Netherton <ja...@gmail.com>
AuthorDate: Wed Oct 12 07:28:13 2022 +0100
Fallback to mocked responses if XChange APIs are not available
Fixes #4169
---
.../xchange/it/XchangeBinanceTestResource.java | 30 +++---
.../xchange/it/XchangeKrakenTestResource.java | 30 +++---
.../xchange/it/XchangeTestResourceBase.java | 105 +++++++++++++++++++++
3 files changed, 135 insertions(+), 30 deletions(-)
diff --git a/integration-tests/xchange/src/test/java/org/apache/camel/quarkus/component/xchange/it/XchangeBinanceTestResource.java b/integration-tests/xchange/src/test/java/org/apache/camel/quarkus/component/xchange/it/XchangeBinanceTestResource.java
index 04ad911fac..aa16a0ac72 100644
--- a/integration-tests/xchange/src/test/java/org/apache/camel/quarkus/component/xchange/it/XchangeBinanceTestResource.java
+++ b/integration-tests/xchange/src/test/java/org/apache/camel/quarkus/component/xchange/it/XchangeBinanceTestResource.java
@@ -16,30 +16,30 @@
*/
package org.apache.camel.quarkus.component.xchange.it;
-import java.util.Map;
+public class XchangeBinanceTestResource extends XchangeTestResourceBase {
-import org.apache.camel.quarkus.test.mock.backend.MockBackendUtils;
-import org.apache.camel.quarkus.test.wiremock.WireMockTestResourceLifecycleManager;
+ @Override
+ String getCryptoExchangeName() {
+ return "binance";
+ }
-public class XchangeBinanceTestResource extends WireMockTestResourceLifecycleManager {
+ @Override
+ String getHealthStatusField() {
+ return "msg";
+ }
@Override
- public Map<String, String> start() {
- Map<String, String> options = super.start();
- String wireMockUrl = options.get("wiremock.url");
- if (wireMockUrl != null) {
- options.put("wiremock.binance.url", wireMockUrl);
- }
- return options;
+ String getExpectedHealthStatus() {
+ return "normal";
}
@Override
- protected String getRecordTargetBaseUrl() {
- return "https://api.binance.com/";
+ String getHealthEndpointUrl() {
+ return getRecordTargetBaseUrl() + "sapi/v1/system/status";
}
@Override
- protected boolean isMockingEnabled() {
- return MockBackendUtils.startMockBackend(false);
+ protected String getRecordTargetBaseUrl() {
+ return "https://api.binance.com/";
}
}
diff --git a/integration-tests/xchange/src/test/java/org/apache/camel/quarkus/component/xchange/it/XchangeKrakenTestResource.java b/integration-tests/xchange/src/test/java/org/apache/camel/quarkus/component/xchange/it/XchangeKrakenTestResource.java
index 7fd83202c3..84d8826e42 100644
--- a/integration-tests/xchange/src/test/java/org/apache/camel/quarkus/component/xchange/it/XchangeKrakenTestResource.java
+++ b/integration-tests/xchange/src/test/java/org/apache/camel/quarkus/component/xchange/it/XchangeKrakenTestResource.java
@@ -16,30 +16,30 @@
*/
package org.apache.camel.quarkus.component.xchange.it;
-import java.util.Map;
+public class XchangeKrakenTestResource extends XchangeTestResourceBase {
-import org.apache.camel.quarkus.test.mock.backend.MockBackendUtils;
-import org.apache.camel.quarkus.test.wiremock.WireMockTestResourceLifecycleManager;
+ @Override
+ String getCryptoExchangeName() {
+ return "kraken";
+ }
-public class XchangeKrakenTestResource extends WireMockTestResourceLifecycleManager {
+ @Override
+ String getHealthStatusField() {
+ return "result.status";
+ }
@Override
- public Map<String, String> start() {
- Map<String, String> options = super.start();
- String wireMockUrl = options.get("wiremock.url");
- if (wireMockUrl != null) {
- options.put("wiremock.kraken.url", wireMockUrl);
- }
- return options;
+ String getExpectedHealthStatus() {
+ return "online";
}
@Override
- protected String getRecordTargetBaseUrl() {
- return "https://api.kraken.com/";
+ String getHealthEndpointUrl() {
+ return getRecordTargetBaseUrl() + "0/public/SystemStatus";
}
@Override
- protected boolean isMockingEnabled() {
- return MockBackendUtils.startMockBackend(false);
+ protected String getRecordTargetBaseUrl() {
+ return "https://api.kraken.com/";
}
}
diff --git a/integration-tests/xchange/src/test/java/org/apache/camel/quarkus/component/xchange/it/XchangeTestResourceBase.java b/integration-tests/xchange/src/test/java/org/apache/camel/quarkus/component/xchange/it/XchangeTestResourceBase.java
new file mode 100644
index 0000000000..2f5155e3c1
--- /dev/null
+++ b/integration-tests/xchange/src/test/java/org/apache/camel/quarkus/component/xchange/it/XchangeTestResourceBase.java
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.camel.quarkus.component.xchange.it;
+
+import java.util.Map;
+
+import io.restassured.RestAssured;
+import io.restassured.path.json.JsonPath;
+import org.apache.camel.quarkus.test.mock.backend.MockBackendUtils;
+import org.apache.camel.quarkus.test.wiremock.WireMockTestResourceLifecycleManager;
+
+public abstract class XchangeTestResourceBase extends WireMockTestResourceLifecycleManager {
+ private boolean mockingEnabled = true;
+
+ XchangeTestResourceBase() {
+ if (!MockBackendUtils.startMockBackend(false)) {
+ LOG.infof("Checking the status of the %s API", getCryptoExchangeName());
+ if (isCryptoApiAccessible()) {
+ LOG.infof("Real backend will be used for %s", getCryptoExchangeName());
+ this.mockingEnabled = false;
+ } else {
+ LOG.warnf("Falling back to mock backend for %s as %s is not accessible", getCryptoExchangeName(),
+ getRecordTargetBaseUrl());
+ }
+ }
+ }
+
+ @Override
+ public Map<String, String> start() {
+ Map<String, String> options = super.start();
+ String wireMockUrl = options.get("wiremock.url");
+ if (wireMockUrl != null) {
+ options.put(getWireMockUrlProperty(), wireMockUrl);
+ }
+ return options;
+ }
+
+ @Override
+ protected boolean isMockingEnabled() {
+ return mockingEnabled;
+ }
+
+ String getWireMockUrlProperty() {
+ return String.format("wiremock.%s.url", getCryptoExchangeName());
+ }
+
+ boolean isCryptoApiAccessible() {
+ try {
+ JsonPath body = RestAssured.get(getHealthEndpointUrl())
+ .then()
+ .statusCode(200)
+ .extract()
+ .body()
+ .jsonPath();
+ String status = body.getString(getHealthStatusField());
+ LOG.infof("Status of the %s API is %s", getCryptoExchangeName(), status);
+ return status != null && status.equals(getExpectedHealthStatus());
+ } catch (Exception e) {
+ LOG.warnf(e, "Failed to contact the %s API", getCryptoExchangeName());
+ return false;
+ }
+ }
+
+ /**
+ * Gets the name of the crypto exchange.
+ *
+ * @return The name of the crypto exchange
+ */
+ abstract String getCryptoExchangeName();
+
+ /**
+ * Gets the URL of the health endpoint for the crypto exchange API.
+ *
+ * @return The URL of the crypto exchange API health endpoint
+ */
+ abstract String getHealthEndpointUrl();
+
+ /**
+ * Gets the name of the JSON field containing the crypto exchange health status.
+ *
+ * @return The name of the JSON field containing the crypto exchange health status
+ */
+ abstract String getHealthStatusField();
+
+ /**
+ * Gets the expected value of the JSON field containing the crypto exchange health status.
+ *
+ * @return The expected value of the JSON field containing the crypto exchange health status
+ */
+ abstract String getExpectedHealthStatus();
+}