use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method nextThrowsIfNonStringRetrieved.
// Tests_SRS_DEVICETWIN_25_060: [ If the next element from the query response is an object other than String, then this method shall throw IOException ]
@Test(expected = IOException.class)
public void nextThrowsIfNonStringRetrieved(@Mocked DeviceTwinDevice mockedDevice) throws IotHubException, IOException {
// arrange
final String connectionString = "testString";
constructorExpectations(connectionString);
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
new NonStrictExpectations() {
{
Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.TWIN);
result = mockedQuery;
Deencapsulation.invoke(mockedQuery, "hasNext");
result = true;
Deencapsulation.invoke(mockedQuery, "next");
result = 5;
}
};
Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
// act
DeviceTwinDevice result = testTwin.getNextDeviceTwin(testQuery);
// assert
new Verifications() {
{
Deencapsulation.invoke(mockedQuery, "continueQuery", new Class[] { String.class }, anyString);
times = 0;
Deencapsulation.invoke(mockedQuery, "sendQueryRequest", new Class[] { IotHubConnectionString.class, URL.class, HttpMethod.class, Long.class }, any, any, HttpMethod.POST, any);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method nextThrowsIfNoNewElements.
// Tests_SRS_DEVICETWIN_25_058: [ The method shall check if hasNext returns true and throw NoSuchElementException otherwise ]
@Test(expected = NoSuchElementException.class)
public void nextThrowsIfNoNewElements(@Mocked DeviceTwinDevice mockedDevice) throws IotHubException, IOException {
// arrange
final String connectionString = "testString";
constructorExpectations(connectionString);
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
new NonStrictExpectations() {
{
Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.TWIN);
result = mockedQuery;
Deencapsulation.invoke(mockedQuery, "next");
result = new NoSuchElementException();
}
};
Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
// act
DeviceTwinDevice result = testTwin.getNextDeviceTwin(testQuery);
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method nextRetrievesCorrectlyWithoutModuleId.
// Tests_SRS_DEVICETWIN_25_059: [ The method shall parse the next element from the query response as Twin Document using TwinState and provide the response on DeviceTwinDevice.]
@Test
public void nextRetrievesCorrectlyWithoutModuleId() throws IotHubException, IOException {
// arrange
final Integer version = 15;
final String etag = "validEtag";
final String connectionString = "testString";
final int connectTimeout = 1234;
final int readTimeout = 5678;
constructorExpectations(connectionString);
DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString, DeviceTwinClientOptions.builder().httpConnectTimeout(connectTimeout).httpReadTimeout(readTimeout).build());
final String expectedString = "testJsonAsNext";
final String modelId = "testModelId";
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 = null;
mockedTwinState.getVersion();
result = version;
mockedTwinState.getETag();
result = etag;
mockedTwinState.getModelId();
result = modelId;
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);
// assert
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.getModuleId());
assertEquals(result.getModelId(), result.getModelId());
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class JobClientTest method nextThrowsIfNoNewElements.
@Test(expected = NoSuchElementException.class)
public void nextThrowsIfNoNewElements(@Mocked Query mockedQuery) throws IotHubException, IOException {
// arrange
final String connectionString = "testString";
JobClient testJobClient = JobClient.createFromConnectionString(connectionString);
new NonStrictExpectations() {
{
Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.DEVICE_JOB);
result = mockedQuery;
Deencapsulation.invoke(mockedQuery, "hasNext");
result = false;
Deencapsulation.invoke(mockedQuery, "next");
result = new NoSuchElementException();
}
};
Query testQuery = testJobClient.queryDeviceJob(VALID_SQL_QUERY);
// act
testJobClient.getNextJob(testQuery);
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class JobClientTest method hasNextThrowsIfQueryHasNextThrows.
@Test(expected = IotHubException.class)
public void hasNextThrowsIfQueryHasNextThrows(@Mocked Query mockedQuery) throws IotHubException, IOException {
// arrange
final String connectionString = "testString";
JobClient testJobClient = JobClient.createFromConnectionString(connectionString);
new NonStrictExpectations() {
{
Deencapsulation.newInstance(Query.class, new Class[] { String.class, Integer.class, QueryType.class }, anyString, anyInt, QueryType.DEVICE_JOB);
result = mockedQuery;
Deencapsulation.invoke(mockedQuery, "hasNext");
result = new IotHubException();
}
};
Query testQuery = testJobClient.queryDeviceJob(VALID_SQL_QUERY);
// act
boolean result = testJobClient.hasNextJob(testQuery);
}
Aggregations