use of com.microsoft.azure.sdk.iot.deps.serializer.TwinParser in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method constructorCreatesNewTwinObject.
/*
**Tests_SRS_DEVICETWIN_25_004: [**The constructor shall create a new twin object which will hence forth be used as a storage for all the properties provided by user.**]**
*/
@Test
public void constructorCreatesNewTwinObject(@Mocked final TwinParser mockedTwinParserObject) throws IOException {
new StrictExpectations() {
{
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;
}
};
//act
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
TwinParser actualTwinParserObj = Deencapsulation.getField(testTwin, "twinParser");
//assert
assertNotNull(actualTwinParserObj);
}
use of com.microsoft.azure.sdk.iot.deps.serializer.TwinParser 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.deps.serializer.TwinParser 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.deps.serializer.TwinParser 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;
}
};
}
use of com.microsoft.azure.sdk.iot.deps.serializer.TwinParser in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method updateReportedPropOnResponseCallsStatusCB.
/*
**Tests_SRS_DEVICETWIN_25_027: [**If the message is of type DeviceTwin and DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_RESPONSE then the user call with a valid status is triggered.**]**
*/
@Test
public void updateReportedPropOnResponseCallsStatusCB(@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(200));
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.OK, withAny(new Object()));
times = 1;
actualTwinParserObj.updateTwin(anyString);
times = 0;
actualTwinParserObj.updateDesiredProperty(anyString);
times = 0;
}
};
}
Aggregations