Search in sources :

Example 11 with Message

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

the class AmqpSendTest method send_throwsIOException_when_open_has_not_been_called.

// Tests_SRS_SERVICE_SDK_JAVA_AMQPSEND_12_009: [The event handler shall throw IOException if the send handler object is not initialized]
// Assert
@Test(expected = IOException.class)
public void send_throwsIOException_when_open_has_not_been_called() 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);
    // Act
    amqpSend.send(deviceId, message);
}
Also used : 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 12 with Message

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

the class AmqpSendTest method send_creates_ProtonMessage.

// Tests_SRS_SERVICE_SDK_JAVA_AMQPSEND_12_006: [The event handler shall create a Proton message with the given content]
@Test
public void send_creates_ProtonMessage() 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();
    AmqpSendHandler handler = Deencapsulation.getField(amqpSend, "amqpSendHandler");
    // Assert
    new Expectations() {

        {
            Deencapsulation.invoke(handler, "createProtonMessage", deviceId, message);
        }
    };
    // Act
    amqpSend.send(deviceId, message);
}
Also used : Expectations(mockit.Expectations) Message(com.microsoft.azure.sdk.iot.service.Message) AmqpSendHandler(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler) AmqpSend(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSend) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Test(org.junit.Test)

Example 13 with Message

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

the class ServiceClientTests method serviceClientTokenRenewalWithAzureSasCredential.

@Test
@StandardTierHubOnlyTest
public void serviceClientTokenRenewalWithAzureSasCredential() throws Exception {
    RegistryManager registryManager = new RegistryManager(iotHubConnectionString, RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    TestDeviceIdentity testDeviceIdentity = Tools.getTestDevice(iotHubConnectionString, IotHubClientProtocol.AMQPS, AuthenticationType.SAS, false);
    Device device = testDeviceIdentity.getDevice();
    ServiceClient serviceClient;
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
    serviceClient = new ServiceClient(iotHubConnectionStringObj.getHostName(), sasCredential, testInstance.protocol);
    serviceClient.open();
    Message message = new Message(SMALL_PAYLOAD);
    serviceClient.send(device.getDeviceId(), message);
    // deliberately expire the SAS token to provoke a 401 to ensure that the registry manager is using the shared
    // access signature that is set here.
    sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
    try {
        serviceClient.send(device.getDeviceId(), message);
        fail("Expected sending cloud to device message to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
    } catch (IOException e) {
        // For service client, the unauthorized exception is wrapped by an IOException, so we need to unwrap it here
        if (e.getCause() instanceof IotHubUnathorizedException) {
            log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
        } else {
            throw e;
        }
    }
    // Renew the expired shared access signature
    serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    sasCredential.update(serviceSasToken.toString());
    // The final c2d send should succeed since the shared access signature has been renewed
    serviceClient.send(device.getDeviceId(), message);
    serviceClient.close();
    registryManager.close();
    Tools.disposeTestIdentity(testDeviceIdentity, iotHubConnectionString);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) Message(com.microsoft.azure.sdk.iot.service.Message) CorrelationDetailsLoggingAssert.buildExceptionMessage(tests.integration.com.microsoft.azure.sdk.iot.helpers.CorrelationDetailsLoggingAssert.buildExceptionMessage) Device(com.microsoft.azure.sdk.iot.service.Device) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) AzureSasCredential(com.azure.core.credential.AzureSasCredential) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IOException(java.io.IOException) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) TestDeviceIdentity(tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 14 with Message

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

the class ReceiveMessagesCommon method createCloudToDeviceMessage.

protected Message createCloudToDeviceMessage(int messageSize) throws IotHubException, IOException {
    byte[] payload = new byte[messageSize];
    new Random().nextBytes(payload);
    com.microsoft.azure.sdk.iot.service.Message serviceMessage = new com.microsoft.azure.sdk.iot.service.Message(payload);
    serviceMessage.setCorrelationId(expectedCorrelationId);
    serviceMessage.setMessageId(expectedMessageId);
    serviceMessage.setProperties(messageProperties);
    return serviceMessage;
}
Also used : Message(com.microsoft.azure.sdk.iot.service.Message) CorrelationDetailsLoggingAssert.buildExceptionMessage(tests.integration.com.microsoft.azure.sdk.iot.helpers.CorrelationDetailsLoggingAssert.buildExceptionMessage) com.microsoft.azure.sdk.iot.service(com.microsoft.azure.sdk.iot.service) Message(com.microsoft.azure.sdk.iot.service.Message)

Example 15 with Message

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

the class TokenCredentialTests method cloudToDeviceTelemetryWithTokenCredential.

@Test
public void cloudToDeviceTelemetryWithTokenCredential() throws Exception {
    // only run tests for standard tier hubs
    Assume.assumeFalse(isBasicTierHub);
    // We remove and recreate the device for a clean start
    RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
    Device device = Device.createDevice("some-device-" + UUID.randomUUID(), AuthenticationType.SAS);
    registryManager.addDevice(device);
    Device deviceGetBefore = registryManager.getDevice(device.getDeviceId());
    // Create service client
    ServiceClient serviceClient = buildServiceClientWithTokenCredential(IotHubServiceClientProtocol.AMQPS);
    serviceClient.open();
    Message message = new Message("some message".getBytes(StandardCharsets.UTF_8));
    serviceClient.send(device.getDeviceId(), message);
    Device deviceGetAfter = registryManager.getDevice(device.getDeviceId());
    serviceClient.close();
    registryManager.removeDevice(device.getDeviceId());
    // Assert
    assertEquals(0, deviceGetBefore.getCloudToDeviceMessageCount());
    assertEquals(1, deviceGetAfter.getCloudToDeviceMessageCount());
    registryManager.close();
}
Also used : Message(com.microsoft.azure.sdk.iot.service.Message) Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) Test(org.junit.Test)

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