use of com.microsoft.azure.sdk.iot.device.DeviceTwin in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method getDeviceTwinRequestCompleteTriggersStatusCB.
@Test
public void getDeviceTwinRequestCompleteTriggersStatusCB() throws IOException {
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
IotHubEventCallback deviceTwinRequestMessageCallback = Deencapsulation.newInnerInstance("deviceTwinRequestMessageCallback", testTwin);
deviceTwinRequestMessageCallback.execute(IotHubStatusCode.OK, null);
new Verifications() {
{
mockedStatusCB.execute(IotHubStatusCode.OK, null);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method getDeviceTwinResponseDoesNotCallUpdateTwinIfStatusNotOk.
/*
**Tests_SRS_DEVICETWIN_25_029: [**If the message is of type DeviceTwin and DEVICE_OPERATION_TWIN_GET_RESPONSE then the user call with a valid status is triggered.**]**
*/
@Test
public void getDeviceTwinResponseDoesNotCallUpdateTwinIfStatusNotOk(@Mocked TwinParser mockedTwinParserObject) throws IOException {
//arrange
final byte[] body = {};
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
MessageCallback deviceTwinResponseMessageCallback = Deencapsulation.newInnerInstance("deviceTwinResponseMessageCallback", testTwin);
final DeviceTwinMessage testMessage = new DeviceTwinMessage(body);
testMessage.setStatus(String.valueOf(401));
testMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_GET_RESPONSE);
//act
deviceTwinResponseMessageCallback.execute(testMessage, null);
//assert
final TwinParser actualTwinParserObj = Deencapsulation.getField(testTwin, "twinParser");
final IotHubEventCallback actualStatusCB = Deencapsulation.getField(testTwin, "deviceTwinStatusCallback");
new Verifications() {
{
actualStatusCB.execute(IotHubStatusCode.UNAUTHORIZED, withAny(new Object()));
times = 1;
actualTwinParserObj.updateTwin(anyString);
times = 0;
}
};
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method subscribeToDesiredSetsCorrectOperation.
/*
**Tests_SRS_DEVICETWIN_25_017: [**The method shall create a treemap to store callbacks for desired property notifications specified in onDesiredPropertyChange.**]**
**Tests_SRS_DEVICETWIN_25_018: [**If not already subscribed then this method shall create a device twin message with empty payload and set its type as DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST.**]**
**Tests_SRS_DEVICETWIN_25_019: [**If not already subscribed then this method shall send the message using sendEventAsync.**]**
*/
@Test
public void subscribeToDesiredSetsCorrectOperation(@Mocked final TwinParser mockedTwinParserObject, @Mocked final DeviceTwinMessage mockedDeviceTwinMessage, @Mocked final PropertyCallBack<String, Object> mockedDesiredCB) throws IOException {
new NonStrictExpectations() {
{
new TwinParser(withAny(new TwinChangedCallback() {
@Override
public void execute(Map<String, Object> map) {
}
}), withAny(new TwinChangedCallback() {
@Override
public void execute(Map<String, Object> map) {
}
}));
result = mockedTwinParserObject;
new DeviceTwinMessage(withAny(new byte[0]));
result = mockedDeviceTwinMessage;
}
};
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
Map<Property, Pair<PropertyCallBack<String, Object>, Object>> desiredMap = new HashMap<>();
desiredMap.put(new Property("DesiredProp", "DesiredValue"), new Pair<>(mockedDesiredCB, null));
testTwin.subscribeDesiredPropertiesNotification(desiredMap);
final ConcurrentSkipListMap<String, Pair<PropertyCallBack<String, Object>, Object>> actualMap = Deencapsulation.getField(testTwin, "onDesiredPropertyChangeMap");
assertNotNull(actualMap);
assertFalse(actualMap.isEmpty());
assertTrue(actualMap.containsKey("DesiredProp"));
assertEquals(actualMap.get("DesiredProp").getKey(), mockedDesiredCB);
new Verifications() {
{
mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST);
times = 1;
mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin 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 DeviceTwinMessage mockedDeviceTwinMessage) throws IOException {
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
final byte[] body = {};
new NonStrictExpectations() {
{
new DeviceTwinMessage(body);
result = mockedDeviceTwinMessage;
}
};
testTwin.getDeviceTwin();
new Verifications() {
{
mockedDeviceTwinMessage.setRequestId(anyString);
times = 1;
mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_GET_REQUEST);
times = 1;
mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.DeviceTwin in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method updateReportedPropOnResponseCallsStatusCBErrorIfNullStatus.
/*
**Tests_SRS_DEVICETWIN_25_028: [**If the message is of type DeviceTwin and DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE and if the status is null then the user is notified on the status callback registered by the user as ERROR.**]**
*/
@Test
public void updateReportedPropOnResponseCallsStatusCBErrorIfNullStatus(@Mocked TwinParser mockedTwinParserObject) throws IOException {
//arrange
final byte[] body = {};
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
MessageCallback deviceTwinResponseMessageCallback = Deencapsulation.newInnerInstance("deviceTwinResponseMessageCallback", testTwin);
final DeviceTwinMessage testMessage = new DeviceTwinMessage(body);
testMessage.setStatus(null);
testMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE);
//act
deviceTwinResponseMessageCallback.execute(testMessage, null);
//assert
final TwinParser actualTwinParserObj = Deencapsulation.getField(testTwin, "twinParser");
final IotHubEventCallback actualStatusCB = Deencapsulation.getField(testTwin, "deviceTwinStatusCallback");
new Verifications() {
{
actualStatusCB.execute(IotHubStatusCode.ERROR, withAny(new Object()));
times = 1;
actualTwinParserObj.updateTwin(anyString);
times = 0;
actualTwinParserObj.updateDesiredProperty(anyString);
times = 0;
}
};
}
Aggregations