Search in sources :

Example 6 with IotHubEventCallback

use of com.microsoft.azure.sdk.iot.device.IotHubEventCallback in project azure-iot-sdk-java by Azure.

the class DigitalTwinClientTests method invokeRootLevelCommand.

@Test
@StandardTierHubOnlyTest
public void invokeRootLevelCommand() throws IOException {
    // arrange
    String commandName = "getMaxMinReport";
    String commandInput = "\"" + ZonedDateTime.now(ZoneOffset.UTC).minusMinutes(5).format(DateTimeFormatter.ISO_DATE_TIME) + "\"";
    String jsonStringInput = "{\"prop\":\"value\"}";
    DigitalTwinInvokeCommandRequestOptions options = new DigitalTwinInvokeCommandRequestOptions();
    options.setConnectTimeoutInSeconds(15);
    options.setResponseTimeoutInSeconds(15);
    // setup device callback
    Integer deviceSuccessResponseStatus = 200;
    Integer deviceFailureResponseStatus = 500;
    // Device method callback
    DeviceMethodCallback deviceMethodCallback = (methodName, methodData, context) -> {
        String jsonRequest = new String((byte[]) methodData, StandardCharsets.UTF_8);
        if (methodName.equalsIgnoreCase(commandName)) {
            return new DeviceMethodData(deviceSuccessResponseStatus, jsonRequest);
        } else {
            return new DeviceMethodData(deviceFailureResponseStatus, jsonRequest);
        }
    };
    // IotHub event callback
    IotHubEventCallback iotHubEventCallback = (responseStatus, callbackContext) -> {
    };
    deviceClient.subscribeToDeviceMethod(deviceMethodCallback, commandName, iotHubEventCallback, commandName);
    // act
    DigitalTwinCommandResponse responseWithNoPayload = this.digitalTwinClient.invokeCommand(deviceId, commandName, null);
    DigitalTwinCommandResponse responseWithJsonStringPayload = this.digitalTwinClient.invokeCommand(deviceId, commandName, jsonStringInput);
    DigitalTwinCommandResponse responseWithDatePayload = this.digitalTwinClient.invokeCommand(deviceId, commandName, commandInput);
    ServiceResponseWithHeaders<DigitalTwinCommandResponse, DigitalTwinInvokeCommandHeaders> datePayloadResponseWithHeaders = this.digitalTwinClient.invokeCommandWithResponse(deviceId, commandName, commandInput, options);
    // assert
    assertEquals(deviceSuccessResponseStatus, responseWithNoPayload.getStatus());
    assertEquals("\"\"", responseWithNoPayload.getPayload());
    assertEquals(deviceSuccessResponseStatus, responseWithJsonStringPayload.getStatus());
    assertEquals(jsonStringInput, responseWithJsonStringPayload.getPayload());
    assertEquals(deviceSuccessResponseStatus, responseWithDatePayload.getStatus());
    assertEquals(commandInput, responseWithDatePayload.getPayload());
    assertEquals(deviceSuccessResponseStatus, datePayloadResponseWithHeaders.body().getStatus());
    assertEquals(commandInput, datePayloadResponseWithHeaders.body().getPayload());
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) Device(com.microsoft.azure.sdk.iot.service.Device) SasTokenTools(tests.integration.com.microsoft.azure.sdk.iot.helpers.SasTokenTools) Arrays(java.util.Arrays) MultiplexingClient(com.microsoft.azure.sdk.iot.device.MultiplexingClient) ClientOptions(com.microsoft.azure.sdk.iot.device.ClientOptions) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient) IotHubClientProtocol(com.microsoft.azure.sdk.iot.device.IotHubClientProtocol) URISyntaxException(java.net.URISyntaxException) ZonedDateTime(java.time.ZonedDateTime) DigitalTwinClientOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions) MultiplexingClientException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientException) ProxyOptions(com.microsoft.azure.sdk.iot.service.ProxyOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DigitalTwinCommandResponse(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinCommandResponse) Proxy(java.net.Proxy) After(org.junit.After) Map(java.util.Map) DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) ZoneOffset(java.time.ZoneOffset) DefaultHttpProxyServer(org.littleshoot.proxy.impl.DefaultHttpProxyServer) Parameterized(org.junit.runners.Parameterized) AfterClass(org.junit.AfterClass) Tools(tests.integration.com.microsoft.azure.sdk.iot.helpers.Tools) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest) AzureSasCredential(com.azure.core.credential.AzureSasCredential) DeviceMethodCallback(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback) TwinPropertyCallBack(com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertyCallBack) ServiceResponseWithHeaders(com.microsoft.rest.ServiceResponseWithHeaders) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) DigitalTwinInvokeCommandHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinInvokeCommandHeaders) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) UpdateOperationUtility(com.microsoft.azure.sdk.iot.service.digitaltwin.UpdateOperationUtility) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TokenCredential(com.azure.core.credential.TokenCredential) DigitalTwinInvokeCommandRequestOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinInvokeCommandRequestOptions) HttpProxyServer(org.littleshoot.proxy.HttpProxyServer) DeviceMethodData(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData) BeforeClass(org.junit.BeforeClass) RegistryManagerOptions(com.microsoft.azure.sdk.iot.service.RegistryManagerOptions) RunWith(org.junit.runner.RunWith) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) DigitalTwinGetHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.customized.DigitalTwinGetHeaders) DigitalTwinUpdateRequestOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinUpdateRequestOptions) ArrayList(java.util.ArrayList) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) HashSet(java.util.HashSet) RestException(com.microsoft.rest.RestException) Timeout(org.junit.rules.Timeout) IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) Pair(com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair) BasicDigitalTwin(com.microsoft.azure.sdk.iot.service.digitaltwin.serialization.BasicDigitalTwin) Before(org.junit.Before) Property(com.microsoft.azure.sdk.iot.device.DeviceTwin.Property) TestCase.fail(junit.framework.TestCase.fail) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) E2ETestConstants(tests.integration.com.microsoft.azure.sdk.iot.digitaltwin.helpers.E2ETestConstants) AuthenticationType(com.microsoft.azure.sdk.iot.service.auth.AuthenticationType) Rule(org.junit.Rule) Ignore(org.junit.Ignore) IotHubConnectionStringBuilder(com.microsoft.azure.sdk.iot.service.IotHubConnectionStringBuilder) DateTimeFormatter(java.time.format.DateTimeFormatter) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) DigitalTwinCommandResponse(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinCommandResponse) DigitalTwinInvokeCommandHeaders(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinInvokeCommandHeaders) DeviceMethodData(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DigitalTwinInvokeCommandRequestOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.models.DigitalTwinInvokeCommandRequestOptions) DeviceMethodCallback(com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 7 with IotHubEventCallback

use of com.microsoft.azure.sdk.iot.device.IotHubEventCallback in project azure-iot-sdk-java by Azure.

the class IotHubTransportPacketTest method getStatusReturnsStatus.

// Tests_SRS_IOTHUBTRANSPORTPACKET_11_001: [The constructor shall save the message, callback, status, startTimeMillis, and callback context.]
// Tests_SRS_IOTHUBTRANSPORTPACKET_34_005: [This function shall return the saved status.]
@Test
public void getStatusReturnsStatus() {
    // arrange
    final Map<String, Object> context = new HashMap<>();
    IotHubStatusCode expectedStatus = IotHubStatusCode.MESSAGE_CANCELLED_ONCLOSE;
    // act
    IotHubTransportPacket packet = new IotHubTransportPacket(mockMsg, mockCallback, context, expectedStatus, 10, null);
    IotHubEventCallback testCallback = packet.getCallback();
    // assert
    final IotHubEventCallback expectedCallback = mockCallback;
    assertThat(testCallback, is(expectedCallback));
    assertEquals(expectedStatus, packet.getStatus());
}
Also used : IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) IotHubStatusCode(com.microsoft.azure.sdk.iot.device.IotHubStatusCode) IotHubTransportPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportPacket) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 8 with IotHubEventCallback

