use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class MultiplexingClientTests method registrationsUnwindForClientOfDifferentHostName.
@Test
public void registrationsUnwindForClientOfDifferentHostName() throws Exception {
Device nonMultiplexedDevice = Tools.getTestDevice(iotHubConnectionString, testInstance.protocol, AuthenticationType.SAS, false).getDevice();
String deviceConnectionString = registryManager.getDeviceConnectionString(nonMultiplexedDevice);
// intentionally change the hostname of the device to simulate registering a device with a different hostname
// to a multiplexing client. It shouldn't matter that the hostname itself isn't tied to an actual IoT Hub since
// no network requests should be made before this hostname validation.
String actualHostName = IotHubConnectionString.createConnectionString(iotHubConnectionString).getHostName();
deviceConnectionString = deviceConnectionString.replace(actualHostName, "some-fake-host-name.azure-devices.net");
DeviceClient deviceClientWithDifferentHostName = new DeviceClient(deviceConnectionString, testInstance.protocol);
registrationsUnwindForUnsupportedOperationExceptions(deviceClientWithDifferentHostName);
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class MultiplexingClientTests method registrationsUnwindForDifferentProtocolClient.
@Test
public void registrationsUnwindForDifferentProtocolClient() throws Exception {
// Protocol for the new client is AMQPS if the test parameters are for AMQPS_WS, and vice versa. MultiplexingClient
// should throw an exception since this new client's protocol doesn't match, even though both AMQPS and AMQPS_WS are valid
// protocols
IotHubClientProtocol protocol = testInstance.protocol == IotHubClientProtocol.AMQPS ? IotHubClientProtocol.AMQPS_WS : IotHubClientProtocol.AMQPS;
Device newDevice = Tools.getTestDevice(iotHubConnectionString, protocol, AuthenticationType.SAS, false).getDevice();
String deviceConnectionString = registryManager.getDeviceConnectionString(newDevice);
DeviceClient differentProtocolDeviceClient = new DeviceClient(deviceConnectionString, protocol);
registrationsUnwindForUnsupportedOperationExceptions(differentProtocolDeviceClient);
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class MultiplexingClientTests method registerDevicesWithIncorrectCredentialsBeforeOpenThrowsOnOpen.
@ContinuousIntegrationTest
@Test
public void registerDevicesWithIncorrectCredentialsBeforeOpenThrowsOnOpen() throws Exception {
testInstance.setup(DEVICE_MULTIPLEX_COUNT);
for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(i));
}
List<DeviceClient> clientsWithIncorrectCredentials = new ArrayList<>();
for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
// shift the keys for each device so that device n uses key for device n + 1 (and final device uses key for device 0)
String incorrectConnectionString;
if (i == DEVICE_MULTIPLEX_COUNT - 1) {
incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(0));
incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(0).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
} else {
incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(i + 1));
incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(i + 1).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
}
DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
clientsWithIncorrectCredentials.add(clientWithIncorrectCredentials);
}
testInstance.multiplexingClient.registerDeviceClients(clientsWithIncorrectCredentials);
boolean expectedExceptionThrown = false;
try {
testInstance.multiplexingClient.open();
} catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
assertEquals(DEVICE_MULTIPLEX_COUNT, registrationExceptions.size());
for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
String deviceId = testInstance.deviceIdentityArray.get(i).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 Tools method getSasTestDevice.
private static TestDeviceIdentity getSasTestDevice(String iotHubConnectionString, IotHubClientProtocol protocol, boolean needCleanTwin) throws URISyntaxException, IOException, IotHubException, GeneralSecurityException {
// and the subsequent callers just used one of those devices
synchronized (testSasDeviceQueueLock) {
TestDeviceIdentity testDeviceIdentity;
if (!needCleanTwin && testSasDeviceWithTwinQueue.size() > 0) {
log.debug("Acquiring test device from testSasDeviceWithTwinQueue");
testDeviceIdentity = testSasDeviceWithTwinQueue.remove();
} else {
if (testSasDeviceQueue.size() < 1) {
// No cached devices to return, so create a new set of devices to cache, and return one of the newly created devices
log.debug("Proactively adding another {} devices to the SAS test device queue", PROACTIVE_TEST_DEVICE_REGISRATION_COUNT);
List<Device> devicesToAdd = new ArrayList<>();
for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
Device deviceToAdd = Device.createDevice("test-device-" + UUID.randomUUID().toString(), AuthenticationType.SAS);
deviceToAdd.setSymmetricKey(new SymmetricKey());
devicesToAdd.add(deviceToAdd);
}
addDevices(devicesToAdd, iotHubConnectionString);
for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
testSasDeviceQueue.add(new TestDeviceIdentity(null, devicesToAdd.get(i)));
}
}
log.debug("Acquiring test device from testSasDeviceQueue");
testDeviceIdentity = testSasDeviceQueue.remove();
}
testDeviceIdentity.setDeviceClient(new DeviceClient(getRegistyManager(iotHubConnectionString).getDeviceConnectionString(testDeviceIdentity.getDevice()), protocol));
return testDeviceIdentity;
}
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class SendMessagesIT method SendMessagesOverMqtt.
@Test
public void SendMessagesOverMqtt() throws URISyntaxException, IOException {
String messageString = "Java client e2e test message over Mqtt protocol";
Message msg = new Message(messageString);
DeviceClient client = new DeviceClient(DeviceConnectionString.get(iotHubConnectionString, deviceMqtt), IotHubClientProtocol.MQTT);
client.open();
for (int i = 0; i < NUM_MESSAGES_PER_CONNECTION; ++i) {
try {
Success messageSent = new Success();
EventCallback callback = new EventCallback();
client.sendEventAsync(msg, callback, messageSent);
Integer waitDuration = 0;
while (!messageSent.getResult()) {
Thread.sleep(RETRY_MILLISECONDS);
if ((waitDuration += RETRY_MILLISECONDS) > SEND_TIMEOUT_MILLISECONDS) {
break;
}
}
if (!messageSent.getResult()) {
Assert.fail("Sending message over MQTT protocol failed");
}
} catch (Exception e) {
Assert.fail("Sending message over MQTT protocol failed");
}
}
client.closeNow();
}
Aggregations