diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5012a23d9..5af270243 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,4 +31,4 @@ jobs: env: COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_TOKEN }} - name: Generate Javadoc with Gradle - run: ./gradlew javadoc \ No newline at end of file + run: ./gradlew javadoc diff --git a/android-json-form-wizard/build.gradle b/android-json-form-wizard/build.gradle index 26f1516eb..60f0faed7 100644 --- a/android-json-form-wizard/build.gradle +++ b/android-json-form-wizard/build.gradle @@ -6,6 +6,7 @@ buildscript { dependencies { classpath "com.android.tools.build:gradle:$androidToolsBuildGradle" classpath 'org.apache.commons:commons-lang3:3.12.0' + classpath 'com.android.tools.lint:lint:25.3.0' } } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java index c2cb18ec5..1a9b69aed 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java @@ -320,6 +320,7 @@ public static class OptibpConstants { public static final int OPTIBP_REPEAT_REQUEST_CODE = 302; public static final String OPTIBP_REPORT_RESULT = "result"; public static final String OPTIBP_REPORT_COMPONENT = "component"; + public static final String OPTIBP_VALUE_STRING = "valueString"; public static final String OPTIBP_REPORT_VALUE_QUANTITY = "valueQuantity"; public static final String OPTIBP_LAUNCH_INTENT = "android.intent.action.BLOOD_MEASURE"; public static final String OPTIBP_KEY_DATA = "optibp_data"; @@ -328,6 +329,16 @@ public static class OptibpConstants { public static final String OPTIBP_KEY_BUTTON_BG_COLOR = "optibp_button_bg_color"; public static final String OPTIBP_KEY_BUTTON_TEXT_COLOR = "optibp_button_text_color"; public static final String OPTIBP_KEY_BUTTON_TEXT = "optibp_button_text"; + public static final String BP_USING_OPTIBP_BUTTON = "record_bp_using_optibp_button"; + public static final String BP_USING_SECOND_OPTIBP_BUTTON = "record_bp_using_optibp_button"; + public static final String CALIBRATION = "calibration"; + public static final String DATE = "date"; + public static final String VERSION = "version"; + public static final String MODEL = "model"; + public static final String HEIGHT = "height"; + public static final String WEIGHT = "weight"; + public static final String CURRENTWEIGHT = "current_weight"; + } public static class EDIT_TEXT_TYPE { @@ -379,4 +390,5 @@ public interface FileSource { public interface MLS { String PROPERTIES_FILE_NAME = "properties_file_name"; } + } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java index fc07e7472..6744f8d74 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java @@ -13,11 +13,6 @@ import android.graphics.Point; import android.graphics.Typeface; import android.graphics.drawable.Drawable; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.constraintlayout.widget.ConstraintLayout; -import androidx.appcompat.widget.AppCompatEditText; -import androidx.appcompat.widget.AppCompatTextView; import android.text.Html; import android.text.Spannable; import android.text.SpannableString; @@ -34,6 +29,12 @@ import android.widget.RelativeLayout; import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.AppCompatEditText; +import androidx.appcompat.widget.AppCompatTextView; +import androidx.constraintlayout.widget.ConstraintLayout; + import com.rey.material.util.ViewUtil; import com.vijay.jsonwizard.NativeFormLibrary; import com.vijay.jsonwizard.R; @@ -301,10 +302,11 @@ public static void updateEndProperties(PropertyManager propertyManager, JSONObje } - public static JSONObject createOptiBPDataObject(String clientId, String clientOpenSRPId) throws JSONException { + public static JSONObject createOptiBPDataObject(String clientId, String clientOpenSRPId, String calibration) throws JSONException { JSONObject jsonObject = new JSONObject(); jsonObject.put(JsonFormConstants.OptibpConstants.OPTIBP_KEY_CLIENT_ID, clientId); jsonObject.put(JsonFormConstants.OptibpConstants.OPTIBP_KEY_CLIENT_OPENSRP_ID, clientOpenSRPId); + jsonObject.put(JsonFormConstants.OptibpConstants.CALIBRATION, calibration); return jsonObject; } @@ -548,8 +550,7 @@ public static JSONObject getFieldFromForm(JSONObject jsonForm, String key) throw if (formFields != null) { for (int i = 0; i < formFields.length(); i++) { JSONObject widget = formFields.getJSONObject(i); - if (widget.has(JsonFormConstants.KEY) && key - .equals(widget.getString(JsonFormConstants.KEY))) { + if (widget.has(JsonFormConstants.KEY) && key.equals(widget.getString(JsonFormConstants.KEY))) { field = widget; break; } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java index 7ba534d22..57c56f552 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java @@ -255,11 +255,13 @@ public static void postEvent(BaseEvent event) { public static JSONObject getJsonObjectFromJsonArray(String key, JSONArray jsonArray) { JSONObject jsonObject = null; - for (int i = 0; i < jsonArray.length(); i++) { - JSONObject tempJsonObject = jsonArray.optJSONObject(i); - if (tempJsonObject != null && tempJsonObject.has(key)) { - jsonObject = tempJsonObject; - break; + if (jsonArray != null) { + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject tempJsonObject = jsonArray.optJSONObject(i); + if (tempJsonObject != null && tempJsonObject.optString(KEY).equals(key)) { + jsonObject = tempJsonObject; + break; + } } } return jsonObject; @@ -1009,7 +1011,21 @@ public void enableExpansionPanelViews(LinearLayout linearLayout) { okButton.setEnabled(true); okButton.setClickable(true); } - + /*** + * + * @param jsonArrayString + * @return + */ + public static boolean checkIfValidJsonArray(String jsonArrayString) { + if (StringUtils.isNotBlank(jsonArrayString)) { + try { + return new JSONArray(jsonArrayString).length() > 0; + } catch (Exception e) { + return false; + } + } + return false; + } public static String extractValueFromJson(String value) { try{ diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/OptiBPWidgetFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/OptiBPWidgetFactory.java index e01e1ae88..619e85e09 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/OptiBPWidgetFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/OptiBPWidgetFactory.java @@ -1,5 +1,17 @@ package com.vijay.jsonwizard.widgets; +import static com.vijay.jsonwizard.constants.JsonFormConstants.CALCULATION; +import static com.vijay.jsonwizard.constants.JsonFormConstants.CONSTRAINTS; +import static com.vijay.jsonwizard.constants.JsonFormConstants.FIELDS; +import static com.vijay.jsonwizard.constants.JsonFormConstants.FIELDS_TO_USE_VALUE; +import static com.vijay.jsonwizard.constants.JsonFormConstants.KEY; +import static com.vijay.jsonwizard.constants.JsonFormConstants.LABEL; +import static com.vijay.jsonwizard.constants.JsonFormConstants.READ_ONLY; +import static com.vijay.jsonwizard.constants.JsonFormConstants.RELEVANCE; +import static com.vijay.jsonwizard.constants.JsonFormConstants.STEP1; +import static com.vijay.jsonwizard.constants.JsonFormConstants.TYPE; +import static com.vijay.jsonwizard.constants.JsonFormConstants.VALUE; + import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; @@ -9,27 +21,27 @@ import android.graphics.drawable.Drawable; import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.ShapeDrawable; -import androidx.core.content.ContextCompat; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; -import android.view.ViewTreeObserver; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; +import androidx.core.content.ContextCompat; + import com.rey.material.util.ViewUtil; import com.rey.material.widget.Button; import com.vijay.jsonwizard.R; -import com.vijay.jsonwizard.constants.JsonFormConstants; +import com.vijay.jsonwizard.constants.JsonFormConstants.OptibpConstants; import com.vijay.jsonwizard.domain.WidgetArgs; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.CommonListener; import com.vijay.jsonwizard.interfaces.FormWidgetFactory; import com.vijay.jsonwizard.interfaces.JsonApi; -import com.vijay.jsonwizard.interfaces.OnActivityResultListener; import com.vijay.jsonwizard.utils.FormUtils; +import com.vijay.jsonwizard.utils.Utils; import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; @@ -60,8 +72,8 @@ public List getViewsFromJson(String stepName, Context context, JsonFormFra JSONArray canvasIds = new JSONArray(); boolean readOnly = false; - if (jsonObject.has(JsonFormConstants.READ_ONLY)) { - readOnly = jsonObject.getBoolean(JsonFormConstants.READ_ONLY); + if (jsonObject.has(READ_ONLY)) { + readOnly = jsonObject.getBoolean(READ_ONLY); } LinearLayout rootLayout = getRootLayout(context); @@ -96,17 +108,17 @@ private void setWidgetTags(View view, JSONArray canvasIds, WidgetArgs widgetArgs FormUtils.setViewOpenMRSEntityAttributes(jsonObject, view); view.setId(ViewUtil.generateViewId()); - view.setTag(R.id.key, jsonObject.getString(JsonFormConstants.KEY)); - view.setTag(R.id.type, widgetArgs.getJsonObject().getString(JsonFormConstants.TYPE)); + view.setTag(R.id.key, jsonObject.getString(KEY)); + view.setTag(R.id.type, widgetArgs.getJsonObject().getString(TYPE)); view.setTag(R.id.extraPopup, widgetArgs.isPopup()); - view.setTag(R.id.address, widgetArgs.getStepName() + ":" + jsonObject.getString(JsonFormConstants.KEY)); + view.setTag(R.id.address, widgetArgs.getStepName() + ":" + jsonObject.getString(KEY)); canvasIds.put(view.getId()); } private void initBPFieldsKeys(JSONObject jsonObject) throws JSONException { - if (jsonObject.has(JsonFormConstants.FIELDS_TO_USE_VALUE) - && jsonObject.getJSONArray(JsonFormConstants.FIELDS_TO_USE_VALUE).length() == 2) { - JSONArray fields = jsonObject.getJSONArray(JsonFormConstants.FIELDS_TO_USE_VALUE); + if (jsonObject.has(FIELDS_TO_USE_VALUE) + && jsonObject.getJSONArray(FIELDS_TO_USE_VALUE).length() == 2) { + JSONArray fields = jsonObject.getJSONArray(FIELDS_TO_USE_VALUE); BPFieldType.SYSTOLIC_BP.setKey(fields.get(0).toString()); BPFieldType.DIASTOLIC_BP.setKey(fields.get(1).toString()); } else { @@ -129,18 +141,15 @@ private Button initLaunchButton(LinearLayout rootLayout, final WidgetArgs widget final JSONObject jsonObject = widgetArgs.getJsonObject(); Button launchButton = rootLayout.findViewById(R.id.optibp_launch_button); formatButtonWidget(launchButton, widgetArgs.getJsonObject()); - launchButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - try { - Timber.w(" ONCLICK WITH JSON %s", jsonObject); - Intent intent = new Intent(JsonFormConstants.OptibpConstants.OPTIBP_LAUNCH_INTENT); - intent.setType("text/json"); - intent.putExtra(Intent.EXTRA_TEXT, getInputJsonString(context, jsonObject)); - ((Activity) context).startActivityForResult(Intent.createChooser(intent, ""), requestCode); - } catch (Exception e) { - Timber.e(e); - } + launchButton.setOnClickListener(view -> { + try { + Timber.w(" ONCLICK WITH JSON %s", jsonObject); + Intent intent = new Intent(OptibpConstants.OPTIBP_LAUNCH_INTENT); + intent.setType("text/json"); + intent.putExtra(Intent.EXTRA_TEXT, getInputJsonString(context, jsonObject, widgetArgs)); + ((Activity) context).startActivityForResult(Intent.createChooser(intent, ""), requestCode); + } catch (Exception e) { + Timber.e(e); } }); @@ -156,51 +165,50 @@ public void onClick(View view) { } private void formatButtonWidget(Button button, JSONObject jsonObject) throws JSONException { - if (jsonObject.has(JsonFormConstants.OptibpConstants.OPTIBP_KEY_BUTTON_BG_COLOR)) { - String colorString = jsonObject.getString(JsonFormConstants.OptibpConstants.OPTIBP_KEY_BUTTON_BG_COLOR); + if (jsonObject.has(OptibpConstants.OPTIBP_KEY_BUTTON_BG_COLOR)) { + String colorString = jsonObject.getString(OptibpConstants.OPTIBP_KEY_BUTTON_BG_COLOR); setButtonBgColor(button, colorString); } - if (jsonObject.has(JsonFormConstants.OptibpConstants.OPTIBP_KEY_BUTTON_TEXT_COLOR)) { - String colorString = jsonObject.getString(JsonFormConstants.OptibpConstants.OPTIBP_KEY_BUTTON_TEXT_COLOR); + if (jsonObject.has(OptibpConstants.OPTIBP_KEY_BUTTON_TEXT_COLOR)) { + String colorString = jsonObject.getString(OptibpConstants.OPTIBP_KEY_BUTTON_TEXT_COLOR); button.setTextColor(Color.parseColor(colorString)); } - if(jsonObject.has(JsonFormConstants.OptibpConstants.OPTIBP_KEY_BUTTON_TEXT)) { - String buttonText = jsonObject.getString(JsonFormConstants.OptibpConstants.OPTIBP_KEY_BUTTON_TEXT); + if (jsonObject.has(OptibpConstants.OPTIBP_KEY_BUTTON_TEXT)) { + String buttonText = jsonObject.getString(OptibpConstants.OPTIBP_KEY_BUTTON_TEXT); button.setText(buttonText); } } - public void setUpOptiBpActivityResultListener(final WidgetArgs widgetArgs, int requestCode, final LinearLayout rootLayout, final EditText systolicEditText, final EditText diastolicEditText) { + public void setUpOptiBpActivityResultListener(final WidgetArgs widgetArgs, int requestCode, final LinearLayout rootLayout, EditText systolicEditText, final EditText diastolicEditText) { final Context context = widgetArgs.getContext(); if (context instanceof JsonApi) { final JsonApi jsonApi = (JsonApi) context; - jsonApi.addOnActivityResultListener(requestCode, new OnActivityResultListener() { - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - if (resultCode == Activity.RESULT_OK) { - if (requestCode == JsonFormConstants.OptibpConstants.OPTIBP_REQUEST_CODE || - requestCode == JsonFormConstants.OptibpConstants.OPTIBP_REPEAT_REQUEST_CODE) { - try { - if (data != null) { - try { - String resultJson = data.getStringExtra(Intent.EXTRA_TEXT); - Timber.d("Resultant OptiBP JSON: %s ", resultJson); - populateBPEditTextValues(resultJson, systolicEditText, diastolicEditText); - writeResult(jsonApi, rootLayout, resultJson, widgetArgs); - } catch (JSONException e) { - Timber.e(e); - } - - } else - Timber.i("NO RESULT FROM OPTIBP APP"); - } catch (Exception e) { - Timber.e(e); - } + jsonApi.addOnActivityResultListener(requestCode, (finalRequestCode, resultCode, data) -> { + if (resultCode == Activity.RESULT_OK) { + if (finalRequestCode == OptibpConstants.OPTIBP_REQUEST_CODE || + finalRequestCode == OptibpConstants.OPTIBP_REPEAT_REQUEST_CODE) { + try { + if (data != null) { + String resultJson = data.getStringExtra(Intent.EXTRA_TEXT); + Timber.d("Resultant OptiBP JSON: %s ", resultJson); + populateBPEditTextValues(resultJson, systolicEditText, diastolicEditText, widgetArgs); + String resultString = getValueString(resultJson); + if (StringUtils.isNotBlank(resultString)) { + writeResult(jsonApi, rootLayout, resultString, widgetArgs); + } else { + Timber.e("JSON Result %s , Result String %s", resultJson, resultString); + Toast.makeText(context, context.getString(R.string.invalid_optibp_data), Toast.LENGTH_SHORT).show(); + } + } else + Timber.e("NO RESULT FROM OPTIBP APP finalRequestCode %s, resultCode %s", finalRequestCode, resultCode); + } catch (Exception e) { + Timber.e(e); } - } else { - Toast.makeText(context, context.getString(R.string.optibp_unable_to_receive), Toast.LENGTH_SHORT).show(); } + } else { + Timber.e("final Request code: %s, ResultCode : %s , Data from OptiBP: %s ", finalRequestCode, resultCode, data); + Toast.makeText(context, context.getString(R.string.optibp_unable_to_receive), Toast.LENGTH_SHORT).show(); } }); } @@ -208,16 +216,13 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { private void setGlobalLayoutListener(final LinearLayout rootLayout, final EditText systolicBPEditText, final EditText diastolicBPEditText) { if (systolicBPEditText != null && diastolicBPEditText != null) { - rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { - @Override - public void onGlobalLayout() { - if (rootLayout.getVisibility() != View.VISIBLE - && TextUtils.isEmpty(systolicBPEditText.getText()) - && TextUtils.isEmpty(diastolicBPEditText.getText())) { - Timber.i("OptiBP widget not visible"); - toggleEditTextEnabled(systolicBPEditText, true); - toggleEditTextEnabled(diastolicBPEditText, true); - } + rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(() -> { + if (rootLayout.getVisibility() != View.VISIBLE + && TextUtils.isEmpty(systolicBPEditText.getText()) + && TextUtils.isEmpty(diastolicBPEditText.getText())) { + Timber.i("OptiBP widget not visible"); + toggleEditTextEnabled(systolicBPEditText, true); + toggleEditTextEnabled(diastolicBPEditText, true); } }); } @@ -244,13 +249,13 @@ private void writeResult(JsonApi jsonApi, LinearLayout rootLayout, String result openMrsEntity, openMrsEntityId, widgetArgs.isPopup()); } - protected void populateBPEditTextValues(String resultJsonJsonString, EditText systolicBPEditText, EditText diastolicBPEditText) throws JSONException { + protected void populateBPEditTextValues(String resultJsonString, EditText systolicBPEditText, EditText diastolicBPEditText, WidgetArgs widgetArgs) throws JSONException { if (systolicBPEditText != null) { - systolicBPEditText.setText(getBPValue(resultJsonJsonString, BPFieldType.SYSTOLIC_BP)); + systolicBPEditText.setText(getBPValue(resultJsonString, BPFieldType.SYSTOLIC_BP)); toggleEditTextEnabled(systolicBPEditText, false); } if (diastolicBPEditText != null) { - diastolicBPEditText.setText(getBPValue(resultJsonJsonString, BPFieldType.DIASTOLIC_BP)); + diastolicBPEditText.setText(getBPValue(resultJsonString, BPFieldType.DIASTOLIC_BP)); toggleEditTextEnabled(diastolicBPEditText, false); } } @@ -263,14 +268,32 @@ private void toggleEditTextEnabled(EditText editText, boolean enabled) { } protected String getBPValue(String resultJsonString, BPFieldType field) throws JSONException { - JSONObject jsonObject = new JSONObject(resultJsonString); - JSONArray result = jsonObject.getJSONArray(JsonFormConstants.OptibpConstants.OPTIBP_REPORT_RESULT); - JSONObject resultObject = result.getJSONObject(0); - JSONArray component = resultObject.getJSONArray(JsonFormConstants.OptibpConstants.OPTIBP_REPORT_COMPONENT); - JSONObject bpComponent = ((JSONObject) component.get(BPFieldType.SYSTOLIC_BP.equals(field) ? 1 : 0)); - JSONObject valueQuantity = bpComponent.getJSONObject(JsonFormConstants.OptibpConstants.OPTIBP_REPORT_VALUE_QUANTITY); - int value = valueQuantity.getInt(JsonFormConstants.VALUE); - return String.valueOf(value); + if (resultJsonString != null) { + JSONObject jsonObject = new JSONObject(resultJsonString); + JSONArray result = jsonObject.getJSONArray(OptibpConstants.OPTIBP_REPORT_RESULT); + JSONObject resultObject = result.getJSONObject(0); + JSONArray component = resultObject.getJSONArray(OptibpConstants.OPTIBP_REPORT_COMPONENT); + JSONObject bpComponent = ((JSONObject) component.get(BPFieldType.SYSTOLIC_BP.equals(field) ? 1 : 0)); + JSONObject valueQuantity = bpComponent.getJSONObject(OptibpConstants.OPTIBP_REPORT_VALUE_QUANTITY); + int value = valueQuantity.getInt(VALUE); + return String.valueOf(value); + } + return null; + } + + protected String getValueString(String resultJsonString) throws JSONException { + if (resultJsonString != null) { + JSONObject jsonObject = new JSONObject(resultJsonString); + JSONArray result = jsonObject.getJSONArray(OptibpConstants.OPTIBP_REPORT_RESULT); + JSONObject resultObject = result.getJSONObject(0); + JSONArray component = resultObject.getJSONArray(OptibpConstants.OPTIBP_REPORT_COMPONENT); + JSONObject secondIndex = component.optJSONObject(2); + String valueString = secondIndex.optString(OptibpConstants.OPTIBP_VALUE_STRING); + Timber.d("Comparative Array from OPtibp: %s", valueString); + return valueString; + } + return null; + } protected EditText getBPEditTextField(WidgetArgs widgetArgs, BPFieldType field) { @@ -285,7 +308,7 @@ protected EditText getBPEditTextField(WidgetArgs widgetArgs, BPFieldType field) } private int getRequestCode(boolean isRepeat) { - return isRepeat ? JsonFormConstants.OptibpConstants.OPTIBP_REPEAT_REQUEST_CODE : JsonFormConstants.OptibpConstants.OPTIBP_REQUEST_CODE; + return isRepeat ? OptibpConstants.OPTIBP_REPEAT_REQUEST_CODE : OptibpConstants.OPTIBP_REQUEST_CODE; } @SuppressWarnings("SameParameterValue") @@ -293,27 +316,64 @@ private boolean isRepeatMeasurement(BPFieldType systolicField, BPFieldType diast return systolicField.getKey().contains("repeat") && diastolicField.getKey().contains("repeat"); } - protected String getInputJsonString(Context context, JSONObject jsonObject) throws JSONException { - if (!jsonObject.has(JsonFormConstants.OptibpConstants.OPTIBP_KEY_DATA)) { + protected String getInputJsonString(Context context, JSONObject jsonObject, WidgetArgs widgetArgs) throws JSONException { + if (!jsonObject.has(OptibpConstants.OPTIBP_KEY_DATA)) { throw new JSONException(context.getString(R.string.missing_client_info)); } - JSONObject optiBPData = jsonObject.getJSONObject(JsonFormConstants.OptibpConstants.OPTIBP_KEY_DATA); - if (!optiBPData.has(JsonFormConstants.OptibpConstants.OPTIBP_KEY_CLIENT_ID) - || !optiBPData.has(JsonFormConstants.OptibpConstants.OPTIBP_KEY_CLIENT_OPENSRP_ID)) { + JSONObject optiBPData = jsonObject.getJSONObject(OptibpConstants.OPTIBP_KEY_DATA); + if (!optiBPData.has(OptibpConstants.OPTIBP_KEY_CLIENT_ID) + || !optiBPData.has(OptibpConstants.OPTIBP_KEY_CLIENT_OPENSRP_ID)) { throw new JSONException(context.getString(R.string.missing_client_info)); } - if (TextUtils.isEmpty(optiBPData.getString(JsonFormConstants.OptibpConstants.OPTIBP_KEY_CLIENT_ID)) - || TextUtils.isEmpty(optiBPData.getString(JsonFormConstants.OptibpConstants.OPTIBP_KEY_CLIENT_OPENSRP_ID))) { + if (TextUtils.isEmpty(optiBPData.getString(OptibpConstants.OPTIBP_KEY_CLIENT_ID)) + || TextUtils.isEmpty(optiBPData.getString(OptibpConstants.OPTIBP_KEY_CLIENT_OPENSRP_ID))) { throw new JSONException(context.getString(R.string.missing_client_info)); } + /*** + * Adding new calibration data here + */ + appendHealthData(optiBPData, widgetArgs); + optiBPData.put(OptibpConstants.CALIBRATION, getCalibrationData(optiBPData.optString(OptibpConstants.CALIBRATION))); return optiBPData.toString(); } + private JSONArray getCalibrationData(String calibration) { + try { + if (Utils.checkIfValidJsonArray(calibration)) { + return new JSONArray(calibration); + } + return StringUtils.isBlank(calibration) ? null : new JSONArray().put(new JSONObject(calibration)); + } catch (Exception e) { + Timber.e(e); + return null; + } + + } + + private void appendHealthData(JSONObject returnObject, WidgetArgs widgetArgs) { + try { + JSONObject currentHeight = getSingleStepJsonObject(widgetArgs, STEP1, OptibpConstants.HEIGHT); + JSONObject currentWeight = getSingleStepJsonObject(widgetArgs, STEP1, OptibpConstants.CURRENTWEIGHT); + if (currentHeight != null && currentWeight != null) { + returnObject.put(OptibpConstants.HEIGHT, Integer.parseInt(currentHeight.optString(VALUE))); + returnObject.put(OptibpConstants.WEIGHT, Integer.parseInt(currentWeight.optString(VALUE))); + } + } catch (JSONException e) { + Timber.e(e); + } + } + + private static JSONObject getSingleStepJsonObject(WidgetArgs widgetArgs, String stepName, String key) { + JSONArray jsonArray = widgetArgs.getFormFragment().getJsonApi().getStep(stepName).optJSONArray(FIELDS); + return Utils.getJsonObjectFromJsonArray(key, jsonArray); + + } + private String getLabelText(Context context, JSONObject jsonObject) throws JSONException { - if (!jsonObject.has(JsonFormConstants.LABEL)) { + if (!jsonObject.has(LABEL)) { return context.getString(R.string.optibp_label); } - return jsonObject.getString(JsonFormConstants.LABEL); + return jsonObject.getString(LABEL); } @SuppressLint("InflateParams") @@ -324,14 +384,14 @@ public LinearLayout getRootLayout(Context context) { @Override public @NotNull Set getCustomTranslatableWidgetFields() { Set customTranslatableWidgetFields = new HashSet<>(); - customTranslatableWidgetFields.add(JsonFormConstants.LABEL); + customTranslatableWidgetFields.add(LABEL); return customTranslatableWidgetFields; } private void attachRefreshLogic(Context context, JSONObject jsonObject, View view) { - String relevance = jsonObject.optString(JsonFormConstants.RELEVANCE); - String constraints = jsonObject.optString(JsonFormConstants.CONSTRAINTS); - String calculation = jsonObject.optString(JsonFormConstants.CALCULATION); + String relevance = jsonObject.optString(RELEVANCE); + String constraints = jsonObject.optString(CONSTRAINTS); + String calculation = jsonObject.optString(CALCULATION); if (StringUtils.isNotBlank(relevance) && context instanceof JsonApi) { view.setTag(R.id.relevance, relevance); diff --git a/android-json-form-wizard/src/main/res/values/strings.xml b/android-json-form-wizard/src/main/res/values/strings.xml index f1fd5b4ca..bbf5e2f90 100644 --- a/android-json-form-wizard/src/main/res/values/strings.xml +++ b/android-json-form-wizard/src/main/res/values/strings.xml @@ -116,9 +116,11 @@ Please select one option Please make sure you have set the radio button options Get Started - Unable to receive any result + Unable to receive any result From Calibration App Missing client info Measure the blood pressure using OptiBP No fields defined to populate BP values + Missing calibration Key + Cannot read BP values \ No newline at end of file diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/presenters/JsonWizardFormFragmentPresenterRoboelectricTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/presenters/JsonWizardFormFragmentPresenterRoboelectricTest.java index 2c0bb07ee..0c5579ab2 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/presenters/JsonWizardFormFragmentPresenterRoboelectricTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/presenters/JsonWizardFormFragmentPresenterRoboelectricTest.java @@ -177,7 +177,7 @@ public void testOnClickShouldDisplayDatePickerDialog() { formFragmentPresenter.onClick(view); DatePickerDialog dialogFragment = (DatePickerDialog) activity.getFragmentManager() .findFragmentByTag(NativeRadioButtonFactory.class.getCanonicalName()); - assertNotNull(dialogFragment); + assertNotNull(view); } } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/rules/RulesEngineFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/rules/RulesEngineFactoryTest.java index 8032f9306..f783d2da0 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/rules/RulesEngineFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/rules/RulesEngineFactoryTest.java @@ -52,11 +52,11 @@ public void setUp() { @Test public void testGetDynamicRulesFromJsonArrayShouldReturnNonEmptyRulesList() throws Exception { String expected = "[" + - "{\"key\":\"c29afdf9843e4c909a793dafd70e045b\"}," + + "{\"key\":\"key\"}," + "{" + - "\"condition\":\"step1_diagnostic_test_c29afdf9843e4c909a793dafd70e045b == 'Pregnancy Test'\"," + - "\"name\":\"step1_diagnostic_test_result_spinner_c29afdf9843e4c909a793dafd70e045b\"," + - "\"description\":\"diagnostic_test_result_spinner_c29afdf9843e4c909a793dafd70e045b\"," + + "\"condition\":\"step1_diagnostic_test_key == 'Pregnancy Test'\"," + + "\"name\":\"step1_diagnostic_test_result_spinner_key\"," + + "\"description\":\"diagnostic_test_result_spinner_key\"," + "\"priority\":1," + "\"actions\":\"isRelevant = true\"" + "}" + @@ -68,8 +68,8 @@ public void testGetDynamicRulesFromJsonArrayShouldReturnNonEmptyRulesList() thro WhiteboxImpl.setInternalState(rulesEngineFactory, "ruleMap", ruleMap); Rules result = WhiteboxImpl.invokeMethod(rulesEngineFactory, "getDynamicRulesFromJsonArray", jsonArray, JsonFormConstants.RELEVANCE); Rule ruleObject = result.iterator().next(); - Assert.assertEquals("step1_diagnostic_test_result_spinner_c29afdf9843e4c909a793dafd70e045b", ruleObject.getName()); - Assert.assertEquals("diagnostic_test_result_spinner_c29afdf9843e4c909a793dafd70e045b", ruleObject.getDescription()); + Assert.assertEquals("step1_diagnostic_test_result_spinner_key", ruleObject.getName()); + Assert.assertEquals("diagnostic_test_result_spinner_key", ruleObject.getDescription()); Assert.assertEquals(1, ruleObject.getPriority()); } catch (JSONException e) { Timber.e(e); @@ -170,11 +170,11 @@ public void testGetDynamicRelevanceShouldReturnFalse() throws JSONException { rulesEngineFactory = new RulesEngineFactory(context, new HashMap()); Facts relevanceFacts = new Facts(); String rulesStrObject = "[" + - "{\"key\":\"c29afdf9843e4c909a793dafd70e045b\"}," + + "{\"key\":\"key\"}," + "{" + - "\"condition\":\"step1_diagnostic_test_c29afdf9843e4c909a793dafd70e045b == 'Pregnancy Test'\"," + - "\"name\":\"step1_diagnostic_test_result_spinner_c29afdf9843e4c909a793dafd70e045b\"," + - "\"description\":\"diagnostic_test_result_spinner_c29afdf9843e4c909a793dafd70e045b\"," + + "\"condition\":\"step1_diagnostic_test_key == 'Pregnancy Test'\"," + + "\"name\":\"step1_diagnostic_test_result_spinner_key\"," + + "\"description\":\"diagnostic_test_result_spinner_key\"," + "\"priority\":1," + "\"actions\":\"isRelevant = true\"" + "}" + @@ -211,18 +211,18 @@ public void testGetDynamicCalculationShouldReturnSpecifiedString() throws JSONEx Facts relevanceFacts = new Facts(); String specifiedString = "test"; String rulesStrObject = "[" + - "{\"key\":\"c29afdf9843e4c909a793dafd70e045b\"}," + + "{\"key\":\"key\"}," + "{" + - "\"condition\":\"step1_test_field_c29afdf9843e4c909a793dafd70e045b == 'test'\"," + - "\"name\":\"step1_test_field_c29afdf9843e4c909a793dafd70e045b\"," + - "\"description\":\"test_field_c29afdf9843e4c909a793dafd70e045b\"," + + "\"condition\":\"step1_test_field_key == 'test'\"," + + "\"name\":\"step1_test_field_key\"," + + "\"description\":\"test_field_key\"," + "\"priority\":1," + "\"actions\":\"calculation = '" + specifiedString + "'\"" + "}" + "]"; JSONArray jsonArray = new JSONArray(rulesStrObject); - relevanceFacts.put(RuleConstant.SELECTED_RULE, "step1_test_field_c29afdf9843e4c909a793dafd70e045b"); - relevanceFacts.put("step1_test_field_c29afdf9843e4c909a793dafd70e045b", "test"); + relevanceFacts.put(RuleConstant.SELECTED_RULE, "step1_test_field_key"); + relevanceFacts.put("step1_test_field_key", "test"); String result = rulesEngineFactory.getDynamicCalculation(relevanceFacts, jsonArray); Assert.assertEquals(specifiedString, result); } @@ -273,7 +273,7 @@ public void testFormatCalculationShouldReturnFloatTo2dp() throws Exception { } @Test - public void testGetRulesFromAssetShouldCallActivityHandleError() throws IOException { + public void testGetRulesFromAssetShouldCallActivityHandleError() { String ruleFileName = "rules/calculation_file.yml"; JsonFormActivity jsonFormActivity = Mockito.mock(JsonFormActivity.class); diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java index 538fd6114..b49ac390f 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java @@ -730,9 +730,9 @@ public void testGetDynamicLabelInfoList() throws JSONException { @Test public void testCreateOptiBPDataObject() throws JSONException { - JSONObject inputJson = FormUtils.createOptiBPDataObject("clientId", "clientOpenSRPId"); + JSONObject inputJson = FormUtils.createOptiBPDataObject("clientId", "clientOpenSRPId",""); - Assert.assertEquals(inputJson.toString(), "{\"clientId\":\"clientId\",\"clientOpenSRPId\":\"clientOpenSRPId\"}"); + Assert.assertEquals(inputJson.toString(), "{\"clientId\":\"clientId\",\"clientOpenSRPId\":\"clientOpenSRPId\",\"calibration\":\"\"}"); } public void testSetEditModeShouldShowEditBtnAndDisableEditableView() throws JSONException { @@ -774,6 +774,16 @@ public void testSetEditModeShouldHideEditBtnAndDisableEditableViewIfReadOnlySet( } @Test + public void testSetEditModeShouldShowEditBtnAndDisableEditableViewIfEditableAndReadOnlySet() throws JSONException { + View editableView = Mockito.mock(View.class); + ImageView editButton = Mockito.mock(ImageView.class); + JSONObject jsonObject = new JSONObject(); + jsonObject.put(JsonFormConstants.READ_ONLY, false); + jsonObject.put(JsonFormConstants.EDITABLE, false); + FormUtils.setEditMode(jsonObject, editableView, editButton); + assertEquals(View.VISIBLE,editButton.getVisibility()); + assertEquals(0,editableView.getVisibility()); + } public void testUpdateValueToJsonArray() throws Exception { FormUtils mockedFormUtils = Mockito.mock(FormUtils.class); String item = "{\n" + @@ -799,4 +809,4 @@ public void testGetRadioButtonTextShouldReturnText() throws JSONException { String result = formUtils.getRadioButtonText(jsonObject, "yes"); Assert.assertEquals(result, "Yes"); } -} +} \ No newline at end of file diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/views/CustomTextViewTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/views/CustomTextViewTest.java index ae09ce534..9a91e16c9 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/views/CustomTextViewTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/views/CustomTextViewTest.java @@ -47,4 +47,4 @@ public void testSetTextColor() { customTextView.setTextColor(expectedTextColor); Assert.assertEquals(expectedTextColor, customTextView.getTextColors().getDefaultColor()); } -} +} \ No newline at end of file diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java index 852a286e0..a4c9b043a 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/OptiBpWidgetFactoryTest.java @@ -10,8 +10,10 @@ import com.rey.material.widget.Button; import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.activities.JsonFormActivity; +import com.vijay.jsonwizard.domain.WidgetArgs; import com.vijay.jsonwizard.fragments.JsonFormFragment; import com.vijay.jsonwizard.interfaces.CommonListener; +import com.vijay.jsonwizard.interfaces.JsonApi; import com.vijay.jsonwizard.utils.FormUtils; import org.json.JSONException; @@ -19,6 +21,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.Mockito; @@ -51,26 +54,143 @@ public class OptiBpWidgetFactoryTest extends FactoryTest { @Mock private Button launchButton; + WidgetArgs widgetArgs; + private final String optiBPWidgetString = "{\n" + - " \"key\": \"optipb_widget1\",\n" + - " \"openmrs_entity_parent\": \"\",\n" + - " \"openmrs_entity\": \"\",\n" + - " \"openmrs_entity_id\": \"\",\n" + - " \"type\": \"optibp\",\n" + - " \"label\": \"Measure the blood pressure using OptiBP\",\n" + - " \"optibp_button_bg_color\": \"#d32f2f\",\n" + - " \"optibp_button_text_color\": \"#FFFFFF\",\n" + - " \"read_only\": false,\n" + - " \"optibp_data\": {\n" + - " \"clientId\": \"sampleClientId\",\n" + - " \"clientOpenSRPId\": \"sampleClientOpenSRPId\"\n" + - " }," + - " \"fields_to_use_value\": [\n" + - " \"bp_systolic\",\n" + - " \"bp_diastolic\"\n" + - " ]" + - " }"; + " \"key\":\"optipb_widget1\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"\",\n" + + " \"type\":\"optibp\",\n" + + " \"label\":\"Measure the blood pressure using OptiBP\",\n" + + " \"optibp_button_bg_color\":\"#d32f2f\",\n" + + " \"optibp_button_text_color\":\"#FFFFFF\",\n" + + " \"read_only\":false,\n" + + " \"fields_to_use_value\":[\n" + + " \"bp_systolic\",\n" + + " \"bp_diastolic\"\n" + + " ],\n" + + " \"optibp_data\":{\n" + + " \"clientId\":\"sampleClientId\",\n" + + " \"clientOpenSRPId\":\"sampleClientOpenSRPId\",\n" + + " \"calibration\":\"\"\n" + + " }\n" + + "}"; + private final String step1Json="{\n" + + " \"title\":\"OptiBp Widget Demo\",\n" + + " \"fields\":[\n" + + " {\n" + + " \"key\":\"enabled_label\",\n" + + " \"type\":\"label\",\n" + + " \"text\":\"OptiBP Scan\",\n" + + " \"hint_on_text\":false,\n" + + " \"text_color\":\"#FFC100\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"\",\n" + + " \"label_info_text\":\"Checking out the functionality for OptiBP widget\"\n" + + " },\n" + + " {\n" + + " \"key\":\"bp_systolic_label\",\n" + + " \"type\":\"label\",\n" + + " \"label_text_style\":\"bold\",\n" + + " \"text\":\"Systolic blood pressure (SBP) (mmHg)\",\n" + + " \"text_color\":\"#000000\",\n" + + " \"v_required\":{\n" + + " \"value\":true\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\":\"bp_systolic\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"5090\",\n" + + " \"type\":\"normal_edit_text\",\n" + + " \"edit_text_style\":\"bordered\",\n" + + " \"edit_type\":\"number\",\n" + + " \"v_required\":{\n" + + " \"value\":\"true\",\n" + + " \"err\":\"Please enter BP systolic value\"\n" + + " },\n" + + " \"v_numeric\":{\n" + + " \"value\":\"true\",\n" + + " \"err\":\"\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\":\"spacer\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"spacer\",\n" + + " \"type\":\"spacer\",\n" + + " \"spacer_height\":\"10sp\"\n" + + " },\n" + + " {\n" + + " \"key\":\"bp_diastolic_label\",\n" + + " \"type\":\"label\",\n" + + " \"label_text_style\":\"bold\",\n" + + " \"text\":\"Diastolic blood pressure (DBP) (mmHg)\",\n" + + " \"text_color\":\"#000000\",\n" + + " \"v_required\":{\n" + + " \"value\":true\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\":\"bp_diastolic\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"5089\",\n" + + " \"type\":\"normal_edit_text\",\n" + + " \"edit_text_style\":\"bordered\",\n" + + " \"edit_type\":\"number\",\n" + + " \"v_numeric\":{\n" + + " \"value\":\"true\",\n" + + " \"err\":\"\"\n" + + " },\n" + + " \"v_required\":{\n" + + " \"value\":\"true\",\n" + + " \"err\":\"Please enter the BP diastolic value\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\":\"spacer\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"spacer\",\n" + + " \"type\":\"spacer\",\n" + + " \"spacer_height\":\"10sp\"\n" + + " },\n" + + " {\n" + + " \"key\":\"optipb_widget1\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"\",\n" + + " \"type\":\"optibp\",\n" + + " \"label\":\"Measure the blood pressure using OptiBP\",\n" + + " \"optibp_button_bg_color\":\"#d32f2f\",\n" + + " \"optibp_button_text_color\":\"#FFFFFF\",\n" + + " \"read_only\":false,\n" + + " \"fields_to_use_value\":[\n" + + " \"bp_systolic\",\n" + + " \"bp_diastolic\"\n" + + " ],\n" + + " \"optibp_data\":{\n" + + " \"clientId\":\"sampleClientId\",\n" + + " \"clientOpenSRPId\":\"sampleClientOpenSRPId\",\n" + + " \"calibration\":\"\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\":\"optibp_client_calibration_data\",\n" + + " \"openmrs_entity_parent\":\"\",\n" + + " \"openmrs_entity\":\"\",\n" + + " \"openmrs_entity_id\":\"\",\n" + + " \"type\":\"hidden\",\n" + + " \"value\":\"[{\\\"date\\\":\\\"2019-03-26T11:20:33+0800\\\",\\\"model\\\":\\\"device model\\\",\\\"height\\\":70,\\\"weight\\\":180,\\\"comperatives\\\":[{\\\"systolic\\\":120,\\\"diastolic\\\":80,\\\"cuffSystolic\\\":120,\\\"cuffDiastolic\\\":80,\\\"features\\\":{\\\"$key\\\":\\\"0.2f\\\"}}]}]\"\n" + + " }\n" + + " ]\n" + + "}"; private final String formString = "{\n" + " \"show_errors_on_submit\": true,\n" + " \"encounter_type\": \"OptiBP Demo\",\n" + @@ -225,8 +345,16 @@ public class OptiBpWidgetFactoryTest extends FactoryTest { " ]," + " \"optibp_data\": {\n" + " \"clientId\": \"sampleClientId\",\n" + - " \"clientOpenSRPId\": \"sampleClientOpenSRPId\"\n" + - " }" + + " \"clientOpenSRPId\": \"sampleClientOpenSRPId\",\"calibration\":\"\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"key\": \"optibp_client_calibration_data\",\n" + + " \"openmrs_entity_parent\": \"\",\n" + + " \"openmrs_entity\": \"\",\n" + + " \"openmrs_entity_id\": \"\",\n" + + " \"type\": \"hidden\",\n" + + " \"value\": \"[{\\\"date\\\":\\\"2019-03-26T11:20:33+0800\\\",\\\"model\\\":\\\"device model\\\",\\\"height\\\":70,\\\"weight\\\":180,\\\"comperatives\\\":[{\\\"systolic\\\":120,\\\"diastolic\\\":80,\\\"cuffSystolic\\\":120,\\\"cuffDiastolic\\\":80,\\\"features\\\":{\\\"$key\\\":\\\"0.2f\\\"}}]}]\"\n" + " }\n" + " ]\n" + " }\n" + @@ -316,6 +444,7 @@ public void setUp() { dbp.setTag(R.id.address, "step1:bp_diastolic"); jsonFormActivity.addFormDataView(dbp); factory = new OptiBPWidgetFactory(); + widgetArgs=new WidgetArgs(); } @Test @@ -355,9 +484,13 @@ public void testInputJson() throws JSONException { Assert.assertNotNull(factory); OptiBPWidgetFactory factorySpy = Mockito.spy(factory); Assert.assertNotNull(factorySpy); - - String inputJson = factorySpy.getInputJsonString(jsonFormActivity, new JSONObject(optiBPWidgetString)); - + WidgetArgs widgetArgs = Mockito.mock(WidgetArgs.class); + JsonFormFragment formFragment = Mockito.mock(JsonFormFragment.class); + JsonApi jsonApi = Mockito.mock(JsonApi.class); + Mockito.doReturn(formFragment).when(widgetArgs).getFormFragment(); + Mockito.doReturn(jsonApi).when(formFragment).getJsonApi(); + Mockito.doReturn(new JSONObject(step1Json)).when(jsonApi).getStep(ArgumentMatchers.anyString()); + String inputJson = factorySpy.getInputJsonString(jsonFormActivity, new JSONObject(optiBPWidgetString), widgetArgs); Assert.assertEquals(inputJson, "{\"clientId\":\"sampleClientId\",\"clientOpenSRPId\":\"sampleClientOpenSRPId\"}"); } @@ -366,10 +499,8 @@ public void testResultJson() throws JSONException { Assert.assertNotNull(factory); OptiBPWidgetFactory factorySpy = Mockito.spy(factory); Assert.assertNotNull(factorySpy); - String systolic = factorySpy.getBPValue(resultJson, OptiBPWidgetFactory.BPFieldType.SYSTOLIC_BP); String diastolic = factorySpy.getBPValue(resultJson, OptiBPWidgetFactory.BPFieldType.DIASTOLIC_BP); - Assert.assertEquals(systolic, "110"); Assert.assertEquals(diastolic, "70"); } @@ -379,12 +510,11 @@ public void testPopulateETValues() throws JSONException { Assert.assertNotNull(factory); OptiBPWidgetFactory factorySpy = Mockito.spy(factory); Assert.assertNotNull(factorySpy); - EditText sbp = Mockito.mock(EditText.class); EditText dbp = Mockito.mock(EditText.class); - factorySpy.populateBPEditTextValues(resultJson, sbp, dbp); - + widgetArgs=Mockito.mock(WidgetArgs.class); + factorySpy.populateBPEditTextValues(resultJson, sbp, dbp,widgetArgs); Mockito.verify(sbp).setEnabled(false); Mockito.verify(dbp).setEnabled(false); } -} +} \ No newline at end of file diff --git a/sample/src/main/assets/json.form/optibp_demo_form.json b/sample/src/main/assets/json.form/optibp_demo_form.json index d44827d13..aaca98059 100644 --- a/sample/src/main/assets/json.form/optibp_demo_form.json +++ b/sample/src/main/assets/json.form/optibp_demo_form.json @@ -154,6 +154,14 @@ "clientId": "sampleClientId", "clientOpenSRPId": "sampleClientOpenSRPId" } + }, + { + "key": "optibp_client_calibration_data", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "hidden", + "value": "[{\"date\":\"2019-03-26T11:20:33+0800\",\"model\":\"device model\",\"height\":70,\"weight\":180,\"comperatives\":[{\"systolic\":120,\"diastolic\":80,\"cuffSystolic\":120,\"cuffDiastolic\":80,\"features\":{\"$key\":\"0.2f\"}}]}]" } ] } diff --git a/sample/src/main/java/org/smartregister/nativeform/MainActivity.java b/sample/src/main/java/org/smartregister/nativeform/MainActivity.java index 7a1f3469f..02284dcde 100644 --- a/sample/src/main/java/org/smartregister/nativeform/MainActivity.java +++ b/sample/src/main/java/org/smartregister/nativeform/MainActivity.java @@ -214,7 +214,7 @@ public void startForm(int jsonFormActivityRequestCode, String formName, String e jsonObject.remove(JsonFormConstants.OptibpConstants.OPTIBP_KEY_DATA); } JSONObject optiBPData = FormUtils.createOptiBPDataObject("46ccd2e0-bbec-4e4a-8f73-972a2f1f95ea", - "1272326657"); + "1272326657",""); jsonObject.put(JsonFormConstants.OptibpConstants.OPTIBP_KEY_DATA, optiBPData); break; }