Search in sources :

Example 41 with StandardTierHubOnlyTest

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest 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)

Example 42 with StandardTierHubOnlyTest

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest in project azure-iot-sdk-java by Azure.

the class QueryTwinTests method testRawQueryTwinWithConnectionString.

@Test
@StandardTierHubOnlyTest
public void testRawQueryTwinWithConnectionString() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, URISyntaxException, ModuleClientException {
    testInstance.twinServiceClient = new DeviceTwin(iotHubConnectionString);
    testRawQueryTwin();
}
Also used : 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)

Example 43 with StandardTierHubOnlyTest

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest in project azure-iot-sdk-java by Azure.

the class TwinTagsTests method testGetTwinUpdates.

@Test
@StandardTierHubOnlyTest
public void testGetTwinUpdates() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, URISyntaxException, ModuleClientException {
    addMultipleDevices(MAX_DEVICES);
    // Add tag and desired for multiple devices
    for (int i = 0; i < MAX_DEVICES; i++) {
        Set<Pair> tags = new HashSet<>();
        tags.add(new Pair(TAG_KEY + i, TAG_VALUE + i));
        testInstance.devicesUnderTest[i].sCDeviceForTwin.setTags(tags);
        Set<Pair> desiredProperties = new HashSet<>();
        desiredProperties.add(new Pair(PROPERTY_KEY + i, PROPERTY_VALUE + i));
        testInstance.devicesUnderTest[i].sCDeviceForTwin.setDesiredProperties(desiredProperties);
        testInstance.twinServiceClient.updateTwin(testInstance.devicesUnderTest[i].sCDeviceForTwin);
        testInstance.devicesUnderTest[i].sCDeviceForTwin.clearTwin();
    }
    // Update Tags and desired properties on multiple devices
    for (int i = 0; i < MAX_DEVICES; i++) {
        testInstance.twinServiceClient.getTwin(testInstance.devicesUnderTest[i].sCDeviceForTwin);
        Set<Pair> tags = testInstance.devicesUnderTest[i].sCDeviceForTwin.getTags();
        for (Pair tag : tags) {
            tag.setValue(TAG_VALUE_UPDATE + i);
        }
        testInstance.devicesUnderTest[i].sCDeviceForTwin.setTags(tags);
        Set<Pair> desiredProperties = testInstance.devicesUnderTest[i].sCDeviceForTwin.getDesiredProperties();
        for (Pair dp : desiredProperties) {
            dp.setValue(PROPERTY_VALUE_UPDATE + i);
        }
        testInstance.devicesUnderTest[i].sCDeviceForTwin.setDesiredProperties(desiredProperties);
        testInstance.twinServiceClient.updateTwin(testInstance.devicesUnderTest[i].sCDeviceForTwin);
        testInstance.devicesUnderTest[i].sCDeviceForTwin.clearTwin();
    }
    // Read updates on multiple devices
    for (int i = 0; i < MAX_DEVICES; i++) {
        testInstance.twinServiceClient.getTwin(testInstance.devicesUnderTest[i].sCDeviceForTwin);
        for (Pair t : testInstance.devicesUnderTest[i].sCDeviceForTwin.getTags()) {
            assertEquals(buildExceptionMessage("unexpected tag key, expected " + TAG_KEY + i + " but was " + t.getKey(), testInstance.testIdentity.getClient()), TAG_KEY + i, t.getKey());
            assertEquals(buildExceptionMessage("Unexpected tag value, expected " + TAG_VALUE_UPDATE + i + " but was " + t.getValue(), testInstance.testIdentity.getClient()), TAG_VALUE_UPDATE + i, t.getValue());
        }
        for (Pair dp : testInstance.devicesUnderTest[i].sCDeviceForTwin.getDesiredProperties()) {
            assertEquals(buildExceptionMessage("Unexpected desired property key, expected " + PROPERTY_KEY + i + " but was " + dp.getKey(), testInstance.testIdentity.getClient()), PROPERTY_KEY + i, dp.getKey());
            assertEquals(buildExceptionMessage("Unexpected desired property value, expected " + PROPERTY_VALUE_UPDATE + i + " but was " + dp.getValue(), testInstance.testIdentity.getClient()), PROPERTY_VALUE_UPDATE + i, dp.getValue());
        }
        Integer version = testInstance.devicesUnderTest[i].sCDeviceForTwin.getDesiredPropertiesVersion();
        assertNotNull(buildExceptionMessage("Version was null", testInstance.testIdentity.getClient()), version);
    }
}
Also used : Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) HashSet(java.util.HashSet) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) Test(org.junit.Test) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)

Example 44 with StandardTierHubOnlyTest

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest in project azure-iot-sdk-java by Azure.

the class DeviceMethodTests method invokeMethodNullPayloadSucceed.

