Search in sources :

Example 1 with Job

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

the class DeviceMethodSample method cancelScheduleInvokeMethod.

private static void cancelScheduleInvokeMethod(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 minutes in the future.
    Date invokeDateInFuture = new Date(new Date().getTime() + ADD_10_MINUTES_IN_MILLISECONDS);
    System.out.println("Schedule invoke method on the Device in 10 minutes");
    Job job = methodClient.scheduleDeviceMethod(queryCondition, methodName, responseTimeout, connectTimeout, payload, invokeDateInFuture, MAX_EXECUTION_TIME_IN_SECONDS);
    Thread.sleep(WAIT_1_SECOND_TO_CANCEL_IN_MILLISECONDS);
    System.out.println("Cancel job after 1 second");
    job.cancel();
    System.out.println("Wait for job cancelled...");
    JobResult jobResult = job.get();
    while (jobResult.getJobStatus() != JobStatus.cancelled) {
        Thread.sleep(GIVE_100_MILLISECONDS_TO_IOTHUB);
        jobResult = job.get();
    }
    System.out.println("job cancelled");
}
Also used : JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) Job(com.microsoft.azure.sdk.iot.service.devicetwin.Job) Date(java.util.Date)

Example 2 with Job

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

the class DeviceTwinTest method scheduleUpdateTwinCreateJobSucceed.

// Tests_SRS_DEVICETWIN_21_064: [The scheduleUpdateTwin shall create a new instance of the Job class ]
// Tests_SRS_DEVICETWIN_21_068: [The scheduleUpdateTwin shall return the created instance of the Job class ]
@Test
public void scheduleUpdateTwinCreateJobSucceed(@Mocked Job mockedJob, @Mocked DeviceTwinDevice mockedDevice) throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String queryCondition = "validQueryCondition";
    final Date now = new Date();
    final long maxExecutionTimeInSeconds = 100;
    constructorExpectations(connectionString);
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    new NonStrictExpectations() {

        {
            mockedConnectionString.toString();
            result = connectionString;
            Deencapsulation.newInstance(Job.class, new Class[] { String.class }, anyString);
            result = mockedJob;
            times = 1;
        }
    };
    // act
    Job job = testTwin.scheduleUpdateTwin(queryCondition, mockedDevice, now, maxExecutionTimeInSeconds);
    // assert
    assertNotNull(job);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Job(com.microsoft.azure.sdk.iot.service.devicetwin.Job) NonStrictExpectations(mockit.NonStrictExpectations) Date(java.util.Date) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 3 with Job

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

the class JobTest method scheduleDeviceMethodThrowOnInvokeJobClient.

/* Tests_SRS_JOB_21_016: [If scheduleDeviceMethod failed, the scheduleDeviceMethod shall throws IotHubException. Threw by the scheduleUpdateTwin.] */
@Test(expected = IOException.class)
public void scheduleDeviceMethodThrowOnInvokeJobClient() 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 = new IOException();
            times = 1;
        }
    };
    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 : IOException(java.io.IOException) Job(com.microsoft.azure.sdk.iot.service.devicetwin.Job) Date(java.util.Date) Test(org.junit.Test)

Example 4 with Job

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

the class JobTest method scheduleDeviceMethodThrowOnNullMethodName.

/* Tests_SRS_JOB_21_012: [If the methodName is null or empty, the scheduleDeviceMethod shall throws IllegalArgumentException.] */
@Test(expected = IllegalArgumentException.class)
public void scheduleDeviceMethodThrowOnNullMethodName() throws IOException {
    // arrange
    final String queryCondition = "validQueryCondition";
    final String methodName = null;
    final String payload = "validPayload";
    final Date now = new Date();
    final long maxExecutionTimeInSeconds = 100;
    final String connectionString = "validConnectionString";
    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 5 with Job

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

the class JobTest method constructor1CreateJobClient.

/* Tests_SRS_JOB_21_004: [The constructor shall create a new instance of JobClient to manage the Job.] */
@Test
public void constructor1CreateJobClient() throws IOException {
    // arrange
    final String connectionString = "validConnectionString";
    // act
    Job job = Deencapsulation.newInstance(Job.class, new Class[] { String.class }, connectionString);
    // assert
    assertNotNull(Deencapsulation.getField(job, "jobClient"));
    new Verifications() {

        {
            new JobClient(connectionString);
            times = 1;
        }
    };
}
Also used : Job(com.microsoft.azure.sdk.iot.service.devicetwin.Job) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Test(org.junit.Test)

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