Search in sources :

Example 31 with Query

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"));
}
Also used : Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Example 32 with Query

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"));
}
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 33 with Query

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);
}
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 34 with Query

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!");
}
Also used : JobStatus(com.microsoft.azure.sdk.iot.service.jobs.JobStatus) DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) JobsResponseParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsResponseParser) Before(org.junit.Before)

Example 35 with Query

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());
    }
}
Also used : DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)

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