use of com.microsoft.azure.sdk.iot.deps.serializer.MethodParser in project azure-iot-sdk-java by Azure.
the class DeviceMethod method invoke.
/**
* Directly invokes a method on the device and return its result.
*
* @param deviceId is the device identification.
* @param methodName is the name of the method that shall be invoked on the device.
* @param responseTimeoutInSeconds is the maximum waiting time for a response from the device in seconds.
* @param connectTimeoutInSeconds is the maximum waiting time for a response from the connection in seconds.
* @param payload is the the method parameter
* @return the status and payload resulted from the method invoke
* @throws IotHubException This exception is thrown if the response verification failed
* @throws IOException This exception is thrown if the IO operation failed
*/
public synchronized MethodResult invoke(String deviceId, String methodName, Long responseTimeoutInSeconds, Long connectTimeoutInSeconds, Object payload) throws IotHubException, IOException {
/* Codes_SRS_DEVICEMETHOD_21_004: [The invoke shall throw IllegalArgumentException if the provided deviceId is null or empty.] */
if ((deviceId == null) || deviceId.isEmpty()) {
throw new IllegalArgumentException("deviceId is empty or null.");
}
/* Codes_SRS_DEVICEMETHOD_21_005: [The invoke shall throw IllegalArgumentException if the provided methodName is null, empty, or not valid.] */
if ((methodName == null) || methodName.isEmpty()) {
throw new IllegalArgumentException("methodName is empty or null.");
}
/* Codes_SRS_DEVICEMETHOD_21_006: [The invoke shall throw IllegalArgumentException if the provided responseTimeoutInSeconds is negative.] */
/* Codes_SRS_DEVICEMETHOD_21_007: [The invoke shall throw IllegalArgumentException if the provided connectTimeoutInSeconds is negative.] */
/* Codes_SRS_DEVICEMETHOD_21_014: [The invoke shall bypass the Exception if one of the functions called by invoke failed.] */
MethodParser methodParser = new MethodParser(methodName, responseTimeoutInSeconds, connectTimeoutInSeconds, payload);
/* Codes_SRS_DEVICEMETHOD_21_011: [The invoke shall add a HTTP body with Json created by the `serializer.MethodParser`.] */
String json = methodParser.toJson();
if (json == null) {
/* Codes_SRS_DEVICEMETHOD_21_012: [If `MethodParser` return a null Json, the invoke shall throw IllegalArgumentException.] */
throw new IllegalArgumentException("MethodParser return null Json");
}
/* Codes_SRS_DEVICEMETHOD_21_008: [The invoke shall build the Method URL `{iot hub}/twins/{device id}/methods/` by calling getUrlMethod.] */
URL url = this.iotHubConnectionString.getUrlMethod(deviceId);
long responseTimeout, connectTimeout;
if (responseTimeoutInSeconds == null) {
// If timeout is not set, it defaults to 30 seconds
responseTimeout = DEFAULT_RESPONSE_TIMEOUT;
} else {
responseTimeout = responseTimeoutInSeconds;
}
if (connectTimeoutInSeconds == null) {
connectTimeout = DEFAULT_CONNECT_TIMEOUT;
} else {
connectTimeout = connectTimeoutInSeconds;
}
// Calculate total timeout in milliseconds
long timeoutInMs = (responseTimeout + connectTimeout) * THOUSAND_MS;
/* Codes_SRS_DEVICEMETHOD_21_009: [The invoke shall send the created request and get the response using the HttpRequester.] */
/* Codes_SRS_DEVICEMETHOD_21_010: [The invoke shall create a new HttpRequest with http method as `POST`.] */
HttpResponse response = DeviceOperations.request(this.iotHubConnectionString, url, HttpMethod.POST, json.getBytes(StandardCharsets.UTF_8), String.valueOf(requestId++), timeoutInMs);
/* Codes_SRS_DEVICEMETHOD_21_013: [The invoke shall deserialize the payload using the `serializer.MethodParser`.] */
MethodParser methodParserResponse = new MethodParser();
methodParserResponse.fromJson(new String(response.getBody(), StandardCharsets.UTF_8));
/* Codes_SRS_DEVICEMETHOD_21_015: [If the HttpStatus represents success, the invoke shall return the status and payload using the `MethodResult` class.] */
return new MethodResult(methodParserResponse.getStatus(), methodParserResponse.getPayload());
}
use of com.microsoft.azure.sdk.iot.deps.serializer.MethodParser in project azure-iot-sdk-java by Azure.
the class MethodParserTest method Constructor_map_Payload_success.
/* Tests_SRS_METHODPARSER_21_020: [The constructor shall create an instance of the methodParser.] */
/* Tests_SRS_METHODPARSER_21_021: [The constructor shall update the method collection using the provided information.] */
/* Tests_SRS_METHODPARSER_21_034: [The constructor shall set the method operation as `payload`.] */
@Test
public void Constructor_map_Payload_success() {
for (TestMethod testCase : successTestResult) {
// Act
MethodParser methodParser = new MethodParser(testCase.payload);
// Assert
assertMethod(methodParser, null, null, null, null, testCase.payload, "payload");
String json = methodParser.toJson();
Helpers.assertJson(testCase.json, json);
}
}
use of com.microsoft.azure.sdk.iot.deps.serializer.MethodParser in project azure-iot-sdk-java by Azure.
the class MethodParserTest method fromJson_method_succeed.
/* Tests_SRS_METHODPARSER_21_006: [The fromJson shall parse the json and fill the method collection.] */
/* Tests_SRS_METHODPARSER_21_007: [The json can contain values `null`, `"null"`, and `""`, which represents null, the string null, and empty string respectively.] */
/**
* Tests_SRS_METHODPARSER_21_009: [If the json contains the `methodName` identification, the fromJson shall parse the full method, and set the operation as `invoke`.]
* Ex:
* {
* "methodName": "reboot",
* "responseTimeoutInSeconds": 200,
* "connectTimeoutInSeconds": 5,
* "payload":
* {
* "input1": "someInput",
* "input2": "anotherInput"
* }
* }
*/
@Test
public void fromJson_method_succeed() {
// Arrange
MethodParser methodParser = new MethodParser();
for (TestMethod testCase : successTestMethod) {
// Act
methodParser.fromJson(testCase.json);
// Assert
assertMethod(methodParser, testCase.name, testCase.responseTimeout, testCase.connectTimeout, null, testCase.payload, "invoke");
}
}
use of com.microsoft.azure.sdk.iot.deps.serializer.MethodParser in project azure-iot-sdk-java by Azure.
the class MethodParserTest method fromJson_payload_succeed.
/* Tests_SRS_METHODPARSER_21_006: [The fromJson shall parse the json and fill the method collection.] */
/* Tests_SRS_METHODPARSER_21_007: [The json can contain values `null`, `"null"`, and `""`, which represents null, the string null, and empty string respectively.] */
/**
* Tests_SRS_METHODPARSER_21_010: [If the json contains any payload without `methodName` or `status` identification, the fromJson shall parse only the payload, and set the operation as `payload`]
* Ex:
* {
* "input1": "someInput",
* "input2": "anotherInput"
* }
*/
@Test
public void fromJson_payload_succeed() {
for (TestMethod testCase : successTestResult) {
// Arrange
MethodParser methodParser = new MethodParser();
// Act
methodParser.fromJson(testCase.json);
// Assert
assertMethod(methodParser, null, null, null, null, testCase.payload, "payload");
}
}
use of com.microsoft.azure.sdk.iot.deps.serializer.MethodParser in project azure-iot-sdk-java by Azure.
the class MethodParserTest method Constructor_Method_succeed.
/* Tests_SRS_METHODPARSER_21_001: [The constructor shall create an instance of the methodParser.] */
/* Tests_SRS_METHODPARSER_21_002: [The constructor shall update the method collection using the provided information.] */
/* Tests_SRS_METHODPARSER_21_003: [All Strings are case sensitive.] */
/* Tests_SRS_METHODPARSER_21_023: [The constructor shall initialize the method operation as `invoke`.] */
@Test
public void Constructor_Method_succeed() {
for (TestMethod testCase : successTestMethod) {
// Act
MethodParser methodParser = new MethodParser(testCase.name, testCase.responseTimeout, testCase.connectTimeout, testCase.payload);
// Assert
assertMethod(methodParser, testCase.name, testCase.responseTimeout, testCase.connectTimeout, null, testCase.payload, "invoke");
}
}
Aggregations