You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by bv...@apache.org on 2022/12/17 21:02:01 UTC

[camel] 01/01: cleanup tests after enabling by the previous commit

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

bvahdat pushed a commit to branch itest-cleanup
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 9079d26e63a24246c9a3fb6cd65d953fe3433c06
Author: Babak Vahdat <bv...@apache.org>
AuthorDate: Sat Dec 17 22:01:31 2022 +0100

    cleanup tests after enabling by the previous commit
---
 .../FromJmsToJdbcIdempotentConsumerToJmsTest.java  | 132 +++++++++------------
 ...FromJmsToJdbcIdempotentConsumerToJmsXaTest.java |   2 +-
 2 files changed, 56 insertions(+), 78 deletions(-)

diff --git a/tests/camel-itest/src/test/java/org/apache/camel/itest/sql/FromJmsToJdbcIdempotentConsumerToJmsTest.java b/tests/camel-itest/src/test/java/org/apache/camel/itest/sql/FromJmsToJdbcIdempotentConsumerToJmsTest.java
index 37ee580a563..7d87cacee30 100644
--- a/tests/camel-itest/src/test/java/org/apache/camel/itest/sql/FromJmsToJdbcIdempotentConsumerToJmsTest.java
+++ b/tests/camel-itest/src/test/java/org/apache/camel/itest/sql/FromJmsToJdbcIdempotentConsumerToJmsTest.java
@@ -17,10 +17,10 @@
 package org.apache.camel.itest.sql;
 
 import java.net.ConnectException;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.sql.DataSource;
 
