Search in sources :

Example 6 with Query

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

the class QueryTest method sendQueryRequestSetsHeadersIfFound.

// Tests_SRS_QUERY_25_007: [The method shall set the http headers x-ms-continuation and x-ms-max-item-count with request continuation token and page size if they were not null.]
@Test
public void sendQueryRequestSetsHeadersIfFound() throws IotHubException, IOException {
    // arrange
    final String testToken = UUID.randomUUID().toString();
    final int newPageSize = 5;
    final Map<String, String> testHeaderResponseMap = new HashMap<>();
    testHeaderResponseMap.put("x-ms-continuation", testToken);
    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);
    Deencapsulation.invoke(testQuery, "continueQuery", testToken);
    // assert
    new Verifications() {

        {
            new HttpRequest(mockUrl, mockHttpMethod, (byte[]) any);
            times = 2;
            mockHttpRequest.setHeaderField("x-ms-continuation", testToken);
            times = 1;
            mockHttpRequest.setHeaderField(anyString, anyString);
            minTimes = 5;
        }
    };
    assertEquals(testToken, Deencapsulation.getField(testQuery, "responseContinuationToken"));
    assertEquals(DEFAULT_QUERY_TYPE, Deencapsulation.getField(testQuery, "responseQueryType"));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) 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 7 with Query

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

the class QueryTest method sendQueryRequestThrowsOnNullURL.

@Test(expected = IllegalArgumentException.class)
public void sendQueryRequestThrowsOnNullURL() throws IotHubException, IOException {
    // arrange
    final String testToken = UUID.randomUUID().toString();
    final Map<String, String> testHeaderResponseMap = new HashMap<>();
    testHeaderResponseMap.put("x-ms-continuation", testToken);
    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
    testQuery.sendQueryRequest(mockIotHubConnectionString, null, mockHttpMethod, (long) 0);
}
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 8 with Query

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

the class QueryTest method constructorWithSQLQueryThrowsOnNullQuery.

@Test(expected = IllegalArgumentException.class)
public void constructorWithSQLQueryThrowsOnNullQuery() throws IllegalArgumentException {
    // arrange
    final String sqlQuery = null;
    // act
    Query testQuery = Deencapsulation.newInstance(Query.class, String.class, DEFAULT_PAGE_SIZE, DEFAULT_QUERY_TYPE);
}
Also used : Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Example 9 with Query

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

the class QueryTest method constructorWithSQLQueryThrowsOnInvalidQuery.

// Tests_SRS_QUERY_25_002: [If the query is null or empty or is not a valid sql query (containing select and from), the constructor shall throw an IllegalArgumentException.]
@Test(expected = IllegalArgumentException.class)
public void constructorWithSQLQueryThrowsOnInvalidQuery() throws IllegalArgumentException {
    // arrange
    final String sqlQuery = "invalid query";
    // act
    Query testQuery = Deencapsulation.newInstance(Query.class, sqlQuery, DEFAULT_PAGE_SIZE, DEFAULT_QUERY_TYPE);
}
Also used : Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Example 10 with Query

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

the class QueryTest method constructorWithOutSQLQuerySucceeds.

// Tests_SRS_QUERY_25_001: [The constructor shall validate query and save query, pagesize and request type]
// Tests_SRS_QUERY_25_017: [If the query is avaliable then isSqlQuery shall be set to true, and false otherwise.]
@Test
public void constructorWithOutSQLQuerySucceeds() throws IllegalArgumentException {
    // act
    Query testQuery = Deencapsulation.newInstance(Query.class, DEFAULT_PAGE_SIZE, DEFAULT_QUERY_TYPE);
    // assert
    assertNotNull(Deencapsulation.getField(testQuery, "pageSize"));
    assertEquals(DEFAULT_PAGE_SIZE, (int) Deencapsulation.getField(testQuery, "pageSize"));
    assertNull(Deencapsulation.getField(testQuery, "query"));
    assertFalse(Deencapsulation.getField(testQuery, "isSqlQuery"));
    assertEquals(DEFAULT_QUERY_TYPE, Deencapsulation.getField(testQuery, "requestQueryType"));
    assertEquals(QueryType.UNKNOWN, Deencapsulation.getField(testQuery, "responseQueryType"));
    assertNull(Deencapsulation.getField(testQuery, "requestContinuationToken"));
    assertNull(Deencapsulation.getField(testQuery, "responseContinuationToken"));
    assertNull(Deencapsulation.getField(testQuery, "queryResponse"));
}
Also used : Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) 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