-
Notifications
You must be signed in to change notification settings - Fork 884
/
MqttPubAckTest.java
108 lines (92 loc) · 4.66 KB
/
MqttPubAckTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*******************************************************************************
* Copyright (c) 2016 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* https://www.eclipse.org/legal/epl-2.0
* and the Eclipse Distribution License is available at
* https://www.eclipse.org/org/documents/edl-v10.php
*
* Contributors:
* Dave Locke - Original MQTTv3 implementation
* James Sutton - Initial MQTTv5 implementation
*/
package org.eclipse.paho.mqttv5.common.packet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.junit.Assert;
import org.junit.Test;
public class MqttPubAckTest {
private static final int returnCode = MqttReturnCode.RETURN_CODE_UNSPECIFIED_ERROR;
private static final String reasonString = "Reason String 123.";
private static final String userKey1 = "userKey1";
private static final String userKey2 = "userKey2";
private static final String userKey3 = "userKey3";
private static final String userValue1 = "userValue1";
private static final String userValue2 = "userValue2";
private static final String userValue3 = "userValue3";
@Test
public void testEncodingMqttPuback() throws MqttException {
MqttPubAck mqttPubackPacket = generateMqttPubackPacket();
mqttPubackPacket.getHeader();
mqttPubackPacket.getPayload();
}
@Test
public void testDecodingMqttPuback() throws MqttException, IOException {
MqttPubAck mqttPubackPacket = generateMqttPubackPacket();
byte[] header = mqttPubackPacket.getHeader();
byte[] payload = mqttPubackPacket.getPayload();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(header);
outputStream.write(payload);
MqttPubAck decodedPubackPacket = (MqttPubAck) MqttWireMessage.createWireMessage(outputStream.toByteArray());
Assert.assertEquals(returnCode, decodedPubackPacket.getReturnCode());
MqttProperties decodedProperties = decodedPubackPacket.getProperties();
Assert.assertEquals(reasonString, decodedProperties.getReasonString());
Assert.assertTrue(new UserProperty(userKey1, userValue1).equals(decodedProperties.getUserProperties().get(0)));
Assert.assertTrue(new UserProperty(userKey2, userValue2).equals(decodedProperties.getUserProperties().get(1)));
Assert.assertTrue(new UserProperty(userKey3, userValue3).equals(decodedProperties.getUserProperties().get(2)));
}
@Test
public void testDecodingSmallMqttPuback() throws MqttException, IOException {
MqttPubAck mqttPubackPacket = new MqttPubAck(MqttReturnCode.RETURN_CODE_TOPIC_NAME_INVALID, 1, new MqttProperties());
byte[] header = mqttPubackPacket.getHeader();
byte[] payload = mqttPubackPacket.getPayload();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(header);
outputStream.write(payload);
// Total Packet should be 5 bytes long
Assert.assertEquals(5, outputStream.size());
MqttPubAck decodedPubackPacket = (MqttPubAck) MqttWireMessage.createWireMessage(outputStream.toByteArray());
Assert.assertEquals(MqttReturnCode.RETURN_CODE_TOPIC_NAME_INVALID, decodedPubackPacket.getReturnCode());
}
@Test
public void testDecodingVerySmallMqttPuback() throws MqttException, IOException {
MqttPubAck mqttPubackPacket = new MqttPubAck(MqttReturnCode.RETURN_CODE_SUCCESS, 1, new MqttProperties());
byte[] header = mqttPubackPacket.getHeader();
byte[] payload = mqttPubackPacket.getPayload();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(header);
outputStream.write(payload);
// Total Packet should be 4 bytes long
Assert.assertEquals(4, outputStream.size());
MqttPubAck decodedPubackPacket = (MqttPubAck) MqttWireMessage.createWireMessage(outputStream.toByteArray());
Assert.assertEquals(MqttReturnCode.RETURN_CODE_SUCCESS, decodedPubackPacket.getReturnCode());
}
public MqttPubAck generateMqttPubackPacket() throws MqttException{
ArrayList<UserProperty> userDefinedProperties = new ArrayList<UserProperty>();
userDefinedProperties.add(new UserProperty(userKey1, userValue1));
userDefinedProperties.add(new UserProperty(userKey2, userValue2));
userDefinedProperties.add(new UserProperty(userKey3, userValue3));
MqttProperties properties = new MqttProperties();
properties.setReasonString(reasonString);
properties.setUserProperties(userDefinedProperties);
MqttPubAck mqttPubackPacket = new MqttPubAck(returnCode,1, properties);
return mqttPubackPacket;
}
}