Search in sources :

Example 11 with DeviceClient

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();
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) TestDeviceIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) Test(org.junit.Test) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)

Example 12 with DeviceClient

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();
    }
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubClientProtocol(com.microsoft.azure.sdk.iot.device.IotHubClientProtocol)

Example 13 with DeviceClient

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);
}
Also used : BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) ArrayList(java.util.ArrayList) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) MultiplexingClient(com.microsoft.azure.sdk.iot.device.MultiplexingClient) 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 14 with DeviceClient

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);
}
Also used : BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) ArrayList(java.util.ArrayList) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) MultiplexingClient(com.microsoft.azure.sdk.iot.device.MultiplexingClient) 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 15 with DeviceClient

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);
}
Also used : ClientOptions(com.microsoft.azure.sdk.iot.device.ClientOptions) DigitalTwinClientOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions) Device(com.microsoft.azure.sdk.iot.service.Device) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString)

Aggregations

DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)38 Test (org.junit.Test)21 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)16 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)14 Device (com.microsoft.azure.sdk.iot.service.Device)10 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)10 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 IotHubClientProtocol (com.microsoft.azure.sdk.iot.device.IotHubClientProtocol)6 URISyntaxException (java.net.URISyntaxException)6 Message (com.microsoft.azure.sdk.iot.device.Message)5 MultiplexingClientDeviceRegistrationAuthenticationException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException)5 ModuleClient (com.microsoft.azure.sdk.iot.device.ModuleClient)4 UnauthorizedException (com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException)4 Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)3 MultiplexingClient (com.microsoft.azure.sdk.iot.device.MultiplexingClient)3 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)3 DigitalTwinGetHeaders (com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders)3 BasicDigitalTwin (com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin)3