use of com.microsoft.azure.sdk.iot.device.IotHubEventCallback in project azure-iot-sdk-java by Azure.

the class IotHubTransportPacketTest method getCallbackReturnsCallback.

// Tests_SRS_IOTHUBTRANSPORTPACKET_11_001: [The constructor shall save the message, callback, status, startTimeMillis, and callback context.]
// Tests_SRS_IOTHUBTRANSPORTPACKET_11_003: [The function shall return the event callback given in the constructor.]
@Test
public void getCallbackReturnsCallback() {
    // arrange
    final Map<String, Object> context = new HashMap<>();
    IotHubStatusCode expectedStatus = IotHubStatusCode.MESSAGE_CANCELLED_ONCLOSE;
    // act
    IotHubTransportPacket packet = new IotHubTransportPacket(mockMsg, mockCallback, context, expectedStatus, 10, null);
    IotHubEventCallback testCallback = packet.getCallback();
    // assert
    final IotHubEventCallback expectedCallback = mockCallback;
    assertThat(testCallback, is(expectedCallback));
    assertEquals(expectedStatus, packet.getStatus());
}
Also used : IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) IotHubStatusCode(com.microsoft.azure.sdk.iot.device.IotHubStatusCode) IotHubTransportPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportPacket) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 9 with IotHubEventCallback

use of com.microsoft.azure.sdk.iot.device.IotHubEventCallback in project azure-iot-sdk-java by Azure.

the class FileUploadTest method callbackBypassStatus.

