use of com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback 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());
}
Aggregations