Search in sources :

Example 6 with ExportImportDevice

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

the class ExportImportDeviceTest method toParserIllegalStateThrownWhenUsingSelfSignedAuthenticationWithoutPrimaryThumbprintSaved.

// Tests_SRS_SERVICE_SDK_JAVA_IMPORT_EXPORT_DEVICE_34_061: [If this device uses self signed authentication, but does not have a primary and secondary thumbprint saved, an IllegalStateException shall be thrown.]
@Test(expected = IllegalStateException.class)
public void toParserIllegalStateThrownWhenUsingSelfSignedAuthenticationWithoutPrimaryThumbprintSaved() {
    // arrange
    ExportImportDevice device = new ExportImportDevice();
    device.setId("someDevice");
    AuthenticationMechanism authentication = new AuthenticationMechanism(AuthenticationType.SELF_SIGNED);
    X509Thumbprint thumbprint = Deencapsulation.newInstance(X509Thumbprint.class);
    Deencapsulation.setField(thumbprint, "primaryThumbprint", null);
    Deencapsulation.setField(authentication, "thumbprint", thumbprint);
    device.setAuthentication(authentication);
    // act
    reflectivelyInvokeToExportImportDeviceParser(device);
}
Also used : AuthenticationMechanism(com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism) X509Thumbprint(com.microsoft.azure.sdk.iot.service.auth.X509Thumbprint) ExportImportDevice(com.microsoft.azure.sdk.iot.service.ExportImportDevice) Test(org.junit.Test)

Example 7 with ExportImportDevice

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

the class ExportImportTests method runExportJob.

private static List<ExportImportDevice> runExportJob(Optional<StorageAuthenticationType> storageAuthenticationType) throws Exception {
    Boolean excludeKeys = false;
    String containerSasUri = getContainerSasUri(exportContainer);
    boolean exportJobScheduled = false;
    JobProperties exportJob = null;
    while (!exportJobScheduled) {
        try {
            if (storageAuthenticationType.isPresent()) {
                JobProperties exportJobProperties = JobProperties.createForExportJob(containerSasUri, excludeKeys, storageAuthenticationType.get());
                exportJob = registryManager.exportDevices(exportJobProperties);
            } else {
                exportJob = registryManager.exportDevices(containerSasUri, excludeKeys);
            }
            exportJobScheduled = true;
        } catch (IotHubTooManyDevicesException e) {
            // test is being throttled, wait a while and try again
            Thread.sleep(10 * 1000);
        }
    }
    JobProperties.JobStatus jobStatus;
    long startTime = System.currentTimeMillis();
    while (true) {
        exportJob = registryManager.getJob(exportJob.getJobId());
        jobStatus = exportJob.getStatus();
        if (jobStatus == JobProperties.JobStatus.COMPLETED || jobStatus == JobProperties.JobStatus.FAILED) {
            break;
        }
        if (System.currentTimeMillis() - startTime > EXPORT_JOB_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for the export job to complete");
        }
        Thread.sleep(100);
    }
    String exportedDevicesJson = "";
    for (BlobItem blobItem : exportContainer.listBlobs()) {
        BlobInputStream stream = exportContainer.getBlobClient(blobItem.getName()).openInputStream();
        try (Scanner scanner = new Scanner(stream, StandardCharsets.UTF_8.name())) {
            exportedDevicesJson = scanner.next();
        }
    }
    List<ExportImportDevice> result = new ArrayList<>();
    Scanner scanner = new Scanner(exportedDevicesJson);
    while (scanner.hasNextLine()) {
        String exportImportDeviceJson = scanner.nextLine();
        ExportImportDeviceParser parser = new ExportImportDeviceParser(exportImportDeviceJson);
        ExportImportDevice device = Deencapsulation.newInstance(ExportImportDevice.class, new Class[] { ExportImportDeviceParser.class }, parser);
        device.setImportMode(ImportMode.CreateOrUpdate);
        result.add(device);
    }
    scanner.close();
    if (jobStatus != JobProperties.JobStatus.COMPLETED) {
        Assert.fail("The export job was not completed successfully");
    }
    return result;
}
Also used : Scanner(java.util.Scanner) JobProperties(com.microsoft.azure.sdk.iot.service.JobProperties) ExportImportDevice(com.microsoft.azure.sdk.iot.service.ExportImportDevice) ArrayList(java.util.ArrayList) BlobInputStream(com.azure.storage.blob.specialized.BlobInputStream) IotHubTooManyDevicesException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubTooManyDevicesException) BlobItem(com.azure.storage.blob.models.BlobItem) ExportImportDeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser)

