Search in sources :

Example 11 with DeviceMethod

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod in project azure-iot-sdk-java by Azure.

the class DeviceMethodTest method invoke_throwOnToJson_failed.

/* Tests_SRS_DEVICEMETHOD_21_011: [The invoke shall add a HTTP body with Json created by the `serializer.MethodParser`.] */
@Test(expected = IllegalArgumentException.class)
public void invoke_throwOnToJson_failed(@Mocked final MethodParser methodParser) throws Exception {
    //arrange
    DeviceMethod testMethod = DeviceMethod.createFromConnectionString(STANDARD_CONNECTIONSTRING);
    new NonStrictExpectations() {

        {
            methodParser.toJson();
            result = new IllegalArgumentException();
        }
    };
    //act
    testMethod.invoke(STANDARD_DEVICEID, STANDARD_METHODNAME, STANDARD_TIMEOUT_SECONDS, STANDARD_TIMEOUT_SECONDS, STANDARD_PAYLOAD_MAP);
}
Also used : DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) Test(org.junit.Test)

Example 12 with DeviceMethod

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod in project azure-iot-sdk-java by Azure.

the class DeviceMethodTest method invoke_throwOnCreateMethod_failed.

/* Tests_SRS_DEVICEMETHOD_21_005: [The invoke shall throw IllegalArgumentException if the provided methodName is null, empty, or not valid.] */
/* Tests_SRS_DEVICEMETHOD_21_006: [The invoke shall throw IllegalArgumentException if the provided responseTimeoutInSeconds is negative.] */
/* Tests_SRS_DEVICEMETHOD_21_007: [The invoke shall throw IllegalArgumentException if the provided connectTimeoutInSeconds is negative.] */
/* Tests_SRS_DEVICEMETHOD_21_014: [The invoke shall bypass the Exception if one of the functions called by invoke failed.] */
@Test(expected = IllegalArgumentException.class)
public void invoke_throwOnCreateMethod_failed() throws Exception {
    //arrange
    new MockUp<MethodParser>() {

        @Mock
        void $init(String name, Long responseTimeoutInSeconds, Long connectTimeoutInSeconds, Object payload) throws IllegalArgumentException {
            throw new IllegalArgumentException();
        }
    };
    DeviceMethod testMethod = DeviceMethod.createFromConnectionString(STANDARD_CONNECTIONSTRING);
    //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) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) Test(org.junit.Test)

Example 13 with DeviceMethod

use of com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod 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)

Aggregations

DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)13 Test (org.junit.Test)12 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)5 MethodResult (com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)2 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)2 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)1 IOException (java.io.IOException)1 URL (java.net.URL)1