use of com.microsoft.azure.sdk.iot.deps.twin.TwinState in project azure-iot-sdk-java by Azure.
the class DeviceTwin method replaceTwin.
/**
* Replace the full device twin for a given device with the provided device twin.
*
* @param device The device twin object to replace the current device twin object.
* @throws IotHubException If any an IoT hub level exception is thrown. For instance,
* if the request is unauthorized, a exception that extends IotHubException will be thrown.
* @throws IOException If the request failed to send to IoT hub.
* @return The Twin object's current state returned from the service after the replace operation.
*/
public DeviceTwinDevice replaceTwin(DeviceTwinDevice device) throws IotHubException, IOException {
if (device == null || device.getDeviceId() == null || device.getDeviceId().length() == 0) {
throw new IllegalArgumentException("Instantiate a device and set device Id to be used.");
}
URL url;
if ((device.getModuleId() == null) || device.getModuleId().length() == 0) {
url = this.iotHubConnectionString.getUrlTwin(device.getDeviceId());
} else {
url = this.iotHubConnectionString.getUrlModuleTwin(device.getDeviceId(), device.getModuleId());
}
TwinState twinState = new TwinState(device.getTagsMap(), device.getDesiredMap(), null);
String twinJson = twinState.toJsonElement().toString();
Proxy proxy = options.getProxyOptions() == null ? null : options.getProxyOptions().getProxy();
HttpResponse httpResponse = DeviceOperations.request(this.iotHubConnectionString, url, HttpMethod.PUT, twinJson.getBytes(StandardCharsets.UTF_8), String.valueOf(requestId++), options.getHttpConnectTimeout(), options.getHttpReadTimeout(), proxy);
String responseTwinJson = new String(httpResponse.getBody(), StandardCharsets.UTF_8);
twinState = TwinState.createFromTwinJson(responseTwinJson);
DeviceTwinDevice responseTwin;
if (twinState.getModuleId() == null) {
responseTwin = new DeviceTwinDevice(twinState.getDeviceId());
} else {
responseTwin = new DeviceTwinDevice(twinState.getDeviceId(), twinState.getModuleId());
}
responseTwin.setVersion(twinState.getVersion());
responseTwin.setModelId(twinState.getModelId());
responseTwin.setETag(twinState.getETag());
responseTwin.setTags(twinState.getTags());
responseTwin.setDesiredProperties(twinState.getDesiredProperty());
responseTwin.setReportedProperties(twinState.getReportedProperty());
responseTwin.setCapabilities(twinState.getCapabilities());
responseTwin.setConfigurations(twinState.getConfigurations());
responseTwin.setConnectionState(twinState.getConnectionState());
return responseTwin;
}
use of com.microsoft.azure.sdk.iot.deps.twin.TwinState in project azure-iot-sdk-java by Azure.
the class DeviceTwin method jsonToDeviceTwinDevice.
private DeviceTwinDevice jsonToDeviceTwinDevice(String json) {
TwinState twinState = TwinState.createFromTwinJson(json);
DeviceTwinDevice deviceTwinDevice = new DeviceTwinDevice(twinState.getDeviceId());
deviceTwinDevice.setVersion(twinState.getVersion());
deviceTwinDevice.setETag(twinState.getETag());
deviceTwinDevice.setTags(twinState.getTags());
deviceTwinDevice.setDesiredProperties(twinState.getDesiredProperty());
deviceTwinDevice.setReportedProperties(twinState.getReportedProperty());
deviceTwinDevice.setCapabilities(twinState.getCapabilities());
deviceTwinDevice.setConnectionState(twinState.getConnectionState());
deviceTwinDevice.setConfigurations(twinState.getConfigurations());
deviceTwinDevice.setModelId(twinState.getModelId());
deviceTwinDevice.setDeviceScope(twinState.getDeviceScope());
deviceTwinDevice.setParentScopes(twinState.getParentScopes());
if (twinState.getModuleId() != null && !twinState.getModuleId().isEmpty()) {
deviceTwinDevice.setModuleId(twinState.getModuleId());
}
return deviceTwinDevice;
}
use of com.microsoft.azure.sdk.iot.deps.twin.TwinState in project azure-iot-sdk-java by Azure.
the class JobResultTest method constructorParseJson.
/* Tests_SRS_JOBRESULT_21_002: [The constructor shall parse the body using the JobsResponseParser.] */
@Test
public void constructorParseJson() throws IOException {
// arrange
final String json = "validJson";
TwinCollection tags = new TwinCollection();
tags.putFinal("tag1", "val1");
TwinState twinState = new TwinState(tags, null, null);
twinState.setDeviceId(DEVICE_ID);
twinState.setETag(ETAG);
JobsResponseParserExpectations(json, twinState, null, new Date(), null, "scheduleUpdateTwin");
// act
JobResult jobResult = Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, json.getBytes(StandardCharsets.UTF_8));
// assert
new Verifications() {
{
JobsResponseParser.createFromJson(json);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.deps.twin.TwinState in project azure-iot-sdk-java by Azure.
the class JobClientTest method scheduleUpdateThrowsOnInvalidJobId.
/* Tests_SRS_JOBCLIENT_21_005: [If the JobId is null, empty, or invalid, the scheduleUpdateTwin shall throws IllegalArgumentException.] */
@Test(expected = IllegalArgumentException.class)
public void scheduleUpdateThrowsOnInvalidJobId() throws IOException, IotHubException {
// arrange
final String connectionString = "testString";
final String jobId = "invalidJobId";
final String deviceId = "validDeviceId";
final String queryCondition = "validQueryCondition";
final DeviceTwinDevice updateTwin = mockedDeviceTwinDevice;
final Date startTimeUtc = new Date();
final long maxExecutionTimeInSeconds = 10;
Set<Pair> testTags = new HashSet<>();
testTags.add(new Pair("testTag", "tagObject"));
final String json = "validJson";
JobClient testJobClient = null;
new NonStrictExpectations() {
{
IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
result = mockedIotHubConnectionString;
mockedDeviceTwinDevice.getTags();
result = testTags;
mockedDeviceTwinDevice.getDesiredProperties();
result = null;
mockedDeviceTwinDevice.getReportedProperties();
result = null;
new TwinState((TwinCollection) any, null, null);
result = mockedTwinState;
mockedDeviceTwinDevice.getDeviceId();
result = deviceId;
mockedDeviceTwinDevice.getETag();
result = null;
new JobsParser(jobId, mockedTwinState, queryCondition, startTimeUtc, maxExecutionTimeInSeconds);
result = mockedJobsParser;
mockedJobsParser.toJson();
result = json;
IotHubConnectionString.getUrlJobs(anyString, jobId);
result = new MalformedURLException();
}
};
try {
testJobClient = JobClient.createFromConnectionString(connectionString);
} catch (IllegalArgumentException e) {
assertTrue("Test did not run because createFromConnectionString failed to create new instance of the JobClient", true);
}
// act
testJobClient.scheduleUpdateTwin(jobId, queryCondition, updateTwin, startTimeUtc, maxExecutionTimeInSeconds);
}
use of com.microsoft.azure.sdk.iot.deps.twin.TwinState in project azure-iot-sdk-java by Azure.
the class JobClientTest method scheduleUpdateTwinCreateJsonWithProperties.
/* Tests_SRS_JOBCLIENT_21_004: [The scheduleUpdateTwin shall create a json String that represent the twin job using the JobsParser class.] */
@Test
public void scheduleUpdateTwinCreateJsonWithProperties() throws IOException, IotHubException {
// arrange
final String connectionString = "testString";
final String jobId = "validJobId";
final String deviceId = "validDeviceId";
final String queryCondition = "validQueryCondition";
final DeviceTwinDevice updateTwin = mockedDeviceTwinDevice;
final Date startTimeUtc = new Date();
final long maxExecutionTimeInSeconds = 10;
final String json = "validJson";
Set<Pair> testTags = new HashSet<>();
testTags.add(new Pair("testTag", "tagObject"));
Set<Pair> testDesired = new HashSet<>();
testTags.add(new Pair("testDesired", "val1"));
Set<Pair> testResponse = new HashSet<>();
testTags.add(new Pair("testResponse", "val2"));
new Expectations() {
{
IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
result = mockedIotHubConnectionString;
mockedDeviceTwinDevice.getTags();
result = testTags;
mockedDeviceTwinDevice.getDesiredProperties();
result = testDesired;
mockedDeviceTwinDevice.getReportedProperties();
result = testResponse;
new TwinState((TwinCollection) any, (TwinCollection) any, (TwinCollection) any);
result = mockedTwinState;
mockedDeviceTwinDevice.getDeviceId();
result = deviceId;
mockedDeviceTwinDevice.getETag();
result = "1234";
new JobsParser(jobId, mockedTwinState, queryCondition, startTimeUtc, maxExecutionTimeInSeconds);
result = mockedJobsParser;
mockedJobsParser.toJson();
result = json;
IotHubConnectionString.getUrlJobs(anyString, jobId);
result = mockedURL;
DeviceOperations.request(anyString, mockedURL, HttpMethod.PUT, json.getBytes(StandardCharsets.UTF_8), (String) any, anyInt, anyInt, (Proxy) any);
result = mockedHttpResponse;
Deencapsulation.newInstance(JobResult.class, new Class[] { byte[].class }, (byte[]) any);
result = mockedJobResult;
}
};
JobClient testJobClient = JobClient.createFromConnectionString(connectionString);
// act
JobResult jobResult = testJobClient.scheduleUpdateTwin(jobId, queryCondition, updateTwin, startTimeUtc, maxExecutionTimeInSeconds);
}
Aggregations