Search in sources :

Example 6 with IotHubException

use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.

the class DeviceTwinTest method getTwinThrowsVerificationFailure.

/*
    **Tests_SRS_DEVICETWIN_25_010: [** The function shall verify the response status and throw proper Exception **]**
     */
@Test(expected = IotHubException.class)
public void getTwinThrowsVerificationFailure() throws Exception {
    //arrange
    final String connectionString = "testString";
    DeviceTwin testTwin = DeviceTwin.createFromConnectionString(connectionString);
    new NonStrictExpectations() {

        {
            mockedDevice.getDeviceId();
            result = "SomeDevID";
            IotHubExceptionManager.httpResponseVerification(mockedHttpResponse);
            result = new IotHubException();
        }
    };
    //act
    testTwin.getTwin(mockedDevice);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) NonStrictExpectations(mockit.NonStrictExpectations) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) Test(org.junit.Test)

Example 7 with IotHubException

use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.

the class DeviceMethodTest method invoke_throwOnHttpRequester_failed.

/* Tests_SRS_DEVICEMETHOD_21_009: [The invoke shall send the created request and get the response using the HttpRequester.] */
/* Tests_SRS_DEVICEMETHOD_21_010: [The invoke shall create a new HttpRequest with http method as `POST`.] */
@Test(expected = IotHubException.class)
public void invoke_throwOnHttpRequester_failed(@Mocked final MethodParser methodParser, @Mocked final IotHubConnectionStringBuilder mockedConnectionStringBuilder) throws Exception {
    //arrange
    DeviceMethod testMethod = DeviceMethod.createFromConnectionString(STANDARD_CONNECTIONSTRING);
    new NonStrictExpectations() {

        {
            methodParser.toJson();
            result = STANDARD_JSON;
            iotHubConnectionString.getUrlMethod(STANDARD_DEVICEID);
            result = STANDARD_URL;
        }
    };
    new MockUp<DeviceOperations>() {

        @Mock
        HttpResponse request(IotHubConnectionString iotHubConnectionString, URL url, HttpMethod method, byte[] payload, String requestId, long timeoutInMs) throws IOException, IotHubException, IllegalArgumentException {
            throw new IotHubException();
        }
    };
    //act
    testMethod.invoke(STANDARD_DEVICEID, STANDARD_METHODNAME, STANDARD_TIMEOUT_SECONDS, STANDARD_TIMEOUT_SECONDS, STANDARD_PAYLOAD_MAP);
}
Also used : IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) URL(java.net.URL) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Test(org.junit.Test)

Example 8 with IotHubException

use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.

the class DeviceManagerSample method GetDevice.

private static void GetDevice() throws Exception {
    RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
    Device returnDevice = null;
    try {
        returnDevice = registryManager.getDevice(SampleUtils.deviceId);
        System.out.println("Device: " + returnDevice.getDeviceId());
        System.out.println("Device primary key: " + returnDevice.getPrimaryKey());
        System.out.println("Device secondary key: " + returnDevice.getSecondaryKey());
        System.out.println("Device eTag: " + returnDevice.geteTag());
    } catch (IotHubException iote) {
        iote.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IOException(java.io.IOException) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 9 with IotHubException

use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.

the class DeviceMethodSample method main.

/**
     * @param args
     * @throws IOException
     * @throws URISyntaxException
     */
public static void main(String[] args) throws Exception {
    System.out.println("Starting sample...");
    DeviceMethod methodClient = DeviceMethod.createFromConnectionString(iotHubConnectionString);
    try {
        // Manage complete Method
        System.out.println("Getting device Method");
        MethodResult result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, payload);
        if (result == null) {
            throw new IOException("Method invoke returns null");
        }
        System.out.println("Status=" + result.getStatus());
        System.out.println("Payload=" + result.getPayload());
    } catch (IotHubException e) {
        System.out.println(e.getMessage());
    }
    System.out.println("Shutting down sample...");
}
Also used : IOException(java.io.IOException) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)

Example 10 with IotHubException

use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubException in project azure-iot-sdk-java by Azure.

the class IotHubExceptionManagerTest method httpResponseVerification_300.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_012: [The function shall return without exception if the response status equal or less than 300]
@Test
public void httpResponseVerification_300() throws IotHubException {
    // Arrange
    final int status = 300;
    final byte[] body = { 1 };
    final Map<String, List<String>> headerFields = new HashMap<>();
    final byte[] errorReason = { 123, 125 };
    HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
    // Act
    IotHubExceptionManager.httpResponseVerification(response);
    IotHubException iotHubException = new IotHubException();
    IotHubExceptionManager iotHubExceptionManager = new IotHubExceptionManager();
    // Assert
    assertNotEquals(null, iotHubException);
    assertNotEquals(null, iotHubExceptionManager);
}
Also used : IotHubExceptionManager(com.microsoft.azure.sdk.iot.service.exceptions.IotHubExceptionManager) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Test(org.junit.Test)

Aggregations

IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)11 IOException (java.io.IOException)5 Test (org.junit.Test)5 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)4 Device (com.microsoft.azure.sdk.iot.service.Device)3 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)3 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)3 HashMap (java.util.HashMap)3 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)2 IotHubExceptionManager (com.microsoft.azure.sdk.iot.service.exceptions.IotHubExceptionManager)2 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)2 List (java.util.List)2 NonStrictExpectations (mockit.NonStrictExpectations)2 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)1 MethodResult (com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)1 Pair (com.microsoft.azure.sdk.iot.service.devicetwin.Pair)1 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1