use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method updateTwinDoesNotThrowsIfOnlyTagsHasValue.
@Test
public void updateTwinDoesNotThrowsIfOnlyTagsHasValue(@Mocked DeviceTwinDevice mockedDevice) throws Exception {
// arrange
final String connectionString = "testString";
constructorExpectations(connectionString);
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
TwinCollection testMap = new TwinCollection();
testMap.putFinal("TestKey", "TestValue");
new NonStrictExpectations() {
{
mockedDevice.getDeviceId();
result = "SomeDevID";
Deencapsulation.invoke(mockedDevice, "getDesiredMap");
result = null;
Deencapsulation.invoke(mockedDevice, "getTagsMap");
result = testMap;
new TwinState((TwinCollection) any, null, null);
result = mockedTwinState;
mockedTwinState.toJsonElement().toString();
result = "SomeJsonString";
}
};
// act
testTwin.updateTwin(mockedDevice);
// assert
new Verifications() {
{
IotHubConnectionString.getUrlTwin(anyString, anyString);
times = 1;
mockedHttpRequest.setReadTimeoutMillis(anyInt);
times = 1;
mockedHttpRequest.setHeaderField(anyString, anyString);
times = 6;
mockedHttpRequest.send();
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method nextRetrievesCorrectlyWithoutModelId.
@Test
public void nextRetrievesCorrectlyWithoutModelId() throws IotHubException, IOException {
// arrange
final Integer version = 15;
final String etag = "validEtag";
final int connectTimeout = 1234;
final int readTimeout = 5678;
final String connectionString = "someConnectionString";
constructorExpectations(connectionString);
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString, DeviceTwinClientOptions.builder().httpConnectTimeout(connectTimeout).httpReadTimeout(readTimeout).build());
final String expectedString = "testJsonAsNext";
final String moduleId = "testModuleId";
TwinCollection tags = new TwinCollection();
tags.putFinal("tagsKey", "tagsValue");
TwinCollection rp = new TwinCollection();
rp.putFinal("rpKey", "rpValue");
TwinCollection dp = new TwinCollection();
dp.putFinal("dpKey", "dpValue");
new Expectations() {
{
Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.TWIN);
result = mockedQuery;
Deencapsulation.invoke(mockedQuery, "next");
result = expectedString;
mockedTwinState.getDeviceId();
result = "testDeviceID";
mockedTwinState.getModuleId();
result = moduleId;
mockedTwinState.getVersion();
result = version;
mockedTwinState.getETag();
result = etag;
mockedTwinState.getModelId();
result = null;
mockedTwinState.getTags();
result = tags;
mockedTwinState.getDesiredProperty();
result = dp;
mockedTwinState.getReportedProperty();
result = rp;
mockCapabilities.isIotEdge();
result = Boolean.TRUE;
}
};
Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
// act
DeviceTwinDevice result = testTwin.getNextDeviceTwin(testQuery);
assertNotNull(result.getTags());
assertNotNull(result.getReportedProperties());
assertNotNull(result.getDesiredProperties());
assertEquals(version, result.getVersion());
assertEquals(etag, result.getETag());
assertEqualSetAndMap(result.getTags(), (Map) tags);
assertEqualSetAndMap(result.getDesiredProperties(), (Map) dp);
assertEqualSetAndMap(result.getReportedProperties(), (Map) rp);
assertTrue(result.getCapabilities().isIotEdge());
assertNull(result.getModelId());
assertEquals(result.getModuleId(), result.getModuleId());
}
use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method updateTwinThrowsVerificationThrows.
@Test(expected = IotHubException.class)
public void updateTwinThrowsVerificationThrows(@Mocked DeviceTwinDevice mockedDevice) throws Exception {
// arrange
final String connectionString = "testString";
constructorExpectations(connectionString);
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
TwinCollection testMap = new TwinCollection();
testMap.putFinal("TestKey", "TestValue");
new NonStrictExpectations() {
{
mockedDevice.getDeviceId();
result = "SomeDevID";
Deencapsulation.invoke(mockedDevice, "getDesiredMap");
result = testMap;
Deencapsulation.invoke(mockedDevice, "getTagsMap");
result = testMap;
new TwinState((TwinCollection) any, (TwinCollection) any, null);
result = mockedTwinState;
mockedTwinState.toJsonElement().toString();
result = "SomeJsonString";
IotHubExceptionManager.httpResponseVerification(mockedHttpResponse);
result = new IotHubException();
}
};
// act
testTwin.updateTwin(mockedDevice);
}
use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method getTwinSucceeds.
@Test
public void getTwinSucceeds(@Mocked DeviceTwinDevice mockedDevice) throws Exception {
// arrange
final String connectionString = "testString";
constructorExpectations(connectionString);
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
TwinCollection testMap = new TwinCollection();
new NonStrictExpectations() {
{
mockedDevice.getDeviceId();
result = "SomeDevID";
mockedDevice.getModuleId();
result = "SomeModuleID";
TwinState.createFromTwinJson((String) any);
result = mockedTwinState;
mockedTwinState.getCapabilities();
result = mockCapabilities;
mockedTwinState.getConfigurations();
result = mockConfigurations;
}
};
// act
testTwin.getTwin(mockedDevice);
// assert
new Verifications() {
{
mockedHttpRequest.setReadTimeoutMillis(anyInt);
times = 1;
mockedHttpRequest.setHeaderField(anyString, anyString);
times = 6;
mockedHttpRequest.send();
times = 1;
TwinState.createFromTwinJson((String) any);
times = 1;
mockedTwinState.getETag();
times = 1;
Deencapsulation.invoke(mockedDevice, "setETag", anyString);
times = 1;
mockedTwinState.getTags();
times = 1;
Deencapsulation.invoke(mockedDevice, "setTags", testMap);
times = 1;
mockedTwinState.getDesiredProperty();
times = 1;
Deencapsulation.invoke(mockedDevice, "setDesiredProperties", testMap);
times = 1;
mockedTwinState.getReportedProperty();
times = 1;
Deencapsulation.invoke(mockedDevice, "setReportedProperties", testMap);
times = 1;
Deencapsulation.invoke(mockedDevice, "setCapabilities", mockCapabilities);
times = 1;
Deencapsulation.invoke(mockedDevice, "setConfigurations", mockConfigurations);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.deps.twin.TwinCollection in project azure-iot-sdk-java by Azure.
the class ExportImportDeviceParserTest method toJsonForTaggedDevice.
@Test
public void toJsonForTaggedDevice() throws NoSuchAlgorithmException {
// arrange
ExportImportDeviceParser parser = new ExportImportDeviceParser();
parser.setAuthentication(new AuthenticationParser());
parser.getAuthenticationFinal().setType(AuthenticationTypeParser.SAS);
parser.getAuthenticationFinal().setSymmetricKey(new SymmetricKeyParser("", ""));
TwinCollection tags = new TwinCollection();
tags.putFinal("test01", "firstvalue");
tags.putFinal("test02", "secondvalue");
parser.setTags(tags);
// String expectedJson = "{\"authentication\":{\"symmetricKey\":{\"primaryKey\":\"\",\"secondaryKey\":\"\"},\"type\":\"" + SAS_JSON_VALUE + "\"},\"tags\":{\"test01\":\"firstvalue\",\"test02\":\"secondvalue\"}}";
// act
String serializedDevice = parser.toJson();
// assert
assertTrue(serializedDevice.contains("\"tags\":{"));
}
Aggregations