Search in sources :

Example 1 with TwinState

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

the class DeviceTwin method replaceTwin.

/**
 * Replace the full device twin for a given device with the provided device twin.
 *
 * @param device The device twin object to replace the current device twin object.
 * @throws IotHubException If any an IoT hub level exception is thrown. For instance,
 * if the request is unauthorized, a exception that extends IotHubException will be thrown.
 * @throws IOException If the request failed to send to IoT hub.
 * @return The Twin object's current state returned from the service after the replace operation.
 */
public DeviceTwinDevice replaceTwin(DeviceTwinDevice device) throws IotHubException, IOException {
    if (device == null || device.getDeviceId() == null || device.getDeviceId().length() == 0) {
        throw new IllegalArgumentException("Instantiate a device and set device Id to be used.");
    }
    URL url;
    if ((device.getModuleId() == null) || device.getModuleId().length() == 0) {
        url = this.iotHubConnectionString.getUrlTwin(device.getDeviceId());
    } else {
        url = this.iotHubConnectionString.getUrlModuleTwin(device.getDeviceId(), device.getModuleId());
    }
    TwinState twinState = new TwinState(device.getTagsMap(), device.getDesiredMap(), null);
    String twinJson = twinState.toJsonElement().toString();
    Proxy proxy = options.getProxyOptions() == null ? null : options.getProxyOptions().getProxy();
    HttpResponse httpResponse = DeviceOperations.request(this.iotHubConnectionString, url, HttpMethod.PUT, twinJson.getBytes(StandardCharsets.UTF_8), String.valueOf(requestId++), options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    String responseTwinJson = new String(httpResponse.getBody(), StandardCharsets.UTF_8);
    twinState = TwinState.createFromTwinJson(responseTwinJson);
    DeviceTwinDevice responseTwin;
    if (twinState.getModuleId() == null) {
        responseTwin = new DeviceTwinDevice(twinState.getDeviceId());
    } else {
        responseTwin = new DeviceTwinDevice(twinState.getDeviceId(), twinState.getModuleId());
    }
    responseTwin.setVersion(twinState.getVersion());
    responseTwin.setModelId(twinState.getModelId());
    responseTwin.setETag(twinState.getETag());
    responseTwin.setTags(twinState.getTags());
    responseTwin.setDesiredProperties(twinState.getDesiredProperty());
    responseTwin.setReportedProperties(twinState.getReportedProperty());
    responseTwin.setCapabilities(twinState.getCapabilities());
    responseTwin.setConfigurations(twinState.getConfigurations());
    responseTwin.setConnectionState(twinState.getConnectionState());
    return responseTwin;
}
Also used : TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) Proxy(java.net.Proxy) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URL(java.net.URL)

Example 2 with TwinState

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

the class DeviceTwin method jsonToDeviceTwinDevice.

private DeviceTwinDevice jsonToDeviceTwinDevice(String json) {
    TwinState twinState = TwinState.createFromTwinJson(json);
    DeviceTwinDevice deviceTwinDevice = new DeviceTwinDevice(twinState.getDeviceId());
    deviceTwinDevice.setVersion(twinState.getVersion());
    deviceTwinDevice.setETag(twinState.getETag());
    deviceTwinDevice.setTags(twinState.getTags());
    deviceTwinDevice.setDesiredProperties(twinState.getDesiredProperty());
    deviceTwinDevice.setReportedProperties(twinState.getReportedProperty());
    deviceTwinDevice.setCapabilities(twinState.getCapabilities());
    deviceTwinDevice.setConnectionState(twinState.getConnectionState());
    deviceTwinDevice.setConfigurations(twinState.getConfigurations());
    deviceTwinDevice.setModelId(twinState.getModelId());
    deviceTwinDevice.setDeviceScope(twinState.getDeviceScope());
    deviceTwinDevice.setParentScopes(twinState.getParentScopes());
    if (twinState.getModuleId() != null && !twinState.getModuleId().isEmpty()) {
        deviceTwinDevice.setModuleId(twinState.getModuleId());
    }
    return deviceTwinDevice;
}
Also used : TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState)

Example 3 with TwinState

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

the class JobResultTest method constructorParseJson.

/* Tests_SRS_JOBRESULT_21_002: [The constructor shall parse the body using the JobsResponseParser.] */
@Test
public void constructorParseJson() throws IOException {
    // arrange
    final String json = "validJson";
    TwinCollection tags = new TwinCollection();
    tags.putFinal("tag1", "val1");
    TwinState twinState = new TwinState(tags, null, null);
    twinState.setDeviceId(DEVICE_ID);
    twinState.setETag(ETAG);
    JobsResponseParserExpectations(json, twinState, null, new Date(), null, "scheduleUpdateTwin");
    // act
    JobResult jobResult = Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, json.getBytes(StandardCharsets.UTF_8));
    // assert
    new Verifications() {

        {
            JobsResponseParser.createFromJson(json);
            times = 1;
        }
    };
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) Date(java.util.Date) Test(org.junit.Test)

Example 4 with TwinState

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

the class JobClientTest method scheduleUpdateThrowsOnInvalidJobId.

