diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleFeedback.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleFeedback.java index a0ee401ee..f280c75f8 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleFeedback.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleFeedback.java @@ -73,24 +73,27 @@ private void getAvailableFeedbackWidgetsInternal(CallbackOnFinish feedbackEntries = parseFeedbackList(checkResponse); + List feedbackEntries = new ArrayList<>(); + String error = parseFeedbackList(checkResponse, feedbackEntries); + if (error != null) { + feedbackEntries = null; + } - callback.onFinished(feedbackEntries, null); + callback.onFinished(feedbackEntries, error); }, L); } - static List parseFeedbackList(JSONObject requestResponse) { + static String parseFeedbackList(JSONObject requestResponse, List parsedRes) { Log L = SDKCore.instance.L; L.d("[ModuleFeedback] parseFeedbackList, calling"); - List parsedRes = new ArrayList<>(); try { if (requestResponse != null) { JSONArray jArray = requestResponse.optJSONArray("result"); if (jArray == null) { L.w("[ModuleFeedback] parseFeedbackList, response does not have a valid 'result' entry. No widgets retrieved."); - return parsedRes; + return "Response does not have a valid 'result' entry. No widgets retrieved."; } for (int a = 0; a < jArray.length(); a++) { @@ -100,7 +103,7 @@ static List parseFeedbackList(JSONObject requestResponse) String valId = jObj.optString("_id", ""); String valType = jObj.optString("type", ""); String valName = jObj.optString("name", ""); - List valTagsArr = new ArrayList(); + List valTagsArr = new ArrayList<>(); JSONArray jTagArr = jObj.optJSONArray("tg"); if (jTagArr == null) { @@ -138,15 +141,16 @@ static List parseFeedbackList(JSONObject requestResponse) parsedRes.add(se); } catch (Exception ex) { - L.e("[ModuleFeedback] parseFeedbackList, failed to parse json, [" + ex.toString() + "]"); + L.w("[ModuleFeedback] parseFeedbackList, failed to parse json, [" + ex + "]"); } } } } catch (Exception ex) { - L.e("[ModuleFeedback] parseFeedbackList, Encountered exception while parsing feedback list, [" + ex.toString() + "]"); + L.e("[ModuleFeedback] parseFeedbackList, Encountered exception while parsing feedback list, [" + ex + "]"); + return "Encountered exception while parsing feedback list, [" + ex + "]"; } - return parsedRes; + return null; } private void reportFeedbackWidgetManuallyInternal(CountlyFeedbackWidget widgetInfo, JSONObject widgetData, Map widgetResult) { diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleFeedbackTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleFeedbackTests.java index 6bde6b48b..564412421 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleFeedbackTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/ModuleFeedbackTests.java @@ -19,7 +19,6 @@ import static ly.count.sdk.java.internal.TestUtils.getOS; import static ly.count.sdk.java.internal.TestUtils.validateEvent; -import static ly.count.sdk.java.internal.TestUtils.validateEQSize; import static org.mockito.Mockito.mock; @RunWith(JUnit4.class) @@ -50,7 +49,8 @@ private void init(Config cc) { public void parseFeedbackList_null() throws JSONException { init(TestUtils.getConfigFeedback()); - List result = ModuleFeedback.parseFeedbackList(null); + List result = new ArrayList<>(); + String error = ModuleFeedback.parseFeedbackList(null, result); Assert.assertNotNull(result); Assert.assertEquals(0, result.size()); } @@ -69,7 +69,8 @@ public void parseFeedbackList() throws JSONException { JSONObject jObj = new JSONObject(requestJson); - List ret = ModuleFeedback.parseFeedbackList(jObj); + List ret = new ArrayList<>(); + String error = ModuleFeedback.parseFeedbackList(jObj, ret); Assert.assertNotNull(ret); Assert.assertEquals(4, ret.size()); @@ -94,7 +95,8 @@ public void parseFeedbackList_oneGoodWithGarbage() throws JSONException { JSONObject jObj = new JSONObject(requestJson); - List ret = ModuleFeedback.parseFeedbackList(jObj); + List ret = new ArrayList<>(); + String error = ModuleFeedback.parseFeedbackList(jObj, ret); Assert.assertNotNull(ret); Assert.assertEquals(1, ret.size()); ValidateReturnedFeedbackWidget(FeedbackWidgetType.nps, "fsdfsdf", "5f97284635935cc338e78200", new String[] { "/" }, ret.get(0)); @@ -127,7 +129,8 @@ public void parseFeedbackList_faulty() throws JSONException { JSONObject jObj = new JSONObject(requestJson); - List ret = ModuleFeedback.parseFeedbackList(jObj); + List ret = new ArrayList<>(); + String error = ModuleFeedback.parseFeedbackList(jObj, ret); Assert.assertNotNull(ret); Assert.assertEquals(6, ret.size()); @@ -189,8 +192,9 @@ public void getAvailableFeedbackWidgets_garbageJsonInWrongStructure() { garbageArray.put(createGarbageJson()); JSONObject responseJson = new JSONObject(); responseJson.put("xxxx", garbageArray); + String expectedErrorMsg = "Response does not have a valid 'result' entry. No widgets retrieved."; - getAvailableFeedbackWidgets_base(new ArrayList<>(), responseJson); + getAvailableFeedbackWidgets_base(null, responseJson, expectedErrorMsg); } /** @@ -204,6 +208,10 @@ public void getAvailableFeedbackWidgets_null() { } public void getAvailableFeedbackWidgets_base(List expectedWidgets, JSONObject returnedResponse) { + getAvailableFeedbackWidgets_base(expectedWidgets, returnedResponse, null); + } + + public void getAvailableFeedbackWidgets_base(List expectedWidgets, JSONObject returnedResponse, String expectedErrorMsg) { init(TestUtils.getConfigFeedback()); ImmediateRequestI requestMaker = (requestData, customEndpoint, cp, requestShouldBeDelayed, networkingIsEnabled, callback, log) -> { @@ -226,7 +234,11 @@ public void getAvailableFeedbackWidgets_base(List expecte Assert.assertEquals(widgetResponse, expectedWidgets); } else { Assert.assertNull(response); - Assert.assertEquals("Not possible to retrieve widget list. Probably due to lack of connection to the server", error); + if (expectedErrorMsg != null) { + Assert.assertEquals(expectedErrorMsg, error); + } else { + Assert.assertEquals("Not possible to retrieve widget list. Probably due to lack of connection to the server", error); + } } }); }