You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by hu...@apache.org on 2022/03/29 04:32:08 UTC

[rocketmq-mqtt] 40/43: increase code coverage

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

huzongtang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/rocketmq-mqtt.git

commit 1e15e5ee3bcf7ed215b49fa609cc1ebd92f2c598
Author: shendongsd <sh...@cmss.chinamobile.com>
AuthorDate: Tue Mar 22 14:37:53 2022 +0800

    increase code coverage
---
 .../rocketmq/mqtt/common/test/TestMessageUtil.java | 53 ++++++++++++++++
 .../mqtt/common/test/TestNamespaceUtil.java        | 72 ++++++++++++++++++++++
 2 files changed, 125 insertions(+)

diff --git a/mqtt-common/src/test/java/org/apache/rocketmq/mqtt/common/test/TestMessageUtil.java b/mqtt-common/src/test/java/org/apache/rocketmq/mqtt/common/test/TestMessageUtil.java
new file mode 100644
index 0000000..f5f93dd
--- /dev/null
+++ b/mqtt-common/src/test/java/org/apache/rocketmq/mqtt/common/test/TestMessageUtil.java
@@ -0,0 +1,53 @@
+package org.apache.rocketmq.mqtt.common.test;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.buffer.Unpooled;
+import io.netty.buffer.UnpooledByteBufAllocator;
+import io.netty.handler.codec.mqtt.*;
+import org.apache.rocketmq.mqtt.common.model.Message;
+import org.apache.rocketmq.mqtt.common.util.MessageUtil;
+import org.apache.rocketmq.mqtt.common.util.TopicUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.charset.StandardCharsets;
+
+public class TestMessageUtil {
+
+    String messageBody;
+    String topicName;
+    int qos;
+    int mqttId;
+    MqttPublishMessage mqttPublishMessage;
+
+
+    @Before
+    public void Before() {
+        messageBody = "Hello-mqtt";
+        topicName = "topicTest";
+        qos = 0;
+        mqttId = 1;
+        ByteBufAllocator ALLOCATOR = new UnpooledByteBufAllocator(false);
+        byte[] body = messageBody.getBytes(StandardCharsets.UTF_8);
+        ByteBuf payload = ALLOCATOR.buffer();
+        payload.writeBytes(body);
+        mqttPublishMessage = new MqttPublishMessage(new MqttFixedHeader(MqttMessageType.PUBLISH, false, MqttQoS.valueOf(qos), false, 0),
+                new MqttPublishVariableHeader(topicName, mqttId), payload);
+    }
+
+    @Test
+    public void TestToMqttMessage() {
+        Assert.assertEquals(mqttPublishMessage.toString(), MessageUtil.toMqttMessage(topicName, messageBody.getBytes(), qos, mqttId).toString());
+    }
+
+    @Test
+    public void TestToMessage() {
+        Message message = new Message();
+        message.setFirstTopic(topicName);
+        message.putUserProperty(Message.extPropertyQoS, String.valueOf(qos));
+        message.setPayload(messageBody.getBytes(StandardCharsets.UTF_8));
+        Assert.assertEquals(message, MessageUtil.toMessage(mqttPublishMessage));
+    }
+}
diff --git a/mqtt-common/src/test/java/org/apache/rocketmq/mqtt/common/test/TestNamespaceUtil.java b/mqtt-common/src/test/java/org/apache/rocketmq/mqtt/common/test/TestNamespaceUtil.java
new file mode 100644
index 0000000..e80bb6d
--- /dev/null
+++ b/mqtt-common/src/test/java/org/apache/rocketmq/mqtt/common/test/TestNamespaceUtil.java
@@ -0,0 +1,72 @@
+/*
+ *
+ *  * 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.rocketmq.mqtt.common.test;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.rocketmq.mqtt.common.model.Trie;
+import org.apache.rocketmq.mqtt.common.util.NamespaceUtil;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestNamespaceUtil {
+
+    String resource;
+    String namespace;
+    String originResource;
+
+    @Before
+    public void Before() {
+        resource = "namespaceTest001%originResourceTest001";
+        namespace = "namespaceTest001";
+        originResource = "originResourceTest001";
+    }
+
+    @Test
+    public void testDecodeOriginResource() {
+        Assert.assertEquals(originResource, NamespaceUtil.decodeOriginResource(resource));
+    }
+
+    @Test
+    public void testEncodeToNamespaceResource() {
+        Assert.assertEquals(resource, NamespaceUtil.encodeToNamespaceResource(namespace, originResource));
+    }
+
+    @Test
+    public void testDecodeMqttNamespaceIdFromKey() {
+        Assert.assertEquals(namespace, NamespaceUtil.decodeMqttNamespaceIdFromKey(resource));
+    }
+
+    @Test
+    public void testDecodeMqttNamespaceIdFromClientId() {
+        Assert.assertEquals(namespace, NamespaceUtil.decodeMqttNamespaceIdFromClientId(resource));
+    }
+
+    @Test
+    public void testDecodeStoreNamespaceIdFromTopic() {
+        Assert.assertEquals(namespace, NamespaceUtil.decodeStoreNamespaceIdFromTopic(resource));
+    }
+
+    @Test
+    public void testDecodeNamespaceId() {
+        Assert.assertEquals(namespace, NamespaceUtil.decodeNamespaceId(resource));
+    }
+
+}