Search in sources :

Example 21 with Query

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

the class DeviceTwinTest method nextThrowsOnNullQuery.

@Test(expected = IllegalArgumentException.class)
public void nextThrowsOnNullQuery(@Mocked DeviceTwinDevice mockedDevice) throws IotHubException, IOException {
    // arrange
    final String connectionString = "testString";
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    new NonStrictExpectations() {

        {
            Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.TWIN);
            result = mockedQuery;
        }
    };
    Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
    // act
    testTwin.getNextDeviceTwin(null);
}
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) NonStrictExpectations(mockit.NonStrictExpectations) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 22 with Query

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

the class DeviceTwinTest method nextThrowsOnQueryNextThrows.

@Test(expected = IotHubException.class)
public void nextThrowsOnQueryNextThrows(@Mocked DeviceTwinDevice mockedDevice) throws IotHubException, IOException {
    // arrange
    final String connectionString = "testString";
    constructorExpectations(connectionString);
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    new NonStrictExpectations() {

        {
            Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.TWIN);
            result = mockedQuery;
            Deencapsulation.invoke(mockedQuery, "next");
            result = new IotHubException();
        }
    };
    Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
    // act
    testTwin.getNextDeviceTwin(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) NonStrictExpectations(mockit.NonStrictExpectations) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 23 with Query

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

the class DeviceTwinTest method nextRetrievesCorrectlyWithoutModelId.

@Test
public void nextRetrievesCorrectlyWithoutModelId() throws IotHubException, IOException {
    // arrange
    final Integer version = 15;
    final String etag = "validEtag";
    final int connectTimeout = 1234;
    final int readTimeout = 5678;
    final String connectionString = "someConnectionString";
    constructorExpectations(connectionString);
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString, DeviceTwinClientOptions.builder().httpConnectTimeout(connectTimeout).httpReadTimeout(readTimeout).build());
    final String expectedString = "testJsonAsNext";
    final String moduleId = "testModuleId";
    TwinCollection tags = new TwinCollection();
    tags.putFinal("tagsKey", "tagsValue");
    TwinCollection rp = new TwinCollection();
    rp.putFinal("rpKey", "rpValue");
    TwinCollection dp = new TwinCollection();
    dp.putFinal("dpKey", "dpValue");
    new Expectations() {

        {
            Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.TWIN);
            result = mockedQuery;
            Deencapsulation.invoke(mockedQuery, "next");
            result = expectedString;
            mockedTwinState.getDeviceId();
            result = "testDeviceID";
            mockedTwinState.getModuleId();
            result = moduleId;
            mockedTwinState.getVersion();
            result = version;
            mockedTwinState.getETag();
            result = etag;
            mockedTwinState.getModelId();
            result = null;
            mockedTwinState.getTags();
            result = tags;
            mockedTwinState.getDesiredProperty();
            result = dp;
            mockedTwinState.getReportedProperty();
            result = rp;
            mockCapabilities.isIotEdge();
            result = Boolean.TRUE;
        }
    };
    Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
    // act
    DeviceTwinDevice result = testTwin.getNextDeviceTwin(testQuery);
    assertNotNull(result.getTags());
    assertNotNull(result.getReportedProperties());
    assertNotNull(result.getDesiredProperties());
    assertEquals(version, result.getVersion());
    assertEquals(etag, result.getETag());
    assertEqualSetAndMap(result.getTags(), (Map) tags);
    assertEqualSetAndMap(result.getDesiredProperties(), (Map) dp);
    assertEqualSetAndMap(result.getReportedProperties(), (Map) rp);
    assertTrue(result.getCapabilities().isIotEdge());
    assertNull(result.getModelId());
    assertEquals(result.getModuleId(), result.getModuleId());
}
Also used : Expectations(mockit.Expectations) StrictExpectations(mockit.StrictExpectations) NonStrictExpectations(mockit.NonStrictExpectations) TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) 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) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 24 with Query

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

the class DeviceTwinTest method hasNextThrowsIfHasNextOnQueryThrows.

@Test(expected = IotHubException.class)
public void hasNextThrowsIfHasNextOnQueryThrows(@Mocked DeviceTwinDevice mockedDevice) throws IotHubException, IOException {
    // arrange
    final String connectionString = "testString";
    constructorExpectations(connectionString);
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    new NonStrictExpectations() {

        {
            Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.TWIN);
            result = mockedQuery;
            Deencapsulation.invoke(mockedQuery, "hasNext");
            result = new IotHubException();
        }
    };
    Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
    // act
    boolean result = testTwin.hasNextDeviceTwin(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) NonStrictExpectations(mockit.NonStrictExpectations) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 25 with Query

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

the class QueryTest method sendQueryRequestSucceeds.

// Tests_SRS_QUERY_25_008: [The method shall obtain the serilaized query by using QueryRequestParser.]
// Tests_SRS_QUERY_25_009: [The method shall use the provided HTTP Method and send request to IotHub with the serialized body over the provided URL.]
// Tests_SRS_QUERY_25_013: [The method shall create a QueryResponse object with the contents from the response body and save it.]
// Tests_SRS_QUERY_25_020: [This method shall save all the parameters for future use.]
@Test
public void sendQueryRequestSucceeds() 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
    Deencapsulation.invoke(testQuery, "sendQueryRequest", mockIotHubConnectionString, mockUrl, mockHttpMethod, (long) 0);
    // assert
    new Verifications() {

        {
            new HttpRequest(mockUrl, mockHttpMethod, (byte[]) any);
            times = 1;
            mockHttpRequest.setHeaderField("x-ms-max-item-count", String.valueOf(DEFAULT_PAGE_SIZE));
            times = 1;
            mockHttpRequest.setHeaderField("x-ms-continuation", anyString);
            times = 0;
            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)

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