From 82e8ad026f87748e6264e8d9377b3ae2b2cd89f4 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Tue, 24 Oct 2017 10:41:55 +0530 Subject: [PATCH] Code formatting and refactoring for device-mgt-plugins siddhi extension. --- ...java => GetPropertyFunctionExtension.java} | 27 ++++++++++--------- .../src/main/resources/json.siddhiext | 2 +- .../json/getPropertyFunctionTestCase.java | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) rename components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/main/java/org/wso2/extension/siddhi/execution/json/{getPropertyFunctionExtension.java => GetPropertyFunctionExtension.java} (82%) diff --git a/components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/main/java/org/wso2/extension/siddhi/execution/json/getPropertyFunctionExtension.java b/components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/main/java/org/wso2/extension/siddhi/execution/json/GetPropertyFunctionExtension.java similarity index 82% rename from components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/main/java/org/wso2/extension/siddhi/execution/json/getPropertyFunctionExtension.java rename to components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/main/java/org/wso2/extension/siddhi/execution/json/GetPropertyFunctionExtension.java index 8f68577a60..6bb8ddd721 100644 --- a/components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/main/java/org/wso2/extension/siddhi/execution/json/getPropertyFunctionExtension.java +++ b/components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/main/java/org/wso2/extension/siddhi/execution/json/GetPropertyFunctionExtension.java @@ -33,9 +33,9 @@ import org.wso2.siddhi.query.api.exception.ExecutionPlanValidationException; * Accept Type(s): (STRING, STRING) * Return Type(s): (STRING|INT|DOUBLE|FLOAT|OBJECT) */ -public class getPropertyFunctionExtension extends FunctionExecutor { +public class GetPropertyFunctionExtension extends FunctionExecutor { - Attribute.Type returnType = Attribute.Type.STRING; + private Attribute.Type returnType = Attribute.Type.STRING; @Override protected void init(ExpressionExecutor[] attributeExpressionExecutors, @@ -47,32 +47,34 @@ public class getPropertyFunctionExtension extends FunctionExecutor { } if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.STRING) { throw new ExecutionPlanValidationException( - "Invalid parameter type found for the first argument of json:getProperty() function, " + "required " + "Invalid parameter type found for the first argument of json:getProperty() function, required " + Attribute.Type.STRING + ", but found " + attributeExpressionExecutors[0].getReturnType() .toString()); } if (attributeExpressionExecutors[1].getReturnType() != Attribute.Type.STRING) { throw new ExecutionPlanValidationException( - "Invalid parameter type found for the second argument of json:getProperty() function, " + "required " - + Attribute.Type.STRING + ", but found " + attributeExpressionExecutors[1].getReturnType() - .toString()); + "Invalid parameter type found for the second argument of json:getProperty() function, required " + + Attribute.Type.STRING + ", but found " + + attributeExpressionExecutors[1].getReturnType().toString()); } } @Override protected Object execute(Object[] data) { if (data[0] == null) { - throw new ExecutionPlanRuntimeException("Invalid input given to json:getProperty() function. First argument cannot be null"); + throw new ExecutionPlanRuntimeException("Invalid input given to json:getProperty() function." + + " First argument cannot be null"); } if (data[1] == null) { - throw new ExecutionPlanRuntimeException("Invalid input given to json:getProperty() function. Second argument cannot be null"); + throw new ExecutionPlanRuntimeException("Invalid input given to json:getProperty() function. " + + "Second argument cannot be null"); } String jsonString = (String) data[0]; String property = (String) data[1]; - JSONObject jsonObject = null; + Object jsonObject; try { - jsonObject = new JSONObject(jsonString); - return jsonObject.get(property).toString(); + jsonObject = new JSONObject(jsonString).get(property); + return jsonObject == null ? null : jsonObject.toString(); } catch (JSONException e) { throw new ExecutionPlanRuntimeException("Cannot parse JSON String in json:getPeroperty() function. " + e); } @@ -80,7 +82,8 @@ public class getPropertyFunctionExtension extends FunctionExecutor { @Override protected Object execute(Object data) { - return null; //Since the getProperty function takes in 2 parameters, this method does not get called. Hence,not implemented. + return null; //Since the getProperty function takes in 2 parameters, this method does not get called. + // Hence,not implemented. } @Override diff --git a/components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/main/resources/json.siddhiext b/components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/main/resources/json.siddhiext index f1886dd63e..f2a86a7eba 100644 --- a/components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/main/resources/json.siddhiext +++ b/components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/main/resources/json.siddhiext @@ -16,4 +16,4 @@ # under the License. # -getProperty=org.wso2.extension.siddhi.execution.json.getPropertyFunctionExtension +getProperty=org.wso2.extension.siddhi.execution.json.GetPropertyFunctionExtension diff --git a/components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/test/java/org/wso2/extension/siddhi/execution/json/getPropertyFunctionTestCase.java b/components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/test/java/org/wso2/extension/siddhi/execution/json/getPropertyFunctionTestCase.java index 7bc66478c2..c1c49f7882 100644 --- a/components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/test/java/org/wso2/extension/siddhi/execution/json/getPropertyFunctionTestCase.java +++ b/components/extensions/siddhi-extensions/org.wso2.extension.siddhi.execution.json/src/test/java/org/wso2/extension/siddhi/execution/json/getPropertyFunctionTestCase.java @@ -45,7 +45,7 @@ public class getPropertyFunctionTestCase { @Test public void testGetPropertyFunctionExtension() throws InterruptedException { - log.info("getPropertyFunctionExtension TestCase"); + log.info("GetPropertyFunctionExtension TestCase"); SiddhiManager siddhiManager = new SiddhiManager(); String inStreamDefinition = "define stream inputStream (payload string, id string, volume long);";