use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class FileUploadTaskTest method runCreateRequestMessageThrows.
/* Tests_SRS_FILEUPLOADTASK_21_031: [If run failed to send the request, it shall call the userCallback with the status `ERROR`, and abort the upload.] */
@Test
public void runCreateRequestMessageThrows() throws IOException, IllegalArgumentException, URISyntaxException {
// arrange
new NonStrictExpectations() {
{
new FileUploadSasUriRequest(VALID_BLOB_NAME);
result = mockFileUploadSasUriRequest;
mockFileUploadSasUriRequest.toJson();
result = VALID_REQUEST_JSON;
new IotHubTransportMessage(VALID_REQUEST_JSON);
result = new IllegalArgumentException();
}
};
FileUploadTask fileUploadTask = Deencapsulation.newInstance(FileUploadTask.class, new Class[] { String.class, InputStream.class, long.class, HttpsTransportManager.class, IotHubEventCallback.class, Object.class }, VALID_BLOB_NAME, mockInputStream, VALID_STREAM_LENGTH, mockHttpsTransportManager, mockIotHubEventCallback, VALID_CALLBACK_CONTEXT);
// act
Deencapsulation.invoke(fileUploadTask, "run");
// assert
new Verifications() {
{
mockIotHubEventCallback.execute(IotHubStatusCode.ERROR, VALID_CALLBACK_CONTEXT);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttTest method deliveryCompleteDoesNotNotifyListenerIfSubscribeToDesiredProperties.
// Tests_SRS_Mqtt_34_056: [If the acknowledged message is of type
// DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST, DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST,
// or DEVICE_OPERATION_TWIN_UNSUBSCRIBE_DESIRED_PROPERTIES_REQUEST, this function shall not notify the saved
// listener that the message was sent.]
@Test
public void deliveryCompleteDoesNotNotifyListenerIfSubscribeToDesiredProperties() throws TransportException {
// arrange
final int expectedMessageId = 13;
final Message otherMessage = new Message();
final IotHubTransportMessage expectedMessage = new IotHubTransportMessage("some body");
expectedMessage.setDeviceOperationType(DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST);
Mqtt mockMqtt = instantiateMqtt(true, mockedIotHubListener);
Map<Integer, Message> unacknowledgedMessages = new HashMap<>();
unacknowledgedMessages.put(12, otherMessage);
unacknowledgedMessages.put(expectedMessageId, expectedMessage);
Deencapsulation.setField(mockMqtt, "unacknowledgedSentMessages", unacknowledgedMessages);
new NonStrictExpectations() {
{
mockMqttDeliveryToken.getMessageId();
result = expectedMessageId;
}
};
// act
mockMqtt.deliveryComplete(mockMqttDeliveryToken);
// assert
new Verifications() {
{
mockedIotHubListener.onMessageSent(expectedMessage, null, null);
times = 0;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttTest method deliveryCompleteDoesNotNotifyListenerIfUnsubscribeToDesiredProperties.
// Tests_SRS_Mqtt_34_056: [If the acknowledged message is of type
// DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST, DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST,
// or DEVICE_OPERATION_TWIN_UNSUBSCRIBE_DESIRED_PROPERTIES_REQUEST, this function shall not notify the saved
// listener that the message was sent.]
@Test
public void deliveryCompleteDoesNotNotifyListenerIfUnsubscribeToDesiredProperties() throws TransportException {
// arrange
final int expectedMessageId = 13;
final Message otherMessage = new Message();
final IotHubTransportMessage expectedMessage = new IotHubTransportMessage("some body");
expectedMessage.setDeviceOperationType(DEVICE_OPERATION_TWIN_UNSUBSCRIBE_DESIRED_PROPERTIES_REQUEST);
Mqtt mockMqtt = instantiateMqtt(true, mockedIotHubListener);
Map<Integer, Message> unacknowledgedMessages = new HashMap<>();
unacknowledgedMessages.put(12, otherMessage);
unacknowledgedMessages.put(expectedMessageId, expectedMessage);
Deencapsulation.setField(mockMqtt, "unacknowledgedSentMessages", unacknowledgedMessages);
new NonStrictExpectations() {
{
mockMqttDeliveryToken.getMessageId();
result = expectedMessageId;
}
};
// act
mockMqtt.deliveryComplete(mockMqttDeliveryToken);
// assert
new Verifications() {
{
mockedIotHubListener.onMessageSent(expectedMessage, null, null);
times = 0;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class MqttTest method deliveryCompleteDoesNotNotifyListenerIfSubscribeToMethods.
// Tests_SRS_Mqtt_34_056: [If the acknowledged message is of type
// DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST, DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST,
// or DEVICE_OPERATION_TWIN_UNSUBSCRIBE_DESIRED_PROPERTIES_REQUEST, this function shall not notify the saved
// listener that the message was sent.]
@Test
public void deliveryCompleteDoesNotNotifyListenerIfSubscribeToMethods() throws TransportException {
// arrange
final int expectedMessageId = 13;
final Message otherMessage = new Message();
final IotHubTransportMessage expectedMessage = new IotHubTransportMessage("some body");
expectedMessage.setDeviceOperationType(DEVICE_OPERATION_METHOD_SUBSCRIBE_REQUEST);
Mqtt mockMqtt = instantiateMqtt(true, mockedIotHubListener);
Map<Integer, Message> unacknowledgedMessages = new HashMap<>();
unacknowledgedMessages.put(12, otherMessage);
unacknowledgedMessages.put(expectedMessageId, expectedMessage);
Deencapsulation.setField(mockMqtt, "unacknowledgedSentMessages", unacknowledgedMessages);
new NonStrictExpectations() {
{
mockMqttDeliveryToken.getMessageId();
result = expectedMessageId;
}
};
// act
mockMqtt.deliveryComplete(mockMqttDeliveryToken);
// assert
new Verifications() {
{
mockedIotHubListener.onMessageSent(expectedMessage, null, null);
times = 0;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class DeviceTwin method updateReportedProperties.
public synchronized void updateReportedProperties(Set<Property> reportedProperties, Integer version, CorrelatingMessageCallback correlatingMessageCallback, Object correlatingMessageCallbackContext, IotHubEventCallback reportedPropertiesCallback, Object callbackContext) throws IOException {
if (reportedProperties == null) {
throw new IllegalArgumentException("Reported properties cannot be null");
}
TwinCollection reportedPropertiesMap = new TwinCollection();
for (Property p : reportedProperties) {
if (reportedPropertiesMap.containsKey(p.getKey())) {
throw new IOException("Duplicate keys found in reported properties: " + p.getKey());
}
reportedPropertiesMap.putFinal(p.getKey(), p.getValue());
}
String serializedReportedProperties = reportedPropertiesMap.toJsonElement().toString();
if (serializedReportedProperties == null) {
return;
}
IotHubTransportMessage updateReportedPropertiesRequest = new IotHubTransportMessage(serializedReportedProperties.getBytes(StandardCharsets.UTF_8), MessageType.DEVICE_TWIN);
updateReportedPropertiesRequest.setCorrelatingMessageCallback(correlatingMessageCallback);
updateReportedPropertiesRequest.setCorrelatingMessageCallbackContext(correlatingMessageCallbackContext);
updateReportedPropertiesRequest.setConnectionDeviceId(this.config.getDeviceId());
// MQTT does not have the concept of correlationId for request/response handling but it does have a requestId
// To handle this we are setting the correlationId to the requestId to better handle correlation
// whether we use MQTT or AMQP.
updateReportedPropertiesRequest.setRequestId(UUID.randomUUID().toString());
updateReportedPropertiesRequest.setCorrelationId(updateReportedPropertiesRequest.getRequestId());
if (version != null) {
updateReportedPropertiesRequest.setVersion(Integer.toString(version));
}
updateReportedPropertiesRequest.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST);
this.deviceIO.sendEventAsync(updateReportedPropertiesRequest, reportedPropertiesCallback, callbackContext, this.config.getDeviceId());
}
Aggregations