use of com.microsoft.azure.sdk.iot.service.devicetwin.QueryOptions in project azure-iot-sdk-java by Azure.
the class QueryOptionsTest method setPageSizeSuccess.
// Tests_SRS_QUERYOPTIONS_34_007: [This function shall save the provided page size.]
@Test
public void setPageSizeSuccess() {
// arrange
Integer expectedPageSize = 758;
QueryOptions options = new QueryOptions();
// act
options.setPageSize(expectedPageSize);
// assert
Integer actualPageSize = Deencapsulation.getField(options, "pageSize");
assertEquals(expectedPageSize, actualPageSize);
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.QueryOptions in project azure-iot-sdk-java by Azure.
the class QueryOptionsTest method constructorSuccess.
// Tests_SRS_QUERYOPTIONS_34_001: [This constructor shall initialize a QueryOptions object with a default page size of 100 and no continuation token.]
@Test
public void constructorSuccess() {
// act
QueryOptions options = new QueryOptions();
// assert
Integer actualPageSize = Deencapsulation.getField(options, "pageSize");
String actualContinuationToken = Deencapsulation.getField(options, "continuationToken");
assertEquals(new Integer(100), actualPageSize);
assertNull(actualContinuationToken);
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.QueryOptions 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");
}
}
}
Aggregations