use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method updateReportedPropWithVersionCallsTwinAPIForSerialization.
/*
**Tests_SRS_DEVICETWIN_21_024: [**If the version is provided, this method shall set the version for the message by calling setVersion .**]**
*/
@Test
public void updateReportedPropWithVersionCallsTwinAPIForSerialization(@Mocked final IotHubTransportMessage mockedDeviceTwinMessage) throws IOException {
// arrange
final String prop1 = "prop1";
final String prop2 = "prop2";
final String val1 = "val1";
final int val2 = 100;
final HashSet<Property> reportedProp = new HashSet<Property>() {
{
add(new Property(prop1, val1));
add(new Property(prop2, val2));
}
};
final String json = "{\"" + prop2 + "\":" + val2 + ",\"" + prop1 + "\":\"" + val1 + "\"}";
new NonStrictExpectations() {
{
new IotHubTransportMessage(json.getBytes(StandardCharsets.UTF_8), MessageType.DEVICE_TWIN);
result = mockedDeviceTwinMessage;
}
};
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
// act
testTwin.updateReportedProperties(reportedProp, 10);
// assert
new Verifications() {
{
mockedDeviceTwinMessage.setRequestId(anyString);
times = 1;
mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST);
times = 1;
mockedDeviceTwinMessage.setVersion("10");
times = 1;
mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null, anyString);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method getDeviceTwinSetsDeviceTwinOperation.
/*
**Tests_SRS_DEVICETWIN_25_006: [**This method shall set the message type as DEVICE_OPERATION_TWIN_GET_REQUEST by calling setDeviceOperationType.**]**
*/
@Test
public void getDeviceTwinSetsDeviceTwinOperation(@Mocked final IotHubTransportMessage mockedDeviceTwinMessage) {
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
final byte[] body = {};
new NonStrictExpectations() {
{
new IotHubTransportMessage(body, MessageType.DEVICE_TWIN);
result = mockedDeviceTwinMessage;
}
};
testTwin.getDeviceTwin();
new Verifications() {
{
mockedDeviceTwinMessage.setRequestId(anyString);
times = 1;
mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_GET_REQUEST);
times = 1;
mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST);
times = 1;
mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null, null);
times = 2;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method getDeviceTwinSetsEmptyPayload.
/*
**Tests_SRS_DEVICETWIN_25_005: [**The method shall create a device twin message with empty payload to be sent IotHub.**]**
*/
@Test
public void getDeviceTwinSetsEmptyPayload(@Mocked final IotHubTransportMessage mockedDeviceTwinMessage) {
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
final byte[] body = {};
new NonStrictExpectations() {
{
new IotHubTransportMessage(body, MessageType.DEVICE_TWIN);
result = mockedDeviceTwinMessage;
}
};
testTwin.getDeviceTwin();
new Verifications() {
{
mockedDeviceTwinMessage.setRequestId(anyString);
times = 1;
mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_GET_REQUEST);
times = 1;
mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST);
times = 1;
mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null, null);
times = 2;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method getDeviceTwinSucceeds.
/*
**Tests_SRS_DEVICETWIN_25_005: [**The method shall create a device twin message with empty payload to be sent IotHub.**]**
**Tests_SRS_DEVICETWIN_25_007: [**This method shall set the request id for the message by calling setRequestId .**]**
**Tests_SRS_DEVICETWIN_25_008: [**This method shall send the message to the lower transport layers by calling sendEventAsync.**]**
*/
@Test
public void getDeviceTwinSucceeds(@Mocked final IotHubTransportMessage mockedDeviceTwinMessage) {
// arrange
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
final byte[] body = {};
new NonStrictExpectations() {
{
new IotHubTransportMessage(body, MessageType.DEVICE_TWIN);
result = mockedDeviceTwinMessage;
}
};
// act
testTwin.getDeviceTwin();
// assert
new Verifications() {
{
mockedDeviceTwinMessage.setRequestId(anyString);
times = 1;
mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_GET_REQUEST);
times = 1;
mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST);
times = 1;
mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null, null);
times = 2;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method getDeviceTwinResponseWithPropertiesCallsTwinPropertyCallBack.
@Test
public void getDeviceTwinResponseWithPropertiesCallsTwinPropertyCallBack(@Mocked final Property mockedProperty) {
// arrange
final String reportedProp1 = "ReportedProp1";
final String reportedVal2 = "ReportedValue2";
final Integer reportedVersion = 15;
final String desiredProp1 = "DesiredProp1";
final String desiredValue2 = "DesiredValue2";
final Integer desiredVersion = 10;
final String json = "{" + "\"desired\":{\"" + desiredProp1 + "\":\"" + desiredValue2 + "\", \"$version\":" + desiredVersion + "}," + "\"reported\":{\"" + reportedProp1 + "\":\"" + reportedVal2 + "\", \"$version\":" + reportedVersion + "}" + "}";
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericTwinPropertyCB, null);
MessageCallback deviceTwinResponseMessageCallback = Deencapsulation.newInnerInstance("deviceTwinResponseMessageCallback", testTwin);
final IotHubTransportMessage testMessage = new IotHubTransportMessage(json.getBytes(StandardCharsets.UTF_8), MessageType.DEVICE_TWIN);
testMessage.setStatus(String.valueOf(200));
testMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_GET_RESPONSE);
// act
deviceTwinResponseMessageCallback.execute(testMessage, null);
// assert
new Verifications() {
{
Deencapsulation.newInstance(Property.class, new Class[] { String.class, Object.class, Integer.class, boolean.class, Date.class, Integer.class, String.class, String.class }, desiredProp1, desiredValue2, desiredVersion, false, null, null, null, null);
times = 1;
Deencapsulation.newInstance(Property.class, new Class[] { String.class, Object.class, Integer.class, boolean.class, Date.class, Integer.class }, reportedProp1, reportedVal2, reportedVersion, true, null, null);
times = 1;
mockedGenericTwinPropertyCB.TwinPropertyCallBack((Property) any, null);
times = 2;
}
};
}
Aggregations