Search in sources :

Example 1 with MethodResult

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

the class DeviceMethodTest method invoke_succeed.

/* Tests_SRS_DEVICEMETHOD_21_015: [If the HttpStatus represents success, the invoke shall return the status and payload using the `MethodResult` class.] */
@Test
public void invoke_succeed(@Mocked final MethodParser methodParser, @Mocked final DeviceOperations request, @Mocked final IotHubServiceSasToken iotHubServiceSasToken, @Mocked final IotHubConnectionStringBuilder mockedConnectionStringBuilder) throws Exception {
    //arrange
    DeviceMethod testMethod = DeviceMethod.createFromConnectionString(STANDARD_CONNECTIONSTRING);
    new NonStrictExpectations() {

        {
            iotHubConnectionString.getUrlMethod(STANDARD_DEVICEID);
            result = STANDARD_URL;
            methodParser.toJson();
            result = STANDARD_JSON;
            methodParser.getPayload();
            result = STANDARD_PAYLOAD_STR;
            methodParser.getStatus();
            result = 123;
        }
    };
    //act
    MethodResult result = testMethod.invoke(STANDARD_DEVICEID, STANDARD_METHODNAME, STANDARD_TIMEOUT_SECONDS, STANDARD_TIMEOUT_SECONDS, STANDARD_PAYLOAD_MAP);
    //assert
    assertThat(result.getStatus(), is(123));
    assertThat(result.getPayload().toString(), is(STANDARD_PAYLOAD_STR));
    new Verifications() {

        {
            methodParser.toJson();
            times = 1;
            iotHubConnectionString.getUrlMethod(STANDARD_DEVICEID);
            times = 1;
            methodParser.getPayload();
            times = 1;
            methodParser.getStatus();
            times = 1;
        }
    };
}
Also used : DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) Test(org.junit.Test)

Example 2 with MethodResult

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

the class MethodResultTest method constructorCreatesNewMethodResult_NullPayload.

@Test
public void constructorCreatesNewMethodResult_NullPayload() {
    //act
    MethodResult methodResult = new MethodResult(123, null);
    //assert
    assertNotNull(methodResult);
    assertThat(methodResult.getStatus(), is(123));
    assertNull(methodResult.getPayload());
}
Also used : MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) Test(org.junit.Test)

Example 3 with MethodResult

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

the class DeviceMethodIT method invokeMethod_nullPayload_succeed.

@Test
public void invokeMethod_nullPayload_succeed() throws Exception {
    // Arrange
    TestDevice testDevice = devices.get(0);
    // Act
    MethodResult result = methodServiceClient.invoke(testDevice.getDeviceId(), METHOD_LOOPBACK, RESPONSE_TIMEOUT, CONNECTION_TIMEOUT, null);
    testDevice.waitIotHub(1, 10);
    // Assert
    assertNotNull(result);
    assertEquals((long) METHOD_SUCCESS, (long) result.getStatus());
    assertEquals(METHOD_LOOPBACK + ":null", result.getPayload());
    assertEquals(0, testDevice.getStatusError());
}
Also used : MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) Test(org.junit.Test)

Example 4 with MethodResult

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

the class DeviceMethodIT method invokeMethod_defaultConnectionTimeout_succeed.

@Test
public void invokeMethod_defaultConnectionTimeout_succeed() throws Exception {
    // Arrange
    TestDevice testDevice = devices.get(0);
    // Act
    MethodResult result = methodServiceClient.invoke(testDevice.getDeviceId(), METHOD_DELAY_IN_MILLISECONDS, RESPONSE_TIMEOUT, null, "100");
    testDevice.waitIotHub(1, 10);
    // Assert
    assertNotNull(result);
    assertEquals((long) METHOD_SUCCESS, (long) result.getStatus());
    assertEquals(METHOD_DELAY_IN_MILLISECONDS + ":succeed", result.getPayload());
    assertEquals(0, testDevice.getStatusError());
}
Also used : MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) Test(org.junit.Test)

Example 5 with MethodResult

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

the class DeviceMethodIT method invokeMethod_invokeParallel_succeed.

@Test
public void invokeMethod_invokeParallel_succeed() throws Exception {
    // Arrange
    TestDevice testDevice = devices.get(0);
    CountDownLatch cdl = new CountDownLatch(NUMBER_INVOKES_PARALLEL);
    List<RunnableInvoke> runs = new LinkedList<>();
    for (int i = 0; i < NUMBER_INVOKES_PARALLEL; i++) {
        RunnableInvoke runnableInvoke = new RunnableInvoke(testDevice.getDeviceId(), "Thread" + i, cdl);
        new Thread(runnableInvoke).start();
        runs.add(runnableInvoke);
    }
    cdl.await();
    for (RunnableInvoke run : runs) {
        MethodResult result = run.getResult();
        assertNotNull(result);
        assertEquals((long) METHOD_SUCCESS, (long) result.getStatus());
        assertEquals(run.getExpectedPayload(), result.getPayload().toString());
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) MethodResult(com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult) Test(org.junit.Test)

Aggregations

MethodResult (com.microsoft.azure.sdk.iot.service.devicetwin.MethodResult)17 Test (org.junit.Test)16 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)2 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)2 IotHubGatewayTimeoutException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubGatewayTimeoutException)2 IOException (java.io.IOException)2 IotHubNotFoundException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubNotFoundException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URISyntaxException (java.net.URISyntaxException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 LinkedList (java.util.LinkedList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1