Search in sources :

Example 21 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class RegistryManagerTests method apply_configuration_e2e.

@Test
@StandardTierHubOnlyTest
public void apply_configuration_e2e() throws Exception {
    // Arrange
    RegistryManagerTestInstance testInstance = new RegistryManagerTestInstance();
    Device deviceSetup = Device.createFromId(testInstance.deviceId, DeviceStatus.Enabled, null);
    Tools.addDeviceWithRetry(testInstance.registryManager, deviceSetup);
    final HashMap<String, Object> testDeviceContent = new HashMap<String, Object>() {

        {
            put("properties.desired.chiller-water", new HashMap<String, Object>() {

                {
                    put("temperature", 66);
                    put("pressure", 28);
                }
            });
        }
    };
    ConfigurationContent content = new ConfigurationContent();
    content.setDeviceContent(testDeviceContent);
    boolean expectedExceptionThrown = false;
    // Act
    try {
        testInstance.registryManager.applyConfigurationContentOnDevice(testInstance.deviceId, content);
    } catch (IotHubBadFormatException e) {
        expectedExceptionThrown = true;
    }
    assertTrue("Bad format exception wasn't thrown but was expected", expectedExceptionThrown);
}
Also used : IotHubBadFormatException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException) HashMap(java.util.HashMap) Device(com.microsoft.azure.sdk.iot.service.Device) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) ConfigurationContent(com.microsoft.azure.sdk.iot.service.ConfigurationContent) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) 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 22 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class RegistryManagerTests method deviceLifecycleWithProxy.

@Test
public void deviceLifecycleWithProxy() throws Exception {
    Proxy testProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
    ProxyOptions proxyOptions = new ProxyOptions(testProxy);
    RegistryManagerOptions registryManagerOptions = RegistryManagerOptions.builder().proxyOptions(proxyOptions).build();
    RegistryManagerTestInstance testInstance = new RegistryManagerTestInstance(registryManagerOptions);
    // -Create-//
    Device deviceAdded = Device.createFromId(testInstance.deviceId, DeviceStatus.Enabled, null);
    Tools.addDeviceWithRetry(testInstance.registryManager, deviceAdded);
    // -Read-//
    Device deviceRetrieved = testInstance.registryManager.getDevice(testInstance.deviceId);
    // -Update-//
    Device deviceUpdated = testInstance.registryManager.getDevice(testInstance.deviceId);
    deviceUpdated.setStatus(DeviceStatus.Disabled);
    deviceUpdated = testInstance.registryManager.updateDevice(deviceUpdated);
    // -Delete-//
    testInstance.registryManager.removeDevice(testInstance.deviceId);
    // Assert
    assertEquals(buildExceptionMessage("", hostName), testInstance.deviceId, deviceAdded.getDeviceId());
    assertEquals(buildExceptionMessage("", hostName), testInstance.deviceId, deviceRetrieved.getDeviceId());
    assertEquals(buildExceptionMessage("", hostName), DeviceStatus.Disabled, deviceUpdated.getStatus());
    assertTrue(buildExceptionMessage("", hostName), deviceWasDeletedSuccessfully(testInstance.registryManager, testInstance.deviceId));
}
Also used : Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) InetSocketAddress(java.net.InetSocketAddress) Device(com.microsoft.azure.sdk.iot.service.Device) RegistryManagerOptions(com.microsoft.azure.sdk.iot.service.RegistryManagerOptions) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) 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 23 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class RegistryManagerTests method serviceValidatesSymmetricKey.

@Test
public void serviceValidatesSymmetricKey() throws IOException, IotHubException {
    RegistryManagerTestInstance testInstance = new RegistryManagerTestInstance();
    Device device = Device.createDevice(testInstance.deviceId, AuthenticationType.SAS);
    SymmetricKey symmetricKey = new SymmetricKey();
    symmetricKey.setPrimaryKey("1");
    symmetricKey.setSecondaryKey("2");
    device.setSymmetricKey(symmetricKey);
    try {
        testInstance.registryManager.addDevice(device);
        fail("Adding the device should have failed since an invalid symmetric key was provided");
    } catch (IotHubBadFormatException ex) {
    // expected throw
    }
}
Also used : IotHubBadFormatException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException) Device(com.microsoft.azure.sdk.iot.service.Device) SymmetricKey(com.microsoft.azure.sdk.iot.service.auth.SymmetricKey) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) 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 24 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class ServiceClientTests method cloudToDeviceTelemetry.

