use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class SendMessagesTests method tokenExpiredAfterOpenButBeforeSendHttp.
@Test
@ContinuousIntegrationTest
public void tokenExpiredAfterOpenButBeforeSendHttp() throws Exception {
final long SECONDS_FOR_SAS_TOKEN_TO_LIVE = 3;
final long MILLISECONDS_TO_WAIT_FOR_TOKEN_TO_EXPIRE = 5000;
if (testInstance.protocol != HTTPS || testInstance.authenticationType != SAS || testInstance.useHttpProxy) {
// as long as token did not expire before open. X509 doesn't apply either
return;
}
this.testInstance.setup();
String soonToBeExpiredSASToken = generateSasTokenForIotDevice(hostName, testInstance.identity.getDeviceId(), ((TestDeviceIdentity) testInstance.identity).getDevice().getPrimaryKey(), SECONDS_FOR_SAS_TOKEN_TO_LIVE);
DeviceClient client = new DeviceClient(soonToBeExpiredSASToken, testInstance.protocol);
client.open();
// Force the SAS token to expire before sending messages
Thread.sleep(MILLISECONDS_TO_WAIT_FOR_TOKEN_TO_EXPIRE);
IotHubServicesCommon.sendMessagesExpectingSASTokenExpiration(client, testInstance.protocol.toString(), 1, RETRY_MILLISECONDS, SEND_TIMEOUT_MILLISECONDS, testInstance.authenticationType);
client.closeNow();
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class ProvisioningCommon method assertProvisionedDeviceWorks.
private void assertProvisionedDeviceWorks(String iothubUri, String deviceId) throws IOException, URISyntaxException {
for (IotHubClientProtocol iotHubClientProtocol : IotHubClientProtocol.values()) {
if (iotHubClientProtocol == IotHubClientProtocol.MQTT_WS || iotHubClientProtocol == IotHubClientProtocol.AMQPS_WS) {
// MQTT_WS/AMQP_WS does not support X509 because of a bug on service
continue;
}
DeviceClient deviceClient = DeviceClient.createFromSecurityProvider(iothubUri, deviceId, testInstance.securityProvider, iotHubClientProtocol);
deviceClient.closeNow();
}
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientTests method getMultiplexedDigitalTwinsRegisteredBeforeOpen.
// Open a multiplexed connection with two devices, each with a different model Id. Verify that their reported twin has
// the expected model Ids.
@Test
@StandardTierHubOnlyTest
public void getMultiplexedDigitalTwinsRegisteredBeforeOpen() throws IotHubException, IOException, URISyntaxException, MultiplexingClientException, InterruptedException {
if (protocol == MQTT || protocol == MQTT_WS) {
// multiplexing isn't supported over MQTT, so it can't be tested
return;
}
MultiplexingClient multiplexingClient = new MultiplexingClient(deviceClient.getConfig().getIotHubHostname(), protocol);
List<DeviceClient> multiplexedDeviceClients = new ArrayList<>();
multiplexedDeviceClients.add(createDeviceClient(protocol, E2ETestConstants.THERMOSTAT_MODEL_ID));
multiplexedDeviceClients.add(createDeviceClient(protocol, E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID));
// register the devices before the multiplexing client has been opened
multiplexingClient.registerDeviceClients(multiplexedDeviceClients);
multiplexingClient.open();
// act
String thermostatDeviceId = multiplexedDeviceClients.get(0).getConfig().getDeviceId();
BasicDigitalTwin thermostatResponse = digitalTwinClient.getDigitalTwin(thermostatDeviceId, BasicDigitalTwin.class);
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> thermostatResponseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(thermostatDeviceId, BasicDigitalTwin.class);
String temperatureControllerDeviceId = multiplexedDeviceClients.get(1).getConfig().getDeviceId();
BasicDigitalTwin temperatureControllerResponse = digitalTwinClient.getDigitalTwin(temperatureControllerDeviceId, BasicDigitalTwin.class);
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> temperatureControllerResponseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(temperatureControllerDeviceId, BasicDigitalTwin.class);
// assert
assertEquals(thermostatResponse.getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
assertEquals(thermostatResponseWithHeaders.body().getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
assertEquals(temperatureControllerResponse.getMetadata().getModelId(), E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID);
assertEquals(temperatureControllerResponseWithHeaders.body().getMetadata().getModelId(), E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID);
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientTests method getMultiplexedDigitalTwinsRegisteredAfterOpen.
// Open a multiplexed connection with no devices, then register two devices, each with a different model Id.
// Verify that their reported twin has the expected model Ids.
@Test
@StandardTierHubOnlyTest
public void getMultiplexedDigitalTwinsRegisteredAfterOpen() throws IotHubException, IOException, URISyntaxException, MultiplexingClientException, InterruptedException {
if (protocol == MQTT || protocol == MQTT_WS) {
// multiplexing isn't supported over MQTT, so it can't be tested
return;
}
MultiplexingClient multiplexingClient = new MultiplexingClient(deviceClient.getConfig().getIotHubHostname(), protocol);
List<DeviceClient> multiplexedDeviceClients = new ArrayList<>();
multiplexedDeviceClients.add(createDeviceClient(protocol, E2ETestConstants.THERMOSTAT_MODEL_ID));
multiplexedDeviceClients.add(createDeviceClient(protocol, E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID));
multiplexingClient.open();
// register the devices after the multiplexing client has been opened
multiplexingClient.registerDeviceClients(multiplexedDeviceClients);
// act
String thermostatDeviceId = multiplexedDeviceClients.get(0).getConfig().getDeviceId();
BasicDigitalTwin thermostatResponse = digitalTwinClient.getDigitalTwin(thermostatDeviceId, BasicDigitalTwin.class);
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> thermostatResponseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(thermostatDeviceId, BasicDigitalTwin.class);
String temperatureControllerDeviceId = multiplexedDeviceClients.get(1).getConfig().getDeviceId();
BasicDigitalTwin temperatureControllerResponse = digitalTwinClient.getDigitalTwin(temperatureControllerDeviceId, BasicDigitalTwin.class);
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> temperatureControllerResponseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(temperatureControllerDeviceId, BasicDigitalTwin.class);
// assert
assertEquals(thermostatResponse.getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
assertEquals(thermostatResponseWithHeaders.body().getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
assertEquals(temperatureControllerResponse.getMetadata().getModelId(), E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID);
assertEquals(temperatureControllerResponseWithHeaders.body().getMetadata().getModelId(), E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID);
}
use of com.microsoft.azure.sdk.iot.device.DeviceClient in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientTests method createDeviceClient.
private DeviceClient createDeviceClient(IotHubClientProtocol protocol, String modelId) throws IOException, IotHubException, URISyntaxException {
ClientOptions options = new ClientOptions();
options.setModelId(modelId);
this.deviceId = DEVICE_ID_PREFIX.concat(UUID.randomUUID().toString());
Device device = Device.createDevice(deviceId, AuthenticationType.SAS);
Device registeredDevice = registryManager.addDevice(device);
String deviceConnectionString = registryManager.getDeviceConnectionString(registeredDevice);
return new DeviceClient(deviceConnectionString, protocol, options);
}
Aggregations