/* Tests_SRS_JOBCLIENT_21_005: [If the JobId is null, empty, or invalid, the scheduleUpdateTwin shall throws IllegalArgumentException.] */
@Test(expected = IllegalArgumentException.class)
public void scheduleUpdateThrowsOnInvalidJobId() throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String jobId = "invalidJobId";
    final String deviceId = "validDeviceId";
    final String queryCondition = "validQueryCondition";
    final DeviceTwinDevice updateTwin = mockedDeviceTwinDevice;
    final Date startTimeUtc = new Date();
    final long maxExecutionTimeInSeconds = 10;
    Set<Pair> testTags = new HashSet<>();
    testTags.add(new Pair("testTag", "tagObject"));
    final String json = "validJson";
    JobClient testJobClient = null;
    new NonStrictExpectations() {

        {
            IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
            result = mockedIotHubConnectionString;
            mockedDeviceTwinDevice.getTags();
            result = testTags;
            mockedDeviceTwinDevice.getDesiredProperties();
            result = null;
            mockedDeviceTwinDevice.getReportedProperties();
            result = null;
            new TwinState((TwinCollection) any, null, null);
            result = mockedTwinState;
            mockedDeviceTwinDevice.getDeviceId();
            result = deviceId;
            mockedDeviceTwinDevice.getETag();
            result = null;
            new JobsParser(jobId, mockedTwinState, queryCondition, startTimeUtc, maxExecutionTimeInSeconds);
            result = mockedJobsParser;
            mockedJobsParser.toJson();
            result = json;
            IotHubConnectionString.getUrlJobs(anyString, jobId);
            result = new MalformedURLException();
        }
    };
    try {
        testJobClient = JobClient.createFromConnectionString(connectionString);
    } catch (IllegalArgumentException e) {
        assertTrue("Test did not run because createFromConnectionString failed to create new instance of the JobClient", true);
    }
    // act
    testJobClient.scheduleUpdateTwin(jobId, queryCondition, updateTwin, startTimeUtc, maxExecutionTimeInSeconds);
}
Also used : TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) MalformedURLException(java.net.MalformedURLException) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) JobsParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsParser) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Date(java.util.Date) NonStrictExpectations(mockit.NonStrictExpectations) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with TwinState

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

the class JobClientTest method scheduleUpdateTwinCreateJsonWithProperties.

/* Tests_SRS_JOBCLIENT_21_004: [The scheduleUpdateTwin shall create a json String that represent the twin job using the JobsParser class.] */
@Test
public void scheduleUpdateTwinCreateJsonWithProperties() throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String jobId = "validJobId";
    final String deviceId = "validDeviceId";
    final String queryCondition = "validQueryCondition";
    final DeviceTwinDevice updateTwin = mockedDeviceTwinDevice;
    final Date startTimeUtc = new Date();
    final long maxExecutionTimeInSeconds = 10;
    final String json = "validJson";
    Set<Pair> testTags = new HashSet<>();
    testTags.add(new Pair("testTag", "tagObject"));
    Set<Pair> testDesired = new HashSet<>();
    testTags.add(new Pair("testDesired", "val1"));
    Set<Pair> testResponse = new HashSet<>();
    testTags.add(new Pair("testResponse", "val2"));
    new Expectations() {

        {
            IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
            result = mockedIotHubConnectionString;
            mockedDeviceTwinDevice.getTags();
            result = testTags;
            mockedDeviceTwinDevice.getDesiredProperties();
            result = testDesired;
            mockedDeviceTwinDevice.getReportedProperties();
            result = testResponse;
            new TwinState((TwinCollection) any, (TwinCollection) any, (TwinCollection) any);
            result = mockedTwinState;
            mockedDeviceTwinDevice.getDeviceId();
            result = deviceId;
            mockedDeviceTwinDevice.getETag();
            result = "1234";
            new JobsParser(jobId, mockedTwinState, queryCondition, startTimeUtc, maxExecutionTimeInSeconds);
            result = mockedJobsParser;
            mockedJobsParser.toJson();
            result = json;
            IotHubConnectionString.getUrlJobs(anyString, jobId);
            result = mockedURL;
            DeviceOperations.request(anyString, mockedURL, HttpMethod.PUT, json.getBytes(StandardCharsets.UTF_8), (String) any, anyInt, anyInt, (Proxy) any);
            result = mockedHttpResponse;
            Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, (byte[]) any);
            result = mockedJobResult;
        }
    };
    JobClient testJobClient = JobClient.createFromConnectionString(connectionString);
    // act
    JobResult jobResult = testJobClient.scheduleUpdateTwin(jobId, queryCondition, updateTwin, startTimeUtc, maxExecutionTimeInSeconds);
}
Also used : Expectations(mockit.Expectations) NonStrictExpectations(mockit.NonStrictExpectations) TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) JobsParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsParser) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Date(java.util.Date) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

TwinState (com.microsoft.azure.sdk.iot.deps.twin.TwinState)36 Test (org.junit.Test)31 Date (java.util.Date)27 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)15 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)13 TwinCollection (com.microsoft.azure.sdk.iot.deps.twin.TwinCollection)12 NonStrictExpectations (mockit.NonStrictExpectations)12 JobsParser (com.microsoft.azure.sdk.iot.deps.serializer.JobsParser)8 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)8 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)8 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)8 HashSet (java.util.HashSet)8 Expectations (mockit.Expectations)5 Verifications (mockit.Verifications)5 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)4 Proxy (java.net.Proxy)3 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)2 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)2 URL (java.net.URL)2 SimpleDateFormat (java.text.SimpleDateFormat)2