use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method receiveReturnsNullMessageIfTopicWasNotPost.
/*
Tests_SRS_MqttDeviceMethod_25_027: [**This method shall parse topic to look for Post topic ($iothub/methods/POST/) and throw unsupportedoperation exception other wise.**]**
*/
@Test(expected = UnsupportedOperationException.class)
public void receiveReturnsNullMessageIfTopicWasNotPost() throws IOException {
//arrange
String topic = "$iothub/methods/Not_POST/testMethod/?$rid=10";
byte[] actualPayload = "TestPayload".getBytes();
ConcurrentSkipListMap<String, byte[]> testAllReceivedMessages = new ConcurrentSkipListMap<>();
testAllReceivedMessages.put(topic, actualPayload);
Deencapsulation.setField(mockedMqtt, "allReceivedMessages", testAllReceivedMessages);
MqttDeviceMethod testMethod = new MqttDeviceMethod();
testMethod.start();
//act
Message testMessage = testMethod.receive();
}
use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnection method receiveMessage.
/**
* Receives a message, if one exists.
*
* @return the message received, or null if none exists.
*
* @throws IllegalStateException if the connection state is currently closed.
* @throws IOException if receiving on any of messaging clients fail.
*/
public Message receiveMessage() throws IllegalStateException, IOException {
// the function shall throw an IllegalStateException.]
if (this.state == State.CLOSED) {
throw new IllegalStateException("The MQTT connection is currently closed. Call open() before attempting " + "to receive a message.");
}
Message message = null;
// Codes_SRS_MQTTIOTHUBCONNECTION_15_014: [The function shall attempt to consume a message
// from various messaging clients.]
/*
**Codes_SRS_MQTTIOTHUBCONNECTION_25_016: [**If any of the messaging clients fail to receive, the function shall throw an IOException.**]**
*/
message = this.deviceMethod.receive();
if (message == null) {
message = deviceTwin.receive();
}
if (message == null) {
message = deviceMessaging.receive();
}
return message;
}
use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.
the class HttpsSingleMessage method toMessage.
/**
* Returns the Iot Hub message represented by the HTTPS message.
*
* @return the IoT Hub message represented by the HTTPS message.
*/
public Message toMessage() {
// Codes_SRS_HTTPSSINGLEMESSAGE_11_007: [The function shall return an IoT Hub message with a copy of the message body as its body.]
Message msg = new Message(this.getBody());
// Codes_SRS_HTTPSSINGLEMESSAGE_11_008: [The function shall return an IoT Hub message with application-defined properties that have the prefix 'iothub-app' removed.]
for (MessageProperty property : this.properties) {
String propertyName = httpsAppPropertyToAppProperty(property.getName());
msg.setProperty(propertyName, property.getValue());
}
return msg;
}
use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.
the class IotHubOutboundPacketTest method getMessageReturnsMessage.
// Tests_SRS_IOTHUBOUTBOUNDPACKET_11_001: [The constructor shall save the message, callback, and callback context.]
// Tests_SRS_IOTHUBOUTBOUNDPACKET_11_002: [The function shall return the message given in the constructor.]
@Test
public void getMessageReturnsMessage() {
final Map<String, Object> context = new HashMap<>();
IotHubOutboundPacket packet = new IotHubOutboundPacket(mockMsg, mockCallback, context);
Message testMsg = packet.getMessage();
final Message expectedMsg = mockMsg;
assertThat(testMsg, is(expectedMsg));
}
use of com.microsoft.azure.sdk.iot.device.Message in project azure-iot-sdk-java by Azure.
the class MqttTest method receiveThrowsIOExceptionWhenParsePayloadReturnsNull.
/*
**Tests_SRS_Mqtt_25_025: [**If the call to parsePayload returns null when topic is non-null then this method will throw IOException**]**
*/
@Test(expected = IOException.class)
public void receiveThrowsIOExceptionWhenParsePayloadReturnsNull() throws IOException, MqttException {
//arrange
final byte[] payload = { 0x61, 0x62, 0x63 };
baseConstructorExpectations(true);
new MockUp<MqttMessaging>() {
@Mock
String parseTopic() throws IOException {
return mockParseTopic;
}
@Mock
byte[] parsePayload(String topic) throws IOException {
return null;
}
};
final Mqtt mockMqtt = new MqttMessaging(serverUri, clientId, userName, password, mockIotHubSSLContext);
//act
try {
Message receivedMessage = mockMqtt.receive();
} finally {
testCleanUp(mockMqtt);
}
}
Aggregations