You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by mi...@apache.org on 2022/10/28 14:40:20 UTC
[incubator-eventmesh] branch rabbitmq-connector updated: refine unit test
This is an automated email from the ASF dual-hosted git repository.
mikexue 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 426f08bd refine unit test
new 40a22e88 Merge pull request #2000 from mroccyen/rabbitmq-connector-unit-test
426f08bd is described below
commit 426f08bd79207f0cb4af3fbd5121f0407f232615
Author: mroccyen <qi...@126.com>
AuthorDate: Fri Oct 28 16:33:58 2022 +0800
refine unit test
---
.../connector/rabbitmq/client/RabbitmqClient.java | 19 +-----
.../cloudevent/RabbitmqCloudEventTest.java | 69 ++++++++++++++++++++++
.../rabbitmq/consumer/RabbitmqConsumerTest.java | 10 ++++
.../rabbitmq/producer/RabbitmqProducerTest.java | 10 ++++
4 files changed, 90 insertions(+), 18 deletions(-)
diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/client/RabbitmqClient.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/client/RabbitmqClient.java
index f3b780af..d4a2f128 100644
--- a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/client/RabbitmqClient.java
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/client/RabbitmqClient.java
@@ -55,9 +55,8 @@ public class RabbitmqClient {
ConnectionFactory factory = rabbitmqConnectionFactory.createConnectionFactory();
factory.setHost(host.trim());
factory.setPort(port);
- virtualHost = virtualHost.trim().startsWith("/") ? virtualHost : "/" + virtualHost;
if (StringUtils.isNotEmpty(virtualHost)) {
- factory.setVirtualHost(virtualHost);
+ factory.setVirtualHost(virtualHost.trim());
}
factory.setUsername(username);
factory.setPassword(passwd.trim());
@@ -99,14 +98,6 @@ public class RabbitmqClient {
channel.queueBind(queueName, exchangeName, routingKey);
} catch (Exception ex) {
logger.error("[RabbitmqClient] binding happen exception.", ex);
- } finally {
- try {
- if (channel != null && channel.isOpen()) {
- channel.close();
- }
- } catch (Exception ex) {
- logger.error("[RabbitmqClient] binding channel close happen exception.", ex);
- }
}
}
@@ -123,14 +114,6 @@ public class RabbitmqClient {
channel.queueUnbind(queueName, exchangeName, routingKey);
} catch (Exception ex) {
logger.error("[RabbitmqClient] unbinding happen exception.", ex);
- } finally {
- try {
- if (channel != null && channel.isOpen()) {
- channel.close();
- }
- } catch (Exception ex) {
- logger.error("[RabbitmqClient] unbinding channel close happen exception.", ex);
- }
}
}
diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/cloudevent/RabbitmqCloudEventTest.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/cloudevent/RabbitmqCloudEventTest.java
new file mode 100644
index 00000000..99a7c284
--- /dev/null
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/test/java/org/apache/eventmesh/connector/rabbitmq/cloudevent/RabbitmqCloudEventTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.cloudevent;
+
+import java.net.URI;
+import java.time.OffsetDateTime;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import io.cloudevents.CloudEvent;
+import io.cloudevents.core.builder.CloudEventBuilder;
+
+public class RabbitmqCloudEventTest {
+
+ private CloudEvent cloudEvent;
+
+ @Before
+ public void before() {
+ cloudEvent = CloudEventBuilder.v1()
+ .withId("1")
+ .withTime(OffsetDateTime.now())
+ .withSource(URI.create("testsource"))
+ .withSubject("topic")
+ .withType(String.class.getCanonicalName())
+ .withDataContentType("text/plain")
+ .withData("data".getBytes())
+ .build();
+ }
+
+ @Test
+ public void toByteArray() throws Exception {
+ RabbitmqCloudEventWriter writer = new RabbitmqCloudEventWriter();
+ RabbitmqCloudEvent rabbitmqCloudEvent = writer.writeBinary(cloudEvent);
+ Assert.assertEquals(cloudEvent.getSubject(), "topic");
+
+ byte[] data = RabbitmqCloudEvent.toByteArray(rabbitmqCloudEvent);
+ Assert.assertNotNull(data);
+ }
+
+ @Test
+ public void getFromByteArray() throws Exception {
+ RabbitmqCloudEventWriter writer = new RabbitmqCloudEventWriter();
+ RabbitmqCloudEvent rabbitmqCloudEvent = writer.writeBinary(cloudEvent);
+ Assert.assertEquals(cloudEvent.getSubject(), "topic");
+
+ byte[] data = RabbitmqCloudEvent.toByteArray(rabbitmqCloudEvent);
+ Assert.assertNotNull(data);
+
+ RabbitmqCloudEvent event = RabbitmqCloudEvent.getFromByteArray(data);
+ Assert.assertEquals(event.getExtensions().get("subject"), "topic");
+ }
+}
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
index a5fd4a87..aba79a14 100644
--- 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
@@ -36,6 +36,16 @@ import io.cloudevents.core.builder.CloudEventBuilder;
public class RabbitmqConsumerTest extends RabbitmqServer {
+ @Test
+ public void isStarted() {
+ Assert.assertTrue(rabbitmqConsumer.isStarted());
+ }
+
+ @Test
+ public void isClosed() {
+ Assert.assertFalse(rabbitmqConsumer.isClosed());
+ }
+
@Test
public void subscribe() throws Exception {
final int expectedCount = 5;
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
index f38fd5e1..96333d5f 100644
--- 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
@@ -36,6 +36,16 @@ import io.cloudevents.core.builder.CloudEventBuilder;
public class RabbitmqProducerTest extends RabbitmqServer {
+ @Test
+ public void isStarted() {
+ Assert.assertTrue(rabbitmqProducer.isStarted());
+ }
+
+ @Test
+ public void isClosed() {
+ Assert.assertFalse(rabbitmqProducer.isClosed());
+ }
+
@Test
public void publish() throws Exception {
final int expectedCount = 5;
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org