Search in sources :

Example 11 with JobResult

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

the class JobClientTest method scheduleUpdateTwinSendPUT.

/* Tests_SRS_JOBCLIENT_21_010: [The scheduleUpdateTwin shall send a PUT request to the iothub using the created uri and json.] */
@Test
public void scheduleUpdateTwinSendPUT() 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"));
    new NonStrictExpectations() {

        {
            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);
    // assert
    new Verifications() {

        {
            DeviceOperations.request(anyString, mockedURL, HttpMethod.PUT, json.getBytes(StandardCharsets.UTF_8), (String) any, anyInt, anyInt, (Proxy) any);
            times = 1;
        }
    };
}
Also used : TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) JobsParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsParser) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Verifications(mockit.Verifications) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Date(java.util.Date) NonStrictExpectations(mockit.NonStrictExpectations) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with JobResult

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

the class JobClientTest method getJobParseResponse.

/* Tests_SRS_JOBCLIENT_21_029: [The getJob shall parse the iothub response and return it as JobResult.] */
@Test
public void getJobParseResponse() throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String jobId = "validJobId";
    JobClient testJobClient = null;
    new Expectations() {

        {
            IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
            result = mockedIotHubConnectionString;
            IotHubConnectionString.getUrlJobs(anyString, jobId);
            result = mockedURL;
            DeviceOperations.request(anyString, mockedURL, HttpMethod.GET, (byte[]) any, (String) any, anyInt, anyInt, (Proxy) any);
            result = mockedHttpResponse;
            Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, (byte[]) any);
            result = mockedJobResult;
        }
    };
    try {
        testJobClient = JobClient.createFromConnectionString(connectionString);
    } catch (IllegalArgumentException e) {
        assertTrue("Test did not run because createFromConnectionString failed to create new instance of the JobClient", true);
    }
    // act
    JobResult jobResult = testJobClient.getJob(jobId);
    // assert
    assertNotNull(jobResult);
}
Also used : Expectations(mockit.Expectations) NonStrictExpectations(mockit.NonStrictExpectations) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Test(org.junit.Test)

Example 13 with JobResult

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

the class JobResultTest method constructorStoreJsonContentNoTags.

/* Tests_SRS_JOBRESULT_21_004: [The constructor shall locally store all results information in the provided body.] */
@Test
public void constructorStoreJsonContentNoTags() throws IOException {
    // arrange
    final String json = "validJson";
    final Date now = new Date();
    TwinCollection desired = new TwinCollection();
    desired.putFinal("prop1", "val1");
    TwinState twinState = new TwinState(null, desired, null);
    twinState.setDeviceId(DEVICE_ID);
    twinState.setETag(ETAG);
    JobsResponseParserExpectations(json, twinState, null, now, null, "scheduleUpdateTwin");
    // act
    JobResult jobResult = Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, json.getBytes(StandardCharsets.UTF_8));
    // assert
    assertNotNull(Deencapsulation.getField(jobResult, "updateTwin"));
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) Date(java.util.Date) Test(org.junit.Test)

Example 14 with JobResult

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

the class JobClientTests method scheduleDeviceMethod.

private static void scheduleDeviceMethod(JobClient jobClient) throws IOException, IotHubException, InterruptedException {
    DeviceTestManager deviceTestManger = devices.get(0);
    final String deviceId = testDevice.getDeviceId();
    final String queryCondition = "DeviceId IN ['" + deviceId + "']";
    // Act
    String jobId = JOB_ID_NAME + UUID.randomUUID();
    jobClient.scheduleDeviceMethod(jobId, queryCondition, DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, PAYLOAD_STRING, new Date(), MAX_EXECUTION_TIME_IN_SECONDS);
    JobResult jobResult = jobClient.getJob(jobId);
    while (jobResult.getJobStatus() != JobStatus.completed) {
        Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_MILLISECONDS);
        jobResult = jobClient.getJob(jobId);
    }
    log.info("job finished with status {}", jobResult.getJobStatus());
    if (jobResult.getJobStatus().equals(JobStatus.completed)) {
        jobResult = queryDeviceJobResult(jobId, JobType.scheduleDeviceMethod, JobStatus.completed);
    } else {
        fail("Failed to schedule a method invocation, job status " + jobResult.getJobStatus() + ":" + jobResult.getStatusMessage());
    }
    MethodResult methodResult = jobResult.getOutcomeResult();
    assertNotNull("Device method didn't return any outcome", methodResult);
    assertEquals(200L, (long) methodResult.getStatus());
    assertEquals(DeviceEmulator.METHOD_LOOPBACK + ":" + PAYLOAD_STRING, methodResult.getPayload());
    // asserts for the client side.
    assertEquals(0, deviceTestManger.getStatusError());
}
Also used : DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Date(java.util.Date) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)

