use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class QueryTest method constructorWithSQLQuerySucceeds.
// Tests_SRS_QUERY_25_001: [The constructor shall validate query and save query, pagesize and request type]
@Test
public void constructorWithSQLQuerySucceeds() throws IllegalArgumentException {
// arrange
final String sqlQuery = DEFAULT_QUERY;
// act
Query testQuery = Deencapsulation.newInstance(Query.class, sqlQuery, DEFAULT_PAGE_SIZE, DEFAULT_QUERY_TYPE);
// assert
assertNotNull(Deencapsulation.getField(testQuery, "pageSize"));
assertEquals(DEFAULT_PAGE_SIZE, (int) Deencapsulation.getField(testQuery, "pageSize"));
assertEquals(sqlQuery, Deencapsulation.getField(testQuery, "query"));
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"));
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class QueryTest method sendQueryRequestDoesNotSetNullHeaders.
@Test
public void sendQueryRequestDoesNotSetNullHeaders() 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);
Deencapsulation.invoke(testQuery, "continueQuery", String.class);
// assert
new Verifications() {
{
new HttpRequest(mockUrl, mockHttpMethod, (byte[]) any);
times = 2;
mockHttpRequest.setHeaderField("x-ms-max-item-count", String.valueOf(DEFAULT_PAGE_SIZE));
times = 2;
mockHttpRequest.setHeaderField("x-ms-continuation", anyString);
times = 0;
mockHttpRequest.setHeaderField(anyString, anyString);
minTimes = 10;
}
};
assertEquals(testToken, Deencapsulation.getField(testQuery, "responseContinuationToken"));
assertEquals(DEFAULT_QUERY_TYPE, Deencapsulation.getField(testQuery, "responseQueryType"));
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method hasNextSucceeds.
// Tests_SRS_DEVICETWIN_25_055: [ If a queryResponse is available, this method shall return true as is to the user. ]
// Tests_SRS_DEVICETWIN_25_054: [ The method shall check if a response to query is avaliable by calling hasNext on the query object.]
@Test
public void hasNextSucceeds(@Mocked DeviceTwinDevice mockedDevice) throws IotHubException, IOException {
// arrange
final String connectionString = "testString";
final int connectTimeout = 1234;
final int readTimeout = 5678;
constructorExpectations(connectionString);
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString, DeviceTwinClientOptions.builder().httpConnectTimeout(connectTimeout).httpReadTimeout(readTimeout).build());
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 = true;
}
};
Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
// act
boolean result = testTwin.hasNextDeviceTwin(testQuery);
assertTrue(result);
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class JobClientTests method cleanToStart.
@Before
public void cleanToStart() throws IOException, IotHubException {
for (DeviceTestManager device : devices) {
device.clearStatistics();
}
log.info("Waiting for all previously scheduled jobs to finish...");
long startTime = System.currentTimeMillis();
Query activeJobsQuery = jobClient.queryDeviceJob("SELECT * FROM devices.jobs");
while (activeJobsQuery.hasNext()) {
JobsResponseParser job = JobsResponseParser.createFromJson(activeJobsQuery.next().toString());
JobStatus jobStatus = jobClient.getJob(job.getJobId()).getJobStatus();
while (jobStatus.equals(JobStatus.enqueued) || jobStatus.equals(JobStatus.queued) || jobStatus.equals(JobStatus.running) || jobStatus.equals(JobStatus.scheduled)) {
try {
Thread.sleep(500);
jobStatus = jobClient.getJob(job.getJobId()).getJobStatus();
} catch (InterruptedException e) {
fail("Unexpected interrupted exception occurred");
}
if (System.currentTimeMillis() - startTime > MAX_TIME_WAIT_FOR_PREVIOUSLY_SCHEDULED_JOBS_TO_FINISH_IN_MILLIS) {
fail("Waited too long for previously scheduled jobs to finish");
}
}
}
log.info("Done waiting for jobs to finish!");
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class ProvisioningCommon method assertProvisionedDeviceCapabilitiesAreExpected.
protected void assertProvisionedDeviceCapabilitiesAreExpected(DeviceCapabilities expectedDeviceCapabilities, String provisionedHubConnectionString) throws IOException, IotHubException, InterruptedException {
DeviceTwin deviceTwin = DeviceTwin.createFromConnectionString(provisionedHubConnectionString, DeviceTwinClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
boolean deviceFoundInCorrectHub = false;
Query query = null;
long startTime = System.currentTimeMillis();
while (!deviceFoundInCorrectHub) {
query = deviceTwin.queryTwin("SELECT * FROM devices WHERE deviceId = '" + testInstance.provisionedDeviceId + "'");
deviceFoundInCorrectHub = deviceTwin.hasNextDeviceTwin(query);
Thread.sleep(3000);
if (System.currentTimeMillis() - startTime > QUERY_TIMEOUT_MILLISECONDS) {
fail(CorrelationDetailsLoggingAssert.buildExceptionMessageDpsIndividualOrGroup("Timed out waiting for provisioned device " + testInstance.provisionedDeviceId + " to be found in expected hub", getHostName(provisioningServiceConnectionString), testInstance.groupId, testInstance.registrationId));
}
}
DeviceTwinDevice provisionedDevice = deviceTwin.getNextDeviceTwin(query);
if (expectedDeviceCapabilities.isIotEdge()) {
assertTrue(CorrelationDetailsLoggingAssert.buildExceptionMessageDpsIndividualOrGroup("Provisioned device isn't edge device: " + testInstance.provisionedDeviceId, getHostName(provisioningServiceConnectionString), testInstance.groupId, testInstance.registrationId), provisionedDevice.getCapabilities().isIotEdge());
} else {
assertTrue(CorrelationDetailsLoggingAssert.buildExceptionMessageDpsIndividualOrGroup("Provisioned device shouldn't be edge device " + testInstance.provisionedDeviceId, getHostName(provisioningServiceConnectionString), testInstance.groupId, testInstance.registrationId), provisionedDevice.getCapabilities() == null || !provisionedDevice.getCapabilities().isIotEdge());
}
}
Aggregations