Search in sources :

Example 6 with JobClient

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

the class JobClientTest method scheduleUpdateThrowsOnInvalidJobId.

/* Tests_SRS_JOBCLIENT_21_005: [If the JobId is null, empty, or invalid, the scheduleUpdateTwin shall throws IllegalArgumentException.] */
@Test(expected = IllegalArgumentException.class)
public void scheduleUpdateThrowsOnInvalidJobId() throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String jobId = "invalidJobId";
    final String deviceId = "validDeviceId";
    final String queryCondition = "validQueryCondition";
    final DeviceTwinDevice updateTwin = mockedDeviceTwinDevice;
    final Date startTimeUtc = new Date();
    final long maxExecutionTimeInSeconds = 10;
    Set<Pair> testTags = new HashSet<>();
    testTags.add(new Pair("testTag", "tagObject"));
    final String json = "validJson";
    JobClient testJobClient = null;
    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 = new MalformedURLException();
        }
    };
    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
    testJobClient.scheduleUpdateTwin(jobId, queryCondition, updateTwin, startTimeUtc, maxExecutionTimeInSeconds);
}
Also used : TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) MalformedURLException(java.net.MalformedURLException) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) JobsParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsParser) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) 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 7 with JobClient

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

the class JobClientTest method scheduleUpdateThrowsOnNullStartTimeUtc.

/* Tests_SRS_JOBCLIENT_21_007: [If the startTimeUtc is null, the scheduleUpdateTwin shall throws IllegalArgumentException.] */
@Test(expected = IllegalArgumentException.class)
public void scheduleUpdateThrowsOnNullStartTimeUtc() throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String jobId = "validJobId";
    final String queryCondition = "validQueryCondition";
    final DeviceTwinDevice updateTwin = mockedDeviceTwinDevice;
    final Date startTimeUtc = null;
    final long maxExecutionTimeInSeconds = 10;
    JobClient testJobClient = null;
    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
    testJobClient.scheduleUpdateTwin(jobId, queryCondition, updateTwin, startTimeUtc, maxExecutionTimeInSeconds);
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Date(java.util.Date) Test(org.junit.Test)

Example 8 with JobClient

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

the class JobClientTest method scheduleDeviceMethodThrowsOnSendPUT.

/* Tests_SRS_JOBCLIENT_21_021: [If the scheduleDeviceMethod failed to send a PUT request, it shall throw IOException.] */
/* Tests_SRS_JOBCLIENT_21_022: [If the scheduleDeviceMethod failed to verify the iothub response, it shall throw IotHubException.] */
@Test(expected = IOException.class)
public void scheduleDeviceMethodThrowsOnSendPUT() 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 = new IOException();
        }
    };
    JobClient testJobClient = JobClient.createFromConnectionString(connectionString);
    // act
    testJobClient.scheduleDeviceMethod(jobId, queryCondition, methodName, null, null, payload, startTimeUtc, maxExecutionTimeInSeconds);
}
Also used : JobsParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsParser) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IOException(java.io.IOException) NonStrictExpectations(mockit.NonStrictExpectations) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Date(java.util.Date) HashSet(java.util.HashSet) MethodParser(com.microsoft.azure.sdk.iot.deps.serializer.MethodParser) Test(org.junit.Test)

Example 9 with JobClient

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

the class JobClientTest method scheduleDeviceMethodParseResponse.

/* Tests_SRS_JOBCLIENT_21_023: [The scheduleDeviceMethod shall parse the iothub response and return it as JobResult.] */
@Test
public void scheduleDeviceMethodParseResponse() 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 Expectations() {

        {
            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
    assertNotNull(jobResult);
}
Also used : Expectations(mockit.Expectations) NonStrictExpectations(mockit.NonStrictExpectations) 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) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Date(java.util.Date) HashSet(java.util.HashSet) MethodParser(com.microsoft.azure.sdk.iot.deps.serializer.MethodParser) Test(org.junit.Test)

Example 10 with JobClient

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

the class JobClientTest method scheduleDeviceMethodThrowsOnInvalidJobId.

/* Tests_SRS_JOBCLIENT_21_014: [If the JobId is null, empty, or invalid, the scheduleDeviceMethod shall throws IllegalArgumentException.] */
@Test(expected = IllegalArgumentException.class)
public void scheduleDeviceMethodThrowsOnInvalidJobId() throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String jobId = "invalidJobId";
    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";
    JobClient testJobClient = null;
    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 = new MalformedURLException();
        }
    };
    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
    testJobClient.scheduleDeviceMethod(jobId, queryCondition, methodName, null, null, payload, startTimeUtc, maxExecutionTimeInSeconds);
}
Also used : MalformedURLException(java.net.MalformedURLException) JobsParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsParser) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) NonStrictExpectations(mockit.NonStrictExpectations) Date(java.util.Date) HashSet(java.util.HashSet) MethodParser(com.microsoft.azure.sdk.iot.deps.serializer.MethodParser) Test(org.junit.Test)

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