Code formatting and refactoring for device-mgt-plugins siddhi extension.

apim420
Menaka Jayawardena 7 years ago
parent d085b78dcf
commit 1ccae434cf

@ -33,9 +33,9 @@ import org.wso2.siddhi.query.api.exception.ExecutionPlanValidationException;
* Accept Type(s): (STRING, STRING) * Accept Type(s): (STRING, STRING)
* Return Type(s): (STRING|INT|DOUBLE|FLOAT|OBJECT) * 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 @Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors, protected void init(ExpressionExecutor[] attributeExpressionExecutors,
@ -47,32 +47,34 @@ public class getPropertyFunctionExtension extends FunctionExecutor {
} }
if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.STRING) { if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException( 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() + Attribute.Type.STRING + ", but found " + attributeExpressionExecutors[0].getReturnType()
.toString()); .toString());
} }
if (attributeExpressionExecutors[1].getReturnType() != Attribute.Type.STRING) { if (attributeExpressionExecutors[1].getReturnType() != Attribute.Type.STRING) {
throw new ExecutionPlanValidationException( throw new ExecutionPlanValidationException(
"Invalid parameter type found for the second argument of json:getProperty() function, " + "required " "Invalid parameter type found for the second argument of json:getProperty() function, required "
+ Attribute.Type.STRING + ", but found " + attributeExpressionExecutors[1].getReturnType() + Attribute.Type.STRING + ", but found "
.toString()); + attributeExpressionExecutors[1].getReturnType().toString());
} }
} }
@Override @Override
protected Object execute(Object[] data) { protected Object execute(Object[] data) {
if (data[0] == null) { 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) { 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 jsonString = (String) data[0];
String property = (String) data[1]; String property = (String) data[1];
JSONObject jsonObject = null; Object jsonObject;
try { try {
jsonObject = new JSONObject(jsonString); jsonObject = new JSONObject(jsonString).get(property);
return jsonObject.get(property).toString(); return jsonObject == null ? null : jsonObject.toString();
} catch (JSONException e) { } catch (JSONException e) {
throw new ExecutionPlanRuntimeException("Cannot parse JSON String in json:getPeroperty() function. " + e); throw new ExecutionPlanRuntimeException("Cannot parse JSON String in json:getPeroperty() function. " + e);
} }
@ -80,7 +82,8 @@ public class getPropertyFunctionExtension extends FunctionExecutor {
@Override @Override
protected Object execute(Object data) { 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 @Override

@ -16,4 +16,4 @@
# under the License. # under the License.
# #
getProperty=org.wso2.extension.siddhi.execution.json.getPropertyFunctionExtension getProperty=org.wso2.extension.siddhi.execution.json.GetPropertyFunctionExtension

@ -45,7 +45,7 @@ public class getPropertyFunctionTestCase {
@Test @Test
public void testGetPropertyFunctionExtension() throws InterruptedException { public void testGetPropertyFunctionExtension() throws InterruptedException {
log.info("getPropertyFunctionExtension TestCase"); log.info("GetPropertyFunctionExtension TestCase");
SiddhiManager siddhiManager = new SiddhiManager(); SiddhiManager siddhiManager = new SiddhiManager();
String inStreamDefinition = "define stream inputStream (payload string, id string, volume long);"; String inStreamDefinition = "define stream inputStream (payload string, id string, volume long);";

Loading…
Cancel
Save