88
99import org .a5calls .android .a5calls .model .Contact ;
1010import org .a5calls .android .a5calls .model .Issue ;
11+ import org .a5calls .android .a5calls .model .Outcome ;
12+ import org .json .JSONException ;
13+ import org .json .JSONObject ;
1114import org .junit .After ;
1215import org .junit .Before ;
1316import 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 ();
0 commit comments