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);
}
}
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;
}
};
}
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;
}
};
}
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;
}
};
}
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);
}
Aggregations