use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method nextThrowsOnNullQuery.
@Test(expected = IllegalArgumentException.class)
public void nextThrowsOnNullQuery(@Mocked DeviceTwinDevice mockedDevice) throws IotHubException, IOException {
// arrange
final String connectionString = "testString";
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;
}
};
Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
// act
testTwin.getNextDeviceTwin(null);
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method nextThrowsOnQueryNextThrows.
@Test(expected = IotHubException.class)
public void nextThrowsOnQueryNextThrows(@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 IotHubException();
}
};
Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
// act
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 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.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method hasNextThrowsIfHasNextOnQueryThrows.
@Test(expected = IotHubException.class)
public void hasNextThrowsIfHasNextOnQueryThrows(@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 = new IotHubException();
}
};
Query testQuery = testTwin.queryTwin(VALID_SQL_QUERY);
// act
boolean result = testTwin.hasNextDeviceTwin(testQuery);
}
use of com.microsoft.azure.sdk.iot.service.devicetwin.Query in project azure-iot-sdk-java by Azure.
the class QueryTest method sendQueryRequestSucceeds.
// Tests_SRS_QUERY_25_008: [The method shall obtain the serilaized query by using QueryRequestParser.]
// Tests_SRS_QUERY_25_009: [The method shall use the provided HTTP Method and send request to IotHub with the serialized body over the provided URL.]
// Tests_SRS_QUERY_25_013: [The method shall create a QueryResponse object with the contents from the response body and save it.]
// Tests_SRS_QUERY_25_020: [This method shall save all the parameters for future use.]
@Test
public void sendQueryRequestSucceeds() throws IotHubException, IOException {
// arrange
final String testToken = UUID.randomUUID().toString();
final Map<String, String> testHeaderResponseMap = new HashMap<>();
testHeaderResponseMap.put("x-ms-continuation", testToken);
testHeaderResponseMap.put("x-ms-item-type", DEFAULT_QUERY_TYPE.getValue());
Query testQuery = Deencapsulation.newInstance(Query.class, DEFAULT_QUERY, DEFAULT_PAGE_SIZE, DEFAULT_QUERY_TYPE);
new NonStrictExpectations() {
{
mockHttpResponse.getHeaderFields();
result = testHeaderResponseMap;
}
};
// act
Deencapsulation.invoke(testQuery, "sendQueryRequest", mockIotHubConnectionString, mockUrl, mockHttpMethod, (long) 0);
// assert
new Verifications() {
{
new HttpRequest(mockUrl, mockHttpMethod, (byte[]) any);
times = 1;
mockHttpRequest.setHeaderField("x-ms-max-item-count", String.valueOf(DEFAULT_PAGE_SIZE));
times = 1;
mockHttpRequest.setHeaderField("x-ms-continuation", anyString);
times = 0;
mockHttpRequest.setHeaderField(anyString, anyString);
minTimes = 5;
}
};
assertEquals(testToken, Deencapsulation.getField(testQuery, "responseContinuationToken"));
assertEquals(DEFAULT_QUERY_TYPE, Deencapsulation.getField(testQuery, "responseQueryType"));
}
Aggregations