Search in sources :

Example 1 with QueryCollection

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

the class DeviceTwinTest method getNextDeviceTwinCollectionWithOptionsSuccess.

// Tests_SRS_DEVICETWIN_34_078: [If the provided deviceTwinQueryCollection has a next set to give, this function shall retrieve that set from deviceTwinQueryCollection, cast its contents to DeviceTwinDevice objects, and return it in a QueryCollectionResponse object.]
// Tests_SRS_DEVICETWIN_34_079: [The returned QueryCollectionResponse object shall contain the continuation token needed to retrieve the next set with.]
@Test
public void getNextDeviceTwinCollectionWithOptionsSuccess() throws IOException, IotHubException {
    // arrange
    DeviceTwin deviceTwin = new DeviceTwin(STANDARD_CONNECTIONSTRING);
    String expectedContinuationToken = "some continuation token";
    String expectedJsonString = "some json string";
    new MockUp<DeviceTwin>() {

        @Mock
        boolean hasNext(QueryCollection deviceTwinQueryCollection) {
            return true;
        }

        @Mock
        DeviceTwinDevice jsonToDeviceTwinDevice(String json) throws IOException {
            return new DeviceTwinDevice();
        }
    };
    new StrictExpectations() {

        {
            Deencapsulation.invoke(mockQueryCollection, "next", new Class[] { QueryOptions.class }, mockQueryOptions);
            result = mockQueryCollectionResponse;
            mockQueryCollectionResponse.getCollection();
            result = mockCollection;
            mockCollection.iterator();
            result = mockIterator;
            mockIterator.hasNext();
            result = true;
            mockIterator.next();
            result = expectedJsonString;
            mockIterator.hasNext();
            result = true;
            mockIterator.next();
            result = expectedJsonString;
            mockIterator.hasNext();
            result = false;
            mockQueryCollectionResponse.getContinuationToken();
            result = expectedContinuationToken;
            Deencapsulation.newInstance(QueryCollectionResponse.class, new Class[] { Collection.class, String.class }, (Collection) any, expectedContinuationToken);
            result = mockQueryCollectionResponse;
        }
    };
    // act
    deviceTwin.next(mockQueryCollection, mockQueryOptions);
}
Also used : QueryCollection(com.microsoft.azure.sdk.iot.service.devicetwin.QueryCollection) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) StrictExpectations(mockit.StrictExpectations) NonStrictExpectations(mockit.NonStrictExpectations) MockUp(mockit.MockUp) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 2 with QueryCollection

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

the class QueryTwinTests method queryCollectionCanReturnEmptyQueryResults.

@Test
@StandardTierHubOnlyTest
@ContinuousIntegrationTest
public void queryCollectionCanReturnEmptyQueryResults() throws IOException, IotHubException {
    String fullQuery = "select * from devices where deviceId='nonexistantdevice'";
    DeviceTwin twinClient = new DeviceTwin(iotHubConnectionString, DeviceTwinClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    QueryCollection twinQuery = twinClient.queryTwinCollection(fullQuery);
    QueryOptions options = new QueryOptions();
    QueryCollectionResponse<DeviceTwinDevice> response = twinClient.next(twinQuery, options);
    assertNull(response.getContinuationToken());
    assertTrue(response.getCollection().isEmpty());
}
Also used : QueryCollection(com.microsoft.azure.sdk.iot.service.devicetwin.QueryCollection) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) QueryOptions(com.microsoft.azure.sdk.iot.service.devicetwin.QueryOptions) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)

Example 3 with QueryCollection

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

the class QueryTwinTests method testQueryTwinWithContinuationToken.

