use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class JobClientTest method queryJobResponseWithoutJobTypeAndJobJobStausSucceeds.
@Test
public void queryJobResponseWithoutJobTypeAndJobJobStausSucceeds(@Mocked Query mockedQuery) throws IotHubException, IOException {
// arrange
final String connectionString = "testString";
JobClient testJobClient = JobClient.createFromConnectionString(connectionString);
new Expectations() {
{
Deencapsulation.newInstance(Query.class, new Class[] { Integer.class, QueryType.class }, anyInt, QueryType.JOB_RESPONSE);
result = mockedQuery;
}
};
// act
testJobClient.queryJobResponse(null, null);
// assert
new Verifications() {
{
mockedQuery.sendQueryRequest((TokenCredentialCache) any, (AzureSasCredential) any, (IotHubConnectionString) any, (URL) any, HttpMethod.GET, anyInt, anyInt, (Proxy) any);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class JobClientTest method scheduleUpdateTwinCreateURL.
/* Tests_SRS_JOBCLIENT_21_009: [The scheduleUpdateTwin shall create a URL for Jobs using the iotHubConnectionString.] */
@Test
public void scheduleUpdateTwinCreateURL() 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"));
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 = 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);
// assert
new Verifications() {
{
IotHubConnectionString.getUrlJobs(anyString, jobId);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class JobClientTest method queryDeviceJobThrowsOnNewQueryThrows.
@Test(expected = IotHubException.class)
public void queryDeviceJobThrowsOnNewQueryThrows(@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;
mockedQuery.sendQueryRequest((TokenCredentialCache) any, (AzureSasCredential) any, (IotHubConnectionString) any, (URL) any, HttpMethod.POST, anyInt, anyInt, (Proxy) any);
result = new IotHubException();
}
};
// act
testJobClient.queryDeviceJob(VALID_SQL_QUERY);
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class JobClientTest method hasNextSucceeds.
// Tests_SRS_JOBCLIENT_25_047: [hasNextJob shall return true if the next job exist, false other wise.]
@Test
public void hasNextSucceeds(@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 = true;
}
};
Query testQuery = testJobClient.queryDeviceJob(VALID_SQL_QUERY);
// act
boolean result = testJobClient.hasNextJob(testQuery);
// assert
new Verifications() {
{
mockedQuery.sendQueryRequest((TokenCredentialCache) any, (AzureSasCredential) any, (IotHubConnectionString) any, (URL) any, HttpMethod.POST, anyInt, anyInt, (Proxy) any);
times = 1;
}
};
assertTrue(result);
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class Query method sendQueryRequest.
/**
* Sends request for the query to the IotHub.
*
* @param credentialCache The RBAC authorization token provider. May be null if azureSasCredential or iotHubConnectionString is not.
* @param azureSasCredential The SAS authorization token provider. May be null if credential or iotHubConnectionString is not.
* @param iotHubConnectionString The iot hub connection string that SAS tokens will be derived from. May be null if azureSasCredential or credential is not.
* @param url URL to Query on.
* @param method HTTP Method for the requesting a query.
* @param httpConnectTimeout the http connect timeout to use for this request.
* @param httpReadTimeout the http read timeout to use for this request.
* @param proxy the proxy to use, or null if no proxy should be used.
* @return QueryResponse object which holds the response Iterator.
* @throws IOException If any of the input parameters are not valid.
* @throws IotHubException If HTTP response other then status ok is received.
*/
public QueryResponse sendQueryRequest(TokenCredentialCache credentialCache, AzureSasCredential azureSasCredential, IotHubConnectionString iotHubConnectionString, URL url, HttpMethod method, int httpConnectTimeout, int httpReadTimeout, Proxy proxy) throws IOException, IotHubException {
this.url = url;
this.httpMethod = method;
this.azureSasCredential = azureSasCredential;
this.credentialCache = credentialCache;
this.iotHubConnectionString = iotHubConnectionString;
this.httpConnectTimeout = httpConnectTimeout;
this.httpReadTimeout = httpReadTimeout;
this.proxy = proxy;
byte[] payload;
Map<String, String> queryHeaders = new HashMap<>();
if (this.requestContinuationToken != null) {
queryHeaders.put(CONTINUATION_TOKEN_KEY, requestContinuationToken);
}
queryHeaders.put(PAGE_SIZE_KEY, String.valueOf(pageSize));
DeviceOperations.setHeaders(queryHeaders);
if (isSqlQuery) {
QueryRequestParser requestParser = new QueryRequestParser(this.query);
payload = requestParser.toJson().getBytes(StandardCharsets.UTF_8);
} else {
payload = new byte[0];
}
String authorizationToken;
if (credentialCache != null) {
authorizationToken = this.credentialCache.getTokenString();
} else if (azureSasCredential != null) {
authorizationToken = azureSasCredential.getSignature();
} else {
authorizationToken = new IotHubServiceSasToken(iotHubConnectionString).toString();
}
HttpResponse httpResponse = DeviceOperations.request(authorizationToken, url, method, payload, null, httpConnectTimeout, httpReadTimeout, proxy);
this.responseContinuationToken = null;
Map<String, String> headers = httpResponse.getHeaderFields();
for (Map.Entry<String, String> header : headers.entrySet()) {
switch(header.getKey()) {
case CONTINUATION_TOKEN_KEY:
this.responseContinuationToken = header.getValue();
break;
case ITEM_TYPE_KEY:
this.responseQueryType = QueryType.fromString(header.getValue());
break;
default:
break;
}
}
if (this.responseQueryType == null || this.responseQueryType == QueryType.UNKNOWN) {
throw new IotHubException("Query response type is not defined by IotHub");
}
if (this.requestQueryType != this.responseQueryType) {
throw new IotHubException("Query response does not match query request");
}
this.queryResponse = new QueryResponse(new String(httpResponse.getBody(), StandardCharsets.UTF_8));
return this.queryResponse;
}
Aggregations