Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue that message properties are not sent out when using MQTT #39

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.microsoft.azure.sdk.iot.device.transport.mqtt;

import com.microsoft.azure.sdk.iot.device.Message;
import com.microsoft.azure.sdk.iot.device.MessageProperty;

import java.io.IOException;
import java.util.Map;
Expand Down Expand Up @@ -167,10 +168,34 @@ public void send(Message message) throws IOException
throw new IOException("Message cannot be null");
}

MessageProperty[] messageProperties = message.getProperties();
String messagePublishTopic;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your change. This looks good and is much needed. Can you please also add a test case around this in device\iot-device-client\src\test\java\tests\unit\com\microsoft\azure\sdk\iot\device\transport\mqtt\MqttMessagingTest.java ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test case has been added, please review, thanks.

if(messageProperties.length > 0)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(this.publishTopic);
boolean needAmpersand = false;
for(MessageProperty property : message.getProperties())
{
if(needAmpersand)
{
stringBuilder.append('&');
}
stringBuilder.append(property.getName());
stringBuilder.append('=');
stringBuilder.append(property.getValue());
needAmpersand = true;
}
messagePublishTopic = stringBuilder.toString();
}
else
{
messagePublishTopic = this.publishTopic;
}
/*
**Codes_SRS_MqttMessaging_25_024: [**send method shall publish a message to the IOT Hub on the publish topic by calling method publish().**]**
*/
this.publish(this.publishTopic, message.getBytes());
this.publish(messagePublishTopic, message.getBytes());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.microsoft.azure.sdk.iot.device.Message;

import com.microsoft.azure.sdk.iot.device.MessageProperty;
import mockit.*;

import org.junit.Test;
Expand Down Expand Up @@ -435,7 +436,6 @@ public void sendShallMessageToLowerLayer(@Mocked final Mqtt mockMqtt) throws IOE
}
};


}

@Test (expected = IOException.class)
Expand Down Expand Up @@ -492,5 +492,45 @@ public void sendShallThrowIOExceptionIfMessageIsNull(@Mocked final Mqtt mockMqtt

}

/*
**Tests_SRS_MqttMessaging_25_026: [**send method shall publish a message to the IOT Hub on the publish topic with message property bag by calling method publish().**]**
*/
@Test
public void sendShallMessageWithPropertiesToLowerLayer(@Mocked final Mqtt mockMqtt) throws IOException
{
final byte[] messageBody = {0x61, 0x62, 0x63};
final String propertyName = "key";
final String propertyValue = "value";
final MessageProperty[] messageProperties = new MessageProperty[]
{
new MessageProperty(propertyName, propertyValue)
};
new NonStrictExpectations()
{
{
mockMessage.getBytes();
result = messageBody;
mockMessage.getProperties();
result = messageProperties;
mockMqtt.publish(anyString, messageBody);
}
};

MqttMessaging testMqttMessaging = new MqttMessaging(serverUri, clientId, userName, password);
testMqttMessaging.send(mockMessage);
final String publishTopicWithProperties = String.format(
"devices/%s/messages/events/%s=%s", clientId, propertyName, propertyValue);

new Verifications()
{
{
mockMessage.getBytes();
times = 2;
mockMessage.getProperties();
mockMqtt.publish(publishTopicWithProperties, messageBody);
times = 1;
}
};
}

}