33import com .github .tomakehurst .wiremock .junit .WireMockRule ;
44import com .github .tomakehurst .wiremock .verification .LoggedRequest ;
55import com .jayway .awaitility .Awaitility ;
6- import com .pubnub .api .callbacks .PNCallback ;
76import com .pubnub .api .PubNubException ;
8- import com .pubnub .api .enums .PushType ;
7+ import com .pubnub .api .callbacks .PNCallback ;
8+ import com .pubnub .api .endpoints .TestHarness ;
99import com .pubnub .api .models .consumer .PNPublishResult ;
1010import com .pubnub .api .models .consumer .PNStatus ;
11- import com .pubnub .api .endpoints .TestHarness ;
1211import org .junit .Before ;
1312import org .junit .Rule ;
1413import org .junit .Test ;
1514
1615import java .io .IOException ;
17- import java .util .Arrays ;
16+ import java .util .HashMap ;
1817import java .util .List ;
18+ import java .util .Map ;
1919import java .util .concurrent .TimeUnit ;
2020import java .util .concurrent .atomic .AtomicInteger ;
2121
2222import static com .github .tomakehurst .wiremock .client .WireMock .*;
2323import static org .junit .Assert .assertEquals ;
24+ import static org .junit .Assert .assertFalse ;
2425
2526public class CreatePushNotificationTest extends TestHarness {
2627
2728 @ Rule
2829 public WireMockRule wireMockRule = new WireMockRule ();
2930
3031 private CreatePushNotification instance ;
32+ private Map <String , Object > pushPayload ;
3133
3234
3335 @ Before
3436 public void beforeEach () throws IOException {
3537 instance = this .createPubNubInstance (8080 ).createPushNotification ();
38+
39+ pushPayload = new HashMap <>();
40+ pushPayload .put ("a" , "b" );
41+ pushPayload .put ("c" , "d" );
42+
3643 }
3744
3845 @ Test
3946 public void appleSyncTest () throws PubNubException , InterruptedException {
4047
41- stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_apns%22%3A%5B%22a %22%2C%22b%22%5D %7D" ))
48+ stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_apns%22%3A%7B%22c %22%3A%22d%22% 2C%22a%22%3A% 22b%22%7D %7D" ))
4249 .willReturn (aResponse ().withBody ("[1,\" Sent\" ,\" 14598111595318003\" ]" )));
4350
44- instance .pushType ( PushType . APNS ). channel ("testChannel" )
45- .pushPayload ( Arrays . asList ( "a" , "b" ) ).sync ();
51+ instance .channel ("testChannel" )
52+ .addApplePayload ( pushPayload ).sync ();
4653
4754 List <LoggedRequest > requests = findAll (getRequestedFor (urlMatching ("/.*" )));
4855 assertEquals (1 , requests .size ());
4956 assertEquals ("myUUID" , requests .get (0 ).queryParameter ("uuid" ).firstValue ());
5057 }
5158
5259 @ Test
53- public void googleSyncTest () throws PubNubException , InterruptedException {
54- stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_gcm%22%3A%5B%22a %22%2C%22b%22%5D %7D" ))
60+ public void googleSyncTest () throws InterruptedException , PubNubException {
61+ stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_gcm%22%3A%7B%22c %22%3A%22d%22% 2C%22a%22%3A% 22b%22%7D %7D" ))
5562 .willReturn (aResponse ().withBody ("[1,\" Sent\" ,\" 14598111595318003\" ]" )));
5663
5764
58- instance .pushType (PushType .GCM ).channel ("testChannel" )
59- .pushPayload (Arrays .asList ("a" , "b" )).sync ();
65+ instance .channel ("testChannel" ).addGooglePayload (pushPayload ).sync ();
6066
6167 List <LoggedRequest > requests = findAll (getRequestedFor (urlMatching ("/.*" )));
6268 assertEquals (1 , requests .size ());
@@ -66,11 +72,29 @@ public void googleSyncTest() throws PubNubException, InterruptedException {
6672 @ Test
6773 public void microsoftSyncTest () throws PubNubException , InterruptedException {
6874
69- stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_mpns%22%3A%5B%22a%22%2C%22b%22%5D%7D" ))
75+ stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_mpns%22%3A%7B%22c%22%3A%22d%22%2C%22a%22%3A%22b%22%7D%7D" ))
76+ .willReturn (aResponse ().withBody ("[1,\" Sent\" ,\" 14598111595318003\" ]" )));
77+
78+ instance .channel ("testChannel" )
79+ .addMicrosoftPayload (pushPayload ).sync ();
80+
81+ List <LoggedRequest > requests = findAll (getRequestedFor (urlMatching ("/.*" )));
82+ assertEquals (1 , requests .size ());
83+ assertEquals ("myUUID" , requests .get (0 ).queryParameter ("uuid" ).firstValue ());
84+ }
85+
86+ @ Test
87+ public void allPayloadsSyncTest () throws InterruptedException , PubNubException {
88+
89+ stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_mpns%22%3A%7B%22c%22%3A%22d%22%2C%22a%22%3A%22b%22%7D%2C%22c%22%3A%22d%22%2C%22a%22%3A%22b%22%2C%22pn_gcm%22%3A%7B%22c%22%3A%22d%22%2C%22a%22%3A%22b%22%7D%2C%22pn_apns%22%3A%7B%22c%22%3A%22d%22%2C%22a%22%3A%22b%22%7D%7D" ))
7090 .willReturn (aResponse ().withBody ("[1,\" Sent\" ,\" 14598111595318003\" ]" )));
7191
72- instance .pushType (PushType .MPNS ).channel ("testChannel" )
73- .pushPayload (Arrays .asList ("a" , "b" )).sync ();
92+ instance .channel ("testChannel" )
93+ .addApplePayload (pushPayload )
94+ .addMicrosoftPayload (pushPayload )
95+ .addGooglePayload (pushPayload )
96+ .addPubNubPayload (pushPayload )
97+ .sync ();
7498
7599 List <LoggedRequest > requests = findAll (getRequestedFor (urlMatching ("/.*" )));
76100 assertEquals (1 , requests .size ());
@@ -80,15 +104,16 @@ public void microsoftSyncTest() throws PubNubException, InterruptedException {
80104 @ Test
81105 public void appleAsyncTest () throws PubNubException , InterruptedException {
82106
83- stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_apns%22:[% 22a%22,% 22b%22] %7D" ))
107+ stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_apns%22%3A%7B%22c%22%3A%22d%22%2C% 22a%22%3A% 22b%22%7D %7D" ))
84108 .willReturn (aResponse ().withBody ("[1,\" Sent\" ,\" 14598111595318003\" ]" )));
85109
86110 final AtomicInteger atomic = new AtomicInteger (0 );
87- instance .pushType ( PushType . APNS ). channel ("testChannel" )
88- .pushPayload ( Arrays . asList ( "a" , "b" ) ).async (new PNCallback <PNPublishResult >() {
111+ instance .channel ("testChannel" )
112+ .addApplePayload ( pushPayload ).async (new PNCallback <PNPublishResult >() {
89113 @ Override
90114 public void onResponse (PNPublishResult result , PNStatus status ) {
91115 List <LoggedRequest > requests = findAll (getRequestedFor (urlMatching ("/.*" )));
116+ assertFalse (status .isError ());
92117 assertEquals (1 , requests .size ());
93118 assertEquals ("myUUID" , requests .get (0 ).queryParameter ("uuid" ).firstValue ());
94119 atomic .addAndGet (1 );
@@ -102,14 +127,15 @@ public void onResponse(PNPublishResult result, PNStatus status) {
102127
103128 @ Test
104129 public void googleAsyncTest () throws PubNubException , InterruptedException {
105- stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_gcm%22:[% 22a%22,% 22b%22] %7D" ))
130+ stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_gcm%22%3A%7B%22c%22%3A%22d%22%2C% 22a%22%3A% 22b%22%7D %7D" ))
106131 .willReturn (aResponse ().withBody ("[1,\" Sent\" ,\" 14598111595318003\" ]" )));
107132 final AtomicInteger atomic = new AtomicInteger (0 );
108- instance .pushType ( PushType . GCM ). channel ("testChannel" )
109- .pushPayload ( Arrays . asList ( "a" , "b" ) ).async (new PNCallback <PNPublishResult >() {
133+ instance .channel ("testChannel" )
134+ .addGooglePayload ( pushPayload ).async (new PNCallback <PNPublishResult >() {
110135 @ Override
111136 public void onResponse (PNPublishResult result , PNStatus status ) {
112137 List <LoggedRequest > requests = findAll (getRequestedFor (urlMatching ("/.*" )));
138+ assertFalse (status .isError ());
113139 assertEquals (1 , requests .size ());
114140 assertEquals ("myUUID" , requests .get (0 ).queryParameter ("uuid" ).firstValue ());
115141 atomic .addAndGet (1 );
@@ -124,15 +150,16 @@ public void onResponse(PNPublishResult result, PNStatus status) {
124150 @ Test
125151 public void microsoftAsyncTest () throws PubNubException , InterruptedException {
126152
127- stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_mpns%22:[% 22a%22,% 22b%22] %7D" ))
153+ stubFor (get (urlPathEqualTo ("/publish/myPublishKey/mySubscribeKey/0/testChannel/0/%7B%22pn_mpns%22%3A%7B%22c%22%3A%22d%22%2C% 22a%22%3A% 22b%22%7D %7D" ))
128154 .willReturn (aResponse ().withBody ("[1,\" Sent\" ,\" 14598111595318003\" ]" )));
129155
130156 final AtomicInteger atomic = new AtomicInteger (0 );
131- instance .pushType ( PushType . MPNS ). channel ("testChannel" )
132- .pushPayload ( Arrays . asList ( "a" , "b" ) ).async (new PNCallback <PNPublishResult >() {
157+ instance .channel ("testChannel" )
158+ .addMicrosoftPayload ( pushPayload ).async (new PNCallback <PNPublishResult >() {
133159 @ Override
134160 public void onResponse (PNPublishResult result , PNStatus status ) {
135161 List <LoggedRequest > requests = findAll (getRequestedFor (urlMatching ("/.*" )));
162+ assertFalse (status .isError ());
136163 assertEquals (1 , requests .size ());
137164 assertEquals ("myUUID" , requests .get (0 ).queryParameter ("uuid" ).firstValue ());
138165 atomic .addAndGet (1 );
0 commit comments