@Test
@StandardTierHubOnlyTest
@ContinuousIntegrationTest
public void invokeMethodNullPayloadSucceed() throws Exception {
    // Arrange
    super.openDeviceClientAndSubscribeToMethods();
    DeviceTestManager deviceTestManger = this.testInstance.deviceTestManager;
    // Act
    MethodResult result;
    if (testInstance.identity instanceof TestModuleIdentity) {
        result = testInstance.methodServiceClient.invoke(testInstance.identity.getDeviceId(), ((TestModuleIdentity) testInstance.identity).getModuleId(), DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, null);
    } else {
        result = testInstance.methodServiceClient.invoke(testInstance.identity.getDeviceId(), DeviceEmulator.METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, null);
    }
    deviceTestManger.waitIotHub(1, 10);
    // Assert
    assertNotNull(buildExceptionMessage("method result was null", testInstance.deviceTestManager.client), result);
    assertEquals(buildExceptionMessage("Expected SUCCESS but got " + result.getStatus(), testInstance.deviceTestManager.client), (long) DeviceEmulator.METHOD_SUCCESS, (long) result.getStatus());
    assertEquals(buildExceptionMessage("Expected " + DeviceEmulator.METHOD_LOOPBACK + ":null" + " but got " + result.getPayload(), deviceTestManger.client), DeviceEmulator.METHOD_LOOPBACK + ":null", result.getPayload());
    Assert.assertEquals(buildExceptionMessage("Unexpected status errors occurred", testInstance.deviceTestManager.client), 0, deviceTestManger.getStatusError());
}
Also used : DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) TestModuleIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestModuleIdentity) 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) 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 45 with StandardTierHubOnlyTest

use of tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest in project azure-iot-sdk-java by Azure.

the class DeviceMethodTests method invokeMethodRecoverFromTimeoutSucceed.

@Test
@StandardTierHubOnlyTest
@ContinuousIntegrationTest
public void invokeMethodRecoverFromTimeoutSucceed() throws Exception {
    // Arrange
    super.openDeviceClientAndSubscribeToMethods();
    DeviceTestManager deviceTestManger = this.testInstance.deviceTestManager;
    try {
        if (testInstance.identity instanceof TestModuleIdentity) {
            testInstance.methodServiceClient.invoke(testInstance.identity.getDeviceId(), ((TestModuleIdentity) testInstance.identity).getModuleId(), DeviceEmulator.METHOD_DELAY_IN_MILLISECONDS, (long) 5, CONNECTION_TIMEOUT, "7000");
        } else {
            testInstance.methodServiceClient.invoke(testInstance.identity.getDeviceId(), DeviceEmulator.METHOD_DELAY_IN_MILLISECONDS, (long) 5, CONNECTION_TIMEOUT, "7000");
        }
        assert true;
    } catch (IotHubGatewayTimeoutException expected) {
    // Don't do anything. Expected throw.
    }
    // Act
    MethodResult result;
    if (testInstance.identity instanceof TestModuleIdentity) {
        result = testInstance.methodServiceClient.invoke(testInstance.identity.getDeviceId(), ((TestModuleIdentity) testInstance.identity).getModuleId(), DeviceEmulator.METHOD_DELAY_IN_MILLISECONDS, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, "100");
    } else {
        result = testInstance.methodServiceClient.invoke(testInstance.identity.getDeviceId(), DeviceEmulator.METHOD_DELAY_IN_MILLISECONDS, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, "100");
    }
    deviceTestManger.waitIotHub(1, 10);
    // Assert
    assertNotNull(buildExceptionMessage("method result was null", testInstance.deviceTestManager.client), result);
    assertEquals(buildExceptionMessage("Expected SUCCESS but got " + result.getStatus(), testInstance.deviceTestManager.client), (long) DeviceEmulator.METHOD_SUCCESS, (long) result.getStatus());
    assertEquals(buildExceptionMessage("Expected " + DeviceEmulator.METHOD_DELAY_IN_MILLISECONDS + ":succeed" + " But got " + result.getPayload(), testInstance.deviceTestManager.client), DeviceEmulator.METHOD_DELAY_IN_MILLISECONDS + ":succeed", result.getPayload());
    Assert.assertEquals(buildExceptionMessage("Unexpected status errors occurred", testInstance.deviceTestManager.client), 0, deviceTestManger.getStatusError());
}
Also used : DeviceTestManager(tests.integration.com.microsoft.azure.sdk.iot.helpers.DeviceTestManager) IotHubGatewayTimeoutException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubGatewayTimeoutException) TestModuleIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestModuleIdentity) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) 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) 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

StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)45 Test (org.junit.Test)40 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)32 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)31 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)28 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)19 DigitalTwinTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest)12 AzureSasCredential (com.azure.core.credential.AzureSasCredential)10 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)10 DigitalTwinGetHeaders (com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders)9 BasicDigitalTwin (com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin)9 Device (com.microsoft.azure.sdk.iot.service.Device)8 IOException (java.io.IOException)8 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)7 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)7 Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)6 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)6 DigitalTwinClient (com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient)6 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)5 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)5