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