This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.x by this push:
new feff82f8c47 Add possibility to override LRAClient (#10505)
feff82f8c47 is described below
commit feff82f8c47ee032a1cca1f661295b85bfd205cf
Author: Johannes Boßle <[email protected]>
AuthorDate: Tue Jun 27 20:12:32 2023 +0200
Add possibility to override LRAClient (#10505)
* Add possibility to override LRAClient
* [CAMEL-19508] add a common prepareRequest method
* [CAMEL-19508] ensure default headers remain as is
---------
Co-authored-by: Johannes Boßle <[email protected]>
---
.../org/apache/camel/service/lra/LRAClient.java | 37 ++++++-----
.../apache/camel/service/lra/LRASagaService.java | 12 +++-
.../apache/camel/service/lra/LRAClientTest.java | 66 +++++++++++++++++++
.../camel/service/lra/LRASagaServiceTest.java | 75 ++++++++++++++++++++++
4 files changed, 174 insertions(+), 16 deletions(-)
diff --git
a/components/camel-lra/src/main/java/org/apache/camel/service/lra/LRAClient.java
b/components/camel-lra/src/main/java/org/apache/camel/service/lra/LRAClient.java
index 0c91d514a3b..77833c16264 100644
---
a/components/camel-lra/src/main/java/org/apache/camel/service/lra/LRAClient.java
+++
b/components/camel-lra/src/main/java/org/apache/camel/service/lra/LRAClient.java
@@ -47,9 +47,16 @@ public class LRAClient implements Closeable {
private final String lraUrl;
public LRAClient(LRASagaService sagaService) {
- this.sagaService = sagaService;
+ this(sagaService, HttpClient.newHttpClient());
+ }
- client = HttpClient.newHttpClient();
+ public LRAClient(LRASagaService sagaService, HttpClient client) {
+ if (client == null) {
+ throw new IllegalArgumentException("HttpClient must not be null");
+ }
+
+ this.sagaService = sagaService;
+ this.client = client;
lraUrl = new LRAUrlBuilder()
.host(sagaService.getCoordinatorUrl())
@@ -58,8 +65,7 @@ public class LRAClient implements Closeable {
}
public CompletableFuture<URL> newLRA() {
- HttpRequest request = HttpRequest.newBuilder()
- .uri(URI.create(lraUrl + COORDINATOR_PATH_START))
+ HttpRequest request = prepareRequest(URI.create(lraUrl +
COORDINATOR_PATH_START))
.POST(HttpRequest.BodyPublishers.ofString(""))
.build();
@@ -109,11 +115,10 @@ public class LRAClient implements Closeable {
if (step.getTimeoutInMilliseconds().isPresent()) {
lraEndpoint = lraEndpoint + "?" + HEADER_TIME_LIMIT + "=" +
step.getTimeoutInMilliseconds().get();
}
- HttpRequest request = HttpRequest.newBuilder()
- .uri(URI.create(lraEndpoint))
- .header(HEADER_LINK, link.toString())
- .header(Exchange.SAGA_LONG_RUNNING_ACTION, lra.toString())
- .header("Content-Type", "text/plain")
+ HttpRequest request = prepareRequest(URI.create(lraEndpoint))
+ .setHeader(HEADER_LINK, link.toString())
+ .setHeader(Exchange.SAGA_LONG_RUNNING_ACTION,
lra.toString())
+ .setHeader("Content-Type", "text/plain")
.PUT(HttpRequest.BodyPublishers.ofString(link.toString()))
.build();
@@ -130,9 +135,8 @@ public class LRAClient implements Closeable {
}
public CompletableFuture<Void> complete(URL lra) {
- HttpRequest request = HttpRequest.newBuilder()
- .uri(URI.create(lra.toString() + COORDINATOR_PATH_CLOSE))
- .header("Content-Type", "text/plain")
+ HttpRequest request = prepareRequest(URI.create(lra.toString() +
COORDINATOR_PATH_CLOSE))
+ .setHeader("Content-Type", "text/plain")
.PUT(HttpRequest.BodyPublishers.ofString(""))
.build();
@@ -148,9 +152,8 @@ public class LRAClient implements Closeable {
}
public CompletableFuture<Void> compensate(URL lra) {
- HttpRequest request = HttpRequest.newBuilder()
- .uri(URI.create(lra.toString() + COORDINATOR_PATH_CANCEL))
- .header("Content-Type", "text/plain")
+ HttpRequest request = prepareRequest(URI.create(lra.toString() +
COORDINATOR_PATH_CANCEL))
+ .setHeader("Content-Type", "text/plain")
.PUT(HttpRequest.BodyPublishers.ofString(""))
.build();
@@ -165,6 +168,10 @@ public class LRAClient implements Closeable {
});
}
+ protected HttpRequest.Builder prepareRequest(URI uri) {
+ return HttpRequest.newBuilder().uri(uri);
+ }
+
private URL toURL(Object url) {
if (url == null) {
return null;
diff --git
a/components/camel-lra/src/main/java/org/apache/camel/service/lra/LRASagaService.java
b/components/camel-lra/src/main/java/org/apache/camel/service/lra/LRASagaService.java
index de6870fbc30..9e66c82a75a 100644
---
a/components/camel-lra/src/main/java/org/apache/camel/service/lra/LRASagaService.java
+++
b/components/camel-lra/src/main/java/org/apache/camel/service/lra/LRASagaService.java
@@ -94,10 +94,20 @@ public class LRASagaService extends ServiceSupport
implements StaticService, Cam
.newDefaultScheduledThreadPool(this, "saga-lra");
}
if (this.client == null) {
- this.client = new LRAClient(this);
+ this.client = createLRAClient();
}
}
+ /**
+ * Use this method to override some behavior within the LRAClient
+ *
+ * @return the LRAClient to be used within the LRASagaService
+ *
+ */
+ protected LRAClient createLRAClient() {
+ return new LRAClient(this);
+ }
+
@Override
protected void doStop() throws Exception {
if (this.executorService != null) {
diff --git
a/components/camel-lra/src/test/java/org/apache/camel/service/lra/LRAClientTest.java
b/components/camel-lra/src/test/java/org/apache/camel/service/lra/LRAClientTest.java
new file mode 100644
index 00000000000..647c1e91fe4
--- /dev/null
+++
b/components/camel-lra/src/test/java/org/apache/camel/service/lra/LRAClientTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.service.lra;
+
+import java.net.http.HttpClient;
+
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+public class LRAClientTest extends CamelTestSupport {
+
+ public LRAClientTest() {
+ setUseRouteBuilder(false);
+ }
+
+ @DisplayName("Tests whether LRAClient is using a default HttpClient")
+ @Test
+ void testCanCreateLRAClient() throws Exception {
+ LRASagaService sagaService = new LRASagaService();
+ applyMockProperties(sagaService);
+ LRAClient client = new LRAClient(sagaService);
+ Assertions.assertNotNull(client, "client must not be null after
initializing");
+ }
+
+ @DisplayName("Tests whether LRAClient is using a custom set HttpClient")
+ @Test
+ void testCanCreateLRAClientWithCustomHttpClient() throws Exception {
+ LRASagaService sagaService = new LRASagaService();
+ applyMockProperties(sagaService);
+ LRAClient client = new LRAClient(sagaService,
HttpClient.newBuilder().build());
+ Assertions.assertNotNull(client, "client must not be null after
initializing");
+ }
+
+ @DisplayName("Tests whether LRAClient is throwing an exception if
httpclient is null")
+ @Test
+ void testCannotCreateLRAClientWithoutHttpClient() throws Exception {
+ LRASagaService sagaService = new LRASagaService();
+ applyMockProperties(sagaService);
+ Assertions.assertThrows(IllegalArgumentException.class, () -> new
LRAClient(sagaService, null),
+ "no client should result in IllegalArgumentException");
+ }
+
+ private void applyMockProperties(LRASagaService sagaService) {
+ sagaService.setCoordinatorUrl("mockCoordinatorUrl");
+ sagaService.setLocalParticipantUrl("mockLocalParticipantUrl");
+
sagaService.setLocalParticipantContextPath("mockLocalParticipantContextPath");
+ sagaService.setCoordinatorContextPath("mockCoordinatorContextPath");
+ }
+
+}
diff --git
a/components/camel-lra/src/test/java/org/apache/camel/service/lra/LRASagaServiceTest.java
b/components/camel-lra/src/test/java/org/apache/camel/service/lra/LRASagaServiceTest.java
new file mode 100644
index 00000000000..80b398b424a
--- /dev/null
+++
b/components/camel-lra/src/test/java/org/apache/camel/service/lra/LRASagaServiceTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.service.lra;
+
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+public class LRASagaServiceTest extends CamelTestSupport {
+
+ public LRASagaServiceTest() {
+ setUseRouteBuilder(false);
+ }
+
+ @DisplayName("Tests whether doStart() is creating a LRAClient")
+ @Test
+ void testCanCreateLRAClient() throws Exception {
+ LRASagaService sagaService = new LRASagaService();
+ applyMockProperties(sagaService);
+ sagaService.setCamelContext(this.context());
+ sagaService.doStart();
+
+ LRAClient client = sagaService.getClient();
+ Assertions.assertNotNull(client, "lraClient must not be null");
+ }
+
+ @DisplayName("Tests whether doStart() is creating an alternative
LRAClient")
+ @Test
+ void testCanCreateAlternativeLRAClient() throws Exception {
+ LRASagaService sagaService = new AlternativeLRASagaService();
+ applyMockProperties(sagaService);
+ sagaService.setCamelContext(this.context());
+ sagaService.doStart();
+
+ LRAClient client = sagaService.getClient();
+ Assertions.assertNotNull(client, "lraClient must not be null");
+
+ Assertions.assertInstanceOf(AlternativeLRAClient.class, client,
"client must be an instance of AlternativeLRAClient");
+ }
+
+ private void applyMockProperties(LRASagaService sagaService) {
+ sagaService.setCoordinatorUrl("mockCoordinatorUrl");
+ sagaService.setLocalParticipantUrl("mockLocalParticipantUrl");
+
sagaService.setLocalParticipantContextPath("mockLocalParticipantContextPath");
+ sagaService.setCoordinatorContextPath("mockCoordinatorContextPath");
+ }
+
+ private class AlternativeLRASagaService extends LRASagaService {
+ protected LRAClient createLRAClient() {
+ return new AlternativeLRAClient(this);
+ }
+ }
+
+ private class AlternativeLRAClient extends LRAClient {
+ public AlternativeLRAClient(LRASagaService sagaService) {
+ super(sagaService);
+ }
+ }
+
+}