Skip to content

Commit

Permalink
refactor: parse feedback list
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed Oct 11, 2023
1 parent d9ebb5c commit c2914ad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,27 @@ private void getAvailableFeedbackWidgetsInternal(CallbackOnFinish<List<CountlyFe

L.d("[ModuleFeedback] Retrieved request: [" + checkResponse + "]");

List<CountlyFeedbackWidget> feedbackEntries = parseFeedbackList(checkResponse);
List<CountlyFeedbackWidget> feedbackEntries = new ArrayList<>();
String error = parseFeedbackList(checkResponse, feedbackEntries);
if (error != null) {
feedbackEntries = null;
}

callback.onFinished(feedbackEntries, null);
callback.onFinished(feedbackEntries, error);
}, L);
}

static List<CountlyFeedbackWidget> parseFeedbackList(JSONObject requestResponse) {
static String parseFeedbackList(JSONObject requestResponse, List<CountlyFeedbackWidget> parsedRes) {
Log L = SDKCore.instance.L;
L.d("[ModuleFeedback] parseFeedbackList, calling");

List<CountlyFeedbackWidget> 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++) {
Expand All @@ -100,7 +103,7 @@ static List<CountlyFeedbackWidget> parseFeedbackList(JSONObject requestResponse)
String valId = jObj.optString("_id", "");
String valType = jObj.optString("type", "");
String valName = jObj.optString("name", "");
List<String> valTagsArr = new ArrayList<String>();
List<String> valTagsArr = new ArrayList<>();

JSONArray jTagArr = jObj.optJSONArray("tg");
if (jTagArr == null) {
Expand Down Expand Up @@ -138,15 +141,16 @@ static List<CountlyFeedbackWidget> 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<String, Object> widgetResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -50,7 +49,8 @@ private void init(Config cc) {
public void parseFeedbackList_null() throws JSONException {
init(TestUtils.getConfigFeedback());

List<CountlyFeedbackWidget> result = ModuleFeedback.parseFeedbackList(null);
List<CountlyFeedbackWidget> result = new ArrayList<>();
String error = ModuleFeedback.parseFeedbackList(null, result);
Assert.assertNotNull(result);
Assert.assertEquals(0, result.size());
}
Expand All @@ -69,7 +69,8 @@ public void parseFeedbackList() throws JSONException {

JSONObject jObj = new JSONObject(requestJson);

List<CountlyFeedbackWidget> ret = ModuleFeedback.parseFeedbackList(jObj);
List<CountlyFeedbackWidget> ret = new ArrayList<>();
String error = ModuleFeedback.parseFeedbackList(jObj, ret);
Assert.assertNotNull(ret);
Assert.assertEquals(4, ret.size());

Expand All @@ -94,7 +95,8 @@ public void parseFeedbackList_oneGoodWithGarbage() throws JSONException {

JSONObject jObj = new JSONObject(requestJson);

List<CountlyFeedbackWidget> ret = ModuleFeedback.parseFeedbackList(jObj);
List<CountlyFeedbackWidget> 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));
Expand Down Expand Up @@ -127,7 +129,8 @@ public void parseFeedbackList_faulty() throws JSONException {

JSONObject jObj = new JSONObject(requestJson);

List<CountlyFeedbackWidget> ret = ModuleFeedback.parseFeedbackList(jObj);
List<CountlyFeedbackWidget> ret = new ArrayList<>();
String error = ModuleFeedback.parseFeedbackList(jObj, ret);
Assert.assertNotNull(ret);
Assert.assertEquals(6, ret.size());

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -204,6 +208,10 @@ public void getAvailableFeedbackWidgets_null() {
}

public void getAvailableFeedbackWidgets_base(List<CountlyFeedbackWidget> expectedWidgets, JSONObject returnedResponse) {
getAvailableFeedbackWidgets_base(expectedWidgets, returnedResponse, null);
}

public void getAvailableFeedbackWidgets_base(List<CountlyFeedbackWidget> expectedWidgets, JSONObject returnedResponse, String expectedErrorMsg) {
init(TestUtils.getConfigFeedback());

ImmediateRequestI requestMaker = (requestData, customEndpoint, cp, requestShouldBeDelayed, networkingIsEnabled, callback, log) -> {
Expand All @@ -226,7 +234,11 @@ public void getAvailableFeedbackWidgets_base(List<CountlyFeedbackWidget> 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);
}
}
});
}
Expand Down

0 comments on commit c2914ad

Please sign in to comment.