This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 4087a79f3f1 camel-lra: provide additional unit tests (#11809)
4087a79f3f1 is described below

commit 4087a79f3f195fa9770bc40cf9f6bcbb6d36061d
Author: Johannes Boßle <[email protected]>
AuthorDate: Mon Oct 23 14:41:15 2023 +0200

    camel-lra: provide additional unit tests (#11809)
    
    * apply formatting
    
    * add more unit-tests for camel-lra
    
    * apply changes from review
    
    ---------
    
    Co-authored-by: Johannes Boßle <[email protected]>
---
 .../apache/camel/service/lra/LRASagaService.java   |   1 -
 .../apache/camel/service/lra/LRAClientTest.java    |  74 ++++++++++++++
 .../camel/service/lra/LRASagaCoordinatorTest.java  | 113 +++++++++++++++++++++
 .../camel/service/lra/LRASagaServiceTest.java      |  35 +++++++
 4 files changed, 222 insertions(+), 1 deletion(-)

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 67bfdf64ffd..1370f640638 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
@@ -66,7 +66,6 @@ public class LRASagaService extends ServiceSupport implements 
StaticService, Cam
                 .thenApply(url -> new LRASagaCoordinator(LRASagaService.this, 
url));
     }
 
-
     @Override
     public CompletableFuture<CamelSagaCoordinator> getSaga(String id) {
         CompletableFuture<CamelSagaCoordinator> coordinator;
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
index 647c1e91fe4..5021c3a7309 100644
--- 
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
@@ -16,12 +16,18 @@
  */
 package org.apache.camel.service.lra;
 
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
 import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
 
+import org.apache.camel.Exchange;
 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;
+import org.mockito.Mockito;
 
 public class LRAClientTest extends CamelTestSupport {
 
@@ -56,6 +62,63 @@ public class LRAClientTest extends CamelTestSupport {
                 "no client should result in IllegalArgumentException");
     }
 
+    @DisplayName("Tests whether LRAClient is calling prepareRequest with 
exchange from newLRA()")
+    @Test
+    void testCallsPrepareRequestWithExchangeInNewLra() {
+        LRASagaService sagaService = new LRASagaService();
+        applyMockProperties(sagaService);
+        LRAClient client = new LRAClient(sagaService) {
+            protected HttpRequest.Builder prepareRequest(URI uri, Exchange 
exchange) {
+                throw new ExchangeRuntimeException(exchange);
+            }
+        };
+        Exchange exchange = Mockito.mock(Exchange.class);
+        Assertions.assertThrows(ExchangeRuntimeException.class, () -> 
client.newLRA(exchange));
+    }
+
+    @DisplayName("Tests whether LRAClient is calling prepareRequest with 
exchange from compensate()")
+    @Test
+    void testCallsPrepareRequestWithExchangeInCompensate() throws 
MalformedURLException {
+        LRASagaService sagaService = new LRASagaService();
+        applyMockProperties(sagaService);
+        LRAClient client = new LRAClient(sagaService) {
+            protected HttpRequest.Builder prepareRequest(URI uri, Exchange 
exchange) {
+                throw new ExchangeRuntimeException(exchange);
+            }
+        };
+        Exchange exchange = Mockito.mock(Exchange.class);
+        Assertions.assertThrows(ExchangeRuntimeException.class,
+                () -> client.compensate(new URL("https://localhost/saga";), 
exchange));
+    }
+
+    @DisplayName("Tests whether LRAClient is calling prepareRequest with 
exchange from complete()")
+    @Test
+    void testCallsPrepareRequestWithExchangeInComplete() throws 
MalformedURLException {
+        LRASagaService sagaService = new LRASagaService();
+        applyMockProperties(sagaService);
+        LRAClient client = new LRAClient(sagaService) {
+            protected HttpRequest.Builder prepareRequest(URI uri, Exchange 
exchange) {
+                throw new ExchangeRuntimeException(exchange);
+            }
+        };
+        Exchange exchange = Mockito.mock(Exchange.class);
+        Assertions.assertThrows(ExchangeRuntimeException.class,
+                () -> client.complete(new URL("https://localhost/saga";), 
exchange));
+    }
+
+    @DisplayName("Tests prepare request works without exchange")
+    @Test
+    void testPrepareRequestWithoutExchange() throws Exception {
+        LRASagaService sagaService = new LRASagaService();
+        applyMockProperties(sagaService);
+        LRAClient client = new LRAClient(sagaService);
+        URI uri = new URI("https://lcoalhost/someURI";);
+        HttpRequest.Builder expected = HttpRequest.newBuilder(uri);
+        HttpRequest.Builder actual = client.prepareRequest(uri, null);
+
+        Assertions.assertEquals(actual.build(), expected.build());
+    }
+
     private void applyMockProperties(LRASagaService sagaService) {
         sagaService.setCoordinatorUrl("mockCoordinatorUrl");
         sagaService.setLocalParticipantUrl("mockLocalParticipantUrl");
@@ -63,4 +126,15 @@ public class LRAClientTest extends CamelTestSupport {
         sagaService.setCoordinatorContextPath("mockCoordinatorContextPath");
     }
 
+    private static class ExchangeRuntimeException extends RuntimeException {
+        private Exchange exchange;
+
+        public ExchangeRuntimeException(Exchange exchange) {
+            this.exchange = exchange;
+        }
+
+        public Exchange get() {
+            return exchange;
+        }
+    }
 }
diff --git 
a/components/camel-lra/src/test/java/org/apache/camel/service/lra/LRASagaCoordinatorTest.java
 
b/components/camel-lra/src/test/java/org/apache/camel/service/lra/LRASagaCoordinatorTest.java
new file mode 100644
index 00000000000..66a41d61bfc
--- /dev/null
+++ 
b/components/camel-lra/src/test/java/org/apache/camel/service/lra/LRASagaCoordinatorTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.URL;
+import java.util.Collections;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.saga.CamelSagaStep;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+public class LRASagaCoordinatorTest extends CamelTestSupport {
+
+    private URL url;
+
+    private LRASagaService sagaService;
+
+    private LRAClient client;
+
+    private Exchange exchange;
+
+    private LRASagaCoordinator coordinator;
+
+    @BeforeEach
+    public void setup() throws Exception {
+        url = new URL("https://localhost/saga";);
+        sagaService = Mockito.mock(LRASagaService.class);
+        client = Mockito.mock(LRAClient.class);
+        Mockito.when(sagaService.getClient()).thenReturn(client);
+        exchange = Mockito.mock(Exchange.class);
+
+        coordinator = new LRASagaCoordinator(sagaService, url);
+    }
+
+    public LRASagaCoordinatorTest() {
+        setUseRouteBuilder(false);
+    }
+
+    @DisplayName("Tests whether no sagaService is causing exception")
+    @Test
+    void testSagaServiceIsNotNull() throws Exception {
+        Assertions.assertThrows(IllegalArgumentException.class, () -> new 
LRASagaCoordinator(null, url));
+    }
+
+    @DisplayName("Tests whether no sagaService is causing exception")
+    @Test
+    void testUrlIsNotNull() throws Exception {
+        Assertions.assertThrows(IllegalArgumentException.class, () -> new 
LRASagaCoordinator(sagaService, null));
+    }
+
+    @DisplayName("Tests whether join is called on LRAClient")
+    @Test
+    void testBeginStep() throws Exception {
+        CamelSagaStep step = new CamelSagaStep(Optional.empty(), 
Optional.empty(), Collections.emptyMap(), Optional.empty());
+
+        CompletableFuture<Void> expected = 
CompletableFuture.completedFuture(null);
+        Mockito.when(client.join(Mockito.eq(url), 
Mockito.any(LRASagaStep.class), Mockito.eq(exchange))).thenReturn(expected);
+
+        CompletableFuture<Void> actual = coordinator.beginStep(exchange, step);
+        Assertions.assertSame(expected, actual);
+
+        Mockito.verify(sagaService).getClient();
+        Mockito.verify(client).join(Mockito.eq(url), 
Mockito.any(LRASagaStep.class), Mockito.eq(exchange));
+    }
+
+    @DisplayName("Tests whether complete is called on LRAClient")
+    @Test
+    void testComplete() throws Exception {
+        CompletableFuture<Void> expected = 
CompletableFuture.completedFuture(null);
+        Mockito.when(client.complete(url, exchange)).thenReturn(expected);
+
+        CompletableFuture<Void> actual = coordinator.complete(exchange);
+
+        Assertions.assertSame(expected, actual);
+        Mockito.verify(sagaService).getClient();
+        Mockito.verify(client).complete(url, exchange);
+    }
+
+    @DisplayName("Tests whether compensate is called on LRAClient")
+    @Test
+    void testCompensate() throws Exception {
+        CompletableFuture<Void> expected = 
CompletableFuture.completedFuture(null);
+        Mockito.when(client.compensate(url, exchange)).thenReturn(expected);
+
+        CompletableFuture<Void> actual = coordinator.compensate(exchange);
+
+        Assertions.assertSame(expected, actual);
+        Mockito.verify(sagaService).getClient();
+        Mockito.verify(client).compensate(url, exchange);
+    }
+
+}
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
index 80b398b424a..a41c1ba5a6e 100644
--- 
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
@@ -16,10 +16,15 @@
  */
 package org.apache.camel.service.lra;
 
+import java.net.URL;
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.camel.Exchange;
 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;
+import org.mockito.Mockito;
 
 public class LRASagaServiceTest extends CamelTestSupport {
 
@@ -53,6 +58,23 @@ public class LRASagaServiceTest extends CamelTestSupport {
         Assertions.assertInstanceOf(AlternativeLRAClient.class, client, 
"client must be an instance of AlternativeLRAClient");
     }
 
+    @DisplayName("Tests whether newSaga(Exchange) is passing on Exchange to 
the client")
+    @Test
+    void testCallsClientWithExchange() throws Exception {
+        LRAClient client = Mockito.mock(LRAClient.class);
+        LRASagaService sagaService = new ClientMockingLRASagaService(client);
+        applyMockProperties(sagaService);
+        sagaService.setCamelContext(this.context());
+        sagaService.doStart();
+
+        Exchange exchange = Mockito.mock(Exchange.class);
+
+        CompletableFuture<URL> expected = CompletableFuture.failedFuture(new 
RuntimeException("failed"));
+        Mockito.when(client.newLRA(exchange)).thenReturn(expected);
+        sagaService.newSaga(exchange);
+        Mockito.verify(client).newLRA(exchange);
+    }
+
     private void applyMockProperties(LRASagaService sagaService) {
         sagaService.setCoordinatorUrl("mockCoordinatorUrl");
         sagaService.setLocalParticipantUrl("mockLocalParticipantUrl");
@@ -72,4 +94,17 @@ public class LRASagaServiceTest extends CamelTestSupport {
         }
     }
 
+    private class ClientMockingLRASagaService extends LRASagaService {
+
+        private final LRAClient client;
+
+        public ClientMockingLRASagaService(LRAClient client) {
+            this.client = client;
+        }
+
+        protected LRAClient createLRAClient() {
+            return client;
+        }
+    }
+
 }

Reply via email to