use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method sendPublishesMessageForUpdateReportedPropertiesOnCorrectTopic.
/*
**Tests_SRS_MQTTDEVICETWIN_25_026: [send method shall build the update reported properties request topic of the format mentioned in spec ($iothub/twin/PATCH/properties/reported/?$rid={request id}&$version={base version}) if the operation is of type DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST.]
*/
@Test
public void sendPublishesMessageForUpdateReportedPropertiesOnCorrectTopic(@Mocked final Mqtt mockMqtt, @Mocked final IotHubTransportMessage mockMessage) throws TransportException {
// arrange
final byte[] actualPayload = { 0x61, 0x62, 0x63 };
final String expectedTopic = "$iothub/twin/PATCH/properties/reported/?$rid=" + mockReqId + "&$version=" + mockVersion;
MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
testTwin.start();
new NonStrictExpectations() {
{
mockMessage.getBytes();
result = actualPayload;
mockMessage.getMessageType();
result = MessageType.DEVICE_TWIN;
mockMessage.getDeviceOperationType();
result = DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST;
mockMessage.getRequestId();
result = mockReqId;
mockMessage.getVersion();
result = mockVersion;
}
};
// act
testTwin.send(mockMessage);
// assert
new Verifications() {
{
mockMessage.getBytes();
times = 1;
Deencapsulation.invoke(mockMqtt, "publish", expectedTopic, mockMessage);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveDoesNotSetVersionForDesiredPropNotifRespIfNotFound.
/*
**Tests_SRS_MQTTDEVICETWIN_25_042: [If the topic is of type patch for desired properties then this method shall parse further to look for version which if found is set by calling setVersion]
*/
@Test
public void receiveDoesNotSetVersionForDesiredPropNotifRespIfNotFound() throws TransportException {
final byte[] actualPayload = "NotificationResponseDataContainingDesiredPropertiesDocument".getBytes(StandardCharsets.UTF_8);
final String expectedTopic = "$iothub/twin/PATCH/properties/desired/";
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);
// act
receivedMessage = testTwin.receive();
} finally {
// assert
assertNotNull(receivedMessage);
assertSame(receivedMessage.getMessageType(), MessageType.DEVICE_TWIN);
assertSame(receivedMessage.getDeviceOperationType(), DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_RESPONSE);
assertNull(receivedMessage.getRequestId());
assertNull(receivedMessage.getStatus());
assertNull(receivedMessage.getVersion());
byte[] receivedMessageBytes = receivedMessage.getBytes();
assertEquals(receivedMessageBytes.length, actualPayload.length);
for (int i = 0; i < receivedMessageBytes.length; i++) {
assertEquals(receivedMessageBytes[i], actualPayload[i]);
}
}
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveSetsVersionOnResTopic.
/*
**Tests_SRS_MQTTDEVICETWIN_25_041: [If the topic is of type response topic then this method shall parse further to look for version which if found is set by calling setVersion]
*/
@Test
public void receiveSetsVersionOnResTopic() throws TransportException {
final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
final String expectedTopic = "$iothub/twin/res/" + "201" + "/?$rid=" + mockReqId + "&$version=" + mockVersion;
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);
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("201", receivedMessage.getStatus());
assertEquals(receivedMessage.getVersion(), mockVersion);
}
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class HttpsTransportManagerTest method invokeMethodOnModuleSuccess.
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_017: [This function shall call invokeMethod with the provided request and
// a uri in the format twins/<device id>/modules/<module id>/methods?api-version=<api_version>.]
@Test
public void invokeMethodOnModuleSuccess() throws TransportException, IOException, URISyntaxException {
// arrange
final HttpsTransportManager transportManager = new HttpsTransportManager(mockConfig);
final String expectedDeviceId = "myDevice";
final String expectedModuleId = "myModule";
final String expectedSenderDeviceId = "mySenderDevice";
final String expectedSenderModuleId = "mySenderModule";
final String expectedMethodRequestJson = "someJson";
final String expectedResponseBody = "some body";
// assert
new Expectations(HttpsTransportManager.class) {
{
mockedMethodRequest.toJson();
result = expectedMethodRequestJson;
new IotHubTransportMessage(expectedMethodRequestJson);
result = mockedTransportMessage;
mockedTransportMessage.setIotHubMethod(IotHubMethod.POST);
mockedTransportMessage.setUriPath("/twins/" + expectedDeviceId + "/modules/" + expectedModuleId + "/methods");
mockConfig.getDeviceId();
result = expectedSenderDeviceId;
mockConfig.getModuleId();
result = expectedSenderModuleId;
transportManager.send(mockedTransportMessage, (Map) any);
result = mockResponseMessage;
mockResponseMessage.getStatus();
result = IotHubStatusCode.OK_EMPTY;
mockResponseMessage.getBytes();
result = expectedResponseBody.getBytes(StandardCharsets.UTF_8);
new MethodResult(expectedResponseBody);
}
};
// act
transportManager.invokeMethod(mockedMethodRequest, expectedDeviceId, expectedModuleId);
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class HttpsTransportManagerTest method invokeMethodOnDeviceThrowsIfIotHubRespondsWithErrorCode.
// Tests_SRS_HTTPSTRANSPORTMANAGER_34_026 [If the http response contains an error code, this function shall throw the associated exception.]
@Test(expected = HubOrDeviceIdNotFoundException.class)
public void invokeMethodOnDeviceThrowsIfIotHubRespondsWithErrorCode() throws TransportException, IOException, URISyntaxException {
// arrange
final HttpsTransportManager transportManager = new HttpsTransportManager(mockConfig);
final String expectedDeviceId = "myDevice";
final String expectedSenderDeviceId = "mySenderDevice";
final String expectedSenderModuleId = "mySenderModule";
final String expectedMethodRequestJson = "someJson";
final String expectedResponseBody = "some body";
// assert
new Expectations(HttpsTransportManager.class) {
{
mockedMethodRequest.toJson();
result = expectedMethodRequestJson;
new IotHubTransportMessage(expectedMethodRequestJson);
result = mockedTransportMessage;
mockedTransportMessage.setIotHubMethod(IotHubMethod.POST);
mockedTransportMessage.setUriPath("/twins/" + expectedDeviceId + "/methods");
mockConfig.getDeviceId();
result = expectedSenderDeviceId;
mockConfig.getModuleId();
result = expectedSenderModuleId;
transportManager.send(mockedTransportMessage, (Map) any);
result = mockResponseMessage;
mockResponseMessage.getStatus();
result = IotHubStatusCode.HUB_OR_DEVICE_ID_NOT_FOUND;
mockResponseMessage.getBytes();
result = expectedResponseBody.getBytes(StandardCharsets.UTF_8);
}
};
// act
transportManager.invokeMethod(mockedMethodRequest, expectedDeviceId, "");
}
Aggregations