From cf3daa990775f86f41cac65d2076db4de50d62d9 Mon Sep 17 00:00:00 2001 From: Lukas Date: Fri, 23 Dec 2022 01:30:28 +0100 Subject: [PATCH] Fixed test command exit codes and stderr. Added basic test command testsuite. --- .../cli/commands/cli/TestBrokerCommand.java | 38 ++- .../cli/test_broker/TestBrokerConnectST.java | 267 ++++++++++++++++++ .../test_broker/TestBrokerConnectTlsST.java | 74 +++++ .../cli/test_broker/TestBrokerST.java | 119 ++++++++ .../assertions/TestConnectAssertion.java | 89 ++++++ 5 files changed, 572 insertions(+), 15 deletions(-) create mode 100644 src/systemTest/java/com/hivemq/cli/commands/cli/test_broker/TestBrokerConnectST.java create mode 100644 src/systemTest/java/com/hivemq/cli/commands/cli/test_broker/TestBrokerConnectTlsST.java create mode 100644 src/systemTest/java/com/hivemq/cli/commands/cli/test_broker/TestBrokerST.java create mode 100644 src/systemTest/java/com/hivemq/cli/utils/broker/assertions/TestConnectAssertion.java diff --git a/src/main/java/com/hivemq/cli/commands/cli/TestBrokerCommand.java b/src/main/java/com/hivemq/cli/commands/cli/TestBrokerCommand.java index 364274ac6..2df1e444e 100644 --- a/src/main/java/com/hivemq/cli/commands/cli/TestBrokerCommand.java +++ b/src/main/java/com/hivemq/cli/commands/cli/TestBrokerCommand.java @@ -136,21 +136,27 @@ public TestBrokerCommand(final @NotNull DefaultCLIProperties defaultCLIPropertie return 1; } + int mqtt3ExitCode = 0; + int mqtt5ExitCode = 0; if (version != null) { if (version == MqttVersion.MQTT_3_1_1) { - testMqtt3Features(); + mqtt3ExitCode = testMqtt3Features(); } else if (version == MqttVersion.MQTT_5_0) { - testMqtt5Features(); + mqtt5ExitCode = testMqtt5Features(); } } else { - testMqtt3Features(); - testMqtt5Features(); + mqtt3ExitCode = testMqtt3Features(); + mqtt5ExitCode = testMqtt5Features(); } - return 0; + if (mqtt3ExitCode != 0 || mqtt5ExitCode != 0) { + return 1; + } else { + return 0; + } } - public void testMqtt5Features() { + public int testMqtt5Features() { final Mqtt5FeatureTester mqtt5Tester = new Mqtt5FeatureTester(Objects.requireNonNull(host), Objects.requireNonNull(port), authenticationOptions.getUser(), @@ -168,16 +174,16 @@ public void testMqtt5Features() { connAck = mqtt5Tester.testConnect(); } catch (final Exception e) { Logger.error(e, "Could not connect MQTT 5 client"); - System.out.println("Could not connect MQTT 5 client - " + Throwables.getRootCause(e).getMessage()); - return; + System.err.println("Could not connect MQTT 5 client - " + Throwables.getRootCause(e).getMessage()); + return 1; } if (connAck == null) { System.out.println("NO"); - return; + return 1; } else if (connAck.getReasonCode() != Mqtt5ConnAckReasonCode.SUCCESS) { System.out.println(connAck.getReasonCode()); - return; + return 1; } else { System.out.println("OK"); } @@ -299,9 +305,10 @@ public void testMqtt5Features() { } Logger.info("Finished testing MQTT 5"); + return 0; } - public void testMqtt3Features() { + public int testMqtt3Features() { final Mqtt3FeatureTester mqtt3Tester = new Mqtt3FeatureTester(Objects.requireNonNull(host), Objects.requireNonNull(port), authenticationOptions.getUser(), @@ -319,15 +326,15 @@ public void testMqtt3Features() { connAck = mqtt3Tester.testConnect(); } catch (final Exception e) { Logger.error(e, "Could not connect MQTT 3 client"); - System.out.println("Could not connect MQTT 3 client - " + Throwables.getRootCause(e).getMessage()); - return; + System.err.println("Could not connect MQTT 3 client - " + Throwables.getRootCause(e).getMessage()); + return 1; } if (connAck == null) { System.out.println("NO"); - return; + return 1; } else if (connAck.getReturnCode() != Mqtt3ConnAckReturnCode.SUCCESS) { System.out.println(connAck.getReturnCode()); - return; + return 1; } else { System.out.println("OK"); } @@ -407,6 +414,7 @@ public void testMqtt3Features() { } Logger.info("Finished testing MQTT 3"); + return 0; } @Override diff --git a/src/systemTest/java/com/hivemq/cli/commands/cli/test_broker/TestBrokerConnectST.java b/src/systemTest/java/com/hivemq/cli/commands/cli/test_broker/TestBrokerConnectST.java new file mode 100644 index 000000000..5742d0d46 --- /dev/null +++ b/src/systemTest/java/com/hivemq/cli/commands/cli/test_broker/TestBrokerConnectST.java @@ -0,0 +1,267 @@ +/* + * Copyright 2019-present HiveMQ and the HiveMQ Community + * + * Licensed 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 com.hivemq.cli.commands.cli.test_broker; + +import com.hivemq.cli.utils.MqttVersionConverter; +import com.hivemq.cli.utils.broker.HiveMQ; +import com.hivemq.cli.utils.cli.MqttCli; +import com.hivemq.cli.utils.cli.results.ExecutionResult; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import static com.hivemq.cli.utils.broker.assertions.TestConnectAssertion.assertTestConnectPacket; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class TestBrokerConnectST { + + @RegisterExtension + private static final @NotNull HiveMQ HIVEMQ = HiveMQ.builder().build(); + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_connectWrongHost(final char mqttVersion) throws Exception { + final List testCommand = List.of("test", + "-h", + "wrong-host", + "-p", + String.valueOf(HIVEMQ.getMqttPort()), + "-V", + String.valueOf(mqttVersion)); + + final ExecutionResult executionResult = MqttCli.execute(testCommand); + assertEquals(1, executionResult.getExitCode()); + assertTrue(executionResult.getErrorOutput().contains("Could not connect")); + } + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_connectWrongPort(final char mqttVersion) throws Exception { + final List testCommand = + List.of("test", "-h", HIVEMQ.getHost(), "-p", "22", "-V", String.valueOf(mqttVersion)); + + final ExecutionResult executionResult = MqttCli.execute(testCommand); + assertEquals(1, executionResult.getExitCode()); + assertTrue(executionResult.getErrorOutput().contains("Could not connect")); + } + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_connectInvalidTimeOut(final char mqttVersion) throws Exception { + final List testCommand = List.of("test", + "-h", + HIVEMQ.getHost(), + "-p", + String.valueOf(HIVEMQ.getMqttPort()), + "-V", + String.valueOf(mqttVersion), + "-t", + "random"); + + final ExecutionResult executionResult = MqttCli.execute(testCommand); + assertEquals(2, executionResult.getExitCode()); + assertTrue(executionResult.getErrorOutput() + .contains("Invalid value for option '--timeOut': 'random' is not an int")); + } + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_connectUserName(final char mqttVersion) throws Exception { + final List testCommand = defaultTestCommand(mqttVersion); + testCommand.add("-u"); + testCommand.add("username"); + + final ExecutionResult executionResult = MqttCli.execute(testCommand); + assertTestOutput(executionResult, mqttVersion); + + assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> { + connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion)); + connectAssertion.setUserName("username"); + }); + } + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_connectPassword(final char mqttVersion) throws Exception { + final List testCommand = defaultTestCommand(mqttVersion); + testCommand.add("-pw"); + testCommand.add("password"); + + final ExecutionResult executionResult = MqttCli.execute(testCommand); + + if (mqttVersion == '3') { + assertTrue(executionResult.getErrorOutput() + .contains("Password-Only Authentication is not allowed in MQTT 3")); + assertEquals(1, executionResult.getExitCode()); + } else { + assertTestOutput(executionResult, mqttVersion); + assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> { + connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion)); + connectAssertion.setPassword(ByteBuffer.wrap("password".getBytes(StandardCharsets.UTF_8))); + }); + } + } + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_connectPasswordEnv(final char mqttVersion) throws Exception { + final List testCommand = defaultTestCommand(mqttVersion); + testCommand.add("-pw:env"); + testCommand.add("PASSWORD"); + + final ExecutionResult executionResult = MqttCli.execute(testCommand, Map.of("PASSWORD", "password")); + + if (mqttVersion == '3') { + assertTrue(executionResult.getErrorOutput() + .contains("Password-Only Authentication is not allowed in MQTT 3")); + assertEquals(1, executionResult.getExitCode()); + } else { + assertTestOutput(executionResult, mqttVersion); + assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> { + connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion)); + connectAssertion.setPassword(ByteBuffer.wrap("password".getBytes(StandardCharsets.UTF_8))); + }); + } + } + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_connectPasswordFile(final char mqttVersion) throws Exception { + final Path passwordFile = Files.createTempFile("password-file", ".txt"); + passwordFile.toFile().deleteOnExit(); + Files.writeString(passwordFile, "password"); + + final List testCommand = defaultTestCommand(mqttVersion); + testCommand.add("-pw:file"); + testCommand.add(passwordFile.toString()); + + final ExecutionResult executionResult = MqttCli.execute(testCommand); + + if (mqttVersion == '3') { + assertTrue(executionResult.getErrorOutput() + .contains("Password-Only Authentication is not allowed in MQTT 3")); + assertEquals(1, executionResult.getExitCode()); + } else { + assertTestOutput(executionResult, mqttVersion); + assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> { + connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion)); + connectAssertion.setPassword(ByteBuffer.wrap("password".getBytes(StandardCharsets.UTF_8))); + }); + } + } + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_connectUserNameAndPassword(final char mqttVersion) throws Exception { + final List testCommand = defaultTestCommand(mqttVersion); + testCommand.add("-u"); + testCommand.add("username"); + testCommand.add("-pw"); + testCommand.add("password"); + + final ExecutionResult executionResult = MqttCli.execute(testCommand); + assertTestOutput(executionResult, mqttVersion); + + assertTestOutput(executionResult, mqttVersion); + assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> { + connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion)); + connectAssertion.setUserName("username"); + connectAssertion.setPassword(ByteBuffer.wrap("password".getBytes(StandardCharsets.UTF_8))); + }); + } + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_connectUserNameAndPasswordEnv(final char mqttVersion) throws Exception { + final List testCommand = defaultTestCommand(mqttVersion); + testCommand.add("-u"); + testCommand.add("username"); + testCommand.add("-pw:env"); + testCommand.add("PASSWORD"); + + final ExecutionResult executionResult = MqttCli.execute(testCommand, Map.of("PASSWORD", "password")); + + assertTestOutput(executionResult, mqttVersion); + assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> { + connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion)); + connectAssertion.setUserName("username"); + connectAssertion.setPassword(ByteBuffer.wrap("password".getBytes(StandardCharsets.UTF_8))); + }); + } + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_connectUserNamePasswordFile(final char mqttVersion) throws Exception { + final Path passwordFile = Files.createTempFile("password-file", ".txt"); + passwordFile.toFile().deleteOnExit(); + Files.writeString(passwordFile, "password"); + + final List testCommand = defaultTestCommand(mqttVersion); + testCommand.add("-u"); + testCommand.add("username"); + testCommand.add("-pw:file"); + testCommand.add(passwordFile.toString()); + + final ExecutionResult executionResult = MqttCli.execute(testCommand); + + assertTestOutput(executionResult, mqttVersion); + assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> { + connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion)); + connectAssertion.setUserName("username"); + connectAssertion.setPassword(ByteBuffer.wrap("password".getBytes(StandardCharsets.UTF_8))); + }); + } + + private void assertTestOutput(final @NotNull ExecutionResult executionResult, final char mqttVersion) { + assertEquals(0, executionResult.getExitCode()); + assertTrue(executionResult.getStandardOutput().contains("MQTT " + mqttVersion + ": OK")); + } + + private @NotNull List defaultTestCommand(final char mqttVersion) { + final ArrayList testCommand = new ArrayList<>(); + testCommand.add("test"); + testCommand.add("-h"); + testCommand.add(HIVEMQ.getHost()); + testCommand.add("-p"); + testCommand.add(String.valueOf(HIVEMQ.getMqttPort())); + testCommand.add("-V"); + testCommand.add(String.valueOf(mqttVersion)); + return testCommand; + } +} diff --git a/src/systemTest/java/com/hivemq/cli/commands/cli/test_broker/TestBrokerConnectTlsST.java b/src/systemTest/java/com/hivemq/cli/commands/cli/test_broker/TestBrokerConnectTlsST.java new file mode 100644 index 000000000..0eeb25277 --- /dev/null +++ b/src/systemTest/java/com/hivemq/cli/commands/cli/test_broker/TestBrokerConnectTlsST.java @@ -0,0 +1,74 @@ +/* + * Copyright 2019-present HiveMQ and the HiveMQ Community + * + * Licensed 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 com.hivemq.cli.commands.cli.test_broker; + +import com.google.common.io.Resources; +import com.hivemq.cli.utils.MqttVersionConverter; +import com.hivemq.cli.utils.broker.HiveMQ; +import com.hivemq.cli.utils.cli.MqttCliAsync; +import com.hivemq.cli.utils.cli.results.ExecutionResultAsync; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +import static com.hivemq.cli.utils.broker.assertions.TestConnectAssertion.assertTestConnectPacket; + +class TestBrokerConnectTlsST { + + @RegisterExtension + private static final @NotNull HiveMQ HIVEMQ = HiveMQ.builder().withTlsEnabled(true).build(); + + @RegisterExtension + private final @NotNull MqttCliAsync mqttCli = new MqttCliAsync(); + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_mutualTls(final char mqttVersion) throws Exception { + final String clientKeyPem = Resources.getResource("tls/client-key.pem").getPath(); + final String clientCertPem = Resources.getResource("tls/client-cert.pem").getPath(); + final String serverPem = Resources.getResource("tls/server.pem").getPath(); + + final List publishCommand = List.of("test", + "-h", + HIVEMQ.getHost(), + "-p", + String.valueOf(HIVEMQ.getMqttTlsPort()), + "-V", + String.valueOf(mqttVersion), + "--cafile", + serverPem, + "--key", + clientKeyPem, + "--cert", + clientCertPem); + + final ExecutionResultAsync executionResult = mqttCli.executeAsync(publishCommand); + executionResult.awaitStdOut("Enter private key password:"); + executionResult.write("changeme"); + executionResult.awaitStdOut("MQTT " + mqttVersion + ": OK"); + + assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), + connectAssertion -> connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion( + mqttVersion))); + } +} diff --git a/src/systemTest/java/com/hivemq/cli/commands/cli/test_broker/TestBrokerST.java b/src/systemTest/java/com/hivemq/cli/commands/cli/test_broker/TestBrokerST.java new file mode 100644 index 000000000..3e3b53d0a --- /dev/null +++ b/src/systemTest/java/com/hivemq/cli/commands/cli/test_broker/TestBrokerST.java @@ -0,0 +1,119 @@ +/* + * Copyright 2019-present HiveMQ and the HiveMQ Community + * + * Licensed 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 com.hivemq.cli.commands.cli.test_broker; + +import com.hivemq.cli.utils.MqttVersionConverter; +import com.hivemq.cli.utils.broker.HiveMQ; +import com.hivemq.cli.utils.cli.MqttCli; +import com.hivemq.cli.utils.cli.results.ExecutionResult; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import static com.hivemq.cli.utils.broker.assertions.TestConnectAssertion.assertTestConnectPacket; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class TestBrokerST { + + @RegisterExtension + private static final @NotNull HiveMQ HIVEMQ = HiveMQ.builder().build(); + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_successfulConnectAndTest(final char mqttVersion) throws Exception { + final List publishCommand = defaultTestCommand(mqttVersion); + final ExecutionResult executionResult = MqttCli.execute(publishCommand); + + assertTestOutput(executionResult, mqttVersion); + + assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), + connectAssertion -> connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion( + mqttVersion))); + } + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_AllTests(final char mqttVersion) throws Exception { + final List publishCommand = defaultTestCommand(mqttVersion); + publishCommand.add("-a"); + final ExecutionResult executionResult = MqttCli.execute(publishCommand); + + assertTestOutput(executionResult, mqttVersion); + + assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), + connectAssertion -> connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion( + mqttVersion))); + } + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_Timeout(final char mqttVersion) throws Exception { + final List publishCommand = defaultTestCommand(mqttVersion); + publishCommand.add("-t"); + publishCommand.add("10"); + final ExecutionResult executionResult = MqttCli.execute(publishCommand); + + assertTestOutput(executionResult, mqttVersion); + + assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), + connectAssertion -> connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion( + mqttVersion))); + } + + @ParameterizedTest + @Timeout(value = 3, unit = TimeUnit.MINUTES) + @ValueSource(chars = {'3', '5'}) + void test_QosTries(final char mqttVersion) throws Exception { + final List publishCommand = defaultTestCommand(mqttVersion); + publishCommand.add("-q"); + publishCommand.add("10"); + final ExecutionResult executionResult = MqttCli.execute(publishCommand); + + assertTestOutput(executionResult, mqttVersion); + + assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), + connectAssertion -> connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion( + mqttVersion))); + } + + private void assertTestOutput(final @NotNull ExecutionResult executionResult, final char mqttVersion) { + assertEquals(0, executionResult.getExitCode()); + assertTrue(executionResult.getStandardOutput().contains("MQTT " + mqttVersion + ": OK")); + } + + private @NotNull List defaultTestCommand(final char mqttVersion) { + final ArrayList testCommand = new ArrayList<>(); + testCommand.add("test"); + testCommand.add("-h"); + testCommand.add(HIVEMQ.getHost()); + testCommand.add("-p"); + testCommand.add(String.valueOf(HIVEMQ.getMqttPort())); + testCommand.add("-V"); + testCommand.add(String.valueOf(mqttVersion)); + return testCommand; + } +} diff --git a/src/systemTest/java/com/hivemq/cli/utils/broker/assertions/TestConnectAssertion.java b/src/systemTest/java/com/hivemq/cli/utils/broker/assertions/TestConnectAssertion.java new file mode 100644 index 000000000..f1cca9a1c --- /dev/null +++ b/src/systemTest/java/com/hivemq/cli/utils/broker/assertions/TestConnectAssertion.java @@ -0,0 +1,89 @@ +/* + * Copyright 2019-present HiveMQ and the HiveMQ Community + * + * Licensed 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 com.hivemq.cli.utils.broker.assertions; + +import com.google.common.collect.ImmutableList; +import com.hivemq.extension.sdk.api.packets.connect.ConnectPacket; +import com.hivemq.extension.sdk.api.packets.general.MqttVersion; +import com.hivemq.extension.sdk.api.packets.general.UserProperties; +import com.hivemq.extensions.packets.general.UserPropertiesImpl; +import com.hivemq.mqtt.message.mqtt5.MqttUserProperty; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.nio.ByteBuffer; +import java.util.Optional; +import java.util.function.Consumer; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +public class TestConnectAssertion { + + private @NotNull MqttVersion mqttVersion = MqttVersion.V_5; + private final boolean cleanStart = true; + private final long sessionExpiryInterval = 0; + private final int keepAlive = 60; + private final int receiveMaximum = 65535; + private final long maximumPacketSize = 268435460; + private final int topicAliasMaximum = 0; + private final boolean requestProblemInformation = true; + private final boolean requestResponseInformation = false; + + private @Nullable String userName = null; + private @Nullable ByteBuffer password = null; + + private final @Nullable UserProperties userProperties = + UserPropertiesImpl.of(ImmutableList.builder().build()); + + private TestConnectAssertion() { + } + + public static void assertTestConnectPacket( + final @NotNull ConnectPacket connectPacket, + final @NotNull Consumer connectAssertionConsumer) { + final TestConnectAssertion connectAssertion = new TestConnectAssertion(); + connectAssertionConsumer.accept(connectAssertion); + assertEquals(connectAssertion.mqttVersion, connectPacket.getMqttVersion()); + assertEquals(connectAssertion.cleanStart, connectPacket.getCleanStart()); + assertEquals(connectAssertion.sessionExpiryInterval, connectPacket.getSessionExpiryInterval()); + assertEquals(connectAssertion.keepAlive, connectPacket.getKeepAlive()); + assertEquals(connectAssertion.receiveMaximum, connectPacket.getReceiveMaximum()); + assertEquals(connectAssertion.maximumPacketSize, connectPacket.getMaximumPacketSize()); + assertEquals(connectAssertion.topicAliasMaximum, connectPacket.getTopicAliasMaximum()); + assertEquals(connectAssertion.requestProblemInformation, connectPacket.getRequestProblemInformation()); + assertEquals(connectAssertion.requestResponseInformation, connectPacket.getRequestResponseInformation()); + + assertEquals(Optional.ofNullable(connectAssertion.userName), connectPacket.getUserName()); + assertEquals(Optional.ofNullable(connectAssertion.password), connectPacket.getPassword()); + + assertEquals(connectAssertion.userProperties, connectPacket.getUserProperties()); + assertFalse(connectPacket.getWillPublish().isPresent()); + } + + public void setMqttVersion(final @NotNull MqttVersion mqttVersion) { + this.mqttVersion = mqttVersion; + } + + public void setUserName(final @Nullable String userName) { + this.userName = userName; + } + + public void setPassword(final @Nullable ByteBuffer password) { + this.password = password; + } +}