You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by ch...@apache.org on 2022/10/23 14:40:26 UTC

[incubator-eventmesh] branch rabbitmq-connector updated: add rabbitmq connector unit test

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

chenguangsheng pushed a commit to branch rabbitmq-connector
in repository https://gitbox.apache.org/repos/asf/incubator-eventmesh.git


The following commit(s) were added to refs/heads/rabbitmq-connector by this push:
     new 0361fc0c add rabbitmq connector unit test
     new 845371a4 Merge pull request #1773 from mroccyen/rabbitmq-unit-test
0361fc0c is described below

commit 0361fc0c99a280d042c987147fd4cd2550513cbe
Author: mroccyen <qi...@126.com>
AuthorDate: Sun Oct 23 21:06:54 2022 +0800

    add rabbitmq connector unit test
---
 .../rabbitmq/RabbitmqMockConnectionFactory.java    | 59 ++++++++++++++++
 .../connector/rabbitmq/RabbitmqServer.java         | 52 ++++++++++++++
 .../rabbitmq/consumer/RabbitmqConsumerTest.java    | 81 ++++++++++++++++++++++
 .../rabbitmq/producer/RabbitmqProducerTest.java    | 81 ++++++++++++++++++++++
 .../src/test/resources/rabbitmq-client.properties  | 31 +++++++++
 5 files changed, 304 insertions(+)

diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/RabbitmqMockConnectionFactory.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/RabbitmqMockConnectionFactory.java
new file mode 100644
index 00000000..912e825b
--- /dev/null
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/RabbitmqMockConnectionFactory.java
@@ -0,0 +1,59 @@
+/*
+ * 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.eventmesh.connector.rabbitmq;
+
+import org.apache.eventmesh.connector.rabbitmq.client.RabbitmqConnectionFactory;
+
+import com.github.fridujo.rabbitmq.mock.MockConnectionFactory;
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.Connection;
+import com.rabbitmq.client.ConnectionFactory;
+
+public class RabbitmqMockConnectionFactory extends RabbitmqConnectionFactory {
+
+    private final ConnectionFactory myConnectionFactory;
+
+    private final Connection myConnection;
+
+    private final Channel myChannel;
+
+    public RabbitmqMockConnectionFactory() throws Exception {
+        this.myConnectionFactory = new MockConnectionFactory();
+        this.myConnectionFactory.setHost("127.0.0.1");
+        this.myConnectionFactory.setPort(5672);
+        this.myConnectionFactory.setUsername("root");
+        this.myConnectionFactory.setPassword("123456");
+        this.myConnection = this.myConnectionFactory.newConnection();
+        this.myChannel = myConnection.createChannel();
+    }
+
+    @Override
+    public ConnectionFactory createConnectionFactory() {
+        return this.myConnectionFactory;
+    }
+
+    @Override
+    public Connection createConnection(ConnectionFactory connectionFactory) {
+        return this.myConnection;
+    }
+
+    @Override
+    public Channel createChannel(Connection connection) {
+        return this.myChannel;
+    }
+}
diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/RabbitmqServer.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/RabbitmqServer.java
new file mode 100644
index 00000000..a6287339
--- /dev/null
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/RabbitmqServer.java
@@ -0,0 +1,52 @@
+/*
+ * 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.eventmesh.connector.rabbitmq;
+
+import org.apache.eventmesh.connector.rabbitmq.consumer.RabbitmqConsumer;
+import org.apache.eventmesh.connector.rabbitmq.producer.RabbitmqProducer;
+
+import java.util.Properties;
+
+import org.junit.After;
+import org.junit.Before;
+
+public class RabbitmqServer {
+    protected RabbitmqConsumer rabbitmqConsumer;
+    protected RabbitmqProducer rabbitmqProducer;
+
+    @Before
+    public void setup() throws Exception {
+        RabbitmqMockConnectionFactory rabbitmqMockConnectionFactory = new RabbitmqMockConnectionFactory();
+
+        rabbitmqConsumer = new RabbitmqConsumer();
+        rabbitmqConsumer.setRabbitmqConnectionFactory(rabbitmqMockConnectionFactory);
+        rabbitmqConsumer.init(new Properties());
+        rabbitmqConsumer.start();
+
+        rabbitmqProducer = new RabbitmqProducer();
+        rabbitmqProducer.setRabbitmqConnectionFactory(rabbitmqMockConnectionFactory);
+        rabbitmqProducer.init(new Properties());
+        rabbitmqProducer.start();
+    }
+
+    @After
+    public void shutdown() {
+        rabbitmqConsumer.shutdown();
+        rabbitmqProducer.shutdown();
+    }
+}
diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/consumer/RabbitmqConsumerTest.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/consumer/RabbitmqConsumerTest.java
new file mode 100644
index 00000000..a5fd4a87
--- /dev/null
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/consumer/RabbitmqConsumerTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.eventmesh.connector.rabbitmq.consumer;
+
+import org.apache.eventmesh.api.EventMeshAction;
+import org.apache.eventmesh.api.SendCallback;
+import org.apache.eventmesh.api.SendResult;
+import org.apache.eventmesh.api.exception.OnExceptionContext;
+import org.apache.eventmesh.connector.rabbitmq.RabbitmqServer;
+
+import java.net.URI;
+import java.time.OffsetDateTime;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.cloudevents.CloudEvent;
+import io.cloudevents.core.builder.CloudEventBuilder;
+
+public class RabbitmqConsumerTest extends RabbitmqServer {
+
+    @Test
+    public void subscribe() throws Exception {
+        final int expectedCount = 5;
+        final CountDownLatch downLatch = new CountDownLatch(expectedCount);
+
+        rabbitmqConsumer.registerEventListener((cloudEvent, context) -> {
+            downLatch.countDown();
+            context.commit(EventMeshAction.CommitMessage);
+            Assert.assertEquals(cloudEvent.getSubject(), "topic");
+        });
+
+        rabbitmqConsumer.subscribe("topic");
+
+        Thread.sleep(1000);
+        for (int i = 0; i < expectedCount; i++) {
+            CloudEvent cloudEvent = CloudEventBuilder.v1()
+                    .withId(String.valueOf(i))
+                    .withTime(OffsetDateTime.now())
+                    .withSource(URI.create("testsource"))
+                    .withSubject("topic")
+                    .withType(String.class.getCanonicalName())
+                    .withDataContentType("text/plain")
+                    .withData("data".getBytes())
+                    .build();
+
+            rabbitmqProducer.publish(cloudEvent, new SendCallback() {
+                @Override
+                public void onSuccess(SendResult sendResult) {
+                    Assert.assertEquals(cloudEvent.getId(), sendResult.getMessageId());
+                    Assert.assertEquals(cloudEvent.getSubject(), sendResult.getTopic());
+                }
+
+                @Override
+                public void onException(OnExceptionContext context) {
+                    Assert.assertEquals(cloudEvent.getId(), context.getMessageId());
+                    Assert.assertEquals(cloudEvent.getSubject(), context.getTopic());
+                }
+            });
+        }
+
+        Assert.assertTrue(downLatch.await(5, TimeUnit.MINUTES));
+    }
+}
diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/producer/RabbitmqProducerTest.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/producer/RabbitmqProducerTest.java
new file mode 100644
index 00000000..f38fd5e1
--- /dev/null
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/producer/RabbitmqProducerTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.eventmesh.connector.rabbitmq.producer;
+
+import org.apache.eventmesh.api.EventMeshAction;
+import org.apache.eventmesh.api.SendCallback;
+import org.apache.eventmesh.api.SendResult;
+import org.apache.eventmesh.api.exception.OnExceptionContext;
+import org.apache.eventmesh.connector.rabbitmq.RabbitmqServer;
+
+import java.net.URI;
+import java.time.OffsetDateTime;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.cloudevents.CloudEvent;
+import io.cloudevents.core.builder.CloudEventBuilder;
+
+public class RabbitmqProducerTest extends RabbitmqServer {
+
+    @Test
+    public void publish() throws Exception {
+        final int expectedCount = 5;
+        final CountDownLatch downLatch = new CountDownLatch(expectedCount);
+
+        rabbitmqConsumer.registerEventListener((cloudEvent, context) -> {
+            downLatch.countDown();
+            context.commit(EventMeshAction.CommitMessage);
+            Assert.assertEquals(cloudEvent.getSubject(), "topic");
+        });
+
+        rabbitmqConsumer.subscribe("topic");
+
+        Thread.sleep(1000);
+        for (int i = 0; i < expectedCount; i++) {
+            CloudEvent cloudEvent = CloudEventBuilder.v1()
+                    .withId(String.valueOf(i))
+                    .withTime(OffsetDateTime.now())
+                    .withSource(URI.create("testsource"))
+                    .withSubject("topic")
+                    .withType(String.class.getCanonicalName())
+                    .withDataContentType("text/plain")
+                    .withData("data".getBytes())
+                    .build();
+
+            rabbitmqProducer.publish(cloudEvent, new SendCallback() {
+                @Override
+                public void onSuccess(SendResult sendResult) {
+                    Assert.assertEquals(cloudEvent.getId(), sendResult.getMessageId());
+                    Assert.assertEquals(cloudEvent.getSubject(), sendResult.getTopic());
+                }
+
+                @Override
+                public void onException(OnExceptionContext context) {
+                    Assert.assertEquals(cloudEvent.getId(), context.getMessageId());
+                    Assert.assertEquals(cloudEvent.getSubject(), context.getTopic());
+                }
+            });
+        }
+
+        Assert.assertTrue(downLatch.await(5, TimeUnit.MINUTES));
+    }
+}
diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/resources/rabbitmq-client.properties b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/resources/rabbitmq-client.properties
new file mode 100644
index 00000000..b24967fa
--- /dev/null
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/resources/rabbitmq-client.properties
@@ -0,0 +1,31 @@
+#
+# 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.
+#
+
+####################### rabbitmq server ##################
+eventMesh.server.rabbitmq.host=127.0.0.1
+eventMesh.server.rabbitmq.port=5672
+eventMesh.server.rabbitmq.username=root
+eventMesh.server.rabbitmq.passwd=123456
+eventMesh.server.rabbitmq.virtualHost=test
+
+####################### rabbitmq queue setting ##################
+# DIRECT, FANOUT, TOPIC, HEADERS
+eventMesh.server.rabbitmq.exchangeType=TOPIC
+eventMesh.server.rabbitmq.exchangeName=test
+eventMesh.server.rabbitmq.routingKey=test
+eventMesh.server.rabbitmq.queueName=test
+eventMesh.server.rabbitmq.autoAck=true
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org