Search in sources :

Example 1 with FileUploadCompletionNotification

use of com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification 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 FileUploadCompletionNotification

use of com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification 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)

Example 3 with FileUploadCompletionNotification

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

the class FileUploadTaskTest method runCreateNotificationMessageThrows.

/* Tests_SRS_FILEUPLOADTASK_21_033: [If run failed to send the notification, it shall call the userCallback with the stratus `ERROR`, and abort the upload.] */
@Test
public void runCreateNotificationMessageThrows() throws IOException, IllegalArgumentException, URISyntaxException {
    // arrange
    requestExpectations(VALID_BLOB_NAME, VALID_REQUEST_JSON);
    responseExpectations(VALID_RESPONSE_JSON);
    responseParserExpectations(VALID_BLOB_NAME, VALID_CORRELATION_ID, VALID_HOST_NAME, VALID_CONTAINER_NAME, VALID_SAS_TOKEN);
    blobClientBuilderExpectations();
    new NonStrictExpectations() {

        {
            new FileUploadCompletionNotification(VALID_CORRELATION_ID, true, 0, (String) any);
            result = mockFileUploadStatusParser;
            mockFileUploadStatusParser.toJson();
            result = VALID_NOTIFICATION_JSON;
            new IotHubTransportMessage(VALID_NOTIFICATION_JSON);
            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");
}
Also used : FileUploadTask(com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask) IOException(java.io.IOException) FileUploadCompletionNotification(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 4 with FileUploadCompletionNotification

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

the class FileUploadTaskTest method runSendNotificationThrows.

/* 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 runSendNotificationThrows() throws IOException, IllegalArgumentException, URISyntaxException {
    // arrange
    requestExpectations(VALID_BLOB_NAME, VALID_REQUEST_JSON);
    responseExpectations(VALID_RESPONSE_JSON);
    responseParserExpectations(VALID_BLOB_NAME, VALID_CORRELATION_ID, VALID_HOST_NAME, VALID_CONTAINER_NAME, VALID_SAS_TOKEN);
    blobClientBuilderExpectations();
    new NonStrictExpectations() {

        {
            new FileUploadCompletionNotification(VALID_CORRELATION_ID, true, 0, (String) any);
            result = mockFileUploadStatusParser;
            mockFileUploadStatusParser.toJson();
            result = VALID_NOTIFICATION_JSON;
            new IotHubTransportMessage(VALID_NOTIFICATION_JSON);
            result = mockMessageNotification;
            mockHttpsTransportManager.sendFileUploadNotification(mockMessageNotification);
            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.sendFileUploadNotification(mockMessageNotification);
            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) FileUploadCompletionNotification(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 5 with FileUploadCompletionNotification

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

the class FileUploadTaskTest method runFileUploadStatusParserThrows.

/* Tests_SRS_FILEUPLOADTASK_21_033: [If run failed to send the notification, it shall call the userCallback with the stratus `ERROR`, and abort the upload.] */
@Test
public void runFileUploadStatusParserThrows() throws IOException, IllegalArgumentException, URISyntaxException {
    // arrange
    requestExpectations(VALID_BLOB_NAME, VALID_REQUEST_JSON);
    responseExpectations(VALID_RESPONSE_JSON);
    responseParserExpectations(VALID_BLOB_NAME, VALID_CORRELATION_ID, VALID_HOST_NAME, VALID_CONTAINER_NAME, VALID_SAS_TOKEN);
    blobClientBuilderExpectations();
    new NonStrictExpectations() {

        {
            new FileUploadCompletionNotification(VALID_CORRELATION_ID, true, 0, (String) any);
            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) FileUploadCompletionNotification(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Aggregations

FileUploadCompletionNotification (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification)7 IOException (java.io.IOException)5 BlobClientBuilder (com.azure.storage.blob.BlobClientBuilder)4 FileUploadSasUriRequest (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest)4 FileUploadSasUriResponse (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse)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 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)2 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 Verifications (mockit.Verifications)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