Search in sources :

Example 31 with Device

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

the class ExportImportTests method createListofDevices.

private static List<ExportImportDevice> createListofDevices() {
    // Creating the list of devices to be created, then deleted
    Integer numberOfDevices = 10;
    List<ExportImportDevice> devicesForImport = new ArrayList<>(numberOfDevices);
    for (int i = 0; i < numberOfDevices; i++) {
        String deviceId = "java-bulk-test-" + UUID.randomUUID().toString();
        Device device = Device.createFromId(deviceId, null, null);
        AuthenticationMechanism authentication = new AuthenticationMechanism(device.getSymmetricKey());
        ExportImportDevice deviceToAdd = new ExportImportDevice();
        deviceToAdd.setId(deviceId);
        deviceToAdd.setAuthentication(authentication);
        deviceToAdd.setStatus(DeviceStatus.Enabled);
        TwinCollection tags = new TwinCollection();
        tags.putFinal("test01", "firstvalue");
        deviceToAdd.setTags(tags);
        devicesForImport.add(deviceToAdd);
    }
    return devicesForImport;
}
Also used : TwinCollection(com.microsoft.azure.sdk.iot.deps.twin.TwinCollection) Device(com.microsoft.azure.sdk.iot.service.Device) ExportImportDevice(com.microsoft.azure.sdk.iot.service.ExportImportDevice) AuthenticationMechanism(com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism) ExportImportDevice(com.microsoft.azure.sdk.iot.service.ExportImportDevice) ArrayList(java.util.ArrayList)

Example 32 with Device

use of com.microsoft.azure.sdk.iot.service.Device 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;
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) ArrayList(java.util.ArrayList) SymmetricKey(com.microsoft.azure.sdk.iot.service.auth.SymmetricKey)

Example 33 with Device

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

the class Tools method getX509TestModule.

private static TestModuleIdentity getX509TestModule(String iotHubConnectionString, IotHubClientProtocol protocol, boolean needCleanTwin) throws URISyntaxException, IOException, IotHubException, InterruptedException, ModuleClientException, GeneralSecurityException {
    // remove an identity from the queue.
    synchronized (testX509ModuleQueueLock) {
        TestModuleIdentity testModuleIdentity;
        if (!needCleanTwin && testX509ModuleWithTwinQueue.size() > 0) {
            log.debug("Acquiring test module from testX509ModuleWithTwinQueue");
            testModuleIdentity = testX509ModuleWithTwinQueue.remove();
        } else {
            if (testX509ModuleQueue.size() < 1) {
                // No cached modules to return, so create a new set of modules to cache, and return one of the newly created modules
                log.debug("Proactively adding another {} modules to the SAS test module queue", PROACTIVE_TEST_DEVICE_REGISRATION_COUNT);
                List<Device> devices = new ArrayList<>();
                List<Module> modulesToAdd = new ArrayList<>();
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    TestDeviceIdentity testDeviceIdentity = getTestDevice(iotHubConnectionString, protocol, AuthenticationType.SELF_SIGNED, needCleanTwin);
                    devices.add(testDeviceIdentity.device);
                    Module module = Module.createModule(testDeviceIdentity.device.getDeviceId(), "test-module-" + UUID.randomUUID(), AuthenticationType.SELF_SIGNED);
                    String x509Thumbprint = IntegrationTest.x509CertificateGenerator.getX509Thumbprint();
                    module.setThumbprintFinal(x509Thumbprint, x509Thumbprint);
                    modulesToAdd.add(module);
                }
                addModules(modulesToAdd, iotHubConnectionString);
                for (int i = 0; i < PROACTIVE_TEST_DEVICE_REGISRATION_COUNT; i++) {
                    testX509ModuleQueue.add(new TestModuleIdentity(null, devices.get(i), modulesToAdd.get(i)));
                }
            }
            log.debug("Acquiring test module from testX509ModuleQueue");
            testModuleIdentity = testX509ModuleQueue.remove();
        }
        SSLContext sslContext = SSLContextBuilder.buildSSLContext(IntegrationTest.x509CertificateGenerator.getPublicCertificate(), IntegrationTest.x509CertificateGenerator.getPrivateKey());
        ModuleClient moduleClient = new ModuleClient(DeviceConnectionString.get(iotHubConnectionString, testModuleIdentity.device, testModuleIdentity.module), protocol, sslContext);
        testModuleIdentity.setModuleClient(moduleClient);
        return testModuleIdentity;
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) ArrayList(java.util.ArrayList) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) SSLContext(javax.net.ssl.SSLContext) Module(com.microsoft.azure.sdk.iot.service.Module) ModuleClient(com.microsoft.azure.sdk.iot.device.ModuleClient)

Example 34 with Device

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

the class DeviceTest method createFromId_good_case.

// Tests_SRS_SERVICE_SDK_JAVA_DEVICE_12_003: [The constructor shall create a new instance of Device using the given deviceId and return with it]
@Test
public void createFromId_good_case() throws NoSuchAlgorithmException {
    // Arrange
    String deviceId = "xxx-device";
    new Expectations() {

        {
            Deencapsulation.newInstance(Device.class, deviceId, DeviceStatus.class, SymmetricKey.class);
        }
    };
    // Act
    Device device = Device.createFromId(deviceId, null, null);
    // Assert
    assertNotEquals(device, null);
}
Also used : NonStrictExpectations(mockit.NonStrictExpectations) Expectations(mockit.Expectations) Device(com.microsoft.azure.sdk.iot.service.Device) Test(org.junit.Test)

Example 35 with Device

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

the class DeviceTest method constructor_initialize_properties.

// Tests_SRS_SERVICE_SDK_JAVA_DEVICE_12_006: [The constructor shall initialize all properties to default value]
@Test
public void constructor_initialize_properties() throws NoSuchAlgorithmException {
    // Arrange
    String utcTimeDefault = "0001-01-01T00:00:00";
    String deviceId = "xxx-device";
    // Act
    Device device = Deencapsulation.newInstance(Device.class, deviceId, DeviceStatus.class, SymmetricKey.class);
    // Assert
    assertNotEquals(null, device);
    assertNotEquals(device.getSymmetricKey(), null);
    assertEquals("xxx-device", device.getDeviceId());
    assertEquals("", device.getGenerationId());
    assertEquals("", device.geteTag());
    assertEquals(DeviceStatus.Enabled, device.getStatus());
    assertEquals("", device.getStatusReason());
    assertEquals(utcTimeDefault, device.getStatusUpdatedTime());
    assertEquals(DeviceConnectionState.Disconnected, device.getConnectionState());
    assertEquals(utcTimeDefault, device.getStatusUpdatedTime());
    assertEquals(utcTimeDefault, device.getConnectionStateUpdatedTime());
    assertEquals(utcTimeDefault, device.getLastActivityTime());
    assertEquals(0, device.getCloudToDeviceMessageCount());
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) 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