Example 15 with JobResult

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

the class JobClientTests method scheduleDeviceMethodSucceed.

@Test(timeout = TEST_TIMEOUT_MILLISECONDS)
@Ignore
public void scheduleDeviceMethodSucceed() throws InterruptedException {
    // Arrange
    ExecutorService executor = Executors.newFixedThreadPool(MAX_NUMBER_JOBS);
    DeviceTestManager deviceTestManger = devices.get(0);
    final String deviceId = testDevice.getDeviceId();
    final String queryCondition = "DeviceId IN ['" + deviceId + "']";
    final ConcurrentMap<String, Exception> jobExceptions = new ConcurrentHashMap<>();
    final ConcurrentMap<String, JobResult> jobResults = new ConcurrentHashMap<>();
    final ArrayList<String> jobIdsPending = new ArrayList<>();
    // Act
    for (int i = 0; i < MAX_NUMBER_JOBS; i++) {
        executor.submit(() -> {
            String jobId = JOB_ID_NAME + UUID.randomUUID();
            jobIdsPending.add(jobId);
            try {
                jobClient.scheduleDeviceMethod(jobId, queryCondition, DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, PAYLOAD_STRING, new Date(), MAX_EXECUTION_TIME_IN_SECONDS);
                JobResult jobResult = jobClient.getJob(jobId);
                while (jobResult.getJobStatus() != JobStatus.completed) {
                    Thread.sleep(MAXIMUM_TIME_TO_WAIT_FOR_IOTHUB_MILLISECONDS);
                    jobResult = jobClient.getJob(jobId);
                }
                log.info("job finished with status {}", jobResult.getJobStatus());
                if (jobResult.getJobStatus().equals(JobStatus.completed)) {
                    jobResult = queryDeviceJobResult(jobId, JobType.scheduleDeviceMethod, JobStatus.completed);
                    jobResults.put(jobId, jobResult);
                } else {
                    jobExceptions.put(jobId, new Exception("Scheduled job did not finish with status 'completed' but with " + jobResult.getJobStatus()));
                }
            } catch (IotHubException | IOException | InterruptedException e) {
                jobExceptions.put(jobId, e);
                log.warn("Adding {} to job exceptions...", jobId, e);
            }
            jobIdsPending.remove(jobId);
        });
    }
    cleanupJobs(executor, jobIdsPending);
    // asserts for the service side.
    if (jobExceptions.size() != 0) {
        for (Map.Entry<String, Exception> jobException : jobExceptions.entrySet()) {
            log.error("{} threw", jobException.getKey(), jobException.getValue());
        }
        fail("Service throw an exception enqueuing jobs");
    }
    assertEquals("Missing job result", MAX_NUMBER_JOBS, jobResults.size());
    for (Map.Entry<String, JobResult> jobResult : jobResults.entrySet()) {
        assertNotNull(jobResult.getValue());
        MethodResult methodResult = jobResult.getValue().getOutcomeResult();
        assertNotNull("Device method didn't return any outcome", methodResult);
        assertEquals(200L, (long) methodResult.getStatus());
        assertEquals(DeviceEmulator.METHOD_LOOPBACK + ":" + PAYLOAD_STRING, methodResult.getPayload());
    }
    // asserts for the client side.
    assertEquals(0, deviceTestManger.getStatusError());
}
Also used : JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) Date(java.util.Date) DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) Ignore(org.junit.Ignore) 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)

Aggregations

JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)40 Test (org.junit.Test)27 Date (java.util.Date)25 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)17 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)14 TwinState (com.microsoft.azure.sdk.iot.deps.twin.TwinState)13 HashSet (java.util.HashSet)11 NonStrictExpectations (mockit.NonStrictExpectations)11 JobsParser (com.microsoft.azure.sdk.iot.deps.serializer.JobsParser)9 TwinCollection (com.microsoft.azure.sdk.iot.deps.twin.TwinCollection)9 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)9 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)9 IOException (java.io.IOException)8 Expectations (mockit.Expectations)8 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)6 MethodParser (com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)5 DeviceTestManager (tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager)5 Job (com.microsoft.azure.sdk.iot.service.devicetwin.Job)4 MethodResult (com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)4 Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)4