use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class MultiplexingClientTests method registerDeviceWithIncorrectCredentialsBeforeOpenThrowsOnOpen.
// Before opening the multiplexed connection, register a single device with incorrect credentials. Opening the client
// should throw and the thrown exception should have details on why the open failed
@ContinuousIntegrationTest
@Test
public void registerDeviceWithIncorrectCredentialsBeforeOpenThrowsOnOpen() throws Exception {
testInstance.setup(DEVICE_MULTIPLEX_COUNT);
testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(0));
// Get a valid connection string, but swap out the deviceId for a deviceId that does exist, but whose symmetric key is different
String incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(1)).replace(testInstance.deviceIdentityArray.get(1).getDeviceId(), testInstance.deviceIdentityArray.get(0).getDeviceId());
DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
testInstance.multiplexingClient.registerDeviceClient(clientWithIncorrectCredentials);
boolean expectedExceptionThrown = false;
try {
testInstance.multiplexingClient.open();
} catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
assertEquals(1, registrationExceptions.size());
String deviceId = testInstance.deviceIdentityArray.get(0).getDeviceId();
assertTrue(registrationExceptions.containsKey(deviceId));
assertTrue(registrationExceptions.get(deviceId) instanceof UnauthorizedException);
expectedExceptionThrown = true;
}
testInstance.multiplexingClient.close();
assertTrue("Expected exception was not thrown", expectedExceptionThrown);
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class MultiplexingClientTests method registerDeviceWithIncorrectCredentialsAfterOpenThrows.
// Attempt to register a single device with the wrong connection string. The thrown exception
// should contain all the exceptions thrown by the service.
@ContinuousIntegrationTest
@Test
public void registerDeviceWithIncorrectCredentialsAfterOpenThrows() throws Exception {
testInstance.setup(DEVICE_MULTIPLEX_COUNT);
testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(0));
testInstance.multiplexingClient.open();
// Get a valid connection string, but swap out the deviceId for a deviceId that does exist, but whose symmetric key is different
String incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(1)).replace(testInstance.deviceIdentityArray.get(1).getDeviceId(), testInstance.deviceIdentityArray.get(0).getDeviceId());
DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
boolean expectedExceptionThrown = false;
try {
testInstance.multiplexingClient.registerDeviceClient(clientWithIncorrectCredentials);
} catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
assertEquals(1, registrationExceptions.size());
String deviceId = testInstance.deviceIdentityArray.get(0).getDeviceId();
assertTrue(registrationExceptions.containsKey(deviceId));
assertTrue(registrationExceptions.get(deviceId) instanceof UnauthorizedException);
expectedExceptionThrown = true;
}
testInstance.multiplexingClient.close();
assertTrue("Expected exception was not thrown", expectedExceptionThrown);
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class MultiplexingClientTests method failedRegistrationDoesNotAffectSubsequentRegistrations.
@ContinuousIntegrationTest
@Test
public void failedRegistrationDoesNotAffectSubsequentRegistrations() throws Exception {
testInstance.setup(0);
testInstance.multiplexingClient.open();
TestDeviceIdentity testDeviceIdentity = Tools.getTestDevice(iotHubConnectionString, this.testInstance.protocol, AuthenticationType.SAS, false);
String deviceConnectionString = registryManager.getDeviceConnectionString(testDeviceIdentity.getDevice());
String deviceNotFoundConnectionString = deviceConnectionString.replace(testDeviceIdentity.getDeviceId(), testDeviceIdentity.getDeviceId().toUpperCase());
DeviceClient validDeviceClient = new DeviceClient(deviceConnectionString, testInstance.protocol);
DeviceClient invalidDeviceClient = new DeviceClient(deviceNotFoundConnectionString, testInstance.protocol);
try {
testInstance.multiplexingClient.registerDeviceClient(invalidDeviceClient);
fail("Expected multiplexingClient to throw since it registered a device that did not exist.");
} catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
// expected throw since the deviceId in the connection string does not exist, ignore
}
testInstance.multiplexingClient.registerDeviceClient(validDeviceClient);
testSendingMessageFromDeviceClient(validDeviceClient);
testInstance.multiplexingClient.close();
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class MultiplexingClientTests method registrationsUnwindForX509Client.
@Test
public void registrationsUnwindForX509Client() throws Exception {
// Create a new device client that uses x509 auth, which should throw an UnsupportedOperationException
// since x509 auth isn't supported while multiplexing
Device x509Device = Tools.getTestDevice(iotHubConnectionString, IotHubClientProtocol.MQTT, AuthenticationType.SELF_SIGNED, false).getDevice();
String deviceConnectionString = registryManager.getDeviceConnectionString(x509Device);
DeviceClient x509DeviceClient = new DeviceClient(deviceConnectionString, testInstance.protocol, new IotHubSSLContext().getSSLContext());
registrationsUnwindForUnsupportedOperationExceptions(x509DeviceClient);
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class MultiplexingClientTests method registrationsUnwindForAlreadyOpenClient.
@Test
public void registrationsUnwindForAlreadyOpenClient() throws Exception {
Device nonMultiplexedDevice = Tools.getTestDevice(iotHubConnectionString, testInstance.protocol, AuthenticationType.SAS, false).getDevice();
String deviceConnectionString = registryManager.getDeviceConnectionString(nonMultiplexedDevice);
DeviceClient nonMultiplexedDeviceClient = new DeviceClient(deviceConnectionString, testInstance.protocol);
// By opening the client once, this client can no longer be registered to a multiplexing client
nonMultiplexedDeviceClient.open();
registrationsUnwindForUnsupportedOperationExceptions(nonMultiplexedDeviceClient);
nonMultiplexedDeviceClient.closeNow();
}
Aggregations