Search in sources :

Example 81 with IotHubConnectionString

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

the class DeviceMethodCommon method buildDeviceMethodClientWithAzureSasCredential.

protected static DeviceMethod buildDeviceMethodClientWithAzureSasCredential() {
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
    DeviceMethodClientOptions options = DeviceMethodClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build();
    return new DeviceMethod(iotHubConnectionStringObj.getHostName(), azureSasCredential, options);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) DeviceMethodClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions) AzureSasCredential(com.azure.core.credential.AzureSasCredential) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)

Example 82 with IotHubConnectionString

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

the class GetTwinTests method testGetDeviceTwinWithProxy.

@Test
@StandardTierHubOnlyTest
public void testGetDeviceTwinWithProxy() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, ModuleClientException, URISyntaxException {
    if (testInstance.protocol != IotHubClientProtocol.MQTT || testInstance.authenticationType != AuthenticationType.SAS || testInstance.clientType != ClientType.DEVICE_CLIENT) {
        // when the device is using MQTT with SAS auth
        return;
    }
    super.setUpNewDeviceAndModule();
    String testProxyHostname = "127.0.0.1";
    int testProxyPort = 8892;
    HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap().withPort(testProxyPort).start();
    try {
        Proxy serviceSideProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
        ProxyOptions proxyOptions = new ProxyOptions(serviceSideProxy);
        DeviceTwinClientOptions options = DeviceTwinClientOptions.builder().proxyOptions(proxyOptions).build();
        testInstance.twinServiceClient = DeviceTwin.createFromConnectionString(iotHubConnectionString, options);
        super.testGetDeviceTwin();
    } finally {
        proxyServer.stop();
    }
}
Also used : DefaultHttpProxyServer(org.littleshoot.proxy.impl.DefaultHttpProxyServer) HttpProxyServer(org.littleshoot.proxy.HttpProxyServer) Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) InetSocketAddress(java.net.InetSocketAddress) DeviceTwinClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinClientOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) 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)

Example 83 with IotHubConnectionString

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

the class QueryTwinTests method rawQueryTokenRenewalWithAzureSasCredential.

@Test
@StandardTierHubOnlyTest
public void rawQueryTokenRenewalWithAzureSasCredential() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, URISyntaxException, ModuleClientException {
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
    testInstance.twinServiceClient = new DeviceTwin(iotHubConnectionStringObj.getHostName(), sasCredential);
    // test that the service client can query before the shared access signature expires
    testRawQueryTwin();
    // deliberately expire the SAS token to provoke a 401 to ensure that the registry manager is using the shared
    // access signature that is set here.
    sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
    try {
        testRawQueryTwin();
        fail("Expected query call to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
    } catch (IotHubUnathorizedException e) {
        log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
    }
    // Renew the expired shared access signature
    serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    sasCredential.update(serviceSasToken.toString());
    // test that the service client can query after renewing the shared access signature expires
    testRawQueryTwin();
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) 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 84 with IotHubConnectionString

use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString 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 85 with IotHubConnectionString

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

the class ProvisioningTests method customAllocationFlow.

/**
 * This test flow uses a custom allocation policy to decide which of the two hubs a device should be provisioned to.
 * The custom allocation policy has a webhook to an Azure function, and that function will always dictate to provision
 * the device to the hub with the longest host name. This test verifies that an enrollment with a custom allocation policy
 * pointing to that Azure function will always enroll to the hub with the longest name
 * @param enrollmentType The type of the enrollment to test
 * @throws Exception if an exception occurs during provisioning or while creating the security provider
 */
protected void customAllocationFlow(EnrollmentType enrollmentType) throws Exception {
    if (enrollmentType == EnrollmentType.GROUP && testInstance.attestationType != AttestationType.SYMMETRIC_KEY) {
        // tpm doesn't support group, and x509 group test has not been implemented yet
        return;
    }
    List<String> possibleStartingHubHostNames = new ArrayList<>();
    String farAwayIotHubHostname = IotHubConnectionString.createConnectionString(farAwayIotHubConnectionString).getHostName();
    String iothubHostName = IotHubConnectionString.createConnectionString(iotHubConnectionString).getHostName();
    possibleStartingHubHostNames.add(farAwayIotHubHostname);
    possibleStartingHubHostNames.add(iothubHostName);
    String expectedHubToProvisionTo;
    if (farAwayIotHubHostname.length() > iothubHostName.length()) {
        expectedHubToProvisionTo = farAwayIotHubHostname;
    } else if (iothubHostName.length() > farAwayIotHubHostname.length()) {
        expectedHubToProvisionTo = iothubHostName;
    } else {
        throw new IllegalArgumentException("Both possible hub's cannot have a host name of the same length for this test to work");
    }
    CustomAllocationDefinition customAllocationDefinition = new CustomAllocationDefinition();
    customAllocationDefinition.setApiVersion(CUSTOM_ALLOCATION_WEBHOOK_API_VERSION);
    customAllocationDefinition.setWebhookUrl(customAllocationWebhookUrl);
    testInstance.securityProvider = getSecurityProviderInstance(enrollmentType, AllocationPolicy.CUSTOM, null, customAllocationDefinition, possibleStartingHubHostNames);
    registerDevice(testInstance.protocol, testInstance.securityProvider, provisioningServiceGlobalEndpoint, true, null, expectedHubToProvisionTo, null);
}
Also used : ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString)

Aggregations

IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)196 Test (org.junit.Test)171 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)30 Expectations (mockit.Expectations)21 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)20 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)17 AzureSasCredential (com.azure.core.credential.AzureSasCredential)16 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)13 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)13 Device (com.microsoft.azure.sdk.iot.service.Device)12 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)12 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)12 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)11 URL (java.net.URL)11 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)11 NonStrictExpectations (mockit.NonStrictExpectations)10 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)9 ArrayList (java.util.ArrayList)9 Verifications (mockit.Verifications)9 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)8