Search in sources :

Example 11 with DeviceTwin

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

the class DeviceTwinSample method main.

/**
     * @param args
     * @throws IOException
     * @throws URISyntaxException
     */
public static void main(String[] args) throws Exception {
    System.out.println("Starting sample...");
    DeviceTwin twinClient = DeviceTwin.createFromConnectionString(iotHubConnectionString);
    DeviceTwinDevice device = new DeviceTwinDevice(deviceId);
    try {
        // Manage complete twin
        System.out.println("Getting device twin");
        twinClient.getTwin(device);
        System.out.println(device);
        //Update Twin Tags and Desired Properties
        Set<Pair> tags = new HashSet<Pair>();
        tags.add(new Pair("HomeID", UUID.randomUUID()));
        device.setTags(tags);
        Set<Pair> desProp = new HashSet<Pair>();
        int temp = new Random().nextInt(100);
        desProp.add(new Pair("temp", temp));
        device.setDesiredProperties(desProp);
        System.out.println("Updating device twin");
        twinClient.updateTwin(device);
        System.out.println("Getting device twin");
        twinClient.getTwin(device);
        System.out.println(device);
    } catch (IotHubException e) {
        System.out.println(e.getMessage());
    }
    System.out.println("Shutting down sample...");
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) Random(java.util.Random) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) HashSet(java.util.HashSet) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair)

Example 12 with DeviceTwin

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

the class DeviceTwinTest method constructorThrowsOnImproperCS.

@Test(expected = IllegalArgumentException.class)
public void constructorThrowsOnImproperCS() throws Exception {
    //arrange
    final String connectionString = "ImproperCSFormat";
    new NonStrictExpectations() {

        {
            IotHubConnectionStringBuilder.createConnectionString(connectionString);
            result = new IllegalArgumentException();
        }
    };
    //act
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 13 with DeviceTwin

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

the class DeviceTwinTest method updateTwinThrowsVerificationThrows.

@Test(expected = IotHubException.class)
public void updateTwinThrowsVerificationThrows() throws Exception {
    //arrange
    final String connectionString = "testString";
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    Map<String, Object> testMap = new HashMap<>();
    testMap.put("TestKey", "TestValue");
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            Deencapsulation.invoke(mockedDevice, "getDesiredMap");
            result = testMap;
            Deencapsulation.invoke(mockedDevice, "getTagsMap");
            result = testMap;
            Deencapsulation.invoke(mockedDevice, "getTwinParser");
            result = mockedTwinParser;
            mockedTwinParser.updateTwin((Map<String, Object>) any, null, (Map<String, Object>) any);
            result = "SomeJsonString";
            IotHubExceptionManager.httpResponseVerification(mockedHttpResponse);
            result = new IotHubException();
        }
    };
    //act
    testTwin.updateTwin(mockedDevice);
}
Also used : HashMap(java.util.HashMap) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 14 with DeviceTwin

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

the class DeviceTwinTest method constructorCreatesTwin.

/*
    **Tests_SRS_DEVICETWIN_25_002: [** The constructor shall create an IotHubConnectionStringBuilder object from the given connection string **]**
    **Tests_SRS_DEVICETWIN_25_003: [** The constructor shall create a new DeviceTwin instance and return it **]**
     */
@Test
public void constructorCreatesTwin(@Mocked IotHubConnectionStringBuilder mockedConnectionStringBuilder) throws Exception {
    //arrange
    final String connectionString = "testString";
    //act
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    //assert
    assertNotNull(testTwin);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 15 with DeviceTwin

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

the class DeviceTwinTest method getTwinThrowsOnNullDeviceID.

@Test(expected = IllegalArgumentException.class)
public void getTwinThrowsOnNullDeviceID() throws Exception {
    //arrange
    final String connectionString = "testString";
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = null;
        }
    };
    //act
    testTwin.getTwin(mockedDevice);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Aggregations

DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)30 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)29 Test (org.junit.Test)29 NonStrictExpectations (mockit.NonStrictExpectations)22 Verifications (mockit.Verifications)10 HashMap (java.util.HashMap)8 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)3 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)1 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1