Search in sources :

Example 11 with FileUploadNotification

use of com.microsoft.azure.sdk.iot.service.FileUploadNotification in project azure-iot-sdk-java by Azure.

the class AzureSasCredentialSample method runServiceClientSample.

private static void runServiceClientSample(String iotHubHostName, AzureSasCredential credential, String deviceId) {
    // ServiceClient has some configurable options for setting a custom SSLContext, as well as for setting proxies.
    // For this sample, the default options will be used though.
    ServiceClientOptions options = ServiceClientOptions.builder().build();
    // This constructor takes in your implementation of AzureSasCredential which allows you to use symmetric key based
    // authentication without giving the client your connection string.
    ServiceClient serviceClient = new ServiceClient(iotHubHostName, credential, IotHubServiceClientProtocol.AMQPS, options);
    String cloudToDeviceMessagePayload = "This is a message sent by an RBAC authenticated service client!";
    Message cloudToDeviceMessage = new Message(cloudToDeviceMessagePayload.getBytes(StandardCharsets.UTF_8));
    try {
        System.out.println("Sending cloud to device message to the new device");
        serviceClient.send(deviceId, cloudToDeviceMessage);
        System.out.println("Successfully sent cloud to device message to the new device");
    } catch (IOException | IotHubException e) {
        System.err.println("Failed to send a cloud to device message to the new device");
        e.printStackTrace();
        System.exit(-1);
    }
    try {
        // FeedbackReceiver will use the same authentication mechanism that the ServiceClient itself uses,
        // so the below APIs are also RBAC authenticated.
        FeedbackReceiver feedbackReceiver = serviceClient.getFeedbackReceiver();
        System.out.println("Opening feedback receiver to listen for feedback messages");
        feedbackReceiver.open();
        FeedbackBatch feedbackBatch = feedbackReceiver.receive(FEEDBACK_MESSAGE_LISTEN_SECONDS);
        if (feedbackBatch != null) {
            for (FeedbackRecord feedbackRecord : feedbackBatch.getRecords()) {
                System.out.println(String.format("Feedback record received for device %s with status %s", feedbackRecord.getDeviceId(), feedbackRecord.getStatusCode()));
            }
        } else {
            System.out.println("No feedback records were received");
        }
        feedbackReceiver.close();
    } catch (IOException | InterruptedException e) {
        System.err.println("Failed to listen for feedback messages");
        e.printStackTrace();
        System.exit(-1);
    }
    try {
        // FileUploadNotificationReceiver will use the same authentication mechanism that the ServiceClient itself uses,
        // so the below APIs are also RBAC authenticated.
        FileUploadNotificationReceiver fileUploadNotificationReceiver = serviceClient.getFileUploadNotificationReceiver();
        System.out.println("Opening file upload notification receiver and listening for file upload notifications");
        fileUploadNotificationReceiver.open();
        FileUploadNotification fileUploadNotification = fileUploadNotificationReceiver.receive(FILE_UPLOAD_NOTIFICATION_LISTEN_SECONDS);
        if (fileUploadNotification != null) {
            System.out.println("File upload notification received for device " + fileUploadNotification.getDeviceId());
        } else {
            System.out.println("No feedback records were received");
        }
        fileUploadNotificationReceiver.close();
    } catch (IOException | InterruptedException e) {
        System.err.println("Failed to listen for file upload notification messages");
        e.printStackTrace();
        System.exit(-1);
    }
}
Also used : FileUploadNotificationReceiver(com.microsoft.azure.sdk.iot.service.FileUploadNotificationReceiver) Message(com.microsoft.azure.sdk.iot.service.Message) FeedbackReceiver(com.microsoft.azure.sdk.iot.service.FeedbackReceiver) ServiceClientOptions(com.microsoft.azure.sdk.iot.service.ServiceClientOptions) IOException(java.io.IOException) FileUploadNotification(com.microsoft.azure.sdk.iot.service.FileUploadNotification) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) FeedbackBatch(com.microsoft.azure.sdk.iot.service.FeedbackBatch) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) FeedbackRecord(com.microsoft.azure.sdk.iot.service.FeedbackRecord)

Example 12 with FileUploadNotification

use of com.microsoft.azure.sdk.iot.service.FileUploadNotification in project azure-iot-sdk-java by Azure.

the class FileUploadNotificationTest method constructorSavesParameters.

// Tests_SRS_SERVICE_SDK_JAVA_FILEUPLOADNOTIFICATION_25_001: [** The constructor shall save all the parameters only if they are valid **]
@Test
public void constructorSavesParameters() throws IOException {
    // arrange
    final String actualDeviceId = "TestDeviceId";
    final String actualBlobUri = "TestblobUri";
    final String actualBlobName = "TestBlobName";
    final Date actualLastUpdatedTimeDate = new Date();
    final Long actualBlobSizeInBytes = 10000L;
    final Date actualEnqueuedTimeUtcDate = new Date();
    // act
    FileUploadNotification testFileUploadNotification = new FileUploadNotification(actualDeviceId, actualBlobUri, actualBlobName, actualLastUpdatedTimeDate, actualBlobSizeInBytes, actualEnqueuedTimeUtcDate);
    // assert
    assertEquals(testFileUploadNotification.getDeviceId(), actualDeviceId);
    assertEquals(testFileUploadNotification.getBlobName(), actualBlobName);
    assertEquals(testFileUploadNotification.getBlobUri(), actualBlobUri);
    assertEquals(testFileUploadNotification.getLastUpdatedTimeDate(), actualLastUpdatedTimeDate);
    assertEquals(testFileUploadNotification.getBlobSizeInBytes(), actualBlobSizeInBytes);
    assertEquals(testFileUploadNotification.getEnqueuedTimeUtcDate(), actualEnqueuedTimeUtcDate);
}
Also used : FileUploadNotification(com.microsoft.azure.sdk.iot.service.FileUploadNotification) Date(java.util.Date) Test(org.junit.Test)

