Search in sources :

Example 11 with Query

use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.

the class QueryTest method sendQueryRequestThrowsWhenResponseThrows.

@Test(expected = IOException.class)
public void sendQueryRequestThrowsWhenResponseThrows() throws IotHubException, IOException {
    // arrange
    final String testResponseToken = UUID.randomUUID().toString();
    final Map<String, String> testHeaderResponseMap = new HashMap<>();
    testHeaderResponseMap.put("x-ms-continuation", testResponseToken);
    testHeaderResponseMap.put("x-ms-item-type", DEFAULT_QUERY_TYPE.getValue());
    Query testQuery = Deencapsulation.newInstance(Query.class, DEFAULT_QUERY, DEFAULT_PAGE_SIZE, DEFAULT_QUERY_TYPE);
    new NonStrictExpectations() {

        {
            mockHttpResponse.getHeaderFields();
            result = testHeaderResponseMap;
            Deencapsulation.newInstance(QueryResponse.class, anyString);
            result = new IOException("test");
        }
    };
    // act
    Deencapsulation.invoke(testQuery, "sendQueryRequest", mockIotHubConnectionString, mockUrl, mockHttpMethod, (long) 0);
    // assert
    assertEquals(testResponseToken, Deencapsulation.getField(testQuery, "responseContinuationToken"));
}
Also used : Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) HashMap(java.util.HashMap) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with Query

use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.

the class QueryTest method nextReturnsIfNextExists.

// Tests_SRS_QUERY_25_016: [The method shall return the next element for this QueryResponse.]
@Test
public void nextReturnsIfNextExists() throws IotHubException, IOException {
    // arrange
    final Object mockObject = new Object();
    final Map<String, String> testHeaderResponseMap = new HashMap<>();
    testHeaderResponseMap.put("x-ms-item-type", DEFAULT_QUERY_TYPE.getValue());
    Query testQuery = Deencapsulation.newInstance(Query.class, DEFAULT_QUERY, DEFAULT_PAGE_SIZE, DEFAULT_QUERY_TYPE);
    new NonStrictExpectations() {

        {
            mockHttpResponse.getHeaderFields();
            result = testHeaderResponseMap;
            mockedQueryResponse.hasNext();
            result = true;
            mockedQueryResponse.next();
            result = mockObject;
        }
    };
    Deencapsulation.invoke(testQuery, "sendQueryRequest", mockIotHubConnectionString, mockUrl, mockHttpMethod, (long) 0);
    // act
    Object next = Deencapsulation.invoke(testQuery, "next");
    // assert
    assertEquals(mockObject, next);
}
Also used : Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) HashMap(java.util.HashMap) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Example 13 with Query

use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.

the class QueryTest method sendQueryRequestSetsResContinuationTokenOnlyIfFound.

// Tests_SRS_QUERY_25_010: [The method shall read the continuation token (x-ms-continuation) and response type (x-ms-item-type) from the HTTP Headers and save it.]
@Test
public void sendQueryRequestSetsResContinuationTokenOnlyIfFound() throws IotHubException, IOException {
    // arrange
    final String testResponseToken = UUID.randomUUID().toString();
    final Map<String, String> testHeaderResponseMap = new HashMap<>();
    testHeaderResponseMap.put("x-ms-continuation", testResponseToken);
    testHeaderResponseMap.put("x-ms-item-type", DEFAULT_QUERY_TYPE.getValue());
    Query testQuery = Deencapsulation.newInstance(Query.class, DEFAULT_QUERY, DEFAULT_PAGE_SIZE, DEFAULT_QUERY_TYPE);
    new NonStrictExpectations() {

        {
            mockHttpResponse.getHeaderFields();
            result = testHeaderResponseMap;
        }
    };
    // act
    Deencapsulation.invoke(testQuery, "sendQueryRequest", mockIotHubConnectionString, mockUrl, mockHttpMethod, (long) 0);
    // assert
    assertEquals(testResponseToken, Deencapsulation.getField(testQuery, "responseContinuationToken"));
}
Also used : Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) HashMap(java.util.HashMap) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Example 14 with Query

use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.

the class JobClientTest method nextRetrievesCorrectly.

// Tests_SRS_JOBCLIENT_25_049: [getNextJob shall return next Job Result if the exist, and throw NoSuchElementException other wise.]
// Tests_SRS_JOBCLIENT_25_051: [getNextJob method shall parse the next job element from the query response provide the response as JobResult object.]
@Test
public void nextRetrievesCorrectly(@Mocked Query mockedQuery) throws IotHubException, IOException {
    // arrange
    final String connectionString = "testString";
    JobClient testJobClient = JobClient.createFromConnectionString(connectionString);
    final String expectedString = "testJsonAsNext";
    new NonStrictExpectations() {

        {
            Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.DEVICE_JOB);
            result = mockedQuery;
            Deencapsulation.invoke(mockedQuery, "hasNext");
            result = true;
            Deencapsulation.invoke(mockedQuery, "next");
            result = expectedString;
            Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, (byte[]) any);
            result = mockedJobResult;
        }
    };
    Query testQuery = testJobClient.queryDeviceJob(VALID_SQL_QUERY);
    // act
    testJobClient.getNextJob(testQuery);
    // assert
    new Verifications() {

        {
            mockedQuery.sendQueryRequest((TokenCredentialCache) any, (AzureSasCredential) any, (IotHubConnectionString) any, (URL) any, HttpMethod.POST, anyInt, anyInt, (Proxy) any);
            times = 1;
            Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, expectedString.getBytes(StandardCharsets.UTF_8));
            times = 1;
        }
    };
}
Also used : SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) 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 15 with Query

use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.

the class JobClientTest method nextThrowsIfNonStringRetrieved.

// Tests_SRS_JOBCLIENT_25_050: [getNextJob shall throw IOException if next Job Result exist and is not a string.]
@Test(expected = IOException.class)
public void nextThrowsIfNonStringRetrieved(@Mocked Query mockedQuery) throws IotHubException, IOException {
    // arrange
    final String connectionString = "testString";
    JobClient testJobClient = JobClient.createFromConnectionString(connectionString);
    new NonStrictExpectations() {

        {
            Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.DEVICE_JOB);
            result = mockedQuery;
            Deencapsulation.invoke(mockedQuery, "hasNext");
            result = true;
            Deencapsulation.invoke(mockedQuery, "next");
            result = 5;
        }
    };
    Query testQuery = testJobClient.queryDeviceJob(VALID_SQL_QUERY);
    // act
    testJobClient.getNextJob(testQuery);
}
Also used : SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Aggregations

Query (com.microsoft.azure.sdk.iot.service.devicetwin.Query)63 Test (org.junit.Test)51 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)49 SqlQuery (com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery)34 HashMap (java.util.HashMap)17 NonStrictExpectations (mockit.NonStrictExpectations)17 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)10 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)10 RawTwinQuery (com.microsoft.azure.sdk.iot.service.devicetwin.RawTwinQuery)8 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)8 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)6 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)6 JobResult (com.microsoft.azure.sdk.iot.service.jobs.JobResult)4 IOException (java.io.IOException)3 NoSuchElementException (java.util.NoSuchElementException)3 Verifications (mockit.Verifications)3 TwinCollection (com.microsoft.azure.sdk.iot.deps.twin.TwinCollection)2 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)2 JobClientOptions (com.microsoft.azure.sdk.iot.service.jobs.JobClientOptions)2 Proxy (java.net.Proxy)2