use of com.microsoft.azure.sdk.iot.deps.serializer.TwinChangedCallback in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method subscribeToDesiredCallsUserCBOnDesiredChangeIfUserCBFound.
/*
**Test_SRS_DEVICETWIN_25_022: [**OnDesiredPropertyChange callback shall look for the user registered call back on the property that changed provided in desiredPropertyMap and call the user providing the desired property change key and value pair**]**
*/
@Test
public void subscribeToDesiredCallsUserCBOnDesiredChangeIfUserCBFound(@Mocked final PropertyCallBack<String, Object> mockedDesiredCB) throws IOException {
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
Map<Property, Pair<PropertyCallBack<String, Object>, Object>> desiredMap = new HashMap<>();
desiredMap.put(new Property("DesiredProp1", "DesiredValue1"), new Pair<>(mockedDesiredCB, null));
testTwin.subscribeDesiredPropertiesNotification(desiredMap);
TwinChangedCallback onDesiredChange = Deencapsulation.newInnerInstance("OnDesiredPropertyChanged", testTwin);
final HashMap<String, Object> desiredPropertyMap = new HashMap<>();
desiredPropertyMap.put("DesiredProp1", "DesiredValue1");
//act
onDesiredChange.execute(desiredPropertyMap);
//assert
assertTrue(desiredPropertyMap.isEmpty());
new Verifications() {
{
mockedDesiredCB.PropertyCall("DesiredProp1", "DesiredValue1", null);
times = 1;
desiredPropertyMap.remove("DesiredProp1");
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.deps.serializer.TwinChangedCallback 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.TwinChangedCallback in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method updateReportedPropCallsTwinAPIForSerialization.
/*
**Tests_SRS_DEVICETWIN_25_011: [**The method shall send the property set to Twin Serializer for serilization by calling updateReportedProperty.**]**
*/
@Test
public void updateReportedPropCallsTwinAPIForSerialization(@Mocked final TwinParser mockedTwinParserObject, @Mocked final DeviceTwinMessage mockedDeviceTwinMessage) throws IOException {
final String mockedSerilizedProp = "SerializedReportedProperties";
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;
mockedTwinParserObject.updateReportedProperty(withAny(new HashMap<String, Object>()));
result = mockedSerilizedProp;
new DeviceTwinMessage(withAny(new byte[0]));
result = mockedDeviceTwinMessage;
}
};
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
HashSet<Property> reportedProp = new HashSet<>();
testTwin.updateReportedProperties(reportedProp);
new Verifications() {
{
mockedTwinParserObject.updateReportedProperty(withAny(new HashMap<String, Object>()));
times = 1;
mockedDeviceTwinMessage.setRequestId(anyString);
times = 1;
mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST);
times = 1;
mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.deps.serializer.TwinChangedCallback in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method subscribeToDesiredDoesNotSubscribeIfAlreadySubscribed.
@Test
public void subscribeToDesiredDoesNotSubscribeIfAlreadySubscribed(@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("DesiredProp1", "DesiredValue1"), new Pair<>(mockedDesiredCB, null));
testTwin.subscribeDesiredPropertiesNotification(desiredMap);
Deencapsulation.setField(testTwin, "isSubscribed", true);
desiredMap.put(new Property("DesiredProp2", "DesiredValue2"), 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("DesiredProp1"));
assertTrue(actualMap.containsKey("DesiredProp2"));
assertEquals(actualMap.get("DesiredProp2").getKey(), mockedDesiredCB);
new Verifications() {
{
mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST);
times = 1;
mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null);
times = 1;
}
};
}
Aggregations