Search in sources :

Example 1 with FileUploadSasUriRequest

use of com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest in project azure-iot-sdk-java by Azure.

the class FileUploadTask method run.

@Override
public void run() {
    Thread.currentThread().setName(THREAD_NAME);
    FileUploadSasUriResponse sasUriResponse;
    try {
        sasUriResponse = getFileUploadSasUri(new FileUploadSasUriRequest(this.blobName));
    } catch (IOException | IllegalArgumentException e) {
        log.error("File upload failed to get a SAS URI from Iot Hub", e);
        userCallback.execute(IotHubStatusCode.ERROR, userCallbackContext);
        return;
    }
    FileUploadCompletionNotification fileUploadCompletionNotification = new FileUploadCompletionNotification(sasUriResponse.getCorrelationId(), false, -1, "Failed to upload to storage.");
    try {
        BlobClient blobClient = new BlobClientBuilder().endpoint(sasUriResponse.getBlobUri().toString()).buildClient();
        blobClient.upload(inputStream, streamLength);
        fileUploadCompletionNotification = new FileUploadCompletionNotification(sasUriResponse.getCorrelationId(), true, 0, "Succeed to upload to storage.");
    } catch (Exception e) {
        log.error("File upload failed to upload the stream to the blob", e);
    } finally {
        try {
            sendNotification(fileUploadCompletionNotification);
        } catch (IOException e) {
            log.error("Failed to send file upload status", e);
        }
    }
    if (fileUploadCompletionNotification.getSuccess()) {
        userCallback.execute(IotHubStatusCode.OK, userCallbackContext);
    } else {
        userCallback.execute(IotHubStatusCode.ERROR, userCallbackContext);
    }
}
Also used : BlobClient(com.azure.storage.blob.BlobClient) FileUploadSasUriResponse(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse) IOException(java.io.IOException) FileUploadCompletionNotification(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification) BlobClientBuilder(com.azure.storage.blob.BlobClientBuilder) IotHubServiceException(com.microsoft.azure.sdk.iot.device.exceptions.IotHubServiceException) IOException(java.io.IOException) FileUploadSasUriRequest(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest)

Example 2 with FileUploadSasUriRequest

use of com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest in project azure-iot-sdk-java by Azure.

the class FileUploadTaskTest method runSendRequestThrows.

/* Tests_SRS_FILEUPLOADTASK_21_031: [If run failed to send the request, it shall call the userCallback with the status `ERROR`, and abort the upload.] */
@Test
public void runSendRequestThrows() throws IOException, IllegalArgumentException, URISyntaxException {
    // arrange
    new NonStrictExpectations() {

        {
            new FileUploadSasUriRequest(VALID_BLOB_NAME);
            result = mockFileUploadSasUriRequest;
            mockFileUploadSasUriRequest.toJson();
            result = VALID_REQUEST_JSON;
            new IotHubTransportMessage(VALID_REQUEST_JSON);
            result = mockMessageRequest;
            mockHttpsTransportManager.getFileUploadSasUri(mockMessageRequest);
            result = new IOException();
        }
    };
    FileUploadTask fileUploadTask = Deencapsulation.newInstance(FileUploadTask.class, new Class[] { String.class, InputStream.class, long.class, HttpsTransportManager.class, IotHubEventCallback.class, Object.class }, VALID_BLOB_NAME, mockInputStream, VALID_STREAM_LENGTH, mockHttpsTransportManager, mockIotHubEventCallback, VALID_CALLBACK_CONTEXT);
    // act
    Deencapsulation.invoke(fileUploadTask, "run");
    // assert
    new Verifications() {

        {
            mockHttpsTransportManager.getFileUploadSasUri(mockMessageRequest);
            times = 1;
            mockIotHubEventCallback.execute(IotHubStatusCode.ERROR, VALID_CALLBACK_CONTEXT);
            times = 1;
        }
    };
}
Also used : FileUploadTask(com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask) IOException(java.io.IOException) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) FileUploadSasUriRequest(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest) Test(org.junit.Test)

Example 3 with FileUploadSasUriRequest

use of com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest in project azure-iot-sdk-java by Azure.

the class FileUploadTaskTest method runFileUploadRequestParserThrows.

/* Tests_SRS_FILEUPLOADTASK_21_031: [If run failed to send the request, it shall call the userCallback with the status `ERROR`, and abort the upload.] */
@Test
public void runFileUploadRequestParserThrows() throws IOException, IllegalArgumentException, URISyntaxException {
    // arrange
    new NonStrictExpectations() {

        {
            new FileUploadSasUriRequest(VALID_BLOB_NAME);
            result = new IllegalArgumentException();
        }
    };
    FileUploadTask fileUploadTask = Deencapsulation.newInstance(FileUploadTask.class, new Class[] { String.class, InputStream.class, long.class, HttpsTransportManager.class, IotHubEventCallback.class, Object.class }, VALID_BLOB_NAME, mockInputStream, VALID_STREAM_LENGTH, mockHttpsTransportManager, mockIotHubEventCallback, VALID_CALLBACK_CONTEXT);
    // act
    Deencapsulation.invoke(fileUploadTask, "run");
    // assert
    new Verifications() {

        {
            mockIotHubEventCallback.execute(IotHubStatusCode.ERROR, VALID_CALLBACK_CONTEXT);
            times = 1;
        }
    };
}
Also used : FileUploadTask(com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) FileUploadSasUriRequest(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest) Test(org.junit.Test)