Example 13 with FileUploadNotification

use of com.microsoft.azure.sdk.iot.service.FileUploadNotification in project azure-iot-sdk-java by Azure.

the class FileUploadNotificationTest method getBlobNameGets.

// Tests_SRS_SERVICE_SDK_JAVA_FILEUPLOADNOTIFICATION_25_005: [ The getter for blobName ]
@Test
public void getBlobNameGets() throws IOException {
    // arrange
    final String actualBlobName = "TestBlobName";
    FileUploadNotification testFileUploadNotification = new FileUploadNotification(mockDeviceId, mockBlobUri, actualBlobName, mockLastUpdatedTimeDate, mockBlobSizeInBytes, mockEnqueuedTimeUtcDate);
    // act
    final String testBlobName = testFileUploadNotification.getBlobName();
    // assert
    assertEquals(testBlobName, actualBlobName);
}
Also used : FileUploadNotification(com.microsoft.azure.sdk.iot.service.FileUploadNotification) Test(org.junit.Test)

Example 14 with FileUploadNotification

use of com.microsoft.azure.sdk.iot.service.FileUploadNotification in project azure-iot-sdk-java by Azure.

the class FileUploadNotificationTest method getBlobSizeInBytesGets.

// Tests_SRS_SERVICE_SDK_JAVA_FILEUPLOADNOTIFICATION_25_009: [ The getter for blobSizeInBytes ]
@Test
public void getBlobSizeInBytesGets() throws IOException {
    // arrange
    final Long actualBlobSizeInBytes = 10000L;
    FileUploadNotification testFileUploadNotification = new FileUploadNotification(mockDeviceId, mockBlobUri, mockBlobName, mockLastUpdatedTimeDate, actualBlobSizeInBytes, mockEnqueuedTimeUtcDate);
    // act
    Long testBlobSizeInBytes = testFileUploadNotification.getBlobSizeInBytes();
    // assert
    assertEquals(testBlobSizeInBytes, actualBlobSizeInBytes);
}
Also used : FileUploadNotification(com.microsoft.azure.sdk.iot.service.FileUploadNotification) Test(org.junit.Test)

Example 15 with FileUploadNotification

use of com.microsoft.azure.sdk.iot.service.FileUploadNotification in project azure-iot-sdk-java by Azure.

the class FileUploadNotificationTest method constructorThrowsOnNullSize.

// Tests_SRS_SERVICE_SDK_JAVA_FILEUPLOADNOTIFICATION_25_002: [** If any of the parameters are null or empty then this method shall throw IllegalArgumentException.**]
@Test(expected = IllegalArgumentException.class)
public void constructorThrowsOnNullSize() throws IOException {
    // arrange
    final String actualDeviceId = "TestDeviceId";
    final String actualBlobUri = "TestblobUri";
    final String actualBlobName = "TestBlobName";
    final Date actualLastUpdatedTimeDate = new Date();
    final Long actualBlobSizeInBytes = null;
    final Date actualEnqueuedTimeUtcDate = new Date();
    // act
    FileUploadNotification testFileUploadNotification = new FileUploadNotification(actualDeviceId, actualBlobUri, actualBlobName, actualLastUpdatedTimeDate, actualBlobSizeInBytes, actualEnqueuedTimeUtcDate);
    // assert
    assertEquals(testFileUploadNotification.getDeviceId(), actualDeviceId);
    assertEquals(testFileUploadNotification.getBlobName(), actualBlobName);
    assertEquals(testFileUploadNotification.getBlobUri(), actualBlobUri);
    assertEquals(testFileUploadNotification.getLastUpdatedTimeDate(), actualLastUpdatedTimeDate);
    assertEquals(testFileUploadNotification.getBlobSizeInBytes(), actualBlobSizeInBytes);
    assertEquals(testFileUploadNotification.getEnqueuedTimeUtcDate(), actualEnqueuedTimeUtcDate);
}
Also used : FileUploadNotification(com.microsoft.azure.sdk.iot.service.FileUploadNotification) Date(java.util.Date) Test(org.junit.Test)

Aggregations

FileUploadNotification (com.microsoft.azure.sdk.iot.service.FileUploadNotification)39 Test (org.junit.Test)36 Date (java.util.Date)24 FileUploadNotificationReceiver (com.microsoft.azure.sdk.iot.service.FileUploadNotificationReceiver)6 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)4 Verifications (mockit.Verifications)4 IOException (java.io.IOException)3 FeedbackBatch (com.microsoft.azure.sdk.iot.service.FeedbackBatch)2 FeedbackReceiver (com.microsoft.azure.sdk.iot.service.FeedbackReceiver)2 FeedbackRecord (com.microsoft.azure.sdk.iot.service.FeedbackRecord)2 Message (com.microsoft.azure.sdk.iot.service.Message)2 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)2 ServiceClientOptions (com.microsoft.azure.sdk.iot.service.ServiceClientOptions)2 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)2 FileUploadNotificationParser (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadNotificationParser)1