11package cn .jpush .api .push .mock ;
22
3+ import static org .junit .Assert .*;
4+
35import java .io .IOException ;
46import java .net .URL ;
7+ import java .util .ArrayList ;
8+ import java .util .List ;
9+ import java .util .Map ;
510
611import org .junit .After ;
712import org .junit .AfterClass ;
13+ import org .junit .Before ;
814import org .junit .BeforeClass ;
915
1016import cn .jpush .api .push .PushClient ;
17+ import cn .jpush .api .push .PushResult ;
1118
19+ import com .google .gson .JsonArray ;
20+ import com .google .gson .JsonElement ;
1221import com .google .gson .JsonObject ;
22+ import com .google .gson .JsonParser ;
1323import com .google .gson .JsonPrimitive ;
24+ import com .squareup .okhttp .mockwebserver .MockResponse ;
1425import com .squareup .okhttp .mockwebserver .MockWebServer ;
26+ import com .squareup .okhttp .mockwebserver .RecordedRequest ;
1527
1628public class BaseMockTests {
1729 public static final String REMOTE_HOST = "https://localhost:8081" ;
@@ -21,6 +33,20 @@ public class BaseMockTests {
2133 public static final String masterSecret = "2b38ce69b1de2a7fa95706ea" ;
2234
2335 public static final String CONTENT_TYPE_JSON = "application/json" ;
36+ public static final List <String > SUPPORT_PLATFORM = new ArrayList <String >();
37+ public static final List <String > SUPPORT_AUDIENCE = new ArrayList <String >();
38+
39+ static {
40+ SUPPORT_PLATFORM .add ("android" );
41+ SUPPORT_PLATFORM .add ("ios" );
42+ SUPPORT_PLATFORM .add ("winphone" );
43+
44+ SUPPORT_AUDIENCE .add ("tag" );
45+ SUPPORT_AUDIENCE .add ("tag_and" );
46+ SUPPORT_AUDIENCE .add ("alias" );
47+ SUPPORT_AUDIENCE .add ("registration_id" );
48+ SUPPORT_AUDIENCE .add ("segment" );
49+ }
2450
2551 public static final int SUCCEED_RESULT_CODE = 0 ;
2652 public static final int LACK_OF_PARAMS = 1002 ;
@@ -41,15 +67,115 @@ public class BaseMockTests {
4167 public static final String ALIAS_NO = "alias_no" ;
4268 public static final String REGISTRATION_ID1 = "0900e8d85ef" ;
4369 public static final String REGISTRATION_ID2 = "0a04ad7d8b4" ;
44-
70+
4571 protected static PushClient _client = null ;
4672 protected static MockWebServer _mockServer = null ;
4773 protected static URL _mockUrl = null ;
74+ protected String _currentPayload = null ;
75+ protected int _expectedErrorCode = 0 ;
76+
77+ @ Before
78+ public void before () {
79+ }
80+
81+ private void basicRequestCheck () throws Exception {
82+ RecordedRequest request = _mockServer .takeRequest ();
83+ assertNotNull ("" , request .getHeader ("Authorization" ));
84+ // assertEquals("", _currentPayload, request.getUtf8Body());
85+ assertEquals ("" , CONTENT_TYPE_JSON , request .getHeader ("Content-Type" ));
86+ assertEquals ("" , "keep-alive" , request .getHeader ("Connection" ));
87+ }
4888
4989 @ After
50- public void after () {
51- // validate input params
90+ public void after () throws Exception {
91+ int sendno = 0 ;
92+ int responseCode = 200 ;
93+ int errorCode = 0 ;
94+ String errorMessage = null ;
95+
96+ JsonParser parser = new JsonParser ();
97+ JsonObject json = parser .parse (_currentPayload ).getAsJsonObject ();
98+
99+ if (!json .has ("platform" ) || !json .has ("audience" )
100+ || (!json .has ("notification" ) && !json .has ("message" ))) {
101+ responseCode = 400 ;
102+ errorCode = 1002 ;
103+ errorMessage = "Lack of params." ;
104+
105+ } else {
106+
107+ JsonElement platform = json .get ("platform" );
108+ if (platform .isJsonPrimitive () && !"all" .equals (platform .getAsString ())) {
109+ responseCode = 400 ;
110+ errorCode = 1003 ;
111+ errorMessage = "Invalid param - platform string should only be 'all'" ;
112+ }
113+
114+ if (platform .isJsonArray ()) {
115+ JsonArray platformArray = platform .getAsJsonArray ();
116+ for (int i = 0 ; i < platformArray .size (); i ++) {
117+ String onePlatform = platformArray .get (i ).getAsString ();
118+ if (!SUPPORT_PLATFORM .contains (onePlatform )) {
119+ responseCode = 400 ;
120+ errorCode = 1003 ;
121+ errorMessage = "Invalid param - platform is invalid - " + onePlatform ;
122+ break ;
123+ }
124+ }
125+ }
126+
127+ JsonElement audience = json .get ("audience" );
128+ if (audience .isJsonPrimitive () && !"all" .equals (audience .getAsString ())) {
129+ responseCode = 400 ;
130+ errorCode = 1003 ;
131+ errorMessage = "Invalid param - audience string should only be 'all'" ;
132+ }
133+
134+ if (audience .isJsonObject ()) {
135+ JsonObject audienceObject = audience .getAsJsonObject ();
136+ for (Map .Entry <String , JsonElement > entry : audienceObject .entrySet ()) {
137+ if (!SUPPORT_AUDIENCE .contains (entry .getKey ())) {
138+ responseCode = 400 ;
139+ errorCode = 1003 ;
140+ errorMessage = "Invalid param - audience is invalid - " + entry .getKey ();
141+ }
142+ }
143+ }
144+
145+ if (json .has ("notification" )) {
146+ JsonElement notification = json .get ("notification" );
147+ if (!notification .isJsonObject ()) {
148+ responseCode = 400 ;
149+ errorCode = 1003 ;
150+ errorMessage = "Invalid param - notification is invalid - should not be string value. " ;
151+ } else {
152+
153+ }
154+
155+ } else if (json .has ("message" )) {
156+ JsonElement message = json .get ("message" );
157+ if (!message .isJsonObject ()) {
158+ responseCode = 400 ;
159+ errorCode = 1003 ;
160+ errorMessage = "Invalid param - message is invalid - should not be string value. " ;
161+ }
162+ }
163+
164+ }
165+
166+ if (responseCode == 200 ) {
167+ _mockServer .enqueue (new MockResponse ()
168+ .setBody (getResponseOK (111 , sendno )));
169+ } else {
170+ _mockServer .enqueue (new MockResponse ()
171+ .setResponseCode (responseCode )
172+ .setBody (getResponseError (111 , sendno , errorCode , errorMessage )));
173+ }
174+
175+ PushResult result = _client .sendPush (_currentPayload );
176+ assertEquals ("" , _expectedErrorCode , result .getErrorCode ());
52177
178+ basicRequestCheck ();
53179 }
54180
55181 @ BeforeClass
0 commit comments