Search in sources :

Example 71 with IotHubConnectionString

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;
        }
    };
}
Also used : Expectations(mockit.Expectations) NonStrictExpectations(mockit.NonStrictExpectations) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Verifications(mockit.Verifications) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Test(org.junit.Test)

Example 72 with IotHubConnectionString

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;
        }
    };
}
Also used : TwinState(com.microsoft.azure.sdk.iot.deps.twin.TwinState) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) JobsParser(com.microsoft.azure.sdk.iot.deps.serializer.JobsParser) JobResult(com.microsoft.azure.sdk.iot.service.jobs.JobResult) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Verifications(mockit.Verifications) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) Date(java.util.Date) NonStrictExpectations(mockit.NonStrictExpectations) Pair(com.microsoft.azure.sdk.iot.service.devicetwin.Pair) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 73 with IotHubConnectionString

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);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) NonStrictExpectations(mockit.NonStrictExpectations) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Test(org.junit.Test)

Example 74 with IotHubConnectionString

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);
}
Also used : SqlQuery(com.microsoft.azure.sdk.iot.service.devicetwin.SqlQuery) Query(com.microsoft.azure.sdk.iot.service.devicetwin.Query) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Verifications(mockit.Verifications) JobClient(com.microsoft.azure.sdk.iot.service.jobs.JobClient) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 75 with IotHubConnectionString

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;
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) QueryRequestParser(com.microsoft.azure.sdk.iot.deps.serializer.QueryRequestParser) HashMap(java.util.HashMap) Map(java.util.Map) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Aggregations

IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)196 Test (org.junit.Test)171 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)30 Expectations (mockit.Expectations)21 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)20 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)17 AzureSasCredential (com.azure.core.credential.AzureSasCredential)16 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)13 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)13 Device (com.microsoft.azure.sdk.iot.service.Device)12 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)12 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)12 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)11 URL (java.net.URL)11 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)11 NonStrictExpectations (mockit.NonStrictExpectations)10 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)9 ArrayList (java.util.ArrayList)9 Verifications (mockit.Verifications)9 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)8