Search in sources :

Example 6 with Property

use of com.microsoft.azure.sdk.iot.device.DeviceTwin.Property in project azure-iot-sdk-java by Azure.

the class PropertyTest method constructorSetsKeyAndValue.

/*
    **Tests_SRS_Property_25_001: [**The constructor shall save the key and value representing this property.**]**
     */
@Test
public void constructorSetsKeyAndValue() {
    //act
    Property testProp = new Property("TestProp", 1);
    //assert
    assertNotNull(testProp);
    assertEquals(testProp.getKey(), "TestProp");
    assertEquals(testProp.getValue(), 1);
}
Also used : Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) Test(org.junit.Test)

Example 7 with Property

use of com.microsoft.azure.sdk.iot.device.DeviceTwin.Property in project azure-iot-sdk-java by Azure.

the class DeviceTwinIT method testSubscribeToDesiredPropertiesMultiThreaded.

@Test(timeout = MAX_MILLISECS_TIMEOUT_KILL_TEST)
public void testSubscribeToDesiredPropertiesMultiThreaded() throws IOException, InterruptedException, IotHubException {
    // arrange
    ExecutorService executor = Executors.newFixedThreadPool(MAX_PROPERTIES_TO_TEST);
    for (int i = 0; i < MAX_PROPERTIES_TO_TEST; i++) {
        PropertyState propertyState = new PropertyState();
        propertyState.callBackTriggered = false;
        propertyState.property = new Property(PROPERTY_KEY + i, PROPERTY_VALUE);
        deviceUnderTest.dCDeviceForTwin.propertyStateList.add(propertyState);
        deviceUnderTest.dCDeviceForTwin.setDesiredPropertyCallback(propertyState.property, deviceUnderTest.dCDeviceForTwin, propertyState);
    }
    // act
    deviceClient.subscribeToDesiredProperties(deviceUnderTest.dCDeviceForTwin.getDesiredProp());
    Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB);
    for (int i = 0; i < MAX_PROPERTIES_TO_TEST; i++) {
        final int index = i;
        executor.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    Set<Pair> desiredProperties = new HashSet<>();
                    desiredProperties.add(new Pair(PROPERTY_KEY + index, PROPERTY_VALUE_UPDATE + UUID.randomUUID()));
                    deviceUnderTest.sCDeviceForTwin.setDesiredProperties(desiredProperties);
                    sCDeviceTwin.updateTwin(deviceUnderTest.sCDeviceForTwin);
                } catch (IotHubException | IOException e) {
                    assertTrue(e.getMessage(), true);
                }
            }
        });
        Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB);
    }
    executor.shutdown();
    if (!executor.awaitTermination(10000, TimeUnit.MILLISECONDS)) {
        executor.shutdownNow();
    }
    // assert
    for (PropertyState propertyState : deviceUnderTest.dCDeviceForTwin.propertyStateList) {
        assertTrue(propertyState.property.toString(), propertyState.callBackTriggered);
        assertTrue(((String) propertyState.propertyNewValue).startsWith(PROPERTY_VALUE_UPDATE));
        assertEquals(deviceUnderTest.deviceTwinStatus, STATUS.SUCCESS);
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair)

Example 8 with Property

use of com.microsoft.azure.sdk.iot.device.DeviceTwin.Property in project azure-iot-sdk-java by Azure.

the class DeviceTwinIT method testSubscribeToDesiredPropertiesSequentially.

@Test(timeout = MAX_MILLISECS_TIMEOUT_KILL_TEST)
public void testSubscribeToDesiredPropertiesSequentially() throws IOException, InterruptedException, IotHubException {
    // arrange
    for (int i = 0; i < MAX_PROPERTIES_TO_TEST; i++) {
        PropertyState propertyState = new PropertyState();
        propertyState.callBackTriggered = false;
        propertyState.property = new Property(PROPERTY_KEY + i, PROPERTY_VALUE);
        deviceUnderTest.dCDeviceForTwin.propertyStateList.add(propertyState);
        deviceUnderTest.dCDeviceForTwin.setDesiredPropertyCallback(propertyState.property, deviceUnderTest.dCDeviceForTwin, propertyState);
    }
    // act
    deviceClient.subscribeToDesiredProperties(deviceUnderTest.dCDeviceForTwin.getDesiredProp());
    Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB);
    for (int i = 0; i < MAX_PROPERTIES_TO_TEST; i++) {
        Set<Pair> desiredProperties = new HashSet<>();
        desiredProperties.add(new Pair(PROPERTY_KEY + i, PROPERTY_VALUE_UPDATE + UUID.randomUUID()));
        deviceUnderTest.sCDeviceForTwin.setDesiredProperties(desiredProperties);
        sCDeviceTwin.updateTwin(deviceUnderTest.sCDeviceForTwin);
        Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB);
    }
    // assert
    for (PropertyState propertyState : deviceUnderTest.dCDeviceForTwin.propertyStateList) {
        assertTrue(propertyState.property.toString(), propertyState.callBackTriggered);
        assertTrue(((String) propertyState.propertyNewValue).startsWith(PROPERTY_VALUE_UPDATE));
        assertEquals(deviceUnderTest.deviceTwinStatus, STATUS.SUCCESS);
    }
}
Also used : Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair)

Example 9 with Property

use of com.microsoft.azure.sdk.iot.device.DeviceTwin.Property in project azure-iot-sdk-java by Azure.

the class DeviceTest method setDesiredPropertyCallbackAddsToDesiredProperty.

/*
    **Tests_SRS_DEVICE_25_006: [**The function shall add the property and its callback and context pair to the user map of desired properties.**]**
     */
@Test
public void setDesiredPropertyCallbackAddsToDesiredProperty() {
    //arrange
    Device testDev = new Device() {

        @Override
        public void PropertyCall(String propertyKey, Object propertyValue, Object context) {
        }
    };
    Property test = new Property("DesiredProp1", null);
    //act
    testDev.setDesiredPropertyCallback(test, testDev, null);
    //assert
    HashMap<Property, Pair<PropertyCallBack<String, Object>, Object>> testDesiredMap = testDev.getDesiredProp();
    assertNotNull(testDesiredMap);
    assertEquals(testDesiredMap.get(test).getKey(), testDev);
}
Also used : Device(com.microsoft.azure.sdk.iot.device.DeviceTwin.Device) Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) Pair(com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair) Test(org.junit.Test)

Example 10 with Property

use of com.microsoft.azure.sdk.iot.device.DeviceTwin.Property in project azure-iot-sdk-java by Azure.

the class DeviceTest method setDesiredPropertyCallbackCannotAddNullProperty.

/*
    **Tests_SRS_DEVICE_25_007: [**If the parameter desiredProp is null then this method shall throw IllegalArgumentException**]**
     */
@Test(expected = IllegalArgumentException.class)
public void setDesiredPropertyCallbackCannotAddNullProperty() {
    //arrange
    Device testDev = new Device() {

        @Override
        public void PropertyCall(String propertyKey, Object propertyValue, Object context) {
        }
    };
    Property test = new Property("DesiredProp1", null);
    //act
    testDev.setDesiredPropertyCallback(null, testDev, null);
}
Also used : Device(com.microsoft.azure.sdk.iot.device.DeviceTwin.Device) Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) Test(org.junit.Test)

Aggregations

Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)16 Test (org.junit.Test)13 Device (com.microsoft.azure.sdk.iot.device.DeviceTwin.Device)9 Pair (com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair)5 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)3 ExecutorService (java.util.concurrent.ExecutorService)1