public void cloudToDeviceTelemetry(boolean withProxy, boolean withPayload, boolean withLargestPayload, boolean withCustomSSLContext, boolean withAzureSasCredential) throws Exception {
    // We remove and recreate the device for a clean start
    RegistryManager registryManager = RegistryManager.createFromConnectionString(iotHubConnectionString, RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    TestDeviceIdentity testDeviceIdentity = Tools.getTestDevice(iotHubConnectionString, IotHubClientProtocol.AMQPS, AuthenticationType.SAS, false);
    Device device = testDeviceIdentity.getDevice();
    Device deviceGetBefore = registryManager.getDevice(device.getDeviceId());
    // Create service client
    ProxyOptions proxyOptions = null;
    if (withProxy) {
        Proxy testProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
        proxyOptions = new ProxyOptions(testProxy);
    }
    SSLContext sslContext = null;
    if (withCustomSSLContext) {
        sslContext = new IotHubSSLContext().getSSLContext();
    }
    ServiceClientOptions serviceClientOptions = ServiceClientOptions.builder().proxyOptions(proxyOptions).sslContext(sslContext).build();
    ServiceClient serviceClient;
    if (withAzureSasCredential) {
        serviceClient = buildServiceClientWithAzureSasCredential(testInstance.protocol, serviceClientOptions);
    } else {
        serviceClient = new ServiceClient(iotHubConnectionString, testInstance.protocol, serviceClientOptions);
    }
    serviceClient.open();
    Message message;
    if (withPayload) {
        if (withLargestPayload) {
            message = new Message(LARGEST_PAYLOAD);
        } else {
            message = new Message(SMALL_PAYLOAD);
        }
    } else {
        message = new Message();
    }
    serviceClient.send(device.getDeviceId(), message);
    Device deviceGetAfter = registryManager.getDevice(device.getDeviceId());
    serviceClient.close();
    Tools.disposeTestIdentity(testDeviceIdentity, iotHubConnectionString);
    // Assert
    assertEquals(buildExceptionMessage("", hostName), deviceGetBefore.getDeviceId(), deviceGetAfter.getDeviceId());
    assertEquals(buildExceptionMessage("", hostName), 0, deviceGetBefore.getCloudToDeviceMessageCount());
    assertEquals(buildExceptionMessage("", hostName), 1, deviceGetAfter.getCloudToDeviceMessageCount());
    registryManager.close();
}
Also used : IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) Proxy(java.net.Proxy) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) Message(com.microsoft.azure.sdk.iot.service.Message) CorrelationDetailsLoggingAssert.buildExceptionMessage(tests.integration.com.microsoft.azure.sdk.iot.helpers.CorrelationDetailsLoggingAssert.buildExceptionMessage) Device(com.microsoft.azure.sdk.iot.service.Device) InetSocketAddress(java.net.InetSocketAddress) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) SSLContext(javax.net.ssl.SSLContext) IotHubSSLContext(com.microsoft.azure.sdk.iot.deps.auth.IotHubSSLContext) ServiceClientOptions(com.microsoft.azure.sdk.iot.service.ServiceClientOptions) TestDeviceIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity)

Example 25 with Device

use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.

the class DigitalTwinClientTests method updateDigitalTwin.

