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");
}
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);
}
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);
}
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);
}
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;
}
};
}
Aggregations