Search in sources :

Example 11 with TwinCollection

use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.

the class DeviceTwin method updateReportedProperties.

public synchronized void updateReportedProperties(Set<Property> reportedProperties, Integer version, CorrelatingMessageCallback correlatingMessageCallback, Object correlatingMessageCallbackContext, IotHubEventCallback reportedPropertiesCallback, Object callbackContext) throws IOException {
    if (reportedProperties == null) {
        throw new IllegalArgumentException("Reported properties cannot be null");
    }
    TwinCollection reportedPropertiesMap = new TwinCollection();
    for (Property p : reportedProperties) {
        if (reportedPropertiesMap.containsKey(p.getKey())) {
            throw new IOException("Duplicate keys found in reported properties: " + p.getKey());
        }
        reportedPropertiesMap.putFinal(p.getKey(), p.getValue());
    }
    String serializedReportedProperties = reportedPropertiesMap.toJsonElement().toString();
    if (serializedReportedProperties == null) {
        return;
    }
    IotHubTransportMessage updateReportedPropertiesRequest = new IotHubTransportMessage(serializedReportedProperties.getBytes(StandardCharsets.UTF_8), MessageType.DEVICE_TWIN);
    updateReportedPropertiesRequest.setCorrelatingMessageCallback(correlatingMessageCallback);
    updateReportedPropertiesRequest.setCorrelatingMessageCallbackContext(correlatingMessageCallbackContext);
    updateReportedPropertiesRequest.setConnectionDeviceId(this.config.getDeviceId());
    // MQTT does not have the concept of correlationId for request/response handling but it does have a requestId
    // To handle this we are setting the correlationId to the requestId to better handle correlation
    // whether we use MQTT or AMQP.
    updateReportedPropertiesRequest.setRequestId(UUID.randomUUID().toString());
    updateReportedPropertiesRequest.setCorrelationId(updateReportedPropertiesRequest.getRequestId());
    if (version != null) {
        updateReportedPropertiesRequest.setVersion(Integer.toString(version));
    }
    updateReportedPropertiesRequest.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_UPDATE_REPORTED_PROPERTIES_REQUEST);
    this.deviceIO.sendEventAsync(updateReportedPropertiesRequest, reportedPropertiesCallback, callbackContext, this.config.getDeviceId());
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) IOException(java.io.IOException) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)

Example 12 with TwinCollection

use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.

the class ExportImportTests method createListofDevices.

private static List<ExportImportDevice> createListofDevices() {
    // Creating the list of devices to be created, then deleted
    Integer numberOfDevices = 10;
    List<ExportImportDevice> devicesForImport = new ArrayList<>(numberOfDevices);
    for (int i = 0; i < numberOfDevices; i++) {
        String deviceId = "java-bulk-test-" + UUID.randomUUID().toString();
        Device device = Device.createFromId(deviceId, null, null);
        AuthenticationMechanism authentication = new AuthenticationMechanism(device.getSymmetricKey());
        ExportImportDevice deviceToAdd = new ExportImportDevice();
        deviceToAdd.setId(deviceId);
        deviceToAdd.setAuthentication(authentication);
        deviceToAdd.setStatus(DeviceStatus.Enabled);
        TwinCollection tags = new TwinCollection();
        tags.putFinal("test01", "firstvalue");
        deviceToAdd.setTags(tags);
        devicesForImport.add(deviceToAdd);
    }
    return devicesForImport;
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) Device(com.microsoft.azure.sdk.iot.service.Device) ExportImportDevice(com.microsoft.azure.sdk.iot.service.ExportImportDevice) AuthenticationMechanism(com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism) ExportImportDevice(com.microsoft.azure.sdk.iot.service.ExportImportDevice) ArrayList(java.util.ArrayList)

Example 13 with TwinCollection

use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.

the class DeviceTwinTest method nextRetrievesCorrectlyWithoutModuleId.

// Tests_SRS_DEVICETWIN_25_059: [ The method shall parse the next element from the query response as Twin Document using TwinState and provide the response on DeviceTwinDevice.]
@Test
public void nextRetrievesCorrectlyWithoutModuleId() throws IotHubException, IOException {
    // arrange
    final Integer version = 15;
    final String etag = "validEtag";
    final String connectionString = "testString";
    final int connectTimeout = 1234;
    final int readTimeout = 5678;
    constructorExpectations(connectionString);
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString, DeviceTwinClientOptions.builder().httpConnectTimeout(connectTimeout).httpReadTimeout(readTimeout).build());
    final String expectedString = "testJsonAsNext";
    final String modelId = "testModelId";
    TwinCollection tags = new TwinCollection();
    tags.putFinal("tagsKey", "tagsValue");
    TwinCollection rp = new TwinCollection();
    rp.putFinal("rpKey", "rpValue");
    TwinCollection dp = new TwinCollection();
    dp.putFinal("dpKey", "dpValue");
    new Expectations() {

        {
            Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.TWIN);
            result = mockedQuery;
            Deencapsulation.invoke(mockedQuery, "next");
            result = expectedString;
            mockedTwinState.getDeviceId();
            result = "testDeviceID";
            mockedTwinState.getModuleId();
            result = null;
            mockedTwinState.getVersion();
            result = version;
            mockedTwinState.getETag();
            result = etag;
            mockedTwinState.getModelId();
            result = modelId;
            mockedTwinState.getTags();
            result = tags;
            mockedTwinState.getDesiredProperty();
            result = dp;
            mockedTwinState.getReportedProperty();
            result = rp;
            mockCapabilities.isIotEdge();
            result = Boolean.TRUE;
        }
    };
    Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
    // act
    DeviceTwinDevice result = testTwin.getNextDeviceTwin(testQuery);
    // assert
    assertNotNull(result.getTags());
    assertNotNull(result.getReportedProperties());
    assertNotNull(result.getDesiredProperties());
    assertEquals(version, result.getVersion());
    assertEquals(etag, result.getETag());
    assertEqualSetAndMap(result.getTags(), (Map) tags);
    assertEqualSetAndMap(result.getDesiredProperties(), (Map) dp);
    assertEqualSetAndMap(result.getReportedProperties(), (Map) rp);
    assertTrue(result.getCapabilities().isIotEdge());
    assertNull(result.getModuleId());
    assertEquals(result.getModelId(), result.getModelId());
}
Also used : Expectations(mockit.Expectations) StrictExpectations(mockit.StrictExpectations) NonStrictExpectations(mockit.NonStrictExpectations) TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 14 with TwinCollection

