use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.
the class RegistryManagerTests method apply_configuration_e2e.
@Test
@StandardTierHubOnlyTest
public void apply_configuration_e2e() throws Exception {
// Arrange
RegistryManagerTestInstance testInstance = new RegistryManagerTestInstance();
Device deviceSetup = Device.createFromId(testInstance.deviceId, DeviceStatus.Enabled, null);
Tools.addDeviceWithRetry(testInstance.registryManager, deviceSetup);
final HashMap<String, Object> testDeviceContent = new HashMap<String, Object>() {
{
put("properties.desired.chiller-water", new HashMap<String, Object>() {
{
put("temperature", 66);
put("pressure", 28);
}
});
}
};
ConfigurationContent content = new ConfigurationContent();
content.setDeviceContent(testDeviceContent);
boolean expectedExceptionThrown = false;
// Act
try {
testInstance.registryManager.applyConfigurationContentOnDevice(testInstance.deviceId, content);
} catch (IotHubBadFormatException e) {
expectedExceptionThrown = true;
}
assertTrue("Bad format exception wasn't thrown but was expected", expectedExceptionThrown);
}
use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.
the class RegistryManagerTests method deviceLifecycleWithProxy.
@Test
public void deviceLifecycleWithProxy() throws Exception {
Proxy testProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
ProxyOptions proxyOptions = new ProxyOptions(testProxy);
RegistryManagerOptions registryManagerOptions = RegistryManagerOptions.builder().proxyOptions(proxyOptions).build();
RegistryManagerTestInstance testInstance = new RegistryManagerTestInstance(registryManagerOptions);
// -Create-//
Device deviceAdded = Device.createFromId(testInstance.deviceId, DeviceStatus.Enabled, null);
Tools.addDeviceWithRetry(testInstance.registryManager, deviceAdded);
// -Read-//
Device deviceRetrieved = testInstance.registryManager.getDevice(testInstance.deviceId);
// -Update-//
Device deviceUpdated = testInstance.registryManager.getDevice(testInstance.deviceId);
deviceUpdated.setStatus(DeviceStatus.Disabled);
deviceUpdated = testInstance.registryManager.updateDevice(deviceUpdated);
// -Delete-//
testInstance.registryManager.removeDevice(testInstance.deviceId);
// Assert
assertEquals(buildExceptionMessage("", hostName), testInstance.deviceId, deviceAdded.getDeviceId());
assertEquals(buildExceptionMessage("", hostName), testInstance.deviceId, deviceRetrieved.getDeviceId());
assertEquals(buildExceptionMessage("", hostName), DeviceStatus.Disabled, deviceUpdated.getStatus());
assertTrue(buildExceptionMessage("", hostName), deviceWasDeletedSuccessfully(testInstance.registryManager, testInstance.deviceId));
}
use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.
the class RegistryManagerTests method serviceValidatesSymmetricKey.
@Test
public void serviceValidatesSymmetricKey() throws IOException, IotHubException {
RegistryManagerTestInstance testInstance = new RegistryManagerTestInstance();
Device device = Device.createDevice(testInstance.deviceId, AuthenticationType.SAS);
SymmetricKey symmetricKey = new SymmetricKey();
symmetricKey.setPrimaryKey("1");
symmetricKey.setSecondaryKey("2");
device.setSymmetricKey(symmetricKey);
try {
testInstance.registryManager.addDevice(device);
fail("Adding the device should have failed since an invalid symmetric key was provided");
} catch (IotHubBadFormatException ex) {
// expected throw
}
}
use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.
the class ServiceClientTests method cloudToDeviceTelemetry.
public void cloudToDeviceTelemetry(boolean withProxy, boolean withPayload, boolean withLargestPayload, boolean withCustomSSLContext, boolean withAzureSasCredential) throws Exception {
// We remove and recreate the device for a clean start
RegistryManager registryManager = RegistryManager.createFromConnectionString(iotHubConnectionString, RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
TestDeviceIdentity testDeviceIdentity = Tools.getTestDevice(iotHubConnectionString, IotHubClientProtocol.AMQPS, AuthenticationType.SAS, false);
Device device = testDeviceIdentity.getDevice();
Device deviceGetBefore = registryManager.getDevice(device.getDeviceId());
// Create service client
ProxyOptions proxyOptions = null;
if (withProxy) {
Proxy testProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
proxyOptions = new ProxyOptions(testProxy);
}
SSLContext sslContext = null;
if (withCustomSSLContext) {
sslContext = new IotHubSSLContext().getSSLContext();
}
ServiceClientOptions serviceClientOptions = ServiceClientOptions.builder().proxyOptions(proxyOptions).sslContext(sslContext).build();
ServiceClient serviceClient;
if (withAzureSasCredential) {
serviceClient = buildServiceClientWithAzureSasCredential(testInstance.protocol, serviceClientOptions);
} else {
serviceClient = new ServiceClient(iotHubConnectionString, testInstance.protocol, serviceClientOptions);
}
serviceClient.open();
Message message;
if (withPayload) {
if (withLargestPayload) {
message = new Message(LARGEST_PAYLOAD);
} else {
message = new Message(SMALL_PAYLOAD);
}
} else {
message = new Message();
}
serviceClient.send(device.getDeviceId(), message);
Device deviceGetAfter = registryManager.getDevice(device.getDeviceId());
serviceClient.close();
Tools.disposeTestIdentity(testDeviceIdentity, iotHubConnectionString);
// Assert
assertEquals(buildExceptionMessage("", hostName), deviceGetBefore.getDeviceId(), deviceGetAfter.getDeviceId());
assertEquals(buildExceptionMessage("", hostName), 0, deviceGetBefore.getCloudToDeviceMessageCount());
assertEquals(buildExceptionMessage("", hostName), 1, deviceGetAfter.getCloudToDeviceMessageCount());
registryManager.close();
}
use of com.microsoft.azure.sdk.iot.service.Device in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientTests method updateDigitalTwin.
@Test
@StandardTierHubOnlyTest
public void updateDigitalTwin() throws IOException {
// arrange
String newProperty = "currentTemperature";
String newPropertyPath = "/currentTemperature";
Integer newPropertyValue = 35;
// Property update callback
TwinPropertyCallBack twinPropertyCallBack = (property, context) -> {
Set<Property> properties = new HashSet<>();
properties.add(property);
try {
deviceClient.sendReportedProperties(properties);
} catch (IOException e) {
}
};
// IotHub event callback
IotHubEventCallback iotHubEventCallback = (responseStatus, callbackContext) -> {
};
// start device twin and setup handler for property updates in device
deviceClient.startDeviceTwin(iotHubEventCallback, null, twinPropertyCallBack, null);
Map<Property, Pair<TwinPropertyCallBack, Object>> desiredPropertyUpdateCallback = Collections.singletonMap(new Property(newProperty, null), new Pair<>(twinPropertyCallBack, null));
deviceClient.subscribeToTwinDesiredProperties(desiredPropertyUpdateCallback);
DigitalTwinUpdateRequestOptions optionsWithoutEtag = new DigitalTwinUpdateRequestOptions();
optionsWithoutEtag.setIfMatch("*");
// get digital twin and Etag before update
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class);
DigitalTwinUpdateRequestOptions optionsWithEtag = new DigitalTwinUpdateRequestOptions();
optionsWithEtag.setIfMatch(responseWithHeaders.headers().eTag());
// act
// Add properties at root level - conditional update with max overload
UpdateOperationUtility updateOperationUtility = new UpdateOperationUtility().appendAddPropertyOperation(newPropertyPath, newPropertyValue);
digitalTwinClient.updateDigitalTwinWithResponse(deviceId, updateOperationUtility.getUpdateOperations(), optionsWithEtag);
BasicDigitalTwin digitalTwin = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class).body();
// assert
assertEquals(E2ETestConstants.THERMOSTAT_MODEL_ID, digitalTwin.getMetadata().getModelId());
assertTrue(digitalTwin.getMetadata().getWriteableProperties().containsKey(newProperty));
assertEquals(newPropertyValue, digitalTwin.getMetadata().getWriteableProperties().get(newProperty).getDesiredValue());
}
Aggregations