diff --git a/shenyu-protocol/shenyu-protocol-mqtt/pom.xml b/shenyu-protocol/shenyu-protocol-mqtt/pom.xml index 2ddc80e0afdd..cf8e31fb56de 100644 --- a/shenyu-protocol/shenyu-protocol-mqtt/pom.xml +++ b/shenyu-protocol/shenyu-protocol-mqtt/pom.xml @@ -46,6 +46,12 @@ reflections 0.9.11 + + + org.junit.jupiter + junit-jupiter + test + diff --git a/shenyu-protocol/shenyu-protocol-mqtt/src/main/java/org/apache/shenyu/protocol/mqtt/MqttContext.java b/shenyu-protocol/shenyu-protocol-mqtt/src/main/java/org/apache/shenyu/protocol/mqtt/MqttContext.java index 74e6b0dd6abf..7ecaf06856b5 100644 --- a/shenyu-protocol/shenyu-protocol-mqtt/src/main/java/org/apache/shenyu/protocol/mqtt/MqttContext.java +++ b/shenyu-protocol/shenyu-protocol-mqtt/src/main/java/org/apache/shenyu/protocol/mqtt/MqttContext.java @@ -19,6 +19,8 @@ import org.apache.commons.lang3.StringUtils; +import java.util.Objects; + /** * mqtt env. */ @@ -45,7 +47,7 @@ public class MqttContext { * @return true is correct, false unavailable. */ public static boolean isValid(final String userName, final byte[] passwordInBytes) { - String password = new String(passwordInBytes); + String password = Objects.isNull(passwordInBytes) ? "" : new String(passwordInBytes); if (StringUtils.isEmpty(password) || StringUtils.isEmpty(userName)) { return false; diff --git a/shenyu-protocol/shenyu-protocol-mqtt/src/test/java/org/apache/shenyu/protocol/mqtt/MqttContextTest.java b/shenyu-protocol/shenyu-protocol-mqtt/src/test/java/org/apache/shenyu/protocol/mqtt/MqttContextTest.java new file mode 100644 index 000000000000..2f700c512794 --- /dev/null +++ b/shenyu-protocol/shenyu-protocol-mqtt/src/test/java/org/apache/shenyu/protocol/mqtt/MqttContextTest.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.shenyu.protocol.mqtt; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Test Case For {@link MqttContext}. + */ +public class MqttContextTest { + + private static final String USER_NAME = "testUser"; + + private static final String PASSWORD = "testPass"; + + private final MqttContext mqttContext = new MqttContext(); + + @BeforeEach + public void setUp() { + mqttContext.setUserName(USER_NAME); + mqttContext.setPassword(PASSWORD); + } + + @Test + public void testIsValidWithCorrectCredentials() { + assertTrue(MqttContext.isValid(USER_NAME, PASSWORD.getBytes())); + } + + @Test + public void testIsValidWithNullPasswordInBytes() { + assertFalse(MqttContext.isValid(USER_NAME, null)); + } + + @Test + public void testIsValidWithEmptyPassword() { + assertFalse(MqttContext.isValid(USER_NAME, new byte[0])); + } + + @Test + public void testIsValidWithWrongPassword() { + assertFalse(MqttContext.isValid(USER_NAME, "wrongPass".getBytes())); + } + + @Test + public void testIsValidWithNullUserName() { + assertFalse(MqttContext.isValid(null, PASSWORD.getBytes())); + } + + @Test + public void testIsValidWithEmptyUserName() { + assertFalse(MqttContext.isValid("", PASSWORD.getBytes())); + } +}