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());
}
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());
}
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());
}
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;
}
};
}
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;
}
};
}
Aggregations