Migrate codec-mqtt to junit5 (#11431)

Motivation:

We should update to use junit5 in all modules.

Modifications:

Adjust codec-mqtt tests to use junit5

Result:

Part of https://github.com/netty/netty/issues/10757
This commit is contained in:
Norman Maurer 2021-06-30 15:32:16 +02:00
parent 1049b02f67
commit fe1ed36cc4
6 changed files with 125 additions and 159 deletions

View File

@ -26,9 +26,11 @@ import io.netty.handler.codec.EncoderException;
import io.netty.util.Attribute; import io.netty.util.Attribute;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import org.junit.After;
import org.junit.Before; import org.junit.jupiter.api.AfterEach;
import org.junit.Test; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
@ -36,7 +38,6 @@ import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -46,8 +47,20 @@ import static io.netty.handler.codec.mqtt.MqttTestUtils.validateUnsubscribePaylo
import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.*; import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.*;
import static io.netty.handler.codec.mqtt.MqttQoS.AT_LEAST_ONCE; import static io.netty.handler.codec.mqtt.MqttQoS.AT_LEAST_ONCE;
import static io.netty.handler.codec.mqtt.MqttSubscriptionOption.RetainedHandlingPolicy.SEND_AT_SUBSCRIBE_IF_NOT_YET_EXISTS; import static io.netty.handler.codec.mqtt.MqttSubscriptionOption.RetainedHandlingPolicy.SEND_AT_SUBSCRIBE_IF_NOT_YET_EXISTS;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.mockito.Mockito.*; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.instanceOf;
/** /**
* Unit tests for MqttEncoder and MqttDecoder. * Unit tests for MqttEncoder and MqttDecoder.
@ -83,8 +96,7 @@ public class MqttCodecTest {
* MqttDecoder with an unrealistic max payload size of 1 byte. * MqttDecoder with an unrealistic max payload size of 1 byte.
*/ */
private final MqttDecoder mqttDecoderLimitedMessageSize = new MqttDecoder(1); private final MqttDecoder mqttDecoderLimitedMessageSize = new MqttDecoder(1);
@BeforeEach
@Before
public void setup() throws Exception { public void setup() throws Exception {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
when(ctx.channel()).thenReturn(channel); when(ctx.channel()).thenReturn(channel);
@ -101,7 +113,7 @@ public class MqttCodecTest {
mqttDecoderLimitedMessageSize.handlerAdded(ctx); mqttDecoderLimitedMessageSize.handlerAdded(ctx);
} }
@After @AfterEach
public void after() { public void after() {
for (Object o : out) { for (Object o : out) {
ReferenceCountUtil.release(o); ReferenceCountUtil.release(o);
@ -155,7 +167,7 @@ public class MqttCodecTest {
final MqttMessage decodedMessage = captor.getValue(); final MqttMessage decodedMessage = captor.getValue();
assertTrue(decodedMessage.decoderResult().isFailure()); assertTrue(decodedMessage.decoderResult().isFailure());
Throwable cause = decodedMessage.decoderResult().cause(); Throwable cause = decodedMessage.decoderResult().cause();
assertTrue(cause instanceof DecoderException); assertThat(cause, instanceOf(DecoderException.class));
assertEquals("non-zero reserved flag", cause.getMessage()); assertEquals("non-zero reserved flag", cause.getMessage());
} }
@ -233,8 +245,7 @@ public class MqttCodecTest {
private void checkForSingleDecoderException(ArgumentCaptor<MqttMessage> captor) { private void checkForSingleDecoderException(ArgumentCaptor<MqttMessage> captor) {
final MqttMessage result = captor.getValue(); final MqttMessage result = captor.getValue();
assertTrue("Decoding should have resulted in a DecoderException", assertThat(result.decoderResult().cause(), instanceOf(DecoderException.class));
result.decoderResult().cause() instanceof DecoderException);
} }
@Test @Test
@ -246,11 +257,12 @@ public class MqttCodecTest {
MqttProperties.NO_PROPERTIES, MqttProperties.NO_PROPERTIES,
MqttProperties.NO_PROPERTIES); MqttProperties.NO_PROPERTIES);
try { assertThrows(EncoderException.class, new Executable() {
ByteBuf byteBuf = MqttEncoder.doEncode(ctx, message); @Override
} catch (Exception cause) { public void execute() {
assertTrue(cause instanceof EncoderException); MqttEncoder.doEncode(ctx, message);
} }
});
} }
@Test @Test
@ -406,7 +418,7 @@ public class MqttCodecTest {
final MqttMessage decodedMessage = captor.getValue(); final MqttMessage decodedMessage = captor.getValue();
assertTrue(decodedMessage.decoderResult().isFailure()); assertTrue(decodedMessage.decoderResult().isFailure());
Throwable cause = decodedMessage.decoderResult().cause(); Throwable cause = decodedMessage.decoderResult().cause();
assertTrue(cause instanceof DecoderException); assertThat(cause, instanceOf(DecoderException.class));
assertEquals("AUTH message requires at least MQTT 5", cause.getMessage()); assertEquals("AUTH message requires at least MQTT 5", cause.getMessage());
} }
@ -547,7 +559,6 @@ public class MqttCodecTest {
verify(ctx).fireChannelRead(captor.capture()); verify(ctx).fireChannelRead(captor.capture());
final MqttConnectMessage decodedMessage = captor.getValue(); final MqttConnectMessage decodedMessage = captor.getValue();
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateConnectVariableHeader(message.variableHeader(), decodedMessage.variableHeader()); validateConnectVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
validateConnectPayload(message.payload(), decodedMessage.payload()); validateConnectPayload(message.payload(), decodedMessage.payload());
@ -567,7 +578,6 @@ public class MqttCodecTest {
verify(ctx).fireChannelRead(captor.capture()); verify(ctx).fireChannelRead(captor.capture());
final MqttConnAckMessage decodedMessage = captor.getValue(); final MqttConnAckMessage decodedMessage = captor.getValue();
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateConnAckVariableHeader(message.variableHeader(), decodedMessage.variableHeader()); validateConnAckVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
} }
@ -582,11 +592,11 @@ public class MqttCodecTest {
props.add(new MqttProperties.UserProperty("isSecret", "true")); props.add(new MqttProperties.UserProperty("isSecret", "true"));
props.add(new MqttProperties.UserProperty("tag", "firstTag")); props.add(new MqttProperties.UserProperty("tag", "firstTag"));
props.add(new MqttProperties.UserProperty("tag", "secondTag")); props.add(new MqttProperties.UserProperty("tag", "secondTag"));
assertEquals("Subscription IDs count mismatch", 2, assertEquals(2,
(props.getProperties(SUBSCRIPTION_IDENTIFIER.value())).size()); props.getProperties(SUBSCRIPTION_IDENTIFIER.value()).size());
assertEquals("User properties count mismatch", 3, assertEquals(3,
(props.getProperties(USER_PROPERTY.value())).size()); props.getProperties(USER_PROPERTY.value()).size());
assertEquals("UserProperties count mismatch", 3, assertEquals(3,
((MqttProperties.UserProperties) props.getProperty(USER_PROPERTY.value())).value.size()); ((MqttProperties.UserProperties) props.getProperty(USER_PROPERTY.value())).value.size());
final MqttPublishMessage message = createPublishMessage(props); final MqttPublishMessage message = createPublishMessage(props);
ByteBuf byteBuf = MqttEncoder.doEncode(ctx, message); ByteBuf byteBuf = MqttEncoder.doEncode(ctx, message);
@ -596,7 +606,6 @@ public class MqttCodecTest {
verify(ctx).fireChannelRead(captor.capture()); verify(ctx).fireChannelRead(captor.capture());
final MqttPublishMessage decodedMessage = captor.getValue(); final MqttPublishMessage decodedMessage = captor.getValue();
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validatePublishVariableHeader(message.variableHeader(), decodedMessage.variableHeader()); validatePublishVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
validatePublishPayload(message.payload(), decodedMessage.payload()); validatePublishPayload(message.payload(), decodedMessage.payload());
@ -616,7 +625,6 @@ public class MqttCodecTest {
verify(ctx).fireChannelRead(captor.capture()); verify(ctx).fireChannelRead(captor.capture());
final MqttMessage decodedMessage = captor.getValue(); final MqttMessage decodedMessage = captor.getValue();
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validatePubReplyVariableHeader((MqttPubReplyMessageVariableHeader) message.variableHeader(), validatePubReplyVariableHeader((MqttPubReplyMessageVariableHeader) message.variableHeader(),
(MqttPubReplyMessageVariableHeader) decodedMessage.variableHeader()); (MqttPubReplyMessageVariableHeader) decodedMessage.variableHeader());
@ -633,7 +641,6 @@ public class MqttCodecTest {
verify(ctx).fireChannelRead(captor.capture()); verify(ctx).fireChannelRead(captor.capture());
final MqttMessage decodedMessage = captor.getValue(); final MqttMessage decodedMessage = captor.getValue();
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validatePubReplyVariableHeader((MqttPubReplyMessageVariableHeader) message.variableHeader(), validatePubReplyVariableHeader((MqttPubReplyMessageVariableHeader) message.variableHeader(),
(MqttPubReplyMessageVariableHeader) decodedMessage.variableHeader()); (MqttPubReplyMessageVariableHeader) decodedMessage.variableHeader());
@ -651,15 +658,12 @@ public class MqttCodecTest {
verify(ctx).fireChannelRead(captor.capture()); verify(ctx).fireChannelRead(captor.capture());
final MqttSubAckMessage decodedMessage = captor.getValue(); final MqttSubAckMessage decodedMessage = captor.getValue();
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validatePacketIdAndPropertiesVariableHeader( validatePacketIdAndPropertiesVariableHeader(
(MqttMessageIdAndPropertiesVariableHeader) message.variableHeader(), (MqttMessageIdAndPropertiesVariableHeader) message.variableHeader(),
(MqttMessageIdAndPropertiesVariableHeader) decodedMessage.variableHeader()); (MqttMessageIdAndPropertiesVariableHeader) decodedMessage.variableHeader());
validateSubAckPayload(message.payload(), decodedMessage.payload()); validateSubAckPayload(message.payload(), decodedMessage.payload());
assertArrayEquals( assertArrayEquals(new Integer[] {1, 2, 0, 0x80},
"MqttSubAckPayload QoS mismatch ",
new Integer[] {1, 2, 0, 0x80},
decodedMessage.payload().grantedQoSLevels().toArray()); decodedMessage.payload().grantedQoSLevels().toArray());
} }
@ -715,7 +719,6 @@ public class MqttCodecTest {
mqttDecoder.channelRead(ctx, byteBuf); mqttDecoder.channelRead(ctx, byteBuf);
verify(ctx).fireChannelRead(captor.capture()); verify(ctx).fireChannelRead(captor.capture());
final MqttSubscribeMessage decodedMessage = captor.getValue(); final MqttSubscribeMessage decodedMessage = captor.getValue();
final MqttSubscribeMessage expectedMessage = MqttMessageBuilders.subscribe() final MqttSubscribeMessage expectedMessage = MqttMessageBuilders.subscribe()
.messageId((short) 1) .messageId((short) 1)
.addSubscription("/topic", MqttSubscriptionOption.onlyFromQos(AT_LEAST_ONCE)) .addSubscription("/topic", MqttSubscriptionOption.onlyFromQos(AT_LEAST_ONCE))
@ -747,12 +750,11 @@ public class MqttCodecTest {
verify(ctx).fireChannelRead(captor.capture()); verify(ctx).fireChannelRead(captor.capture());
final MqttUnsubAckMessage decodedMessage = captor.getValue(); final MqttUnsubAckMessage decodedMessage = captor.getValue();
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validatePacketIdAndPropertiesVariableHeader( validatePacketIdAndPropertiesVariableHeader(
(MqttMessageIdAndPropertiesVariableHeader) message.variableHeader(), (MqttMessageIdAndPropertiesVariableHeader) message.variableHeader(),
(MqttMessageIdAndPropertiesVariableHeader) decodedMessage.variableHeader()); (MqttMessageIdAndPropertiesVariableHeader) decodedMessage.variableHeader());
assertEquals("Reason code list doesn't match", message.payload().unsubscribeReasonCodes(), assertEquals(message.payload().unsubscribeReasonCodes(),
decodedMessage.payload().unsubscribeReasonCodes()); decodedMessage.payload().unsubscribeReasonCodes());
} }
@ -773,7 +775,6 @@ public class MqttCodecTest {
verify(ctx).fireChannelRead(captor.capture()); verify(ctx).fireChannelRead(captor.capture());
final MqttMessage decodedMessage = captor.getValue(); final MqttMessage decodedMessage = captor.getValue();
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateReasonCodeAndPropertiesVariableHeader( validateReasonCodeAndPropertiesVariableHeader(
(MqttReasonCodeAndPropertiesVariableHeader) message.variableHeader(), (MqttReasonCodeAndPropertiesVariableHeader) message.variableHeader(),
@ -794,7 +795,6 @@ public class MqttCodecTest {
verify(ctx).fireChannelRead(captor.capture()); verify(ctx).fireChannelRead(captor.capture());
final MqttMessage decodedMessage = captor.getValue(); final MqttMessage decodedMessage = captor.getValue();
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateReasonCodeAndPropertiesVariableHeader( validateReasonCodeAndPropertiesVariableHeader(
(MqttReasonCodeAndPropertiesVariableHeader) message.variableHeader(), (MqttReasonCodeAndPropertiesVariableHeader) message.variableHeader(),
@ -818,7 +818,6 @@ public class MqttCodecTest {
verify(ctx).fireChannelRead(captor.capture()); verify(ctx).fireChannelRead(captor.capture());
final MqttMessage decodedMessage = captor.getValue(); final MqttMessage decodedMessage = captor.getValue();
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateReasonCodeAndPropertiesVariableHeader( validateReasonCodeAndPropertiesVariableHeader(
(MqttReasonCodeAndPropertiesVariableHeader) message.variableHeader(), (MqttReasonCodeAndPropertiesVariableHeader) message.variableHeader(),
@ -841,7 +840,6 @@ public class MqttCodecTest {
verify(versionAttrMock, times(1)).set(MqttVersion.MQTT_5); verify(versionAttrMock, times(1)).set(MqttVersion.MQTT_5);
final MqttConnectMessage decodedConnectMessage = captor.getValue(); final MqttConnectMessage decodedConnectMessage = captor.getValue();
validateFixedHeaders(connectMessage.fixedHeader(), decodedConnectMessage.fixedHeader()); validateFixedHeaders(connectMessage.fixedHeader(), decodedConnectMessage.fixedHeader());
validateConnectVariableHeader(connectMessage.variableHeader(), decodedConnectMessage.variableHeader()); validateConnectVariableHeader(connectMessage.variableHeader(), decodedConnectMessage.variableHeader());
validateConnectPayload(connectMessage.payload(), decodedConnectMessage.payload()); validateConnectPayload(connectMessage.payload(), decodedConnectMessage.payload());
@ -1005,109 +1003,81 @@ public class MqttCodecTest {
// MQTT messages // MQTT messages
private static void validateFixedHeaders(MqttFixedHeader expected, MqttFixedHeader actual) { private static void validateFixedHeaders(MqttFixedHeader expected, MqttFixedHeader actual) {
assertEquals("MqttFixedHeader MqttMessageType mismatch ", expected.messageType(), actual.messageType()); assertEquals(expected.messageType(), actual.messageType());
assertEquals("MqttFixedHeader Qos mismatch ", expected.qosLevel(), actual.qosLevel()); assertEquals(expected.qosLevel(), actual.qosLevel());
} }
private static void validateConnectVariableHeader( private static void validateConnectVariableHeader(
MqttConnectVariableHeader expected, MqttConnectVariableHeader expected,
MqttConnectVariableHeader actual) { MqttConnectVariableHeader actual) {
assertEquals("MqttConnectVariableHeader Name mismatch ", expected.name(), actual.name()); assertEquals(expected.name(), actual.name());
assertEquals( assertEquals(expected.keepAliveTimeSeconds(), actual.keepAliveTimeSeconds());
"MqttConnectVariableHeader KeepAliveTimeSeconds mismatch ", assertEquals(expected.version(), actual.version());
expected.keepAliveTimeSeconds(), assertEquals(expected.version(), actual.version());
actual.keepAliveTimeSeconds());
assertEquals("MqttConnectVariableHeader Version mismatch ", expected.version(), actual.version());
assertEquals("MqttConnectVariableHeader Version mismatch ", expected.version(), actual.version());
validateProperties(expected.properties(), actual.properties()); validateProperties(expected.properties(), actual.properties());
assertEquals("MqttConnectVariableHeader WillQos mismatch ", expected.willQos(), actual.willQos()); assertEquals(expected.willQos(), actual.willQos());
assertEquals("MqttConnectVariableHeader HasUserName mismatch ", expected.hasUserName(), actual.hasUserName()); assertEquals(expected.hasUserName(), actual.hasUserName());
assertEquals("MqttConnectVariableHeader HasPassword mismatch ", expected.hasPassword(), actual.hasPassword()); assertEquals(expected.hasPassword(), actual.hasPassword());
assertEquals( assertEquals(expected.isCleanSession(), actual.isCleanSession());
"MqttConnectVariableHeader IsCleanSession mismatch ", assertEquals(expected.isWillFlag(), actual.isWillFlag());
expected.isCleanSession(), assertEquals(expected.isWillRetain(), actual.isWillRetain());
actual.isCleanSession());
assertEquals("MqttConnectVariableHeader IsWillFlag mismatch ", expected.isWillFlag(), actual.isWillFlag());
assertEquals(
"MqttConnectVariableHeader IsWillRetain mismatch ",
expected.isWillRetain(),
actual.isWillRetain());
} }
private static void validateConnectPayload(MqttConnectPayload expected, MqttConnectPayload actual) { private static void validateConnectPayload(MqttConnectPayload expected, MqttConnectPayload actual) {
assertEquals( assertEquals(expected.clientIdentifier(), actual.clientIdentifier());
"MqttConnectPayload ClientIdentifier mismatch ", assertEquals(expected.userName(), actual.userName());
expected.clientIdentifier(), assertEquals(expected.password(), actual.password());
actual.clientIdentifier()); assertArrayEquals(expected.passwordInBytes(), actual.passwordInBytes());
assertEquals("MqttConnectPayload UserName mismatch ", expected.userName(), actual.userName()); assertEquals(expected.willMessage(), actual.willMessage());
assertEquals("MqttConnectPayload Password mismatch ", expected.password(), actual.password()); assertArrayEquals(expected.willMessageInBytes(), actual.willMessageInBytes());
assertTrue( assertEquals(expected.willTopic(), actual.willTopic());
"MqttConnectPayload Password bytes mismatch ",
Arrays.equals(expected.passwordInBytes(), actual.passwordInBytes()));
assertEquals("MqttConnectPayload WillMessage mismatch ", expected.willMessage(), actual.willMessage());
assertTrue(
"MqttConnectPayload WillMessage bytes mismatch ",
Arrays.equals(expected.willMessageInBytes(), actual.willMessageInBytes()));
assertEquals("MqttConnectPayload WillTopic mismatch ", expected.willTopic(), actual.willTopic());
validateProperties(expected.willProperties(), actual.willProperties()); validateProperties(expected.willProperties(), actual.willProperties());
} }
private static void validateConnAckVariableHeader( private static void validateConnAckVariableHeader(
MqttConnAckVariableHeader expected, MqttConnAckVariableHeader expected,
MqttConnAckVariableHeader actual) { MqttConnAckVariableHeader actual) {
assertEquals( assertEquals(expected.connectReturnCode(), actual.connectReturnCode());
"MqttConnAckVariableHeader MqttConnectReturnCode mismatch",
expected.connectReturnCode(),
actual.connectReturnCode());
} }
private static void validatePublishVariableHeader( private static void validatePublishVariableHeader(
MqttPublishVariableHeader expected, MqttPublishVariableHeader expected,
MqttPublishVariableHeader actual) { MqttPublishVariableHeader actual) {
assertEquals("MqttPublishVariableHeader TopicName mismatch ", expected.topicName(), actual.topicName()); assertEquals(expected.topicName(), actual.topicName());
assertEquals("MqttPublishVariableHeader MessageId mismatch ", expected.packetId(), actual.packetId()); assertEquals(expected.packetId(), actual.packetId());
validateProperties(expected.properties(), actual.properties()); validateProperties(expected.properties(), actual.properties());
} }
private static void validatePublishPayload(ByteBuf expected, ByteBuf actual) { private static void validatePublishPayload(ByteBuf expected, ByteBuf actual) {
assertEquals("PublishPayload mismatch ", 0, expected.compareTo(actual)); assertEquals(0, expected.compareTo(actual));
} }
private static void validateMessageIdVariableHeader( private static void validateMessageIdVariableHeader(
MqttMessageIdVariableHeader expected, MqttMessageIdVariableHeader expected,
MqttMessageIdVariableHeader actual) { MqttMessageIdVariableHeader actual) {
assertEquals("MqttMessageIdVariableHeader MessageId mismatch ", expected.messageId(), actual.messageId()); assertEquals(expected.messageId(), actual.messageId());
} }
private static void validateSubAckPayload(MqttSubAckPayload expected, MqttSubAckPayload actual) { private static void validateSubAckPayload(MqttSubAckPayload expected, MqttSubAckPayload actual) {
assertArrayEquals( assertArrayEquals(expected.reasonCodes().toArray(), actual.reasonCodes().toArray());
"MqttSubAckPayload reason codes mismatch ", assertArrayEquals(expected.grantedQoSLevels().toArray(), actual.grantedQoSLevels().toArray());
expected.reasonCodes().toArray(),
actual.reasonCodes().toArray());
assertArrayEquals(
"MqttSubAckPayload QoS level mismatch ",
expected.grantedQoSLevels().toArray(),
actual.grantedQoSLevels().toArray());
} }
private static void validateDecoderExceptionTooLargeMessage(MqttMessage message) { private static void validateDecoderExceptionTooLargeMessage(MqttMessage message) {
assertNull("MqttMessage payload expected null ", message.payload()); assertNull(message.payload());
assertTrue(message.decoderResult().isFailure()); assertTrue(message.decoderResult().isFailure());
Throwable cause = message.decoderResult().cause(); Throwable cause = message.decoderResult().cause();
assertTrue("MqttMessage DecoderResult cause expected instance of DecoderException ", assertThat(cause, instanceOf(DecoderException.class));
cause instanceof DecoderException);
assertTrue("MqttMessage DecoderResult cause reason expect to contain 'too large message' ", assertTrue(cause.getMessage().contains("too large message:"));
cause.getMessage().contains("too large message:"));
} }
private static void validatePubReplyVariableHeader( private static void validatePubReplyVariableHeader(
MqttPubReplyMessageVariableHeader expected, MqttPubReplyMessageVariableHeader expected,
MqttPubReplyMessageVariableHeader actual) { MqttPubReplyMessageVariableHeader actual) {
assertEquals("MqttPubReplyMessageVariableHeader MessageId mismatch ", assertEquals(expected.messageId(), actual.messageId());
expected.messageId(), actual.messageId()); assertEquals(expected.reasonCode(), actual.reasonCode());
assertEquals("MqttPubReplyMessageVariableHeader reasonCode mismatch ",
expected.reasonCode(), actual.reasonCode());
final MqttProperties expectedProps = expected.properties(); final MqttProperties expectedProps = expected.properties();
final MqttProperties actualProps = actual.properties(); final MqttProperties actualProps = actual.properties();
@ -1116,8 +1086,7 @@ public class MqttCodecTest {
private void validatePacketIdAndPropertiesVariableHeader(MqttMessageIdAndPropertiesVariableHeader expected, private void validatePacketIdAndPropertiesVariableHeader(MqttMessageIdAndPropertiesVariableHeader expected,
MqttMessageIdAndPropertiesVariableHeader actual) { MqttMessageIdAndPropertiesVariableHeader actual) {
assertEquals("MqttMessageIdAndPropertiesVariableHeader MessageId mismatch ", assertEquals(expected.messageId(), actual.messageId());
expected.messageId(), actual.messageId());
final MqttProperties expectedProps = expected.properties(); final MqttProperties expectedProps = expected.properties();
final MqttProperties actualProps = actual.properties(); final MqttProperties actualProps = actual.properties();
validateProperties(expectedProps, actualProps); validateProperties(expectedProps, actualProps);
@ -1125,8 +1094,7 @@ public class MqttCodecTest {
private void validateReasonCodeAndPropertiesVariableHeader(MqttReasonCodeAndPropertiesVariableHeader expected, private void validateReasonCodeAndPropertiesVariableHeader(MqttReasonCodeAndPropertiesVariableHeader expected,
MqttReasonCodeAndPropertiesVariableHeader actual) { MqttReasonCodeAndPropertiesVariableHeader actual) {
assertEquals("MqttReasonCodeAndPropertiesVariableHeader reason mismatch ", assertEquals(expected.reasonCode(), actual.reasonCode());
expected.reasonCode(), actual.reasonCode());
final MqttProperties expectedProps = expected.properties(); final MqttProperties expectedProps = expected.properties();
final MqttProperties actualProps = actual.properties(); final MqttProperties actualProps = actual.properties();
validateProperties(expectedProps, actualProps); validateProperties(expectedProps, actualProps);

View File

@ -16,10 +16,10 @@
package io.netty.handler.codec.mqtt; package io.netty.handler.codec.mqtt;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Collections; import java.util.Collections;

View File

@ -16,21 +16,16 @@
package io.netty.handler.codec.mqtt; package io.netty.handler.codec.mqtt;
import org.junit.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.runner.RunWith; import org.junit.jupiter.params.provider.MethodSource;
import org.junit.runners.Parameterized;
import java.util.Arrays; import java.util.Arrays;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(value = Parameterized.class)
public class MqttMessageBuildersPacketIdTest { public class MqttMessageBuildersPacketIdTest {
@Parameterized.Parameter
public Integer id;
@Parameterized.Parameters(name = "{index}: {0}") static Iterable<Integer> data() {
public static Iterable<Integer> data() {
// we take a subset of valid packetIds // we take a subset of valid packetIds
return Arrays.asList( return Arrays.asList(
0x0001, 0x0001,
@ -41,8 +36,9 @@ public class MqttMessageBuildersPacketIdTest {
); );
} }
@Test @ParameterizedTest()
public void testUnsubAckMessageIdAsShort() { @MethodSource("data")
public void testUnsubAckMessageIdAsShort(Integer id) {
final MqttUnsubAckMessage msg = MqttMessageBuilders.unsubAck() final MqttUnsubAckMessage msg = MqttMessageBuilders.unsubAck()
.packetId(id.shortValue()) .packetId(id.shortValue())
.build(); .build();
@ -53,8 +49,9 @@ public class MqttMessageBuildersPacketIdTest {
); );
} }
@Test @ParameterizedTest
public void testSubAckMessageIdAsShort() { @MethodSource("data")
public void testSubAckMessageIdAsShort(Integer id) {
final MqttSubAckMessage msg = MqttMessageBuilders.subAck() final MqttSubAckMessage msg = MqttMessageBuilders.subAck()
.packetId(id.shortValue()) .packetId(id.shortValue())
.build(); .build();
@ -65,8 +62,9 @@ public class MqttMessageBuildersPacketIdTest {
); );
} }
@Test @ParameterizedTest
public void testPubAckMessageIdAsShort() { @MethodSource("data")
public void testPubAckMessageIdAsShort(Integer id) {
final MqttMessage msg = MqttMessageBuilders.pubAck() final MqttMessage msg = MqttMessageBuilders.pubAck()
.packetId(id.shortValue()) .packetId(id.shortValue())
.build(); .build();
@ -77,8 +75,9 @@ public class MqttMessageBuildersPacketIdTest {
); );
} }
@Test @ParameterizedTest
public void testUnsubAckMessageIdAsInt() { @MethodSource("data")
public void testUnsubAckMessageIdAsInt(Integer id) {
final MqttUnsubAckMessage msg = MqttMessageBuilders.unsubAck() final MqttUnsubAckMessage msg = MqttMessageBuilders.unsubAck()
.packetId(id) .packetId(id)
.build(); .build();
@ -89,8 +88,9 @@ public class MqttMessageBuildersPacketIdTest {
); );
} }
@Test @ParameterizedTest
public void testSubAckMessageIdAsInt() { @MethodSource("data")
public void testSubAckMessageIdAsInt(Integer id) {
final MqttSubAckMessage msg = MqttMessageBuilders.subAck() final MqttSubAckMessage msg = MqttMessageBuilders.subAck()
.packetId(id) .packetId(id)
.build(); .build();
@ -101,8 +101,9 @@ public class MqttMessageBuildersPacketIdTest {
); );
} }
@Test @ParameterizedTest
public void testPubAckMessageIdAsInt() { @MethodSource("data")
public void testPubAckMessageIdAsInt(Integer id) {
final MqttMessage msg = MqttMessageBuilders.pubAck() final MqttMessage msg = MqttMessageBuilders.pubAck()
.packetId(id) .packetId(id)
.build(); .build();

View File

@ -17,9 +17,9 @@
package io.netty.handler.codec.mqtt; package io.netty.handler.codec.mqtt;
import io.netty.handler.codec.mqtt.MqttMessageBuilders.PropertiesInitializer; import io.netty.handler.codec.mqtt.MqttMessageBuilders.PropertiesInitializer;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
public class MqttMessageBuildersTest { public class MqttMessageBuildersTest {

View File

@ -16,14 +16,14 @@
package io.netty.handler.codec.mqtt; package io.netty.handler.codec.mqtt;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static io.netty.handler.codec.mqtt.MqttQoS.AT_LEAST_ONCE; import static io.netty.handler.codec.mqtt.MqttQoS.AT_LEAST_ONCE;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static io.netty.handler.codec.mqtt.MqttTestUtils.validateProperties; import static io.netty.handler.codec.mqtt.MqttTestUtils.validateProperties;
import static io.netty.handler.codec.mqtt.MqttTestUtils.validateSubscribePayload; import static io.netty.handler.codec.mqtt.MqttTestUtils.validateSubscribePayload;
import static io.netty.handler.codec.mqtt.MqttTestUtils.validateUnsubscribePayload; import static io.netty.handler.codec.mqtt.MqttTestUtils.validateUnsubscribePayload;
@ -41,14 +41,14 @@ public class MqttMessageFactoryTest {
MqttMessage unsuback = MqttMessageFactory.newMessage(fixedHeader, variableHeader, null); MqttMessage unsuback = MqttMessageFactory.newMessage(fixedHeader, variableHeader, null);
assertEquals("Message type mismatch", MqttMessageType.UNSUBACK, unsuback.fixedHeader().messageType()); assertEquals(MqttMessageType.UNSUBACK, unsuback.fixedHeader().messageType());
MqttMessageIdAndPropertiesVariableHeader actualVariableHeader = MqttMessageIdAndPropertiesVariableHeader actualVariableHeader =
(MqttMessageIdAndPropertiesVariableHeader) unsuback.variableHeader(); (MqttMessageIdAndPropertiesVariableHeader) unsuback.variableHeader();
assertEquals("MessageId mismatch", SAMPLE_MESSAGE_ID, actualVariableHeader.messageId()); assertEquals(SAMPLE_MESSAGE_ID, actualVariableHeader.messageId());
validateProperties(MqttProperties.NO_PROPERTIES, actualVariableHeader.properties()); validateProperties(MqttProperties.NO_PROPERTIES, actualVariableHeader.properties());
MqttUnsubAckPayload actualPayload = (MqttUnsubAckPayload) unsuback.payload(); MqttUnsubAckPayload actualPayload = (MqttUnsubAckPayload) unsuback.payload();
assertNotNull("payload", actualPayload); assertNotNull(actualPayload);
assertEquals("reason codes size", 0, actualPayload.unsubscribeReasonCodes().size()); assertEquals(0, actualPayload.unsubscribeReasonCodes().size());
} }
@Test @Test
@ -66,15 +66,13 @@ public class MqttMessageFactoryTest {
MqttMessage unsuback = MqttMessageFactory.newMessage(fixedHeader, variableHeader, payload); MqttMessage unsuback = MqttMessageFactory.newMessage(fixedHeader, variableHeader, payload);
assertEquals("Message type mismatch", MqttMessageType.UNSUBACK, unsuback.fixedHeader().messageType()); assertEquals(MqttMessageType.UNSUBACK, unsuback.fixedHeader().messageType());
MqttMessageIdAndPropertiesVariableHeader actualVariableHeader = MqttMessageIdAndPropertiesVariableHeader actualVariableHeader =
(MqttMessageIdAndPropertiesVariableHeader) unsuback.variableHeader(); (MqttMessageIdAndPropertiesVariableHeader) unsuback.variableHeader();
assertEquals("MessageId mismatch", SAMPLE_MESSAGE_ID, actualVariableHeader.messageId()); assertEquals(SAMPLE_MESSAGE_ID, actualVariableHeader.messageId());
validateProperties(properties, actualVariableHeader.properties()); validateProperties(properties, actualVariableHeader.properties());
MqttUnsubAckPayload actualPayload = (MqttUnsubAckPayload) unsuback.payload(); MqttUnsubAckPayload actualPayload = (MqttUnsubAckPayload) unsuback.payload();
assertEquals("Reason code list doesn't match", assertEquals(payload.unsubscribeReasonCodes(), actualPayload.unsubscribeReasonCodes());
payload.unsubscribeReasonCodes(),
actualPayload.unsubscribeReasonCodes());
} }
@Test @Test
@ -89,10 +87,10 @@ public class MqttMessageFactoryTest {
MqttMessage subscribe = MqttMessageFactory.newMessage(fixedHeader, variableHeader, payload); MqttMessage subscribe = MqttMessageFactory.newMessage(fixedHeader, variableHeader, payload);
assertEquals("Message type mismatch", MqttMessageType.SUBSCRIBE, subscribe.fixedHeader().messageType()); assertEquals(MqttMessageType.SUBSCRIBE, subscribe.fixedHeader().messageType());
MqttMessageIdAndPropertiesVariableHeader actualVariableHeader = MqttMessageIdAndPropertiesVariableHeader actualVariableHeader =
(MqttMessageIdAndPropertiesVariableHeader) subscribe.variableHeader(); (MqttMessageIdAndPropertiesVariableHeader) subscribe.variableHeader();
assertEquals("MessageId mismatch", SAMPLE_MESSAGE_ID, actualVariableHeader.messageId()); assertEquals(SAMPLE_MESSAGE_ID, actualVariableHeader.messageId());
validateProperties(MqttProperties.NO_PROPERTIES, actualVariableHeader.properties()); validateProperties(MqttProperties.NO_PROPERTIES, actualVariableHeader.properties());
MqttSubscribePayload actualPayload = (MqttSubscribePayload) subscribe.payload(); MqttSubscribePayload actualPayload = (MqttSubscribePayload) subscribe.payload();
validateSubscribePayload(payload, actualPayload); validateSubscribePayload(payload, actualPayload);
@ -113,10 +111,10 @@ public class MqttMessageFactoryTest {
MqttMessage subscribe = MqttMessageFactory.newMessage(fixedHeader, variableHeader, payload); MqttMessage subscribe = MqttMessageFactory.newMessage(fixedHeader, variableHeader, payload);
assertEquals("Message type mismatch", MqttMessageType.SUBSCRIBE, subscribe.fixedHeader().messageType()); assertEquals(MqttMessageType.SUBSCRIBE, subscribe.fixedHeader().messageType());
MqttMessageIdAndPropertiesVariableHeader actualVariableHeader = MqttMessageIdAndPropertiesVariableHeader actualVariableHeader =
(MqttMessageIdAndPropertiesVariableHeader) subscribe.variableHeader(); (MqttMessageIdAndPropertiesVariableHeader) subscribe.variableHeader();
assertEquals("MessageId mismatch", SAMPLE_MESSAGE_ID, actualVariableHeader.messageId()); assertEquals(SAMPLE_MESSAGE_ID, actualVariableHeader.messageId());
validateProperties(properties, actualVariableHeader.properties()); validateProperties(properties, actualVariableHeader.properties());
MqttSubscribePayload actualPayload = (MqttSubscribePayload) subscribe.payload(); MqttSubscribePayload actualPayload = (MqttSubscribePayload) subscribe.payload();
validateSubscribePayload(payload, actualPayload); validateSubscribePayload(payload, actualPayload);
@ -134,10 +132,10 @@ public class MqttMessageFactoryTest {
MqttMessage unsubscribe = MqttMessageFactory.newMessage(fixedHeader, variableHeader, payload); MqttMessage unsubscribe = MqttMessageFactory.newMessage(fixedHeader, variableHeader, payload);
assertEquals("Message type mismatch", MqttMessageType.UNSUBSCRIBE, unsubscribe.fixedHeader().messageType()); assertEquals(MqttMessageType.UNSUBSCRIBE, unsubscribe.fixedHeader().messageType());
MqttMessageIdAndPropertiesVariableHeader actualVariableHeader = MqttMessageIdAndPropertiesVariableHeader actualVariableHeader =
(MqttMessageIdAndPropertiesVariableHeader) unsubscribe.variableHeader(); (MqttMessageIdAndPropertiesVariableHeader) unsubscribe.variableHeader();
assertEquals("MessageId mismatch", SAMPLE_MESSAGE_ID, actualVariableHeader.messageId()); assertEquals(SAMPLE_MESSAGE_ID, actualVariableHeader.messageId());
validateProperties(MqttProperties.NO_PROPERTIES, actualVariableHeader.properties()); validateProperties(MqttProperties.NO_PROPERTIES, actualVariableHeader.properties());
MqttUnsubscribePayload actualPayload = (MqttUnsubscribePayload) unsubscribe.payload(); MqttUnsubscribePayload actualPayload = (MqttUnsubscribePayload) unsubscribe.payload();
validateUnsubscribePayload(payload, actualPayload); validateUnsubscribePayload(payload, actualPayload);
@ -158,10 +156,10 @@ public class MqttMessageFactoryTest {
MqttMessage unsubscribe = MqttMessageFactory.newMessage(fixedHeader, variableHeader, payload); MqttMessage unsubscribe = MqttMessageFactory.newMessage(fixedHeader, variableHeader, payload);
assertEquals("Message type mismatch", MqttMessageType.UNSUBSCRIBE, unsubscribe.fixedHeader().messageType()); assertEquals(MqttMessageType.UNSUBSCRIBE, unsubscribe.fixedHeader().messageType());
MqttMessageIdAndPropertiesVariableHeader actualVariableHeader = MqttMessageIdAndPropertiesVariableHeader actualVariableHeader =
(MqttMessageIdAndPropertiesVariableHeader) unsubscribe.variableHeader(); (MqttMessageIdAndPropertiesVariableHeader) unsubscribe.variableHeader();
assertEquals("MessageId mismatch", SAMPLE_MESSAGE_ID, actualVariableHeader.messageId()); assertEquals(SAMPLE_MESSAGE_ID, actualVariableHeader.messageId());
validateProperties(properties, actualVariableHeader.properties()); validateProperties(properties, actualVariableHeader.properties());
MqttUnsubscribePayload actualPayload = (MqttUnsubscribePayload) unsubscribe.payload(); MqttUnsubscribePayload actualPayload = (MqttUnsubscribePayload) unsubscribe.payload();
validateUnsubscribePayload(payload, actualPayload); validateUnsubscribePayload(payload, actualPayload);

View File

@ -16,7 +16,8 @@
package io.netty.handler.codec.mqtt; package io.netty.handler.codec.mqtt;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -26,7 +27,7 @@ import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.CONTEN
import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.PAYLOAD_FORMAT_INDICATOR; import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.PAYLOAD_FORMAT_INDICATOR;
import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.SUBSCRIPTION_IDENTIFIER; import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.SUBSCRIPTION_IDENTIFIER;
import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.USER_PROPERTY; import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.USER_PROPERTY;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class MqttPropertiesTest { public class MqttPropertiesTest {
@ -46,10 +47,10 @@ public class MqttPropertiesTest {
public void testGetProperty() { public void testGetProperty() {
MqttProperties props = createSampleProperties(); MqttProperties props = createSampleProperties();
assertEquals("getProperty Content Type", assertEquals(
"text/plain", "text/plain",
((MqttProperties.StringProperty) props.getProperty(CONTENT_TYPE.value())).value); ((MqttProperties.StringProperty) props.getProperty(CONTENT_TYPE.value())).value);
assertEquals("getProperty Subscription ID", assertEquals(
10, 10,
((MqttProperties.IntegerProperty) props.getProperty(SUBSCRIPTION_IDENTIFIER.value())).value.intValue()); ((MqttProperties.IntegerProperty) props.getProperty(SUBSCRIPTION_IDENTIFIER.value())).value.intValue());
@ -59,21 +60,21 @@ public class MqttPropertiesTest {
expectedUserProps.add(new MqttProperties.StringPair("tag", "secondTag")); expectedUserProps.add(new MqttProperties.StringPair("tag", "secondTag"));
List<MqttProperties.StringPair> actualUserProps = List<MqttProperties.StringPair> actualUserProps =
((MqttProperties.UserProperties) props.getProperty(USER_PROPERTY.value())).value; ((MqttProperties.UserProperties) props.getProperty(USER_PROPERTY.value())).value;
assertEquals("getProperty User Properties", expectedUserProps, actualUserProps); assertEquals(expectedUserProps, actualUserProps);
} }
@Test @Test
public void testGetProperties() { public void testGetProperties() {
MqttProperties props = createSampleProperties(); MqttProperties props = createSampleProperties();
assertEquals("getProperties Content Type", assertEquals(
Collections.singletonList(new MqttProperties.StringProperty(CONTENT_TYPE.value(), "text/plain")), Collections.singletonList(new MqttProperties.StringProperty(CONTENT_TYPE.value(), "text/plain")),
props.getProperties(CONTENT_TYPE.value())); props.getProperties(CONTENT_TYPE.value()));
List<MqttProperties.IntegerProperty> expectedSubscriptionIds = new ArrayList<MqttProperties.IntegerProperty>(); List<MqttProperties.IntegerProperty> expectedSubscriptionIds = new ArrayList<MqttProperties.IntegerProperty>();
expectedSubscriptionIds.add(new MqttProperties.IntegerProperty(SUBSCRIPTION_IDENTIFIER.value(), 10)); expectedSubscriptionIds.add(new MqttProperties.IntegerProperty(SUBSCRIPTION_IDENTIFIER.value(), 10));
expectedSubscriptionIds.add(new MqttProperties.IntegerProperty(SUBSCRIPTION_IDENTIFIER.value(), 20)); expectedSubscriptionIds.add(new MqttProperties.IntegerProperty(SUBSCRIPTION_IDENTIFIER.value(), 20));
assertEquals("getProperties Subscription ID", assertEquals(
expectedSubscriptionIds, expectedSubscriptionIds,
props.getProperties(SUBSCRIPTION_IDENTIFIER.value())); props.getProperties(SUBSCRIPTION_IDENTIFIER.value()));
@ -83,7 +84,7 @@ public class MqttPropertiesTest {
expectedUserProps.add(new MqttProperties.UserProperty("tag", "secondTag")); expectedUserProps.add(new MqttProperties.UserProperty("tag", "secondTag"));
List<MqttProperties.UserProperty> actualUserProps = List<MqttProperties.UserProperty> actualUserProps =
(List<MqttProperties.UserProperty>) props.getProperties(USER_PROPERTY.value()); (List<MqttProperties.UserProperty>) props.getProperties(USER_PROPERTY.value());
assertEquals("getProperty User Properties", expectedUserProps, actualUserProps); assertEquals(expectedUserProps, actualUserProps);
} }
@Test @Test
@ -104,9 +105,7 @@ public class MqttPropertiesTest {
expectedProperties.add(expectedUserProperties); expectedProperties.add(expectedUserProperties);
assertEquals("listAll", assertEquals(expectedProperties, props.listAll());
expectedProperties,
props.listAll());
} }
} }