Skip to content

Commit 1909216

Browse files
authored
Use enum when reporting call; add via to search logs (#286)
* Use enum when reporting call; add via to search logs Using enum instead of string when reporting call just adds a bit of type safety. Adds via to search reporting. * Add test for issue search, improve report call test
1 parent 27e143e commit 1909216

File tree

3 files changed

+66
-38
lines changed

3 files changed

+66
-38
lines changed

5calls/app/src/androidTest/java/org/a5calls/android/a5calls/net/FiveCallsApiTest.java

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
import org.a5calls.android.a5calls.model.Contact;
1010
import org.a5calls.android.a5calls.model.Issue;
11+
import org.a5calls.android.a5calls.model.Outcome;
12+
import org.json.JSONException;
13+
import org.json.JSONObject;
1114
import org.junit.After;
1215
import org.junit.Before;
1316
import org.junit.Test;
@@ -352,28 +355,18 @@ public void testGetContacts_malformedJson() {
352355
}
353356

354357
@Test
355-
public void testReportCall() {
356-
byte[] bytes = "{\"ok\":true}".getBytes();
357-
ArrayList<Header> headers = new ArrayList<>();
358-
headers.add(new Header("Content-Type", "text/json"));
359-
HttpResponse response = new HttpResponse(200, headers);
360-
mHttpStack.setResponseToReturn(response);
361-
362-
TestCallListener testCallListener = new TestCallListener();
363-
mApi.registerCallRequestListener(testCallListener);
364-
365-
mApi.reportCall("myIssue", "myRep", "unavailable", "myLocation");
366-
waitForHttpRequestComplete();
367-
368-
assertEquals(1, testCallListener.mCallReported);
369-
assertEquals(0, testCallListener.mCallError);
370-
assertEquals(0, testCallListener.mCallJsonError);
358+
public void testReportCallUnavailable() {
359+
testReportCallStatus(Outcome.Status.UNAVAILABLE, "unavailable");
360+
}
371361

372-
assertEquals(new String(mHttpStack.getLastPostBody()),
373-
"result=unavailable&issueid=myIssue&contactid=myRep&callerid=itMe&via=" +
374-
(FiveCallsApi.TESTING ? "test&" : "android&"));
362+
@Test
363+
public void testReportCallVoicemail() {
364+
testReportCallStatus(Outcome.Status.VOICEMAIL, "voicemail");
365+
}
375366

376-
mApi.unregisterCallRequestListener(testCallListener);
367+
@Test
368+
public void testReportCallContact() {
369+
testReportCallStatus(Outcome.Status.CONTACT, "contact");
377370
}
378371

379372
@Test
@@ -383,7 +376,7 @@ public void testReportCall_serverError() {
383376
TestCallListener testCallListener = new TestCallListener();
384377
mApi.registerCallRequestListener(testCallListener);
385378

386-
mApi.reportCall("myIssue", "myRep", "unavailable", "myLocation");
379+
mApi.reportCall("myIssue", "myRep", Outcome.Status.UNAVAILABLE);
387380
waitForHttpRequestComplete();
388381

389382
assertEquals(0, testCallListener.mCallReported);
@@ -425,6 +418,50 @@ public void newsletterSubscribe_serverError() {
425418
assertFalse(testNewsletterListener.mSubscribeSuccess);
426419
}
427420

421+
@Test
422+
public void reportSearch() {
423+
ArrayList<Header> headers = new ArrayList<>();
424+
headers.add(new Header("Content-Type", "text/json"));
425+
HttpResponse response = new HttpResponse(200, headers);
426+
mHttpStack.setResponseToReturn(response);
427+
428+
mApi.reportSearch("Banana phone");
429+
waitForHttpRequestComplete();
430+
431+
assertEquals("https://api.5calls.org/v1/users/search", mHttpStack.getLastUrl());
432+
try {
433+
JSONObject params = new JSONObject(new String(mHttpStack.getLastPostBody()));
434+
assertEquals("Banana phone", params.get("query"));
435+
assertEquals("android", params.get("via"));
436+
} catch (JSONException e) {
437+
fail(e.getMessage());
438+
}
439+
}
440+
441+
private void testReportCallStatus(Outcome.Status status, String outcomeString) {
442+
ArrayList<Header> headers = new ArrayList<>();
443+
headers.add(new Header("Content-Type", "text/json"));
444+
HttpResponse response = new HttpResponse(200, headers);
445+
mHttpStack.setResponseToReturn(response);
446+
447+
TestCallListener testCallListener = new TestCallListener();
448+
mApi.registerCallRequestListener(testCallListener);
449+
450+
mApi.reportCall("myIssue", "myRep", status);
451+
waitForHttpRequestComplete();
452+
453+
assertEquals(1, testCallListener.mCallReported);
454+
assertEquals(0, testCallListener.mCallError);
455+
assertEquals(0, testCallListener.mCallJsonError);
456+
457+
assertEquals("https://api.5calls.org/v1/report", mHttpStack.getLastUrl());
458+
assertEquals(new String(mHttpStack.getLastPostBody()),
459+
"result=" + outcomeString + "&issueid=myIssue&contactid=myRep&callerid=itMe&via=" +
460+
(FiveCallsApi.TESTING ? "test&" : "android&"));
461+
462+
mApi.unregisterCallRequestListener(testCallListener);
463+
}
464+
428465
private void waitForHttpRequestComplete() {
429466
assertNotNull(mRequestQueue.mRequest);
430467
mRequestQueue.start();

5calls/app/src/main/java/org/a5calls/android/a5calls/controller/RepCallActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ private void reportCall(Outcome outcome, String address) {
202202
mIssue.name, mIssue.contacts.get(mActiveContactIndex).id,
203203
mIssue.contacts.get(mActiveContactIndex).name, outcome.status.toString(), address);
204204
AppSingleton.getInstance(getApplicationContext()).getJsonController().reportCall(
205-
mIssue.id, mIssue.contacts.get(mActiveContactIndex).id, outcome.label, address);
205+
mIssue.id, mIssue.contacts.get(mActiveContactIndex).id, outcome.status);
206206
}
207207

208208
private void setupContactUi(int index, boolean expandLocalSection) {

5calls/app/src/main/java/org/a5calls/android/a5calls/net/FiveCallsApi.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,7 @@ public void onErrorResponse(VolleyError error) {
407407
mRequestQueue.add(reportRequest);
408408
}
409409

410-
// Result is "VOICEMAIL", "unavailable", or "contacted"
411-
// https://github.com/5calls/5calls/blob/master/static/js/main.js#L221
412-
public void reportCall(final String issueId, final String contactId, final String result,
413-
final String zip) {
410+
public void reportCall(final String issueId, final String contactId, final Outcome.Status result) {
414411
String getReport = GET_REPORT;
415412
StringRequest request = new StringRequest(Request.Method.POST, getReport,
416413
new Response.Listener<String>() {
@@ -430,7 +427,7 @@ public void onErrorResponse(VolleyError error) {
430427
protected Map<String, String> getParams() {
431428
Map<String, String> params = new HashMap<String, String>();
432429
params.put("issueid", issueId);
433-
params.put("result", result);
430+
params.put("result", result.toString());
434431
params.put("contactid", contactId);
435432
params.put("via", (BuildConfig.DEBUG && TESTING) ? "test" : "android");
436433
params.put("callerid", mCallerId);
@@ -486,18 +483,12 @@ public void reportSearch(String searchTerm) {
486483
try {
487484
JSONObject jsonBody = new JSONObject();
488485
jsonBody.put("query", searchTerm);
486+
jsonBody.put("via", "android");
489487

490-
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, SEARCH_TRACKING, jsonBody, new Response.Listener<JSONObject>() {
491-
@Override
492-
public void onResponse(JSONObject response) {
493-
// Search report successful - no action needed
494-
}
495-
}, new Response.ErrorListener() {
496-
@Override
497-
public void onErrorResponse(VolleyError error) {
498-
Log.w(TAG, "Search tracking failed: " + error.getMessage());
499-
}
500-
});
488+
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, SEARCH_TRACKING,
489+
jsonBody, response -> {
490+
// Search report successful - no action needed
491+
}, error -> Log.w(TAG, "Search tracking failed: " + error.getMessage()));
501492
request.setTag(TAG);
502493
// Add the request to the RequestQueue.
503494
mRequestQueue.add(request);

0 commit comments

Comments
 (0)