Search in sources :

Example 51 with JobClient

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

the class JobClientTest method queryJobResponseSucceeds.

/*
    Tests_SRS_JOBCLIENT_25_043: [If the pageSize is not specified, default pageSize of 100 shall be used.] SRS_JOBCLIENT_25_044: [The queryDeviceJob shall create a query object for the type JOB_RESPONSE.]
    Tests_SRS_JOBCLIENT_25_045: [The queryDeviceJob shall send a query request on the query object using Query URL, HTTP GET method and wait for the response by calling sendQueryRequest.]
     */
@Test
public void queryJobResponseSucceeds(@Mocked Query mockedQuery) throws IotHubException, IOException {
    // arrange
    final String connectionString = "testString";
    JobClient testJobClient = JobClient.createFromConnectionString(connectionString);
    new Expectations() {

        {
            Deencapsulation.newInstance(Query.class, new Class[] { Integer.class, QueryType.class }, anyInt, QueryType.JOB_RESPONSE);
            result = mockedQuery;
        }
    };
    // act
    testJobClient.queryJobResponse(JOB_TYPE_DEFAULT, JOB_STATUS_DEFAULT);
    // assert
    new Verifications() {

        {
            mockedQuery.sendQueryRequest((TokenCredentialCache) any, (AzureSasCredential) any, (IotHubConnectionString) any, (URL) any, HttpMethod.GET, anyInt, anyInt, (Proxy) any);
            times = 1;
        }
    };
}
Also used : Expectations(mockit.Expectations) NonStrictExpectations(mockit.NonStrictExpectations) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Verifications(mockit.Verifications) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Test(org.junit.Test)

Example 52 with JobClient

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

the class JobClientTest method cancelJobThrowsOnSendPOST.

/* Tests_SRS_JOBCLIENT_21_033: [If the cancelJob failed to send a POST request, it shall throw IOException.] */
/* Tests_SRS_JOBCLIENT_21_034: [If the cancelJob failed to verify the iothub response, it shall throw IotHubException.] */
@Test(expected = IOException.class)
public void cancelJobThrowsOnSendPOST() throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String jobId = "validJobId";
    JobClient testJobClient = null;
    new NonStrictExpectations() {

        {
            IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
            result = mockedIotHubConnectionString;
            IotHubConnectionString.getUrlJobsCancel(anyString, jobId);
            result = mockedURL;
            DeviceOperations.request(anyString, mockedURL, HttpMethod.POST, (byte[]) any, (String) any, anyInt, anyInt, (Proxy) any);
            result = new IOException();
        }
    };
    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.cancelJob(jobId);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IOException(java.io.IOException) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 53 with JobClient

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

the class JobClientTest method cancelJobThrowsOnInvalidJobId.

/* Tests_SRS_JOBCLIENT_21_030: [If the JobId is null, empty, or invalid, the cancelJob shall throws IllegalArgumentException.] */
@Test(expected = IllegalArgumentException.class)
public void cancelJobThrowsOnInvalidJobId() throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String jobId = "invalidJobId";
    JobClient testJobClient = null;
    new NonStrictExpectations() {

        {
            IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
            result = mockedIotHubConnectionString;
            IotHubConnectionString.getUrlJobsCancel(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.cancelJob(jobId);
}
Also used : MalformedURLException(java.net.MalformedURLException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 54 with JobClient

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

the class JobClientTest method getJobSendGET.

/* Tests_SRS_JOBCLIENT_21_026: [The getJob shall send a GET request to the iothub using the created url.] */
@Test
public void getJobSendGET() throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String jobId = "validJobId";
    JobClient testJobClient = null;
    new NonStrictExpectations() {

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

        {
            DeviceOperations.request(anyString, mockedURL, HttpMethod.GET, (byte[]) any, (String) any, anyInt, anyInt, (Proxy) any);
            times = 1;
        }
    };
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Verifications(mockit.Verifications) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 55 with JobClient

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

the class JobClientTest method scheduleDeviceMethodThrowsOnNullJobId.

/* Tests_SRS_JOBCLIENT_21_014: [If the JobId is null, empty, or invalid, the scheduleDeviceMethod shall throws IllegalArgumentException.] */
@Test(expected = IllegalArgumentException.class)
public void scheduleDeviceMethodThrowsOnNullJobId() throws IOException, IotHubException {
    // arrange
    final String connectionString = "testString";
    final String jobId = null;
    final String queryCondition = "validQueryCondition";
    final String methodName = "validMethodName";
    final Set<String> payload = new HashSet<>();
    final Date startTimeUtc = new Date();
    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.scheduleDeviceMethod(jobId, queryCondition, methodName, null, null, payload, startTimeUtc, maxExecutionTimeInSeconds);
}
Also used : 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) 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