Search in sources :

Example 1 with Pair

use of com.microsoft.azure.sdk.iot.service.devicetwin.Pair in project azure-iot-sdk-java by Azure.

the class DeviceTwinDeviceTest method toStringReturnsAll.

/*
    **Tests_SRS_DEVICETWINDEVICE_25_015: [** This method shall append device id, tags, desired and reported properties to string (if present) and return **]**
    */
@Test
public void toStringReturnsAll() {
    //arrange
    DeviceTwinDevice testDevice = new DeviceTwinDevice("testDevice");
    Set<Pair> testDesProp = new HashSet<>();
    testDesProp.add(new Pair("testDes1", "desObject1"));
    testDesProp.add(new Pair("testDes2", "desObject2"));
    testDevice.setDesiredProperties(testDesProp);
    Set<Pair> testTags = new HashSet<>();
    testTags.add(new Pair("testTag1", "tagObject1"));
    testTags.add(new Pair("testTag2", "tagObject2"));
    testDevice.setTags(testTags);
    //act
    String testDeviceString = testDevice.toString();
    //assert
    assertTrue(testDeviceString.contains("testDes1"));
    assertTrue(testDeviceString.contains("desObject1"));
    assertTrue(testDeviceString.contains("testDes2"));
    assertTrue(testDeviceString.contains("desObject2"));
    assertTrue(testDeviceString.contains("testTag1"));
    assertTrue(testDeviceString.contains("tagObject1"));
    assertTrue(testDeviceString.contains("testTag2"));
    assertTrue(testDeviceString.contains("tagObject2"));
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) HashSet(java.util.HashSet) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) Test(org.junit.Test)

Example 2 with Pair

use of com.microsoft.azure.sdk.iot.service.devicetwin.Pair in project azure-iot-sdk-java by Azure.

the class DeviceTwinDeviceTest method setTagsSets.

/*
    **Tests_SRS_DEVICETWINDEVICE_25_007: [** This method shall convert the set of pairs of tags to a map and save it. **]**
     */
@Test
public void setTagsSets() {
    //arrange
    DeviceTwinDevice testDevice = new DeviceTwinDevice("testDevice");
    Set<Pair> testTags = new HashSet<>();
    testTags.add(new Pair("testTag", "tagObject"));
    //act
    testDevice.setTags(testTags);
    //assert
    Set<Pair> actualTags = testDevice.getTags();
    assertEquals(testTags.size(), actualTags.size());
    for (Pair test : actualTags) {
        assertTrue(test.getKey().equals("testTag"));
        assertTrue(test.getValue().equals("tagObject"));
    }
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) HashSet(java.util.HashSet) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) Test(org.junit.Test)

Example 3 with Pair

use of com.microsoft.azure.sdk.iot.service.devicetwin.Pair in project azure-iot-sdk-java by Azure.

the class DeviceTwinDeviceTest method clearClearsMapsDes.

@Test
public void clearClearsMapsDes() {
    //arrange
    DeviceTwinDevice testDevice = new DeviceTwinDevice("testDevice");
    Map<String, Object> testRep = new HashMap<>();
    testRep.put("testRep", "repObject");
    Deencapsulation.invoke(testDevice, "setDesiredProperties", testRep);
    //act
    testDevice.clearDesiredProperties();
    //assert
    Map<String, Object> actualTags = Deencapsulation.invoke(testDevice, "getDesiredMap");
    assertNull(actualTags);
    Set<Pair> actualTagsSet = testDevice.getDesiredProperties();
    assertTrue(actualTagsSet.size() == 0);
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) HashMap(java.util.HashMap) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) Test(org.junit.Test)

Example 4 with Pair

use of com.microsoft.azure.sdk.iot.service.devicetwin.Pair in project azure-iot-sdk-java by Azure.

the class DeviceTwinDeviceTest method gettersAlwaysCreatesNewSets.

@Test
public void gettersAlwaysCreatesNewSets() {
    //arrange
    DeviceTwinDevice testDevice = new DeviceTwinDevice("testDevice");
    Set<Pair> testDesProp = new HashSet<>();
    testDesProp.add(new Pair("testDes", "desObject"));
    testDevice.setDesiredProperties(testDesProp);
    //act
    Set<Pair> actualDesProp = testDevice.getDesiredProperties();
    //assert
    assertEquals(testDesProp.size(), actualDesProp.size());
    assertNotEquals(testDesProp, actualDesProp);
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) HashSet(java.util.HashSet) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) Test(org.junit.Test)

Example 5 with Pair

use of com.microsoft.azure.sdk.iot.service.devicetwin.Pair 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)

Aggregations

Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)32 Test (org.junit.Test)23 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)20 HashSet (java.util.HashSet)10 HashMap (java.util.HashMap)5 Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)3 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)1 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)1 Random (java.util.Random)1 ExecutorService (java.util.concurrent.ExecutorService)1 DeviceConnectionString (tests.integration.com.microsoft.azure.sdk.iot.device.DeviceConnectionString)1