Search in sources :

Example 16 with HttpResponse

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class IotHubExceptionManagerTest method httpResponseVerification_502.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_008: [The function shall throw IotHubBadGatewayException if the Http response status equal 502]
// Assert
@Test(expected = IotHubBadGatewayException.class)
public void httpResponseVerification_502() throws IotHubException {
    // Arrange
    final int status = 502;
    final byte[] body = { 1 };
    final Map<String, List<String>> headerFields = new HashMap<>();
    final byte[] errorReason = { 2, 3, 4, 5 };
    HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
    // Act
    IotHubExceptionManager.httpResponseVerification(response);
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) Test(org.junit.Test)

Example 17 with HttpResponse

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class IotHubExceptionManagerTest method httpResponseVerification_400_withNULLErrorReason.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_013: [If the httpresponse contains a reason message, the function must print this reason in the error message]
// Assert
@Test
public void httpResponseVerification_400_withNULLErrorReason() throws IotHubException {
    // Arrange
    final int status = 400;
    final byte[] body = { 1 };
    final Map<String, List<String>> headerFields = new HashMap<>();
    final byte[] errorReason = "{\"ExceptionMessage\":null}".getBytes();
    HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
    // Act
    try {
        IotHubExceptionManager.httpResponseVerification(response);
        assert true;
    } catch (IotHubBadFormatException expected) {
        // Expected throw.
        assertThat(expected.getMessage(), is("Bad message format!"));
    }
}
Also used : IotHubBadFormatException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) Test(org.junit.Test)

Example 18 with HttpResponse

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class DeviceTwin method getTwin.

/**
     * This method retrieves device twin for the specified device.
     *
     * @param device The device with a valid id for which device twin is to be retrieved.
     * @throws IOException This exception is thrown if the IO operation failed
     * @throws IotHubException This exception is thrown if the response verification failed
     */
public void getTwin(DeviceTwinDevice device) throws IotHubException, IOException {
    if (device == null || device.getDeviceId() == null || device.getDeviceId().length() == 0) {
        /*
            **Codes_SRS_DEVICETWIN_25_004: [** The function shall throw IllegalArgumentException if the input device is null or if deviceId is null or empty **]**
             */
        throw new IllegalArgumentException("Instantiate a device and set device id to be used");
    }
    /*
        **Codes_SRS_DEVICETWIN_25_005: [** The function shall build the URL for this operation by calling getUrlTwin **]**
         */
    URL url = this.iotHubConnectionString.getUrlTwin(device.getDeviceId());
    /*
        **Codes_SRS_DEVICETWIN_25_006: [** The function shall create a new SAS token **]**
        **Codes_SRS_DEVICETWIN_25_007: [** The function shall create a new HttpRequest with http method as Get **]**
        **Codes_SRS_DEVICETWIN_25_008: [** The function shall set the following HTTP headers specified in the IotHub DeviceTwin doc.
                                                1. Key as authorization with value as sastoken
                                                2. Key as request id with a new string value for every request
                                                3. Key as User-Agent with value specified by the clientIdentifier and its version
                                                4. Key as Accept with value as application/json
                                                5. Key as Content-Type and value as application/json
                                                6. Key as charset and value as utf-8
                                                7. Key as If-Match and value as '*'  **]**
         **Codes_SRS_DEVICETWIN_25_009: [** The function shall send the created request and get the response **]**
         **Codes_SRS_DEVICETWIN_25_010: [** The function shall verify the response status and throw proper Exception **]**
         */
    HttpResponse response = DeviceOperations.request(this.iotHubConnectionString, url, HttpMethod.GET, new byte[0], String.valueOf(requestId++), 0);
    String twin = new String(response.getBody(), StandardCharsets.UTF_8);
    /*
        **Codes_SRS_DEVICETWIN_25_011: [** The function shall deserialize the payload by calling updateTwin Api on the twin object **]**
         */
    device.getTwinParser().updateTwin(twin);
    /*
        **Codes_SRS_DEVICETWIN_25_012: [** The function shall set tags, desired property map, reported property map on the user device **]**
         */
    device.setTags(device.getTwinParser().getTagsMap());
    device.setDesiredProperties(device.getTwinParser().getDesiredPropertyMap());
    device.setReportedProperties(device.getTwinParser().getReportedPropertyMap());
}
Also used : HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URL(java.net.URL)

Example 19 with HttpResponse

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class DeviceOperationsTest method request_nullUrl_failed.

/* Tests_SRS_DEVICE_OPERATIONS_21_002: [The request shall throw IllegalArgumentException if the provided `url` is null.] */
@Test(expected = IllegalArgumentException.class)
public void request_nullUrl_failed() throws Exception {
    //arrange
    final IotHubConnectionString iotHubConnectionString = IotHubConnectionStringBuilder.createConnectionString(STANDARD_CONNECTIONSTRING);
    //act
    HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, null, HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, 0);
//assert
}
Also used : HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Example 20 with HttpResponse

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class DeviceOperationsTest method invoke_throwOnAccept_failed.

/* Tests_SRS_DEVICE_OPERATIONS_21_013: [The request shall add to the HTTP header a `Accept` key with `application/json`.] */
@Test(expected = IllegalArgumentException.class)
public void invoke_throwOnAccept_failed(@Mocked IotHubServiceSasToken iotHubServiceSasToken, @Mocked HttpRequest httpRequest) throws Exception {
    //arrange
    new NonStrictExpectations() {

        {
            iotHubServiceSasToken.toString();
            result = STANDARD_SASTOKEN_STRING;
            httpRequest.setReadTimeoutMillis(DEFAULT_HTTP_TIMEOUT_MS);
            result = httpRequest;
            httpRequest.setHeaderField(AUTHORIZATION, STANDARD_SASTOKEN_STRING);
            result = httpRequest;
            httpRequest.setHeaderField(REQUEST_ID, STANDARD_REQUEST_ID);
            result = httpRequest;
            httpRequest.setHeaderField(USER_AGENT, TransportUtils.getJavaServiceClientIdentifier() + TransportUtils.getServiceVersion());
            result = httpRequest;
            httpRequest.setHeaderField(ACCEPT, ACCEPT_VALUE);
            result = new IllegalArgumentException();
        }
    };
    //act
    HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, new URL(STANDARD_URL), HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, 0);
}
Also used : HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL) Test(org.junit.Test)

Aggregations

HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)57 Test (org.junit.Test)44 HashMap (java.util.HashMap)28 List (java.util.List)28 URL (java.net.URL)26 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)16 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)10 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)9 LinkedList (java.util.LinkedList)8 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)7 IOException (java.io.IOException)7 IotHubBadFormatException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException)4 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)2 IotHubExceptionManager (com.microsoft.azure.sdk.iot.service.exceptions.IotHubExceptionManager)2 MethodParser (com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 JsonArray (javax.json.JsonArray)1 JsonObject (javax.json.JsonObject)1 JsonReader (javax.json.JsonReader)1