diff --git a/src/main/java/org/medicmobile/webapp/mobile/ChtExternalApp.java b/src/main/java/org/medicmobile/webapp/mobile/ChtExternalApp.java index c187e0ef..dffd4511 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/ChtExternalApp.java +++ b/src/main/java/org/medicmobile/webapp/mobile/ChtExternalApp.java @@ -85,6 +85,14 @@ public Intent createIntent() { //> PRIVATE + private Serializable getSerializableValue(Object value) { + // ODK does not have boolean data type + if (value instanceof String strValue && ("true".equals(strValue) || "false".equals(strValue))) { + return Boolean.parseBoolean(strValue); + } + return (Serializable) value; + } + private void setIntentExtras(Intent intent, String key, JSONObject data) { try { Object value = data.get(key); @@ -99,7 +107,7 @@ private void setIntentExtras(Intent intent, String key, JSONObject data) { return; } - intent.putExtra(key, (Serializable) value); + intent.putExtra(key, getSerializableValue(value)); } catch (Exception exception) { error(exception, "ChtExternalApp :: Problem setting intent extras. Key=%s, Data=%s", key, data); diff --git a/src/main/java/org/medicmobile/webapp/mobile/ChtExternalAppHandler.java b/src/main/java/org/medicmobile/webapp/mobile/ChtExternalAppHandler.java index 2998a5d5..125d42a0 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/ChtExternalAppHandler.java +++ b/src/main/java/org/medicmobile/webapp/mobile/ChtExternalAppHandler.java @@ -27,19 +27,21 @@ public class ChtExternalAppHandler { } String processResult(int resultCode, Intent intent) { - if (resultCode != RESULT_OK) { - String message = "ChtExternalAppHandler :: Bad result code: %s. The external app either: " + - "explicitly returned this result, didn't return any result or crashed during the operation."; - - warn(this, message, resultCode); - return safeFormat("console.error('" + message + "')", resultCode); - } - try { Optional json = new ChtExternalApp .Response(intent, this.context) .getData(); String data = json.map(JSONObject::toString).orElse(null); + + if (resultCode != RESULT_OK) { + String message = "ChtExternalAppHandler :: Bad result code: %s. The external app either: " + + "explicitly returned this result, did not return any result or crashed during the operation. " + + "[" + data + "]"; + + warn(this, message, resultCode); + return safeFormat("console.error('" + message + "')", resultCode); + } + return makeJavaScript(data); } catch (Exception exception) { diff --git a/src/test/java/org/medicmobile/webapp/mobile/ChtExternalAppHandlerTest.java b/src/test/java/org/medicmobile/webapp/mobile/ChtExternalAppHandlerTest.java index 756fa54f..b245b0b2 100644 --- a/src/test/java/org/medicmobile/webapp/mobile/ChtExternalAppHandlerTest.java +++ b/src/test/java/org/medicmobile/webapp/mobile/ChtExternalAppHandlerTest.java @@ -135,9 +135,30 @@ public void processResult_withBadResultCode_logError() { Intent intent = mock(Intent.class); ChtExternalAppHandler chtExternalAppHandler = new ChtExternalAppHandler(contextMock); String expectedMessageWarn = "ChtExternalAppHandler :: Bad result code: %s. The external app either: " + - "explicitly returned this result, didn't return any result or crashed during the operation."; + "explicitly returned this result, did not return any result or crashed during the operation. [null]"; String expectedMessageConsole = "ChtExternalAppHandler :: Bad result code: " + RESULT_CANCELED + ". The external app either: " + - "explicitly returned this result, didn't return any result or crashed during the operation."; + "explicitly returned this result, did not return any result or crashed during the operation. [null]"; + + //> WHEN + String script = chtExternalAppHandler.processResult(RESULT_CANCELED, intent); + + //> THEN + assertEquals("console.error('" + expectedMessageConsole + "')", script); + medicLogMock.verify(() -> MedicLog.warn(eq(chtExternalAppHandler), eq(expectedMessageWarn), eq(RESULT_CANCELED))); + } + } + + @Test + public void processResult_withBadResultCodeAndData_logError() { + try (MockedStatic medicLogMock = mockStatic(MedicLog.class)) { + //> GIVEN + Intent intent = new Intent(); + intent.putExtra("name", "Eric"); + ChtExternalAppHandler chtExternalAppHandler = new ChtExternalAppHandler(contextMock); + String expectedMessageWarn = "ChtExternalAppHandler :: Bad result code: %s. The external app either: " + + "explicitly returned this result, did not return any result or crashed during the operation. [{\"name\":\"Eric\"}]"; + String expectedMessageConsole = "ChtExternalAppHandler :: Bad result code: " + RESULT_CANCELED + ". The external app either: " + + "explicitly returned this result, did not return any result or crashed during the operation. [{\"name\":\"Eric\"}]"; //> WHEN String script = chtExternalAppHandler.processResult(RESULT_CANCELED, intent); diff --git a/src/test/java/org/medicmobile/webapp/mobile/ChtExternalAppTest.java b/src/test/java/org/medicmobile/webapp/mobile/ChtExternalAppTest.java index 36b412e6..a53e0811 100644 --- a/src/test/java/org/medicmobile/webapp/mobile/ChtExternalAppTest.java +++ b/src/test/java/org/medicmobile/webapp/mobile/ChtExternalAppTest.java @@ -126,7 +126,7 @@ public void createIntent_withSimpleData_setExtrasCorrectly() throws JSONExceptio "\"an.int\": 5," + "\"a.long\": 2147483649," + "\"a.double\": 2.8," + - "\"a.boolean\": true," + + "\"a.boolean\": \"true\"," + "\"a.string\": \"some text\"" + "}"; String arraysJson = "{" +