/* Tests_SRS_FILEUPLOAD_21_014: [The constructor shall create an Event callback `fileUploadStatusCallBack` to receive the upload status.] */
/* Tests_SRS_FILEUPLOAD_21_019: [The FileUploadStatusCallBack shall implements the `IotHubEventCallback` as result of the FileUploadTask.] */
/* Tests_SRS_FILEUPLOAD_21_020: [The FileUploadStatusCallBack shall call the `statusCallback` reporting the received status.] */
@Test
public void callbackBypassStatus() throws IOException {
    // arrange
    final Map<String, Object> context = new HashMap<>();
    constructorExpectations();
    FileUpload fileUpload = new FileUpload(mockConfig);
    IotHubEventCallback testFileUploadStatusCallBack = new FileUploadStatusCallBack();
    // act
    testFileUploadStatusCallBack.execute(IotHubStatusCode.OK_EMPTY, mockFileUploadInProgress);
    // assert
    new Verifications() {

        {
            Deencapsulation.invoke(mockFileUploadInProgress, "triggerCallback", new Class[] { IotHubStatusCode.class }, IotHubStatusCode.OK_EMPTY);
            times = 1;
        }
    };
}
Also used : IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) HashMap(java.util.HashMap) FileUpload(com.microsoft.azure.sdk.iot.device.fileupload.FileUpload) FileUploadStatusCallBack(com.microsoft.azure.sdk.iot.device.fileupload.FileUploadStatusCallBack) Test(org.junit.Test)

Example 10 with IotHubEventCallback

use of com.microsoft.azure.sdk.iot.device.IotHubEventCallback in project azure-iot-sdk-java by Azure.

the class FileUploadTest method callbackDeleteFileUploadInProgress.

/* Tests_SRS_FILEUPLOAD_21_021: [The FileUploadStatusCallBack shall delete the `FileUploadInProgress` that store this file upload context.] */
@Test
public void callbackDeleteFileUploadInProgress(@Mocked final LinkedBlockingDeque<?> mockFileUploadInProgressQueue) throws IOException {
    // arrange
    final Map<String, Object> context = new HashMap<>();
    new NonStrictExpectations() {

        {
            new HttpsTransportManager(mockConfig);
            result = mockHttpsTransportManager;
            Executors.newScheduledThreadPool(10);
            result = mockScheduler;
            new LinkedBlockingDeque<>();
            result = mockFileUploadInProgressQueue;
        }
    };
    FileUpload fileUpload = new FileUpload(mockConfig);
    IotHubEventCallback testFileUploadStatusCallBack = new FileUploadStatusCallBack();
    // act
    testFileUploadStatusCallBack.execute(IotHubStatusCode.OK_EMPTY, mockFileUploadInProgress);
    // assert
    new Verifications() {

        {
            mockFileUploadInProgressQueue.remove(mockFileUploadInProgress);
            times = 1;
        }
    };
}
Also used : HttpsTransportManager(com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager) IotHubEventCallback(com.microsoft.azure.sdk.iot.device.IotHubEventCallback) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) HashMap(java.util.HashMap) FileUpload(com.microsoft.azure.sdk.iot.device.fileupload.FileUpload) FileUploadStatusCallBack(com.microsoft.azure.sdk.iot.device.fileupload.FileUploadStatusCallBack) Test(org.junit.Test)

Aggregations

IotHubEventCallback (com.microsoft.azure.sdk.iot.device.IotHubEventCallback)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)8 AzureSasCredential (com.azure.core.credential.AzureSasCredential)2 TokenCredential (com.azure.core.credential.TokenCredential)2 ClientOptions (com.microsoft.azure.sdk.iot.device.ClientOptions)2 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)2 DeviceMethodCallback (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback)2 DeviceMethodData (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodData)2 Pair (com.microsoft.azure.sdk.iot.device.DeviceTwin.Pair)2 Property (com.microsoft.azure.sdk.iot.device.DeviceTwin.Property)2 TwinPropertyCallBack (com.microsoft.azure.sdk.iot.device.DeviceTwin.TwinPropertyCallBack)2 IotHubClientProtocol (com.microsoft.azure.sdk.iot.device.IotHubClientProtocol)2 IotHubResponseCallback (com.microsoft.azure.sdk.iot.device.IotHubResponseCallback)2 IotHubStatusCode (com.microsoft.azure.sdk.iot.device.IotHubStatusCode)2 MultiplexingClient (com.microsoft.azure.sdk.iot.device.MultiplexingClient)2 MultiplexingClientException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientException)2 FileUpload (com.microsoft.azure.sdk.iot.device.fileupload.FileUpload)2 FileUploadStatusCallBack (com.microsoft.azure.sdk.iot.device.fileupload.FileUploadStatusCallBack)2 IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)2