Example 8 with ExportImportDevice

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

the class ExportImportTests method tearDown.

@AfterClass
public static void tearDown() throws Exception {
    // Deleting all devices created as a part of the bulk import-export test
    List<ExportImportDevice> exportedDevices = runExportJob(Optional.empty());
    List<ExportImportDevice> devicesToBeDeleted = new ArrayList<>();
    for (ExportImportDevice device : exportedDevices) {
        if (device.getId().startsWith("java-bulk-test-")) {
            devicesToBeDeleted.add(device);
        }
    }
    runImportJob(devicesToBeDeleted, ImportMode.Delete, Optional.empty());
    // Cleaning up the containers
    importContainer.delete();
    exportContainer.delete();
    if (registryManager != null) {
        registryManager.close();
        registryManager = null;
    }
}
Also used : ExportImportDevice(com.microsoft.azure.sdk.iot.service.ExportImportDevice) ArrayList(java.util.ArrayList) AfterClass(org.junit.AfterClass)

Example 9 with ExportImportDevice

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

the class ExportImportTests method export_import_e2e.

@Test(timeout = IMPORT_EXPORT_TEST_TIMEOUT_MILLISECONDS)
@ContinuousIntegrationTest
@Ignore
public void export_import_e2e() throws Exception {
    List<ExportImportDevice> devicesForImport = createListofDevices();
    // importing devices - create mode
    runImportJob(devicesForImport, ImportMode.CreateOrUpdate, Optional.empty());
    List<ExportImportDevice> exportedDevices = runExportJob(Optional.empty());
    for (ExportImportDevice importedDevice : devicesForImport) {
        if (!exportedDevices.contains(importedDevice)) {
            Assert.fail("Exported devices list does not contain device with id: " + importedDevice.getId());
        }
    }
    // importing devices - delete mode
    runImportJob(devicesForImport, ImportMode.Delete, Optional.empty());
    exportedDevices = runExportJob(Optional.empty());
    for (ExportImportDevice importedDevice : devicesForImport) {
        if (exportedDevices.contains(importedDevice)) {
            Assert.fail("Device with id: " + importedDevice.getId() + " was not deleted by the import job");
        }
    }
}
Also used : ExportImportDevice(com.microsoft.azure.sdk.iot.service.ExportImportDevice) Ignore(org.junit.Ignore) 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) Test(org.junit.Test) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)

Example 10 with ExportImportDevice

use of com.microsoft.azure.sdk.iot.service.ExportImportDevice 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)

Aggregations

ExportImportDevice (com.microsoft.azure.sdk.iot.service.ExportImportDevice)18 Test (org.junit.Test)13 AuthenticationMechanism (com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism)8 ArrayList (java.util.ArrayList)3 BlobItem (com.azure.storage.blob.models.BlobItem)1 BlobInputStream (com.azure.storage.blob.specialized.BlobInputStream)1 ExportImportDeviceParser (com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser)1 TwinCollection (com.microsoft.azure.sdk.iot.deps.twin.TwinCollection)1 Device (com.microsoft.azure.sdk.iot.service.Device)1 DeviceStatus (com.microsoft.azure.sdk.iot.service.DeviceStatus)1 ImportMode (com.microsoft.azure.sdk.iot.service.ImportMode)1 JobProperties (com.microsoft.azure.sdk.iot.service.JobProperties)1 SymmetricKey (com.microsoft.azure.sdk.iot.service.auth.SymmetricKey)1 X509Thumbprint (com.microsoft.azure.sdk.iot.service.auth.X509Thumbprint)1 IotHubTooManyDevicesException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubTooManyDevicesException)1 Scanner (java.util.Scanner)1 AfterClass (org.junit.AfterClass)1 Ignore (org.junit.Ignore)1 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)1 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)1