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