Search in sources :

Example 1 with BlobInputStream

use of com.azure.storage.blob.specialized.BlobInputStream 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)

Aggregations

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 ExportImportDevice (com.microsoft.azure.sdk.iot.service.ExportImportDevice)1 JobProperties (com.microsoft.azure.sdk.iot.service.JobProperties)1 IotHubTooManyDevicesException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubTooManyDevicesException)1 ArrayList (java.util.ArrayList)1 Scanner (java.util.Scanner)1