use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.

the class DeviceTwinTest method updateTwinDoesNotThrowsIfOnlyDesiredHasValue.

@Test
public void updateTwinDoesNotThrowsIfOnlyDesiredHasValue(@Mocked DeviceTwinDevice mockedDevice) throws Exception {
    // arrange
    final String connectionString = "testString";
    constructorExpectations(connectionString);
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    TwinCollection testMap = new TwinCollection();
    testMap.putFinal("TestKey", "TestValue");
    new Expectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            Deencapsulation.invoke(mockedDevice, "getDesiredMap");
            result = testMap;
            Deencapsulation.invoke(mockedDevice, "getTagsMap");
            result = null;
            new TwinState(null, (TwinCollection) any, null);
            result = mockedTwinState;
            mockedTwinState.toJsonElement().toString();
            result = "SomeJsonString";
        }
    };
    // act
    testTwin.updateTwin(mockedDevice);
    // assert
    new Verifications() {

        {
            IotHubConnectionString.getUrlTwin(anyString, anyString);
            times = 1;
            mockedHttpRequest.setReadTimeoutMillis(anyInt);
            times = 1;
            mockedHttpRequest.setHeaderField(anyString, anyString);
            times = 6;
            mockedHttpRequest.send();
            times = 1;
        }
    };
}
Also used : Expectations(mockit.Expectations) StrictExpectations(mockit.StrictExpectations) NonStrictExpectations(mockit.NonStrictExpectations) TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Verifications(mockit.Verifications) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 15 with TwinCollection

use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.

the class DeviceTwinTest method updateTwinSucceeds.

/*
    **Tests_SRS_DEVICETWIN_25_030: [** The function shall build the URL for this operation by calling getUrlTwinDesired **]**
    **Tests_SRS_DEVICETWIN_25_031: [** The function shall serialize the desired properties map by calling resetDesiredProperty Api on the twin object for the device provided by the user**]**
    **Tests_SRS_DEVICETWIN_25_016: [** The function shall create a new SAS token **]**

    **Tests_SRS_DEVICETWIN_25_017: [** The function shall create a new HttpRequest with http method as Patch **]**

    **Tests_SRS_DEVICETWIN_25_018: [** The function shall set the following HTTP headers specified in the IotHub DeviceTwin doc.
                                                1. Key as authorization with value as sastoken
                                                2. Key as request id with a new string value for every request
                                                3. Key as User-Agent with value specified by the clientIdentifier and its version
                                                4. Key as Accept with value as application/json
                                                5. Key as Content-Type and value as application/json
                                                6. Key as charset and value as utf-8
                                                7. Key as If-Match and value as '*'  **]**

    **Tests_SRS_DEVICETWIN_25_019: [** The function shall send the created request and get the response **]**

    **Tests_SRS_DEVICETWIN_25_020: [** The function shall verify the response status and throw proper Exception **]**

    *Tests_SRS_DEVICETWIN_25_036: [** The function shall verify the response status and throw proper Exception **]**

     */
@Test
public void updateTwinSucceeds(@Mocked DeviceTwinDevice mockedDevice) throws Exception {
    // arrange
    final String connectionString = "testString";
    constructorExpectations(connectionString);
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    TwinCollection testMap = new TwinCollection();
    testMap.putFinal("TestKey", "TestValue");
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            Deencapsulation.invoke(mockedDevice, "getDesiredMap");
            result = testMap;
            Deencapsulation.invoke(mockedDevice, "getTagsMap");
            result = testMap;
            new TwinState((TwinCollection) any, (TwinCollection) any, null);
            result = mockedTwinState;
            mockedTwinState.toJsonElement().toString();
            result = "SomeJsonString";
        }
    };
    // act
    testTwin.updateTwin(mockedDevice);
    // assert
    new Verifications() {

        {
            IotHubConnectionString.getUrlTwin(anyString, anyString);
            times = 1;
            mockedHttpRequest.setReadTimeoutMillis(anyInt);
            times = 1;
            mockedHttpRequest.setHeaderField(anyString, anyString);
            times = 6;
            mockedHttpRequest.send();
            times = 1;
        }
    };
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Aggregations

TwinCollection (com.microsoft.azure.sdk.iot.deps.twin.TwinCollection)23 Test (org.junit.Test)20 TwinState (com.microsoft.azure.sdk.iot.deps.twin.TwinState)12 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)9 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)9 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)9 Date (java.util.Date)9 NonStrictExpectations (mockit.NonStrictExpectations)9 Verifications (mockit.Verifications)5 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)3 Expectations (mockit.Expectations)3 StrictExpectations (mockit.StrictExpectations)3 MethodParser (com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)2 Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)2 SqlQuery (com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery)2 SimpleDateFormat (java.text.SimpleDateFormat)2 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)1 Pair (com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair)1 TwinPropertiesCallback (com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertiesCallback)1 ModuleClient (com.microsoft.azure.sdk.iot.device.ModuleClient)1