Search in sources :

Example 1 with DigitalTwinClient

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

the class TokenCredentialTests method getDigitalTwinWithTokenCredential.

@Test
public void getDigitalTwinWithTokenCredential() throws IOException, IotHubException, URISyntaxException {
    // only run tests for standard tier hubs
    Assume.assumeFalse(isBasicTierHub);
    RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
    DeviceClient deviceClient = createDeviceClient(MQTT, registryManager);
    deviceClient.open();
    // arrange
    DigitalTwinClient digitalTwinClient = buildDigitalTwinClientWithTokenCredential();
    // act
    BasicDigitalTwin response = digitalTwinClient.getDigitalTwin(deviceClient.getConfig().getDeviceId(), BasicDigitalTwin.class);
    ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceClient.getConfig().getDeviceId(), BasicDigitalTwin.class);
    // assert
    assertEquals(response.getMetadata().getModelId(), THERMOSTAT_MODEL_ID);
    assertEquals(responseWithHeaders.body().getMetadata().getModelId(), THERMOSTAT_MODEL_ID);
}
Also used : BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient) Test(org.junit.Test)

Example 2 with DigitalTwinClient

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

the class Tools method buildDigitalTwinClientWithTokenCredential.

public static DigitalTwinClient buildDigitalTwinClientWithTokenCredential() {
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    TokenCredential tokenCredential = buildTokenCredentialFromEnvironment();
    DigitalTwinClientOptions options = DigitalTwinClientOptions.builder().build();
    return new DigitalTwinClient(iotHubConnectionStringObj.getHostName(), tokenCredential, options);
}
Also used : DigitalTwinClientOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) TokenCredential(com.azure.core.credential.TokenCredential) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient)

Example 3 with DigitalTwinClient

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

the class DigitalTwinClientTests method digitalTwinConstructorThrowsForNegativeConnectTimeout.

@Test(expected = IllegalArgumentException.class)
@StandardTierHubOnlyTest
public void digitalTwinConstructorThrowsForNegativeConnectTimeout() {
    // arrange
    DigitalTwinClientOptions clientOptions = DigitalTwinClientOptions.builder().httpConnectTimeout(-1).build();
    digitalTwinClient = new DigitalTwinClient(IOTHUB_CONNECTION_STRING, clientOptions);
}
Also used : DigitalTwinClientOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest) 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 4 with DigitalTwinClient

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

the class DigitalTwinClientTests method digitalTwinClientTokenRenewalWithAzureSasCredential.

@Test
@StandardTierHubOnlyTest
public void digitalTwinClientTokenRenewalWithAzureSasCredential() {
    if (protocol != MQTT) {
        // This test is for the service client, so no need to rerun it for all the different device protocols
        return;
    }
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(IOTHUB_CONNECTION_STRING);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
    digitalTwinClient = new DigitalTwinClient(iotHubConnectionStringObj.getHostName(), sasCredential);
    // get a digital twin with a valid SAS token in the AzureSasCredential instance
    // don't care about the return value, just checking that the request isn't unauthorized
    digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
    // deliberately expire the SAS token to provoke a 401 to ensure that the digital twin client is using the shared
    // access signature that is set here.
    sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
    try {
        digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
        fail("Expected get digital twin call to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
    } catch (RestException e) {
        if (e.response().code() == 401) {
            log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
        } else {
            throw e;
        }
    }
    // Renew the expired shared access signature
    serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    sasCredential.update(serviceSasToken.toString());
    // get a digital twin using the renewed shared access signature
    // don't care about the return value, just checking that the request isn't unauthorized
    digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) RestException(com.microsoft.rest.RestException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest) 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 5 with DigitalTwinClient

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

the class DigitalTwinClientTests method setUp.

@Before
public void setUp() throws URISyntaxException, IOException, IotHubException {
    this.deviceClient = createDeviceClient(protocol);
    deviceClient.open();
    digitalTwinClient = new DigitalTwinClient(IOTHUB_CONNECTION_STRING, DigitalTwinClientOptions.builder().httpReadTimeout(0).build());
}
Also used : DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient) Before(org.junit.Before)

Aggregations

DigitalTwinClient (com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient)8 DigitalTwinClientOptions (com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions)5 Test (org.junit.Test)5 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)4 DigitalTwinTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest)4 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)4 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)3 AzureSasCredential (com.azure.core.credential.AzureSasCredential)2 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)2 DigitalTwinGetHeaders (com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders)2 BasicDigitalTwin (com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin)2 TokenCredential (com.azure.core.credential.TokenCredential)1 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)1 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)1 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)1 RestException (com.microsoft.rest.RestException)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 Before (org.junit.Before)1