Search in sources :

Example 1 with Message

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

the class ServiceClientTest method send_async_future_return_ok.

// Tests_SRS_SERVICE_SDK_JAVA_SERVICECLIENT_12_016: [The function shall create an async wrapper around the send() function call, handle the return value or delegate exception]
@Test
public void send_async_future_return_ok() throws Exception {
    // Arrange
    String iotHubName = "IOTHUBNAME";
    String hostName = "HOSTNAME";
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
    String connectionString = "HostName=" + hostName + "." + iotHubName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    String deviceId = "XXX";
    String content = "HELLO";
    Message iotMessage = new Message(content);
    IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
    ServiceClient serviceClient = ServiceClient.createFromConnectionString(connectionString, iotHubServiceClientProtocol);
    // Assert
    new Expectations() {

        {
            amqpSend.send(deviceId, iotMessage);
            serviceClient.send(deviceId, iotMessage);
        }
    };
    // Act
    CompletableFuture<Void> completableFuture = serviceClient.sendAsync(deviceId, iotMessage);
    completableFuture.get();
}
Also used : Expectations(mockit.Expectations) Message(com.microsoft.azure.sdk.iot.service.Message) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Test(org.junit.Test)

Example 2 with Message

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

the class ServiceClientTest method send_async_future_throw.

// Tests_SRS_SERVICE_SDK_JAVA_SERVICECLIENT_12_016: [The function shall create an async wrapper around the send() function call, handle the return value or delegate exception]
// Assert
@Test(expected = Exception.class)
public void send_async_future_throw() throws Exception {
    // Arrange
    new MockUp<ServiceClient>() {

        @Mock
        public void send(String deviceId, String message) throws IOException {
            throw new IOException();
        }
    };
    String iotHubName = "IOTHUBNAME";
    String hostName = "HOSTNAME";
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
    String connectionString = "HostName=" + hostName + "." + iotHubName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    String deviceId = "XXX";
    String content = "HELLO";
    Message iotMessage = new Message(content);
    IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
    ServiceClient serviceClient = ServiceClient.createFromConnectionString(connectionString, iotHubServiceClientProtocol);
    // Act
    CompletableFuture<Void> completableFuture = serviceClient.sendAsync(deviceId, iotMessage);
    completableFuture.get();
}
Also used : Message(com.microsoft.azure.sdk.iot.service.Message) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) MockUp(mockit.MockUp) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IOException(java.io.IOException) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Test(org.junit.Test)

Example 3 with Message

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

the class ServiceClientTest method send_sender_null.

// Tests_SRS_SERVICE_SDK_JAVA_SERVICECLIENT_12_0012: [The function shall throw IOException if the member AMQP sender object has not been initialized]
// Assert
@Test(expected = IOException.class)
public void send_sender_null() throws Exception {
    // Arrange
    String iotHubName = "IOTHUBNAME";
    String hostName = "HOSTNAME";
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
    String connectionString = "HostName=" + hostName + "." + iotHubName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    String deviceId = "XXX";
    String content = "HELLO";
    Message iotMessage = new Message(content);
    IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
    ServiceClient serviceClient = ServiceClient.createFromConnectionString(connectionString, iotHubServiceClientProtocol);
    Deencapsulation.setField(serviceClient, "amqpMessageSender", null);
    // Act
    serviceClient.send(deviceId, iotMessage);
}
Also used : Message(com.microsoft.azure.sdk.iot.service.Message) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Test(org.junit.Test)

Example 4 with Message

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

the class AmqpSendTest method send_initializes_Reactor.

// Tests_SRS_SERVICE_SDK_JAVA_AMQPSEND_12_007: [The event handler shall initialize the Proton reactor object]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSEND_12_008: [The event handler shall start the Proton reactor object]
@Test
public void send_initializes_Reactor() throws Exception {
    // Arrange
    String hostName = "aaa";
    String userName = "bbb";
    String sasToken = "ccc";
    String deviceId = "deviceId";
    String content = "abcdefghijklmnopqrst";
    Message message = new Message(content);
    IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
    AmqpSend amqpSend = new AmqpSend(hostName, userName, sasToken, iotHubServiceClientProtocol);
    amqpSend.open();
    // Assert
    new Expectations() {

        {
            reactor = proton.reactor(amqpSend);
            reactor.run();
        }
    };
    // Act
    amqpSend.send(deviceId, message);
}
Also used : Expectations(mockit.Expectations) Message(com.microsoft.azure.sdk.iot.service.Message) AmqpSend(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSend) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Test(org.junit.Test)

Example 5 with Message

use of com.microsoft.azure.sdk.iot.service.Message 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)

Aggregations

Message (com.microsoft.azure.sdk.iot.service.Message)16 Test (org.junit.Test)11 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)10 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)8 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)5 IOException (java.io.IOException)5 AmqpSend (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSend)4 Expectations (mockit.Expectations)4 CorrelationDetailsLoggingAssert.buildExceptionMessage (tests.integration.com.microsoft.azure.sdk.iot.helpers.CorrelationDetailsLoggingAssert.buildExceptionMessage)4 Device (com.microsoft.azure.sdk.iot.service.Device)3 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)3 ServiceClientOptions (com.microsoft.azure.sdk.iot.service.ServiceClientOptions)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 FileUploadNotification (com.microsoft.azure.sdk.iot.service.FileUploadNotification)2 FileUploadNotificationReceiver (com.microsoft.azure.sdk.iot.service.FileUploadNotificationReceiver)2 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)2 IotHubUnathorizedException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException)2 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)2