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);
}
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);
}
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);
}
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);
}
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());
}
Aggregations