@Test
@StandardTierHubOnlyTest
public void updateDigitalTwin() throws IOException {
    // arrange
    String newProperty = "currentTemperature";
    String newPropertyPath = "/currentTemperature";
    Integer newPropertyValue = 35;
    // Property update callback
    TwinPropertyCallBack twinPropertyCallBack = (property, context) -> {
        Set<Property> properties = new HashSet<>();
        properties.add(property);
        try {
            deviceClient.sendReportedProperties(properties);
        } catch (IOException e) {
        }
    };
    // IotHub event callback
    IotHubEventCallback iotHubEventCallback = (responseStatus, callbackContext) -> {
    };
    // start device twin and setup handler for property updates in device
    deviceClient.startDeviceTwin(iotHubEventCallback, null, twinPropertyCallBack, null);
    Map<Property, Pair<TwinPropertyCallBack, Object>> desiredPropertyUpdateCallback = Collections.singletonMap(new Property(newProperty, null), new Pair<>(twinPropertyCallBack, null));
    deviceClient.subscribeToTwinDesiredProperties(desiredPropertyUpdateCallback);
    DigitalTwinUpdateRequestOptions optionsWithoutEtag = new DigitalTwinUpdateRequestOptions();
    optionsWithoutEtag.setIfMatch("*");
    // get digital twin and Etag before update
    ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class);
    DigitalTwinUpdateRequestOptions optionsWithEtag = new DigitalTwinUpdateRequestOptions();
    optionsWithEtag.setIfMatch(responseWithHeaders.headers().eTag());
    // act
    // Add properties at root level - conditional update with max overload
    UpdateOperationUtility updateOperationUtility = new UpdateOperationUtility().appendAddPropertyOperation(newPropertyPath, newPropertyValue);
    digitalTwinClient.updateDigitalTwinWithResponse(deviceId, updateOperationUtility.getUpdateOperations(), optionsWithEtag);
    BasicDigitalTwin digitalTwin = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class).body();
    // assert
    assertEquals(E2ETestConstants.THERMOSTAT_MODEL_ID, digitalTwin.getMetadata().getModelId());
    assertTrue(digitalTwin.getMetadata().getWriteableProperties().containsKey(newProperty));
    assertEquals(newPropertyValue, digitalTwin.getMetadata().getWriteableProperties().get(newProperty).getDesiredValue());
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) Device(com.microsoft.azure.sdk.iot.service.Device) SasTokenTools(tests.integration.com.microsoft.azure.sdk.iot.helpers.SasTokenTools) Arrays(java.util.Arrays) MultiplexingClient(com.microsoft.azure.sdk.iot.device.MultiplexingClient) ClientOptions(com.microsoft.azure.sdk.iot.device.ClientOptions) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient) IotHubClientProtocol(com.microsoft.azure.sdk.iot.device.IotHubClientProtocol) URISyntaxException(java.net.URISyntaxException) ZonedDateTime(java.time.ZonedDateTime) DigitalTwinClientOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions) MultiplexingClientException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientException) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DigitalTwinCommandResponse(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinCommandResponse) Proxy(java.net.Proxy) After(org.junit.After) Map(java.util.Map) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) ZoneOffset(java.time.ZoneOffset) DefaultHttpProxyServer(org.littleshoot.proxy.impl.DefaultHttpProxyServer) Parameterized(org.junit.runners.Parameterized) AfterClass(org.junit.AfterClass) Tools(tests.integration.com.microsoft.azure.sdk.iot.helpers.Tools) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest) AzureSasCredential(com.azure.core.credential.AzureSasCredential) DeviceMethodCallback(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback) TwinPropertyCallBack(com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertyCallBack) ServiceResponseWithHeaders(com.microsoft.rest.ServiceResponseWithHeaders) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) DigitalTwinInvokeCommandHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinInvokeCommandHeaders) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) UpdateOperationUtility(com.microsoft.azure.sdk.iot.service.digitaltwin.UpdateOperationUtility) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TokenCredential(com.azure.core.credential.TokenCredential) DigitalTwinInvokeCommandRequestOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinInvokeCommandRequestOptions) HttpProxyServer(org.littleshoot.proxy.HttpProxyServer) DeviceMethodData(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData) BeforeClass(org.junit.BeforeClass) RegistryManagerOptions(com.microsoft.azure.sdk.iot.service.RegistryManagerOptions) RunWith(org.junit.runner.RunWith) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) DigitalTwinUpdateRequestOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinUpdateRequestOptions) ArrayList(java.util.ArrayList) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) HashSet(java.util.HashSet) RestException(com.microsoft.rest.RestException) Timeout(org.junit.rules.Timeout) IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) Pair(com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair) BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) Before(org.junit.Before) Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) TestCase.fail(junit.framework.TestCase.fail) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) E2ETestConstants(tests.integration.com.microsoft.azure.sdk.iot.digitaltwin.helpers.E2ETestConstants) AuthenticationType(com.microsoft.azure.sdk.iot.service.auth.AuthenticationType) Rule(org.junit.Rule) Ignore(org.junit.Ignore) IotHubConnectionStringBuilder(com.microsoft.azure.sdk.iot.service.IotHubConnectionStringBuilder) DateTimeFormatter(java.time.format.DateTimeFormatter) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) UpdateOperationUtility(com.microsoft.azure.sdk.iot.service.digitaltwin.UpdateOperationUtility) Set(java.util.Set) HashSet(java.util.HashSet) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IOException(java.io.IOException) DigitalTwinUpdateRequestOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinUpdateRequestOptions) BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) TwinPropertyCallBack(com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertyCallBack) Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) Pair(com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair) 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)

Aggregations

Device (com.microsoft.azure.sdk.iot.service.Device)68 Test (org.junit.Test)46 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)21 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)19 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)17 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)15 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)13 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)12 IOException (java.io.IOException)11 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)11 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)11 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)10 ArrayList (java.util.ArrayList)10 SymmetricKey (com.microsoft.azure.sdk.iot.service.auth.SymmetricKey)7 RegistryManagerOptions (com.microsoft.azure.sdk.iot.service.RegistryManagerOptions)5 AzureSasCredential (com.azure.core.credential.AzureSasCredential)4 DeviceCapabilities (com.microsoft.azure.sdk.iot.deps.twin.DeviceCapabilities)4 ClientOptions (com.microsoft.azure.sdk.iot.device.ClientOptions)4 DeviceStatus (com.microsoft.azure.sdk.iot.service.DeviceStatus)4 Module (com.microsoft.azure.sdk.iot.service.Module)4