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