use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method sendEventSendsMessageCorrectlyToIotHub.
// Tests_SRS_MQTTIOTHUBCONNECTION_15_008: [The function shall send an event message to the IoT Hub
// given in the configuration.]
// Tests_SRS_MQTTIOTHUBCONNECTION_15_009: [The function shall send the message payload.]
// Tests_SRS_MQTTIOTHUBCONNECTION_15_011: [If the message was successfully received by the service,
// the function shall return status code OK_EMPTY.]
@Test
public void sendEventSendsMessageCorrectlyToIotHub(@Mocked final Message mockMsg) throws IOException {
baseExpectations();
openExpectations();
final byte[] msgBody = { 0x61, 0x62, 0x63 };
new NonStrictExpectations() {
{
mockMsg.getBytes();
result = msgBody;
mockDeviceMessaging.send(mockMsg);
}
};
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.open();
IotHubStatusCode result = connection.sendEvent(mockMsg);
assertEquals(IotHubStatusCode.OK_EMPTY, result);
new Verifications() {
{
mockDeviceMessaging.send(mockMsg);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method sendEventFailsIfConnectionNotYetOpened.
// Tests_SRS_MQTTIOTHUBCONNECTION_15_013: [If the MQTT connection is closed,
// the function shall throw an IllegalStateException.]
@Test(expected = IllegalStateException.class)
public void sendEventFailsIfConnectionNotYetOpened(@Mocked final Message mockMsg) throws IOException {
baseExpectations();
final byte[] msgBody = { 0x61, 0x62, 0x63 };
new NonStrictExpectations() {
{
mockMsg.getBytes();
result = msgBody;
}
};
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.sendEvent(mockMsg);
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method receiveMessageFailsIfConnectionClosed.
// Tests_SRS_MQTTIOTHUBCONNECTION_15_015: [If the MQTT connection is closed,
// the function shall throw an IllegalStateException.]
@Test(expected = IllegalStateException.class)
public void receiveMessageFailsIfConnectionClosed(@Mocked final Message mockMsg) throws IOException {
baseExpectations();
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.receiveMessage();
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method receiveDeviceMethodMessageSucceeds.
@Test
public void receiveDeviceMethodMessageSucceeds() throws IOException {
baseExpectations();
openExpectations();
final byte[] expectedMessageBody = { 0x61, 0x62, 0x63 };
new NonStrictExpectations() {
{
mockDeviceMethods.receive();
result = new DeviceMethodMessage(expectedMessageBody);
}
};
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.open();
Message message = connection.receiveMessage();
byte[] actualMessageBody = message.getBytes();
assertNotNull(message);
for (int i = 0; i < expectedMessageBody.length; i++) {
assertEquals(expectedMessageBody[i], actualMessageBody[i]);
}
new Verifications() {
{
mockDeviceMethods.receive();
times = 1;
mockDeviceTwin.receive();
times = 0;
mockDeviceMessaging.receive();
times = 0;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection in project azure-iot-sdk-java by Azure.
the class MqttIotHubConnectionTest method sendEventSendsDeviceMethodMessage.
@Test
public void sendEventSendsDeviceMethodMessage(@Mocked final DeviceMethodMessage mockDeviceMethodMsg) throws IOException {
baseExpectations();
openExpectations();
final byte[] msgBody = { 0x61, 0x62, 0x63 };
new NonStrictExpectations() {
{
mockDeviceMethodMsg.getBytes();
result = msgBody;
mockDeviceMethodMsg.getMessageType();
result = MessageType.DeviceMethods;
}
};
MqttIotHubConnection connection = new MqttIotHubConnection(mockConfig);
connection.open();
IotHubStatusCode result = connection.sendEvent(mockDeviceMethodMsg);
assertEquals(IotHubStatusCode.OK_EMPTY, result);
new Verifications() {
{
mockDeviceMethods.start();
times = 1;
mockDeviceMethods.send(mockDeviceMethodMsg);
times = 1;
mockDeviceMessaging.send(mockDeviceMethodMsg);
times = 0;
mockDeviceTwin.send(mockDeviceMethodMsg);
times = 0;
}
};
}
Aggregations