+import org.apache.camel.EndpointInject;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.builder.NotifyBuilder;
@@ -40,12 +40,17 @@ import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
- * Jms with JDBC idempotent consumer test.
+ * JMS with JDBC idempotent consumer test.
  */
 public class FromJmsToJdbcIdempotentConsumerToJmsTest extends CamelSpringTestSupport {
 
-    protected JdbcTemplate jdbcTemplate;
-    protected DataSource dataSource;
+    private JdbcTemplate jdbcTemplate;
+
+    @EndpointInject("mock:a")
+    private MockEndpoint mockA;
+
+    @EndpointInject("mock:b")
+    private MockEndpoint mockB;
 
     @Override
     protected AbstractApplicationContext createApplicationContext() {
@@ -58,13 +63,9 @@ public class FromJmsToJdbcIdempotentConsumerToJmsTest extends CamelSpringTestSup
     public void setUp() throws Exception {
         super.setUp();
 
-        dataSource = context.getRegistry().lookupByNameAndType(getDatasourceName(), DataSource.class);
+        DataSource dataSource = context.getRegistry().lookupByNameAndType(getDatasourceName(), DataSource.class);
         jdbcTemplate = new JdbcTemplate(dataSource);
         jdbcTemplate.afterPropertiesSet();
-
-        // cater for slow servers
-        getMockEndpoint("mock:a").setResultWaitTime(40000);
-        getMockEndpoint("mock:b").setResultWaitTime(40000);
     }
 
     protected String getDatasourceName() {
@@ -75,22 +76,19 @@ public class FromJmsToJdbcIdempotentConsumerToJmsTest extends CamelSpringTestSup
     void testJmsToJdbcJmsCommit() throws Exception {
         checkInitialState();
 
-        // use a notify to know when the message is done
-        NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();
+        mockA.expectedMessageCount(1);
+        mockB.expectedMessageCount(1);
 
-        // use mock during testing as well
-        getMockEndpoint("mock:a").expectedMessageCount(1);
-        getMockEndpoint("mock:b").expectedMessageCount(1);
+        // use NotifyBuilder to know when the message is done
+        NotifyBuilder notify
+                = new NotifyBuilder(context).whenExactlyCompleted(1).whenDoneSatisfied(mockA).whenDoneSatisfied(mockB).create();
 
         template.sendBodyAndHeader("activemq2:queue:inbox", "A", "uid", 123);
 
-        // assert mock and wait for the message to be done
-        MockEndpoint.assertIsSatisfied(context);
         assertTrue(notify.matchesWaitTime(), "Should complete 1 message");
 
         // check that there is a message in the database and JMS queue
-        assertEquals(Integer.valueOf(1),
-                jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
+        assertEquals(1, jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
         Object out = consumer.receiveBody("activemq2:queue:outbox", 3000);
         assertEquals("DONE-A", out);
     }
@@ -99,28 +97,22 @@ public class FromJmsToJdbcIdempotentConsumerToJmsTest extends CamelSpringTestSup
     void testJmsToJdbcJmsRollbackAtA() throws Exception {
         checkInitialState();
 
-        // use a notify to know that after 1+6 (1 original + 6 redelivery) attempts from AcitveMQ
-        NotifyBuilder notify = new NotifyBuilder(context).whenDone(7).create();
-
-        getMockEndpoint("mock:a").expectedMessageCount(7);
-        // force exception to occur at mock a
-        getMockEndpoint("mock:a").whenAnyExchangeReceived(new Processor() {
-            @Override
-            public void process(Exchange exchange) throws Exception {
-                throw new ConnectException("Forced cannot connect to database");
-            }
+        mockA.expectedMessageCount(7);
+        mockB.expectedMessageCount(0);
+        mockB.whenAnyExchangeReceived(exchange -> {
+            throw new ConnectException("Forced cannot connect to database");
         });
-        getMockEndpoint("mock:b").expectedMessageCount(0);
+
+        // use NotifyBuilder to know that after 1+6 (1 original + 6 redelivery) attempts from ActiveMQ
+        NotifyBuilder notify
+                = new NotifyBuilder(context).whenExactlyDone(7).whenDoneSatisfied(mockA).whenDoneSatisfied(mockB).create();
 
         template.sendBodyAndHeader("activemq2:queue:inbox", "A", "uid", 123);
 
-        // assert mock and wait for the message to be done
-        MockEndpoint.assertIsSatisfied(context);
         assertTrue(notify.matchesWaitTime(), "Should complete 7 message");
 
-        // check that there is a message in the database and JMS queue
-        assertEquals(Integer.valueOf(0),
-                jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
+        // check that there is no message in the database and JMS queue
+        assertEquals(0, jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
         assertNull(consumer.receiveBody("activemq2:queue:outbox", 3000));
 
         // the message should have been moved to the AMQ DLQ queue
@@ -131,28 +123,22 @@ public class FromJmsToJdbcIdempotentConsumerToJmsTest extends CamelSpringTestSup
     void testJmsToJdbcJmsRollbackAtB() throws Exception {
         checkInitialState();
 
-        // use a notify to know that after 1+6 (1 original + 6 redelivery) attempts from AcitveMQ
-        NotifyBuilder notify = new NotifyBuilder(context).whenDone(7).create();
-
-        getMockEndpoint("mock:a").expectedMessageCount(7);
-        getMockEndpoint("mock:b").expectedMessageCount(7);
-        // force exception to occur at mock b
-        getMockEndpoint("mock:b").whenAnyExchangeReceived(new Processor() {
-            @Override
-            public void process(Exchange exchange) throws Exception {
-                throw new ConnectException("Forced cannot send to AMQ queue");
-            }
+        mockA.expectedMessageCount(7);
+        mockB.expectedMessageCount(7);
+        mockB.whenAnyExchangeReceived(exchange -> {
+            throw new ConnectException("Forced cannot send to AMQ queue");
         });
 
+        // use NotifyBuilder to know that after 1+6 (1 original + 6 redelivery) attempts from ActiveMQ
+        NotifyBuilder notify
+                = new NotifyBuilder(context).whenExactlyDone(7).whenDoneSatisfied(mockA).whenDoneSatisfied(mockB).create();
+
         template.sendBodyAndHeader("activemq2:queue:inbox", "B", "uid", 456);
 
-        // assert mock and wait for the message to be done
-        MockEndpoint.assertIsSatisfied(context);
         assertTrue(notify.matchesWaitTime(), "Should complete 7 messages");
 
-        // check that there is a message in the database and JMS queue
-        assertEquals(Integer.valueOf(0),
-                jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
+        // check that there is no message in the database and JMS queue
+        assertEquals(0, jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
         assertNull(consumer.receiveBody("activemq2:queue:outbox", 3000));
 
         // the message should have been moved to the AMQ DLQ queue
@@ -163,25 +149,21 @@ public class FromJmsToJdbcIdempotentConsumerToJmsTest extends CamelSpringTestSup
     void testFilterIdempotent() throws Exception {
         checkInitialState();
 
-        // use a notify to know when the message is done
-        NotifyBuilder notify = new NotifyBuilder(context).whenDone(3).create();
+        mockA.expectedMessageCount(3);
+        mockB.expectedMessageCount(2);
 
-        // use mock during testing as well
-        getMockEndpoint("mock:a").expectedMessageCount(3);
-        // there should be 1 duplicate
-        getMockEndpoint("mock:b").expectedMessageCount(2);
+        // use NotifyBuilder to know when the message is done
+        NotifyBuilder notify
+                = new NotifyBuilder(context).whenExactlyDone(3).whenDoneSatisfied(mockA).whenDoneSatisfied(mockB).create();
 
         template.sendBodyAndHeader("activemq2:queue:inbox", "D", "uid", 111);
         template.sendBodyAndHeader("activemq2:queue:inbox", "E", "uid", 222);
         template.sendBodyAndHeader("activemq2:queue:inbox", "D", "uid", 111);
 
-        // assert mock and wait for the message to be done
-        MockEndpoint.assertIsSatisfied(context);
         assertTrue(notify.matchesWaitTime(), "Should complete 3 messages");
 
         // check that there is two messages in the database and JMS queue
-        assertEquals(Integer.valueOf(2),
-                jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
+        assertEquals(2, jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
         assertEquals("DONE-D", consumer.receiveBody("activemq2:queue:outbox", 3000));
         assertEquals("DONE-E", consumer.receiveBody("activemq2:queue:outbox", 3000));
     }
@@ -190,35 +172,32 @@ public class FromJmsToJdbcIdempotentConsumerToJmsTest extends CamelSpringTestSup
     void testRetryAfterException() throws Exception {
         checkInitialState();
 
-        final AtomicInteger counter = new AtomicInteger();
-
-        // use a notify to know when the message is done
-        NotifyBuilder notify = new NotifyBuilder(context).whenDone(4).create();
+        mockA.expectedMessageCount(4);
+        mockB.expectedMessageCount(4);
+        mockB.whenAnyExchangeReceived(new Processor() {
+            private boolean errorThrown;
 
-        // use mock during testing as well
-        getMockEndpoint("mock:a").expectedMessageCount(4);
-        // there should be 1 duplicate
-        getMockEndpoint("mock:b").expectedMessageCount(4);
-        getMockEndpoint("mock:b").whenAnyExchangeReceived(new Processor() {
             @Override
             public void process(Exchange exchange) throws Exception {
-                if (counter.getAndIncrement() == 1) {
+                if (!errorThrown) {
+                    errorThrown = true;
                     throw new ConnectException("Forced cannot send to AMQ queue");
                 }
             }
         });
 
+        // use NotifyBuilder to know when the message is done
+        NotifyBuilder notify
+                = new NotifyBuilder(context).whenExactlyDone(4).whenDoneSatisfied(mockA).whenDoneSatisfied(mockB).create();
+
         template.sendBodyAndHeader("activemq2:queue:inbox", "D", "uid", 111);
         template.sendBodyAndHeader("activemq2:queue:inbox", "E", "uid", 222);
         template.sendBodyAndHeader("activemq2:queue:inbox", "F", "uid", 333);
 
-        // assert mock and wait for the message to be done
-        MockEndpoint.assertIsSatisfied(context);
         assertTrue(notify.matchesWaitTime(), "Should complete 4 messages");
 
-        // check that there is two messages in the database and JMS queue
-        assertEquals(Integer.valueOf(3),
-                jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
+        // check that there is three messages in the database and JMS queue
+        assertEquals(3, jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
         assertEquals("DONE-D", consumer.receiveBody("activemq2:queue:outbox", 3000));
         assertEquals("DONE-E", consumer.receiveBody("activemq2:queue:outbox", 3000));
         assertEquals("DONE-F", consumer.receiveBody("activemq2:queue:outbox", 3000));
@@ -226,8 +205,7 @@ public class FromJmsToJdbcIdempotentConsumerToJmsTest extends CamelSpringTestSup
 
     protected void checkInitialState() {
         // check there are no messages in the database and JMS queue
-        assertEquals(Integer.valueOf(0),
-                jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
+        assertEquals(0, jdbcTemplate.queryForObject("select count(*) from CAMEL_MESSAGEPROCESSED", Integer.class));
         assertNull(consumer.receiveBody("activemq2:queue:outbox", 2000));
     }
 
diff --git a/tests/camel-itest/src/test/java/org/apache/camel/itest/sql/FromJmsToJdbcIdempotentConsumerToJmsXaTest.java b/tests/camel-itest/src/test/java/org/apache/camel/itest/sql/FromJmsToJdbcIdempotentConsumerToJmsXaTest.java
index e808cc185e5..c2d29030afb 100644
--- a/tests/camel-itest/src/test/java/org/apache/camel/itest/sql/FromJmsToJdbcIdempotentConsumerToJmsXaTest.java
+++ b/tests/camel-itest/src/test/java/org/apache/camel/itest/sql/FromJmsToJdbcIdempotentConsumerToJmsXaTest.java
@@ -30,7 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.fail;
 
 /**
- * Jms with JDBC idempotent consumer test using XA.
+ * JMS with JDBC idempotent consumer test using XA.
  */
 public class FromJmsToJdbcIdempotentConsumerToJmsXaTest extends FromJmsToJdbcIdempotentConsumerToJmsTest {