Search in sources :

Example 21 with Job

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

the class JobTest method scheduleUpdateTwinThrowOnJobClientFail.

/* Tests_SRS_JOB_21_011: [If the Iothub reported fail as result of the scheduleUpdateTwin, the scheduleUpdateTwin shall throws IotHubException.] */
@Test(expected = IotHubException.class)
public void scheduleUpdateTwinThrowOnJobClientFail() throws IOException, IotHubException {
    // arrange
    final String queryCondition = "validQueryCondition";
    final DeviceTwinDevice deviceTwin = mockedDeviceTwinDevice;
    final Date now = new Date();
    final long maxExecutionTimeInSeconds = 100;
    final String connectionString = "validConnectionString";
    // assert
    new NonStrictExpectations() {

        {
            JobClient.createFromConnectionString(connectionString);
            result = mockedJobClient;
            mockedJobClient.scheduleUpdateTwin((String) any, queryCondition, deviceTwin, now, maxExecutionTimeInSeconds);
            result = mockedJobResult;
            mockedJobResult.getJobStatus();
            result = JobStatus.failed;
            times = 1;
        }
    };
    Job job = Deencapsulation.newInstance(Job.class, new Class[] { String.class }, connectionString);
    // act
    Deencapsulation.invoke(job, "scheduleUpdateTwin", new Class[] { String.class, DeviceTwinDevice.class, Date.class, Long.class }, queryCondition, deviceTwin, now, maxExecutionTimeInSeconds);
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) Job(com.microsoft.azure.sdk.iot.service.devicetwin.Job) Date(java.util.Date) Test(org.junit.Test)

Example 22 with Job

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

the class JobTest method scheduleDeviceMethodInvokeJobClient.

/* Tests_SRS_JOB_21_015: [The scheduleDeviceMethod shall invoke the scheduleDeviceMethod in the JobClient class with the received parameters.] */
@Test
public void scheduleDeviceMethodInvokeJobClient() throws IOException, IotHubException {
    // arrange
    final String queryCondition = "validQueryCondition";
    final String methodName = "validMethodName";
    final String payload = "validPayload";
    final Date now = new Date();
    final long maxExecutionTimeInSeconds = 100;
    final String connectionString = "validConnectionString";
    // assert
    new NonStrictExpectations() {

        {
            JobClient.createFromConnectionString(connectionString);
            result = mockedJobClient;
            mockedJobClient.scheduleDeviceMethod((String) any, queryCondition, methodName, null, null, payload, now, maxExecutionTimeInSeconds);
            result = mockedJobResult;
            times = 1;
            mockedJobResult.getJobStatus();
            result = JobStatus.enqueued;
        }
    };
    Job job = Deencapsulation.newInstance(Job.class, new Class[] { String.class }, connectionString);
    // act
    Deencapsulation.invoke(job, "scheduleDeviceMethod", new Class[] { String.class, String.class, Long.class, Long.class, Object.class, Date.class, Long.class }, queryCondition, methodName, null, null, payload, now, maxExecutionTimeInSeconds);
}
Also used : Job(com.microsoft.azure.sdk.iot.service.devicetwin.Job) Date(java.util.Date) Test(org.junit.Test)

Example 23 with Job

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

the class JobTest method cancelThrowOnInvokeJobClient.

/* Tests_SRS_JOB_21_021: [If cancelJob failed, the cancel shall throws IOException. Threw by the cancelJob.] */
@Test(expected = IOException.class)
public void cancelThrowOnInvokeJobClient() throws IOException, IotHubException {
    // arrange
    final String connectionString = "validConnectionString";
    // assert
    new NonStrictExpectations() {

        {
            JobClient.createFromConnectionString(connectionString);
            result = mockedJobClient;
            mockedJobClient.cancelJob((String) any);
            result = new IOException();
            times = 1;
        }
    };
    Job job = Deencapsulation.newInstance(Job.class, new Class[] { String.class }, connectionString);
    // act
    job.cancel();
}
Also used : IOException(java.io.IOException) Job(com.microsoft.azure.sdk.iot.service.devicetwin.Job) Test(org.junit.Test)

Example 24 with Job

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

the class JobTest method getThrowOnInvokeJobClient.

/* Tests_SRS_JOB_21_019: [If getJob failed, the get shall throws IOException. Threw by the getJob.] */
@Test(expected = IOException.class)
public void getThrowOnInvokeJobClient() throws IOException, IotHubException {
    // arrange
    final String connectionString = "validConnectionString";
    // assert
    new NonStrictExpectations() {

        {
            JobClient.createFromConnectionString(connectionString);
            result = mockedJobClient;
            mockedJobClient.getJob((String) any);
            result = new IOException();
            times = 1;
        }
    };
    Job job = Deencapsulation.newInstance(Job.class, new Class[] { String.class }, connectionString);
    // act
    job.get();
}
Also used : IOException(java.io.IOException) Job(com.microsoft.azure.sdk.iot.service.devicetwin.Job) Test(org.junit.Test)

Example 25 with Job

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

the class DeviceMethodSample method scheduleInvokeMethod.

private static void scheduleInvokeMethod(DeviceMethod methodClient) throws IotHubException, IOException, InterruptedException {
    // query condition that defines the list of device to invoke
    String queryCondition = "DeviceId IN ['" + deviceId + "']";
    // date when the invoke shall be executed
    // 10 seconds in the future.
    Date invokeDateInFuture = new Date(new Date().getTime() + ADD_10_SECONDS_IN_MILLISECONDS);
    System.out.println("Schedule invoke method on the Device in 10 seconds");
    Job job = methodClient.scheduleDeviceMethod(queryCondition, methodName, responseTimeout, connectTimeout, payload, invokeDateInFuture, MAX_EXECUTION_TIME_IN_SECONDS);
    System.out.println("Wait for job completed...");
    JobResult jobResult = job.get();
    while (jobResult.getJobStatus() != JobStatus.completed) {
        Thread.sleep(GIVE_100_MILLISECONDS_TO_IOTHUB);
        jobResult = job.get();
    }
    System.out.println("job completed");
}
Also used : JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) Job(com.microsoft.azure.sdk.iot.service.devicetwin.Job) Date(java.util.Date)

Aggregations

Job (com.microsoft.azure.sdk.iot.service.devicetwin.Job)25 Test (org.junit.Test)23 Date (java.util.Date)19 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)6 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)4 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)4 NonStrictExpectations (mockit.NonStrictExpectations)4 IOException (java.io.IOException)3 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)2 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)2 Verifications (mockit.Verifications)2 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)1 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)1