Search in sources :

Example 11 with JobsParser

use of com.microsoft.azure.sdk.iot.deps.serializer.JobsParser in project azure-iot-sdk-java by Azure.

the class JobClient method scheduleUpdateTwin.

/**
 * Creates a new Job to update twin tags and desired properties on one or multiple devices
 *
 * @param jobId Unique Job Id for this job
 * @param queryCondition Query condition to evaluate which devices to run the job on. It can be {@code null} or empty
 * @param updateTwin Twin object to use for the update
 * @param startTimeUtc Date time in Utc to start the job
 * @param maxExecutionTimeInSeconds Max execution time in seconds, i.e., ttl duration the job can run
 * @return a jobResult object
 * @throws IllegalArgumentException if one of the provided parameters is invalid
 * @throws IOException if the function cannot create a URL for the job
 * @throws IotHubException if the http request failed
 */
public synchronized JobResult scheduleUpdateTwin(String jobId, String queryCondition, DeviceTwinDevice updateTwin, Date startTimeUtc, long maxExecutionTimeInSeconds) throws IllegalArgumentException, IOException, IotHubException {
    URL url;
    if (Tools.isNullOrEmpty(jobId)) {
        throw new IllegalArgumentException("jobId cannot be null or empty");
    }
    if (updateTwin == null) {
        throw new IllegalArgumentException("updateTwin cannot be null");
    }
    if (startTimeUtc == null) {
        throw new IllegalArgumentException("startTimeUtc cannot be null");
    }
    if (maxExecutionTimeInSeconds < 0) {
        throw new IllegalArgumentException("maxExecutionTimeInSeconds cannot be negative");
    }
    JobsParser jobsParser = new JobsParser(jobId, getParserFromDevice(updateTwin), queryCondition, startTimeUtc, maxExecutionTimeInSeconds);
    String json = jobsParser.toJson();
    try {
        url = IotHubConnectionString.getUrlJobs(this.hostName, jobId);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Invalid JobId to create url");
    }
    ProxyOptions proxyOptions = options.getProxyOptions();
    Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
    HttpResponse response = DeviceOperations.request(this.getAuthenticationToken(), url, HttpMethod.PUT, json.getBytes(StandardCharsets.UTF_8), null, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    return new JobResult(response.getBody());
}
Also used : MalformedURLException(java.net.MalformedURLException) Proxy(java.net.Proxy) JobsParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsParser) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URL(java.net.URL)

Example 12 with JobsParser

use of com.microsoft.azure.sdk.iot.deps.serializer.JobsParser in project azure-iot-sdk-java by Azure.

the class JobClient method scheduleDeviceMethod.

/**
 * Creates a new Job to invoke method on one or multiple devices
 *
 * @param jobId Unique Job Id for this job
 * @param queryCondition Query condition to evaluate which devices to run the job on. It can be {@code null} or empty
 * @param methodName Method name to be invoked
 * @param responseTimeoutInSeconds Maximum interval of time, in seconds, that the Direct Method will wait for answer. It can be {@code null}.
 * @param connectTimeoutInSeconds Maximum interval of time, in seconds, that the Direct Method will wait for the connection. It can be {@code null}.
 * @param payload Object that contains the payload defined by the user. It can be {@code null}.
 * @param startTimeUtc Date time in Utc to start the job
 * @param maxExecutionTimeInSeconds Max execution time in seconds, i.e., ttl duration the job can run
 * @return a jobResult object
 * @throws IllegalArgumentException if one of the provided parameters is invalid
 * @throws IOException if the function cannot create a URL for the job, or the IO failed on request
 * @throws IotHubException if the http request failed
 */
public synchronized JobResult scheduleDeviceMethod(String jobId, String queryCondition, String methodName, Long responseTimeoutInSeconds, Long connectTimeoutInSeconds, Object payload, Date startTimeUtc, long maxExecutionTimeInSeconds) throws IllegalArgumentException, IOException, IotHubException {
    URL url;
    if (Tools.isNullOrEmpty(jobId)) {
        throw new IllegalArgumentException("jobId cannot be null or empty");
    }
    if (Tools.isNullOrEmpty(methodName)) {
        throw new IllegalArgumentException("method name cannot be null or empty");
    }
    if (startTimeUtc == null) {
        throw new IllegalArgumentException("startTimeUtc cannot be null");
    }
    if (maxExecutionTimeInSeconds < 0) {
        throw new IllegalArgumentException("maxExecutionTimeInSeconds cannot be less than 0");
    }
    MethodParser cloudToDeviceMethod = new MethodParser(methodName, responseTimeoutInSeconds, connectTimeoutInSeconds, payload);
    JobsParser jobsParser = new JobsParser(jobId, cloudToDeviceMethod, queryCondition, startTimeUtc, maxExecutionTimeInSeconds);
    String json = jobsParser.toJson();
    try {
        url = IotHubConnectionString.getUrlJobs(this.hostName, jobId);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Invalid JobId to create url");
    }
    ProxyOptions proxyOptions = options.getProxyOptions();
    Proxy proxy = proxyOptions != null ? proxyOptions.getProxy() : null;
    HttpResponse response = DeviceOperations.request(this.getAuthenticationToken(), url, HttpMethod.PUT, json.getBytes(StandardCharsets.UTF_8), null, options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
    return new JobResult(response.getBody());
}
Also used : MalformedURLException(java.net.MalformedURLException) Proxy(java.net.Proxy) JobsParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsParser) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URL(java.net.URL) MethodParser(com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)

