Search in sources :

Example 1 with FileUploadSasUriResponse

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

the class FileUploadSasUriResponseTest method getters_succeed.

@Test
public void getters_succeed() {
    // arrange
    String validJson = createJson(VALID_HOST_NAME, VALID_CONTAINER_NAME, VALID_CORRELATION_ID, VALID_BLOB_NAME, VALID_SAS_TOKEN);
    FileUploadSasUriResponse fileUploadSasUriResponse = new FileUploadSasUriResponse(validJson);
    // act
    // assert
    assertEquals(VALID_CORRELATION_ID, fileUploadSasUriResponse.getCorrelationId());
    assertEquals(VALID_HOST_NAME, fileUploadSasUriResponse.getHostName());
    assertEquals(VALID_CONTAINER_NAME, fileUploadSasUriResponse.getContainerName());
    assertEquals(VALID_BLOB_NAME, fileUploadSasUriResponse.getBlobName());
    assertEquals(VALID_SAS_TOKEN, fileUploadSasUriResponse.getSasToken());
}
Also used : FileUploadSasUriResponse(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse) Test(org.junit.Test)

Example 2 with FileUploadSasUriResponse

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

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

the class FileUploadTask method getFileUploadSasUri.

public FileUploadSasUriResponse getFileUploadSasUri(FileUploadSasUriRequest request) throws IOException {
    IotHubTransportMessage message = new IotHubTransportMessage(request.toJson());
    message.setIotHubMethod(IotHubMethod.POST);
    ResponseMessage responseMessage;
    httpsTransportManager.open();
    responseMessage = httpsTransportManager.getFileUploadSasUri(message);
    httpsTransportManager.close();
    String responseMessagePayload = validateServiceStatusCode(responseMessage, "Failed to get the file upload SAS URI");
    if (responseMessagePayload == null || responseMessagePayload.isEmpty()) {
        throw new IOException("Sas URI response message had no payload");
    }
    return new FileUploadSasUriResponse(responseMessagePayload);
}
Also used : ResponseMessage(com.microsoft.azure.sdk.iot.device.ResponseMessage) IOException(java.io.IOException) FileUploadSasUriResponse(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)

Example 4 with FileUploadSasUriResponse

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

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

the class FileUploadTaskTest method runFileUploadResponseParserThrows.

/* Tests_SRS_FILEUPLOADTASK_21_016: [If the `responseMessage` is null, empty, do not contains a valid json, or if the information in json is not correct, the run shall call the `userCallback` reporting the error, and abort the upload.] */
@Test
public void runFileUploadResponseParserThrows() throws IOException, IllegalArgumentException, URISyntaxException {
    // arrange
    requestExpectations(VALID_BLOB_NAME, VALID_REQUEST_JSON);
    new NonStrictExpectations() {

        {
            mockResponseMessage.getStatus();
            result = IotHubStatusCode.OK;
            mockResponseMessage.getBytes();
            result = VALID_RESPONSE_JSON.getBytes(StandardCharsets.UTF_8);
            new FileUploadSasUriResponse(VALID_RESPONSE_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");
}
Also used : FileUploadTask(com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask) FileUploadSasUriResponse(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Aggregations

FileUploadSasUriResponse (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse)8 BlobClientBuilder (com.azure.storage.blob.BlobClientBuilder)4 FileUploadCompletionNotification (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification)4 FileUploadSasUriRequest (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 BlobClient (com.azure.storage.blob.BlobClient)3 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 ResponseMessage (com.microsoft.azure.sdk.iot.device.ResponseMessage)1 IotHubServiceException (com.microsoft.azure.sdk.iot.device.exceptions.IotHubServiceException)1 FileUploadTask (com.microsoft.azure.sdk.iot.device.fileupload.FileUploadTask)1 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)1 Scanner (java.util.Scanner)1 NonStrictExpectations (mockit.NonStrictExpectations)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