use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin in project azure-iot-sdk-java by Azure.
the class DeviceTwinSample method main.
/**
* @param args
* @throws IOException
* @throws URISyntaxException
*/
public static void main(String[] args) throws Exception {
System.out.println("Starting sample...");
DeviceTwin twinClient = DeviceTwin.createFromConnectionString(iotHubConnectionString);
DeviceTwinDevice device = new DeviceTwinDevice(deviceId);
try {
// Manage complete twin
System.out.println("Getting device twin");
twinClient.getTwin(device);
System.out.println(device);
//Update Twin Tags and Desired Properties
Set<Pair> tags = new HashSet<Pair>();
tags.add(new Pair("HomeID", UUID.randomUUID()));
device.setTags(tags);
Set<Pair> desProp = new HashSet<Pair>();
int temp = new Random().nextInt(100);
desProp.add(new Pair("temp", temp));
device.setDesiredProperties(desProp);
System.out.println("Updating device twin");
twinClient.updateTwin(device);
System.out.println("Getting device twin");
twinClient.getTwin(device);
System.out.println(device);
} catch (IotHubException e) {
System.out.println(e.getMessage());
}
System.out.println("Shutting down sample...");
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method constructorThrowsOnImproperCS.
@Test(expected = IllegalArgumentException.class)
public void constructorThrowsOnImproperCS() throws Exception {
//arrange
final String connectionString = "ImproperCSFormat";
new NonStrictExpectations() {
{
IotHubConnectionStringBuilder.createConnectionString(connectionString);
result = new IllegalArgumentException();
}
};
//act
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method updateTwinThrowsVerificationThrows.
@Test(expected = IotHubException.class)
public void updateTwinThrowsVerificationThrows() throws Exception {
//arrange
final String connectionString = "testString";
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
Map<String, Object> testMap = new HashMap<>();
testMap.put("TestKey", "TestValue");
new NonStrictExpectations() {
{
mockedDevice.getDeviceId();
result = "SomeDevID";
Deencapsulation.invoke(mockedDevice, "getDesiredMap");
result = testMap;
Deencapsulation.invoke(mockedDevice, "getTagsMap");
result = testMap;
Deencapsulation.invoke(mockedDevice, "getTwinParser");
result = mockedTwinParser;
mockedTwinParser.updateTwin((Map<String, Object>) any, null, (Map<String, Object>) any);
result = "SomeJsonString";
IotHubExceptionManager.httpResponseVerification(mockedHttpResponse);
result = new IotHubException();
}
};
//act
testTwin.updateTwin(mockedDevice);
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method constructorCreatesTwin.
/*
**Tests_SRS_DEVICETWIN_25_002: [** The constructor shall create an IotHubConnectionStringBuilder object from the given connection string **]**
**Tests_SRS_DEVICETWIN_25_003: [** The constructor shall create a new DeviceTwin instance and return it **]**
*/
@Test
public void constructorCreatesTwin(@Mocked IotHubConnectionStringBuilder mockedConnectionStringBuilder) throws Exception {
//arrange
final String connectionString = "testString";
//act
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
//assert
assertNotNull(testTwin);
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method getTwinThrowsOnNullDeviceID.
@Test(expected = IllegalArgumentException.class)
public void getTwinThrowsOnNullDeviceID() throws Exception {
//arrange
final String connectionString = "testString";
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
new NonStrictExpectations() {
{
mockedDevice.getDeviceId();
result = null;
}
};
//act
testTwin.getTwin(mockedDevice);
}
Aggregations