Example 13 with JobsParser

use of com.microsoft.azure.sdk.iot.deps.serializer.JobsParser in project azure-iot-sdk-java by Azure.

the class JobClientTest method scheduleDeviceMethodSendPUT.

/* Tests_SRS_JOBCLIENT_21_020: [The scheduleDeviceMethod shall send a PUT request to the iothub using the created url and json.] */
@Test
public void scheduleDeviceMethodSendPUT() throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String jobId = "validJobId";
    final String queryCondition = "validQueryCondition";
    final String methodName = "validMethodName";
    final Set<String> payload = new HashSet<>();
    final Date startTimeUtc = new Date();
    final long maxExecutionTimeInSeconds = 10;
    final String json = "validJson";
    new NonStrictExpectations() {

        {
            IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
            result = mockedIotHubConnectionString;
            new MethodParser(methodName, null, null, payload);
            result = mockedMethodParser;
            new JobsParser(jobId, mockedMethodParser, 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
    testJobClient.scheduleDeviceMethod(jobId, queryCondition, methodName, null, null, payload, startTimeUtc, maxExecutionTimeInSeconds);
    // assert
    new Verifications() {

        {
            DeviceOperations.request(anyString, mockedURL, HttpMethod.PUT, json.getBytes(StandardCharsets.UTF_8), (String) any, anyInt, anyInt, (Proxy) any);
            times = 1;
        }
    };
}
Also used : JobsParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsParser) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Date(java.util.Date) HashSet(java.util.HashSet) MethodParser(com.microsoft.azure.sdk.iot.deps.serializer.MethodParser) Test(org.junit.Test)

Example 14 with JobsParser

use of com.microsoft.azure.sdk.iot.deps.serializer.JobsParser in project azure-iot-sdk-java by Azure.

the class JobClientTest method scheduleDeviceMethodCreateUrl.

/* Tests_SRS_JOBCLIENT_21_019: [The scheduleDeviceMethod shall create a URL for Jobs using the iotHubConnectionString.] */
@Test
public void scheduleDeviceMethodCreateUrl() throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String jobId = "validJobId";
    final String queryCondition = "validQueryCondition";
    final String methodName = "validMethodName";
    final Set<String> payload = new HashSet<>();
    final Date startTimeUtc = new Date();
    final long maxExecutionTimeInSeconds = 10;
    final String json = "validJson";
    new NonStrictExpectations() {

        {
            IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
            result = mockedIotHubConnectionString;
            new MethodParser(methodName, null, null, payload);
            result = mockedMethodParser;
            new JobsParser(jobId, mockedMethodParser, 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.scheduleDeviceMethod(jobId, queryCondition, methodName, null, null, payload, startTimeUtc, maxExecutionTimeInSeconds);
    // assert
    new Verifications() {

        {
            IotHubConnectionString.getUrlJobs(anyString, jobId);
            times = 1;
        }
    };
}
Also used : 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) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Date(java.util.Date) HashSet(java.util.HashSet) MethodParser(com.microsoft.azure.sdk.iot.deps.serializer.MethodParser) Test(org.junit.Test)

Example 15 with JobsParser

use of com.microsoft.azure.sdk.iot.deps.serializer.JobsParser in project azure-iot-sdk-java by Azure.

the class JobClientTest method scheduleUpdateTwinCreateJson.

/* Tests_SRS_JOBCLIENT_21_004: [The scheduleUpdateTwin shall create a json String that represent the twin job using the JobsParser class.] */
@Test
public void scheduleUpdateTwinCreateJson() 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"));
    // assert
    new Expectations() {

        {
            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 = 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

JobsParser (com.microsoft.azure.sdk.iot.deps.serializer.JobsParser)16 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)16 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)14 Date (java.util.Date)14 HashSet (java.util.HashSet)14 NonStrictExpectations (mockit.NonStrictExpectations)14 Test (org.junit.Test)14 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)9 TwinState (com.microsoft.azure.sdk.iot.deps.twin.TwinState)8 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)8 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)8 MethodParser (com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)7 Expectations (mockit.Expectations)6 MalformedURLException (java.net.MalformedURLException)4 Verifications (mockit.Verifications)4 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)2 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)2 IOException (java.io.IOException)2 Proxy (java.net.Proxy)2 URL (java.net.URL)2