Search in sources :

Example 61 with JobClient

use of com.microsoft.azure.sdk.iot.service.jobs.JobClient in project azure-iot-sdk-java by Azure.

the class JobClientTests method setUp.

@BeforeClass
public static void setUp() throws IOException, IotHubException, InterruptedException, URISyntaxException {
    iotHubConnectionString = Tools.retrieveEnvironmentVariableValue(TestConstants.IOT_HUB_CONNECTION_STRING_ENV_VAR_NAME);
    isBasicTierHub = Boolean.parseBoolean(Tools.retrieveEnvironmentVariableValue(TestConstants.IS_BASIC_TIER_HUB_ENV_VAR_NAME));
    isPullRequest = Boolean.parseBoolean(Tools.retrieveEnvironmentVariableValue(TestConstants.IS_PULL_REQUEST));
    jobClient = new JobClient(iotHubConnectionString);
    registryManager = new RegistryManager(iotHubConnectionString, RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    String uuid = UUID.randomUUID().toString();
    for (int i = 0; i < MAX_DEVICES; i++) {
        testDevice = Tools.addDeviceWithRetry(registryManager, Device.createFromId(DEVICE_ID_NAME.concat("-" + i + "-" + uuid), DeviceStatus.Enabled, null));
        DeviceTestManager testManager = new DeviceTestManager(new DeviceClient(registryManager.getDeviceConnectionString(testDevice), IotHubClientProtocol.AMQPS));
        testManager.client.open();
        testManager.subscribe(true, true);
        devices.add(testManager);
    }
}
Also used : DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) BeforeClass(org.junit.BeforeClass)

Example 62 with JobClient

use of com.microsoft.azure.sdk.iot.service.jobs.JobClient in project azure-iot-sdk-java by Azure.

the class JobClientTests method buildJobClientWithAzureSasCredential.

private static JobClient buildJobClientWithAzureSasCredential() {
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
    JobClientOptions options = JobClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build();
    return new JobClient(iotHubConnectionStringObj.getHostName(), azureSasCredential, options);
}
Also used : JobClientOptions(com.microsoft.azure.sdk.iot.service.jobs.JobClientOptions) IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient)

Example 63 with JobClient

use of com.microsoft.azure.sdk.iot.service.jobs.JobClient in project azure-iot-sdk-java by Azure.

the class JobClientTests method jobClientTokenRenewalWithAzureSasCredential.

@Test(timeout = TEST_TIMEOUT_MILLISECONDS)
public void jobClientTokenRenewalWithAzureSasCredential() throws InterruptedException, IOException, IotHubException {
    // Arrange
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
    JobClient jobClientWithSasCredential = new JobClient(iotHubConnectionStringObj.getHostName(), sasCredential);
    // JobClient usage should succeed since the shared access signature hasn't expired yet
    scheduleDeviceMethod(jobClientWithSasCredential);
    // deliberately expire the SAS token to provoke a 401 to ensure that the job client is using the shared
    // access signature that is set here.
    sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
    try {
        scheduleDeviceMethod(jobClientWithSasCredential);
        fail("Expected scheduling a job to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
    } catch (IotHubUnathorizedException e) {
        log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
    }
    // Renew the expired shared access signature
    serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    sasCredential.update(serviceSasToken.toString());
    // JobClient usage should succeed since the shared access signature has been renewed
    scheduleDeviceMethod(jobClientWithSasCredential);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 64 with JobClient

use of com.microsoft.azure.sdk.iot.service.jobs.JobClient in project azure-iot-sdk-java by Azure.

the class AzureSasCredentialSample method runJobClientSample.

private static void runJobClientSample(String iotHubHostName, AzureSasCredential credential) {
    // JobClient has some configurable options for HTTP read and connect timeouts, as well as for setting proxies.
    // For this sample, the default options will be used though.
    JobClientOptions options = JobClientOptions.builder().build();
    // This constructor takes in your implementation of AzureSasCredential which allows you to use symmetric key based
    // authentication without giving the client your connection string.
    JobClient jobClient = new JobClient(iotHubHostName, credential, options);
    try {
        System.out.println("Querying all active jobs for your IoT Hub");
        Query deviceJobQuery = jobClient.queryDeviceJob(SqlQuery.createSqlQuery("*", SqlQuery.FromType.JOBS, null, null).getQuery());
        int queriedJobCount = 0;
        while (jobClient.hasNextJob(deviceJobQuery)) {
            queriedJobCount++;
            JobResult job = jobClient.getNextJob(deviceJobQuery);
            System.out.println(String.format("Job %s of type %s has status %s", job.getJobId(), job.getJobType(), job.getJobStatus()));
        }
        if (queriedJobCount == 0) {
            System.out.println("No active jobs found for your IoT Hub");
        }
    } catch (IotHubException | IOException e) {
        System.err.println("Failed to query the jobs for your IoT Hub");
        e.printStackTrace();
        System.exit(-1);
    }
}
Also used : JobClientOptions(com.microsoft.azure.sdk.iot.service.jobs.JobClientOptions) SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IOException(java.io.IOException) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 65 with JobClient

use of com.microsoft.azure.sdk.iot.service.jobs.JobClient in project azure-iot-sdk-java by Azure.

the class JobClientSample method createJobClient.

private static JobClient createJobClient() throws IOException {
    System.out.println("Create JobClient from the connectionString...");
    JobClient jobClient = JobClient.createFromConnectionString(iotHubConnectionString);
    System.out.println("JobClient created with success");
    System.out.println();
    return jobClient;
}
Also used : JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient)

Aggregations

JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)67 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)61 Test (org.junit.Test)61 NonStrictExpectations (mockit.NonStrictExpectations)36 Date (java.util.Date)26 HashSet (java.util.HashSet)21 JobsParser (com.microsoft.azure.sdk.iot.deps.serializer.JobsParser)14 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)14 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)13 Verifications (mockit.Verifications)13 Expectations (mockit.Expectations)11 Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)10 SqlQuery (com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery)10 TwinState (com.microsoft.azure.sdk.iot.deps.twin.TwinState)8 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)8 MethodParser (com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)7 IOException (java.io.IOException)6 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)5 MalformedURLException (java.net.MalformedURLException)4 JobClientOptions (com.microsoft.azure.sdk.iot.service.jobs.JobClientOptions)3