use of com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin in project azure-iot-sdk-java by Azure.
the class TokenCredentialTests method getDigitalTwinWithTokenCredential.
@Test
public void getDigitalTwinWithTokenCredential() throws IOException, IotHubException, URISyntaxException {
// only run tests for standard tier hubs
Assume.assumeFalse(isBasicTierHub);
RegistryManager registryManager = new RegistryManager(iotHubConnectionString);
DeviceClient deviceClient = createDeviceClient(MQTT, registryManager);
deviceClient.open();
// arrange
DigitalTwinClient digitalTwinClient = buildDigitalTwinClientWithTokenCredential();
// act
BasicDigitalTwin response = digitalTwinClient.getDigitalTwin(deviceClient.getConfig().getDeviceId(), BasicDigitalTwin.class);
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceClient.getConfig().getDeviceId(), BasicDigitalTwin.class);
// assert
assertEquals(response.getMetadata().getModelId(), THERMOSTAT_MODEL_ID);
assertEquals(responseWithHeaders.body().getMetadata().getModelId(), THERMOSTAT_MODEL_ID);
}
use of com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientComponentTests method updateDigitalTwin.
@Test
@StandardTierHubOnlyTest
public void updateDigitalTwin() {
// arrange
String newProperty = "currentTemperature";
Integer newPropertyValue = 35;
String newComponent = "newThermostat";
String newComponentPath = "/newThermostat";
Map<String, Object> componentProperties = new HashMap<>();
componentProperties.put(newProperty, newPropertyValue);
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 component at root level - conditional update with max overload
UpdateOperationUtility updateOperationUtility = new UpdateOperationUtility().appendAddComponentOperation(newComponentPath, componentProperties);
digitalTwinClient.updateDigitalTwinWithResponse(deviceId, updateOperationUtility.getUpdateOperations(), optionsWithEtag);
BasicDigitalTwin digitalTwin = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class).body();
// assert
assertEquals(E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID, digitalTwin.getMetadata().getModelId());
assertTrue(digitalTwin.getCustomProperties().containsKey(newComponent));
}
use of com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientTests method getMultiplexedDigitalTwinsRegisteredBeforeOpen.
// Open a multiplexed connection with two devices, each with a different model Id. Verify that their reported twin has
// the expected model Ids.
@Test
@StandardTierHubOnlyTest
public void getMultiplexedDigitalTwinsRegisteredBeforeOpen() throws IotHubException, IOException, URISyntaxException, MultiplexingClientException, InterruptedException {
if (protocol == MQTT || protocol == MQTT_WS) {
// multiplexing isn't supported over MQTT, so it can't be tested
return;
}
MultiplexingClient multiplexingClient = new MultiplexingClient(deviceClient.getConfig().getIotHubHostname(), protocol);
List<DeviceClient> multiplexedDeviceClients = new ArrayList<>();
multiplexedDeviceClients.add(createDeviceClient(protocol, E2ETestConstants.THERMOSTAT_MODEL_ID));
multiplexedDeviceClients.add(createDeviceClient(protocol, E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID));
// register the devices before the multiplexing client has been opened
multiplexingClient.registerDeviceClients(multiplexedDeviceClients);
multiplexingClient.open();
// act
String thermostatDeviceId = multiplexedDeviceClients.get(0).getConfig().getDeviceId();
BasicDigitalTwin thermostatResponse = digitalTwinClient.getDigitalTwin(thermostatDeviceId, BasicDigitalTwin.class);
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> thermostatResponseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(thermostatDeviceId, BasicDigitalTwin.class);
String temperatureControllerDeviceId = multiplexedDeviceClients.get(1).getConfig().getDeviceId();
BasicDigitalTwin temperatureControllerResponse = digitalTwinClient.getDigitalTwin(temperatureControllerDeviceId, BasicDigitalTwin.class);
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> temperatureControllerResponseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(temperatureControllerDeviceId, BasicDigitalTwin.class);
// assert
assertEquals(thermostatResponse.getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
assertEquals(thermostatResponseWithHeaders.body().getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
assertEquals(temperatureControllerResponse.getMetadata().getModelId(), E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID);
assertEquals(temperatureControllerResponseWithHeaders.body().getMetadata().getModelId(), E2ETestConstants.TEMPERATURE_CONTROLLER_MODEL_ID);
}
use of com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientTests method getDigitalTwinWithAzureSasCredential.
@Test
@StandardTierHubOnlyTest
public void getDigitalTwinWithAzureSasCredential() {
if (protocol != MQTT) {
// This test is for the service client, so no need to rerun it for all the different device protocols
return;
}
// arrange
digitalTwinClient = buildDigitalTwinClientWithAzureSasCredential();
// act
BasicDigitalTwin response = digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
ServiceResponseWithHeaders<BasicDigitalTwin, DigitalTwinGetHeaders> responseWithHeaders = digitalTwinClient.getDigitalTwinWithResponse(deviceId, BasicDigitalTwin.class);
// assert
assertEquals(response.getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
assertEquals(responseWithHeaders.body().getMetadata().getModelId(), E2ETestConstants.THERMOSTAT_MODEL_ID);
}
use of com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin 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