Example 4 with FileUploadSasUriRequest

use of com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest in project azure-iot-sdk-java by Azure.

the class FileUploadTaskTest method runCreateRequestMessageThrows.

/* Tests_SRS_FILEUPLOADTASK_21_031: [If run failed to send the request, it shall call the userCallback with the status `ERROR`, and abort the upload.] */
@Test
public void runCreateRequestMessageThrows() throws IOException, IllegalArgumentException, URISyntaxException {
    // arrange
    new NonStrictExpectations() {

        {
            new FileUploadSasUriRequest(VALID_BLOB_NAME);
            result = mockFileUploadSasUriRequest;
            mockFileUploadSasUriRequest.toJson();
            result = VALID_REQUEST_JSON;
            new IotHubTransportMessage(VALID_REQUEST_JSON);
            result = new IllegalArgumentException();
        }
    };
    FileUploadTask fileUploadTask = Deencapsulation.newInstance(FileUploadTask.class, new Class[] { String.class, InputStream.class, long.class, HttpsTransportManager.class, IotHubEventCallback.class, Object.class }, VALID_BLOB_NAME, mockInputStream, VALID_STREAM_LENGTH, mockHttpsTransportManager, mockIotHubEventCallback, VALID_CALLBACK_CONTEXT);
    // act
    Deencapsulation.invoke(fileUploadTask, "run");
    // assert
    new Verifications() {

        {
            mockIotHubEventCallback.execute(IotHubStatusCode.ERROR, VALID_CALLBACK_CONTEXT);
            times = 1;
        }
    };
}
Also used : FileUploadTask(com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) FileUploadSasUriRequest(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest) Test(org.junit.Test)

Example 5 with FileUploadSasUriRequest

use of com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest in project azure-iot-sdk-java by Azure.

the class FileUploadTests method uploadToBlobAsyncSingleFileGranular.

@Test(timeout = MAX_MILLISECS_TIMEOUT_KILL_TEST)
public void uploadToBlobAsyncSingleFileGranular() throws URISyntaxException, IOException, InterruptedException, IotHubException, GeneralSecurityException {
    // arrange
    DeviceClient deviceClient = setUpDeviceClient(testInstance.protocol);
    // act
    FileUploadSasUriResponse sasUriResponse = deviceClient.getFileUploadSasUri(new FileUploadSasUriRequest(testInstance.fileUploadState[0].blobName));
    BlockBlobClient blockBlobClient = new BlobClientBuilder().endpoint(sasUriResponse.getBlobUri().toString()).buildClient().getBlockBlobClient();
    blockBlobClient.upload(testInstance.fileUploadState[0].fileInputStream, testInstance.fileUploadState[0].fileLength);
    FileUploadCompletionNotification fileUploadCompletionNotification = new FileUploadCompletionNotification();
    fileUploadCompletionNotification.setCorrelationId(sasUriResponse.getCorrelationId());
    fileUploadCompletionNotification.setStatusCode(0);
    fileUploadCompletionNotification.setSuccess(true);
    fileUploadCompletionNotification.setStatusDescription("Succeed to upload to storage.");
    deviceClient.completeFileUpload(fileUploadCompletionNotification);
    // assert
    assertEquals(buildExceptionMessage("File upload status should be SUCCESS but was " + testInstance.fileUploadState[0].fileUploadStatus, deviceClient), SUCCESS, testInstance.fileUploadState[0].fileUploadStatus);
    tearDownDeviceClient(deviceClient);
}
Also used : BlockBlobClient(com.azure.storage.blob.specialized.BlockBlobClient) FileUploadSasUriResponse(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse) BlobClientBuilder(com.azure.storage.blob.BlobClientBuilder) FileUploadCompletionNotification(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification) FileUploadSasUriRequest(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest) 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)

Aggregations

FileUploadSasUriRequest (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest)7 BlobClientBuilder (com.azure.storage.blob.BlobClientBuilder)4 FileUploadCompletionNotification (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification)4 FileUploadSasUriResponse (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 BlobClient (com.azure.storage.blob.BlobClient)3 FileUploadTask (com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask)3 NonStrictExpectations (mockit.NonStrictExpectations)3 Verifications (mockit.Verifications)3 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)2 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 BlockBlobClient (com.azure.storage.blob.specialized.BlockBlobClient)1 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)1 IotHubClientProtocol (com.microsoft.azure.sdk.iot.device.IotHubClientProtocol)1 IotHubServiceException (com.microsoft.azure.sdk.iot.device.exceptions.IotHubServiceException)1 Scanner (java.util.Scanner)1 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)1 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)1