use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method sendDoesNotSendOnDifferentMessageType.
/*
Tests_SRS_MqttDeviceMethod_25_017: [**send method shall return if the message is not of Type DeviceMethod.**]**
*/
@Test
public void sendDoesNotSendOnDifferentMessageType(@Mocked final Mqtt mockMqtt, @Mocked final IotHubTransportMessage mockedMessage) throws TransportException {
final byte[] actualPayload = "TestMessage".getBytes(StandardCharsets.UTF_8);
final IotHubTransportMessage testMessage = new IotHubTransportMessage(actualPayload, MessageType.DEVICE_METHODS);
testMessage.setMessageType(MessageType.DEVICE_TWIN);
final MqttDeviceMethod testMethod = new MqttDeviceMethod("", mockConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
testMethod.start();
// act
testMethod.send(testMessage);
// assert
new Verifications() {
{
Deencapsulation.invoke(testMethod, "publish", anyString, mockedMessage);
maxTimes = 0;
Deencapsulation.invoke(testMethod, "subscribe", anyString);
maxTimes = 0;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method receiveReturnsEmptyPayLoadIfNullPayloadParsed.
@Test
public void receiveReturnsEmptyPayLoadIfNullPayloadParsed() throws TransportException {
// arrange
String topic = "$iothub/methods/POST/testMethod/?$rid=10";
byte[] actualPayload = "".getBytes(StandardCharsets.UTF_8);
testreceivedMessages.add(new MutablePair<>(topic, actualPayload));
MqttDeviceMethod testMethod = new MqttDeviceMethod("", mockConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
Deencapsulation.setField(testMethod, "receivedMessages", testreceivedMessages);
testMethod.start();
// act
Message testMessage = testMethod.receive();
IotHubTransportMessage testDMMessage = (IotHubTransportMessage) testMessage;
// assert
assertNotNull(testMessage);
assertEquals(0, testMessage.getBytes().length);
assertEquals(testMessage.getMessageType(), MessageType.DEVICE_METHODS);
assertEquals(testDMMessage.getRequestId(), String.valueOf(10));
assertEquals("testMethod", testDMMessage.getMethodName());
assertEquals(testDMMessage.getDeviceOperationType(), DEVICE_OPERATION_METHOD_RECEIVE_REQUEST);
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method sendThrowsOnNullRequestID.
// Tests_SRS_MqttDeviceMethod_25_021: [send method shall throw an IllegalArgumentException if message contains a null or empty request id if the operation is of type DEVICE_OPERATION_METHOD_SEND_RESPONSE.]
@Test(expected = IllegalArgumentException.class)
public void sendThrowsOnNullRequestID() throws TransportException {
final byte[] actualPayload = "TestMessage".getBytes(StandardCharsets.UTF_8);
final IotHubTransportMessage testMessage = new IotHubTransportMessage(actualPayload, MessageType.DEVICE_METHODS);
testMessage.setMessageType(MessageType.DEVICE_METHODS);
testMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_SEND_RESPONSE);
MqttDeviceMethod testMethod = new MqttDeviceMethod("", mockConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
testMethod.start();
// act
testMethod.send(testMessage);
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceMethodTest method sendThrowsIfNotStarted.
// Tests_SRS_MqttDeviceMethod_25_018: [send method shall throw a TransportException if device method has not been started yet.]
@Test(expected = TransportException.class)
public void sendThrowsIfNotStarted(@Mocked final Mqtt mockMqtt) throws TransportException {
final byte[] actualPayload = "TestMessage".getBytes(StandardCharsets.UTF_8);
final IotHubTransportMessage testMessage = new IotHubTransportMessage(actualPayload, MessageType.DEVICE_METHODS);
MqttDeviceMethod testMethod = new MqttDeviceMethod("", mockConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
// act
testMethod.send(testMessage);
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveSetsReqIdOnResTopic.
/*
**Tests_SRS_MQTTDEVICETWIN_25_040: [If the topic is of type response topic then this method shall parse further to look for request id which if found is set by calling setRequestId]
*/
@Test
public void receiveSetsReqIdOnResTopic() throws TransportException {
final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
final String expectedTopic = "$iothub/twin/res/" + "200" + "/?$rid=" + mockReqId;
IotHubTransportMessage receivedMessage = null;
try {
// arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
testreceivedMessages.add(new MutablePair<>(expectedTopic, actualPayload));
Deencapsulation.setField(testTwin, "receivedMessages", testreceivedMessages);
Deencapsulation.setField(testTwin, "stateLock", new Object());
Map<String, DeviceOperations> requestMap = new HashMap<>();
requestMap.put(mockReqId, DEVICE_OPERATION_TWIN_GET_REQUEST);
Deencapsulation.setField(testTwin, "requestMap", requestMap);
// act
receivedMessage = testTwin.receive();
} finally {
// assert
assertNotNull(receivedMessage);
assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_GET_RESPONSE);
assertEquals(receivedMessage.getRequestId(), mockReqId);
assertEquals("200", receivedMessage.getStatus());
assertNull(receivedMessage.getVersion());
}
}
Aggregations