88import java .text .SimpleDateFormat ;
99import java .util .ArrayList ;
1010import java .util .HashMap ;
11+ import java .util .List ;
12+ import java .util .Map ;
1113import java .util .concurrent .TimeUnit ;
1214
1315public class MessagingClientTest extends TestCase {
@@ -16,6 +18,7 @@ public class MessagingClientTest extends TestCase {
1618
1719 private String customerId ;
1820 private String apiKey ;
21+ private String phoneNumber ;
1922
2023 private SimpleDateFormat rfc2616 = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'" );
2124
@@ -25,13 +28,17 @@ public void setUp() throws Exception {
2528
2629 this .customerId = System .getenv ("CUSTOMER_ID" );
2730 this .apiKey = System .getenv ("API_KEY" );
31+ this .phoneNumber = System .getenv ("PHONE_NUMBER" );
2832
2933 if (this .customerId == null ) {
3034 this .customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890" ;
3135 }
3236 if (this .apiKey == null ) {
3337 this .apiKey = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==" ;
3438 }
39+ if (this .phoneNumber == null ) {
40+ this .phoneNumber = "11234567890" ;
41+ }
3542
3643 mockServer = new MockWebServer ();
3744 mockServer .start ();
@@ -222,6 +229,207 @@ public void testMessagingStatus() throws Exception {
222229 request .getHeader ("x-ts-auth-method" ));
223230 }
224231
232+ public void testCheckChannelCapability () throws Exception {
233+
234+ this .mockServer .enqueue (new MockResponse ().setBody ("{}" ));
235+
236+ MessagingClient client = new MessagingClient (this .customerId ,
237+ this .apiKey ,
238+ this .mockServer .url ("" ).toString ().replaceAll ("/$" , "" ));
239+
240+ String channel = "rcs" ;
241+ client .checkChannelCapability (channel , phoneNumber );
242+
243+ RecordedRequest request = this .mockServer .takeRequest (1 , TimeUnit .SECONDS );
244+
245+ assertEquals ("method is not as expected" , "GET" , request .getMethod ());
246+ assertEquals ("path is not as expected" , "/capability/" + channel + "/" + this .phoneNumber , request .getPath ());
247+ assertEquals ("body is not as expected" , "" , request .getBody ().readUtf8 ());
248+ assertEquals ("Content-Type header is not as expected" , "" ,
249+ request .getHeader ("Content-Type" ));
250+ assertEquals ("x-ts-auth-method header is not as expected" , "HMAC-SHA256" ,
251+ request .getHeader ("x-ts-auth-method" ));
252+ }
253+
254+ public void testCheckCapabilityRBMAgent () throws Exception {
255+
256+ this .mockServer .enqueue (new MockResponse ().setBody ("{}" ));
257+
258+ MessagingClient client = new MessagingClient (this .customerId ,
259+ this .apiKey ,
260+ this .mockServer .url ("" ).toString ().replaceAll ("/$" , "" ));
261+
262+ client .checkCapabilityRBMAgent (phoneNumber , "my_rbm_id" );
263+
264+ RecordedRequest request = this .mockServer .takeRequest (1 , TimeUnit .SECONDS );
265+
266+ assertEquals ("method is not as expected" , "GET" , request .getMethod ());
267+ assertEquals ("path is not as expected" , "/capability/rcs/" + this .phoneNumber + "/my_rbm_id" , request .getPath ());
268+ assertEquals ("body is not as expected" , "" , request .getBody ().readUtf8 ());
269+ assertEquals ("Content-Type header is not as expected" , "" ,
270+ request .getHeader ("Content-Type" ));
271+ assertEquals ("x-ts-auth-method header is not as expected" , "HMAC-SHA256" ,
272+ request .getHeader ("x-ts-auth-method" ));
273+ }
274+
275+
276+ public void testGetMsgStatus () throws Exception {
277+
278+ this .mockServer .enqueue (new MockResponse ().setBody ("{}" ));
279+
280+ MessagingClient client = new MessagingClient (this .customerId ,
281+ this .apiKey ,
282+ this .mockServer .url ("" ).toString ().replaceAll ("/$" , "" ));
283+
284+ String referenceId = "FakeReferenceId" ;
285+ String verifyCode = "123456" ;
286+
287+ client .getMsgStatus (referenceId , verifyCode );
288+
289+ RecordedRequest request = this .mockServer .takeRequest (1 , TimeUnit .SECONDS );
290+
291+ assertEquals ("method is not as expected" , "GET" , request .getMethod ());
292+ assertEquals ("path is not as expected" , "/v1/omnichannel/" + referenceId + "?verify_code=" + verifyCode , request .getPath ());
293+ assertEquals ("body is not as expected" , "" , request .getBody ().readUtf8 ());
294+ assertEquals ("Content-Type header is not as expected" , "" ,
295+ request .getHeader ("Content-Type" ));
296+ assertEquals ("x-ts-auth-method header is not as expected" , "HMAC-SHA256" ,
297+ request .getHeader ("x-ts-auth-method" ));
298+ }
299+
300+ public void testGetMsgStatusWithoutVerifyCode () throws Exception {
301+
302+ this .mockServer .enqueue (new MockResponse ().setBody ("{}" ));
303+
304+ MessagingClient client = new MessagingClient (this .customerId ,
305+ this .apiKey ,
306+ this .mockServer .url ("" ).toString ().replaceAll ("/$" , "" ));
307+
308+ String referenceId = "FakeReferenceId" ;
309+
310+ client .getMsgStatus (referenceId );
311+
312+ RecordedRequest request = this .mockServer .takeRequest (1 , TimeUnit .SECONDS );
313+
314+ assertEquals ("method is not as expected" , "GET" , request .getMethod ());
315+ assertEquals ("path is not as expected" , "/v1/omnichannel/" + referenceId , request .getPath ());
316+ assertEquals ("body is not as expected" , "" , request .getBody ().readUtf8 ());
317+ assertEquals ("Content-Type header is not as expected" , "" ,
318+ request .getHeader ("Content-Type" ));
319+ assertEquals ("x-ts-auth-method header is not as expected" , "HMAC-SHA256" ,
320+ request .getHeader ("x-ts-auth-method" ));
321+ }
322+
323+ public void testCreateMsgTemplate () throws Exception {
324+
325+ this .mockServer .enqueue (new MockResponse ().setBody ("{}" ));
326+
327+ MessagingClient client = new MessagingClient (this .customerId ,
328+ this .apiKey ,
329+ this .mockServer .url ("" ).toString ().replaceAll ("/$" , "" ));
330+
331+ Map <String , Object > params = new HashMap <>();
332+
333+ params .put ("type" , "standard" );
334+ params .put ("channel" , "whatsapp" );
335+ params .put ("name" , "wa_bogo" );
336+
337+ // body map
338+ Map <String , Object > body = new HashMap <>();
339+ body .put ("type" , "text" );
340+ body .put ("text" , "Buy one get one free!" );
341+
342+ // content item
343+ Map <String , Object > contentItem = new HashMap <>();
344+ contentItem .put ("body" , body );
345+
346+ // content list
347+ List <Map <String , Object >> content = new ArrayList <>();
348+ content .add (contentItem );
349+
350+ params .put ("content" , content );
351+
352+ client .createMsgTemplate (params );
353+
354+ RecordedRequest request = this .mockServer .takeRequest (1 , TimeUnit .SECONDS );
355+
356+ assertEquals ("method is not as expected" , "POST" , request .getMethod ());
357+ assertEquals ("path is not as expected" , "/v1/omnichannel/templates" , request .getPath ());
358+ assertEquals ("body is not as expected" , "{\" channel\" :\" whatsapp\" ,\" name\" :\" wa_bogo\" ,\" type\" :\" standard\" ,\" content\" :[{\" body\" :{\" text\" :\" Buy one get one free!\" ,\" type\" :\" text\" }}]}" , request .getBody ().readUtf8 ());
359+ assertEquals ("Content-Type header is not as expected" , "application/json" ,
360+ request .getHeader ("Content-Type" ));
361+ assertEquals ("x-ts-auth-method header is not as expected" , "HMAC-SHA256" ,
362+ request .getHeader ("x-ts-auth-method" ));
363+ }
364+
365+ public void testGetMsgTemplate () throws Exception {
366+
367+ this .mockServer .enqueue (new MockResponse ().setBody ("{}" ));
368+
369+ MessagingClient client = new MessagingClient (this .customerId ,
370+ this .apiKey ,
371+ this .mockServer .url ("" ).toString ().replaceAll ("/$" , "" ));
372+
373+ String channel = "whatsapp" ;
374+ String templateName = "wa_bogo" ;
375+
376+ client .getMsgTemplate (channel , templateName );
377+
378+ RecordedRequest request = this .mockServer .takeRequest (1 , TimeUnit .SECONDS );
379+
380+ assertEquals ("method is not as expected" , "GET" , request .getMethod ());
381+ assertEquals ("path is not as expected" , "/v1/omnichannel/templates/" + channel + "/" + templateName , request .getPath ());
382+ assertEquals ("body is not as expected" , "" , request .getBody ().readUtf8 ());
383+ assertEquals ("Content-Type header is not as expected" , "" ,
384+ request .getHeader ("Content-Type" ));
385+ assertEquals ("x-ts-auth-method header is not as expected" , "HMAC-SHA256" ,
386+ request .getHeader ("x-ts-auth-method" ));
387+ }
388+
389+ public void testGetAllMsgTemplates () throws Exception {
390+
391+ this .mockServer .enqueue (new MockResponse ().setBody ("{}" ));
392+
393+ MessagingClient client = new MessagingClient (this .customerId ,
394+ this .apiKey ,
395+ this .mockServer .url ("" ).toString ().replaceAll ("/$" , "" ));
396+
397+ client .getAllMsgTemplates ();
398+
399+ RecordedRequest request = this .mockServer .takeRequest (1 , TimeUnit .SECONDS );
400+
401+ assertEquals ("method is not as expected" , "GET" , request .getMethod ());
402+ assertEquals ("path is not as expected" , "/v1/omnichannel/templates" , request .getPath ());
403+ assertEquals ("body is not as expected" , "" , request .getBody ().readUtf8 ());
404+ assertEquals ("Content-Type header is not as expected" , "" ,
405+ request .getHeader ("Content-Type" ));
406+ assertEquals ("x-ts-auth-method header is not as expected" , "HMAC-SHA256" ,
407+ request .getHeader ("x-ts-auth-method" ));
408+ }
409+
410+ public void testDeleteMsgTemplate () throws Exception {
411+
412+ this .mockServer .enqueue (new MockResponse ().setBody ("{}" ));
413+
414+ MessagingClient client = new MessagingClient (this .customerId ,
415+ this .apiKey ,
416+ this .mockServer .url ("" ).toString ().replaceAll ("/$" , "" ));
417+
418+ String channel = "whatsapp" ;
419+ String templateName = "wa_bogo" ;
420+
421+ client .deleteMsgTemplate (channel , templateName );
422+
423+ RecordedRequest request = this .mockServer .takeRequest (1 , TimeUnit .SECONDS );
424+
425+ assertEquals ("method is not as expected" , "DELETE" , request .getMethod ());
426+ assertEquals ("path is not as expected" , "/v1/omnichannel/templates/" + channel + "/" + templateName , request .getPath ());
427+ assertEquals ("body is not as expected" , "" , request .getBody ().readUtf8 ());
428+ assertEquals ("Content-Type header is not as expected" , "" ,
429+ request .getHeader ("Content-Type" ));
430+ assertEquals ("x-ts-auth-method header is not as expected" , "HMAC-SHA256" ,
431+ request .getHeader ("x-ts-auth-method" ));
432+ }
225433
226434}
227435
0 commit comments