@Test
@StandardTierHubOnlyTest
@ContinuousIntegrationTest
public void testQueryTwinWithContinuationToken() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, URISyntaxException, ModuleClientException {
    addMultipleDevices(PAGE_SIZE + 1, false);
    // Add same desired on multiple devices so that they can be queried
    final String queryProperty = PROPERTY_KEY_QUERY + UUID.randomUUID().toString();
    final String queryPropertyValue = PROPERTY_VALUE_QUERY + UUID.randomUUID().toString();
    setDesiredProperties(queryProperty, queryPropertyValue, PAGE_SIZE + 1);
    // Query multiple devices having same property
    final String where = "is_defined(properties.desired." + queryProperty + ")";
    SqlQuery sqlQuery;
    if (this.testInstance.clientType == ClientType.MODULE_CLIENT) {
        sqlQuery = SqlQuery.createSqlQuery("*", SqlQuery.FromType.MODULES, where, null);
    } else {
        sqlQuery = SqlQuery.createSqlQuery("*", SqlQuery.FromType.DEVICES, where, null);
    }
    // There is some propagation delay between when all the devices are created and have their twins set, and when
    // they become queryable. This test assumes that eventually, the query result will have multiple pages. To
    // avoid querying too soon, this test repeatedly queries until the continuation token is present in the return value
    // as expected or until a timeout is hit.
    String continuationToken = null;
    Collection<DeviceTwinDevice> queriedDeviceTwinDeviceCollection = null;
    long startTime = System.currentTimeMillis();
    while (continuationToken == null) {
        QueryCollection twinQueryCollection = testInstance.twinServiceClient.queryTwinCollection(sqlQuery.getQuery(), PAGE_SIZE);
        // Run a query and save the continuation token for the second page of results
        QueryCollectionResponse<DeviceTwinDevice> queryCollectionResponse = testInstance.twinServiceClient.next(twinQueryCollection);
        queriedDeviceTwinDeviceCollection = queryCollectionResponse.getCollection();
        continuationToken = queryCollectionResponse.getContinuationToken();
        if (continuationToken == null) {
            log.info("No continuation token detected yet, re-running the query");
            Thread.sleep(200);
        }
        if (System.currentTimeMillis() - startTime > QUERY_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for query to return a continuation token");
        }
    }
    // Re-run the same query using the saved continuation token. The results can be predicted since this test caused them
    QueryOptions options = new QueryOptions();
    options.setContinuationToken(continuationToken);
    options.setPageSize(PAGE_SIZE);
    QueryCollection twinQueryToReRun = testInstance.twinServiceClient.queryTwinCollection(sqlQuery.getQuery());
    Collection<DeviceTwinDevice> continuedDeviceTwinDeviceQuery = testInstance.twinServiceClient.next(twinQueryToReRun, options).getCollection();
    // Assert
    assertEquals((long) PAGE_SIZE, queriedDeviceTwinDeviceCollection.size());
    assertEquals(1, continuedDeviceTwinDeviceQuery.size());
    // since order is not guaranteed, we cannot check that the third updated deviceTwinDevice is the third queried.
    // Instead, all we can check is that each updated device twin identity is in either the initial query or the continued query.
    ArrayList<String> expectedDeviceIds = new ArrayList<>();
    for (int deviceTwinDeviceIndex = 0; deviceTwinDeviceIndex < PAGE_SIZE + 1; deviceTwinDeviceIndex++) {
        expectedDeviceIds.add(testInstance.devicesUnderTest[deviceTwinDeviceIndex].sCDeviceForTwin.getDeviceId());
    }
    Collection<DeviceTwinDevice> allQueriedDeviceTwinDevices = new ArrayList<>(continuedDeviceTwinDeviceQuery);
    continuedDeviceTwinDeviceQuery.addAll(queriedDeviceTwinDeviceCollection);
    for (DeviceTwinDevice deviceTwinDevice : allQueriedDeviceTwinDevices) {
        if (!expectedDeviceIds.contains(deviceTwinDevice.getDeviceId())) {
            fail("Missing deviceTwinDevice: continuation token did not continue query where expected");
        }
    }
}
Also used : QueryCollection(com.microsoft.azure.sdk.iot.service.devicetwin.QueryCollection) SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) QueryOptions(com.microsoft.azure.sdk.iot.service.devicetwin.QueryOptions) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)

Aggregations

IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)3 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)3 QueryCollection (com.microsoft.azure.sdk.iot.service.devicetwin.QueryCollection)3 Test (org.junit.Test)3 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)2 QueryOptions (com.microsoft.azure.sdk.iot.service.devicetwin.QueryOptions)2 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)2 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)2 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)2 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)2 SqlQuery (com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery)1 ArrayList (java.util.ArrayList)1 MockUp (mockit.MockUp)1 NonStrictExpectations (mockit.NonStrictExpectations)1 StrictExpectations (mockit.StrictExpectations)1