Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/org/medicmobile/webapp/mobile/ChtExternalApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSONObject> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MedicLog> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "{" +
Expand Down