Skip to content

Commit 3778a0c

Browse files
author
Javen
committed
1. Refactoring: add base test class
2. Beginning to add local mock webserver
1 parent 0f1909d commit 3778a0c

16 files changed

+641
-228
lines changed

pom.xml

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23
<modelVersion>4.0.0</modelVersion>
3-
4-
<groupId>cn.jpush.api</groupId>
5-
<artifactId>jpush-client</artifactId>
6-
<version>2.3.2-SNAPSHOT</version>
7-
<packaging>jar</packaging>
8-
<url>https://github.com/jpush/jpush-api-java-client</url>
9-
<name>JPush API Java Client</name>
10-
<description>JPush's officially supported Java client library for accessing JPush APIs.</description>
114

12-
<licenses>
5+
<groupId>cn.jpush.api</groupId>
6+
<artifactId>jpush-client</artifactId>
7+
<version>2.3.2-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<url>https://github.com/jpush/jpush-api-java-client</url>
10+
<name>JPush API Java Client</name>
11+
<description>JPush's officially supported Java client library for accessing JPush APIs.</description>
12+
13+
<licenses>
1314
<license>
1415
<name>The Apache Software License, Version 2.0</name>
1516
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
@@ -28,21 +29,21 @@
2829
<version>7</version>
2930
</parent>
3031

31-
<scm>
32-
<url>https://github.com/jpush/jpush-api-java-client</url>
33-
<connection>scm:git:git@github.com:jpush/jpush-api-java-client.git</connection>
34-
<developerConnection>scm:git:git@github.com:jpush/jpush-api-java-client.git</developerConnection>
35-
<tag>HEAD</tag>
36-
</scm>
37-
38-
<developers>
39-
<developer>
40-
<id>jpush</id>
41-
<name>JPush</name>
42-
<email>fajaven@gmail.com</email>
43-
<url>https://github.com/jpush</url>
44-
</developer>
45-
</developers>
32+
<scm>
33+
<url>https://github.com/jpush/jpush-api-java-client</url>
34+
<connection>scm:git:git@github.com:jpush/jpush-api-java-client.git</connection>
35+
<developerConnection>scm:git:git@github.com:jpush/jpush-api-java-client.git</developerConnection>
36+
<tag>HEAD</tag>
37+
</scm>
38+
39+
<developers>
40+
<developer>
41+
<id>jpush</id>
42+
<name>JPush</name>
43+
<email>fajaven@gmail.com</email>
44+
<url>https://github.com/jpush</url>
45+
</developer>
46+
</developers>
4647

4748
<dependencies>
4849
<dependency>
@@ -61,30 +62,35 @@
6162
<artifactId>slf4j-api</artifactId>
6263
<version>1.7.5</version>
6364
</dependency>
64-
<dependency>
65-
<groupId>log4j</groupId>
66-
<artifactId>log4j</artifactId>
67-
<version>1.2.16</version>
68-
</dependency>
69-
<dependency>
70-
<groupId>org.slf4j</groupId>
71-
<artifactId>slf4j-log4j12</artifactId>
72-
<version>1.7.5</version>
73-
</dependency>
74-
<dependency>
75-
<groupId>com.google.guava</groupId>
76-
<artifactId>guava</artifactId>
77-
<version>12.0</version>
78-
</dependency>
65+
<dependency>
66+
<groupId>log4j</groupId>
67+
<artifactId>log4j</artifactId>
68+
<version>1.2.16</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.slf4j</groupId>
72+
<artifactId>slf4j-log4j12</artifactId>
73+
<version>1.7.5</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>com.google.guava</groupId>
77+
<artifactId>guava</artifactId>
78+
<version>12.0</version>
79+
</dependency>
7980

80-
<dependency>
81-
<groupId>joda-time</groupId>
82-
<artifactId>joda-time</artifactId>
83-
<version>2.1</version>
84-
</dependency>
85-
81+
<dependency>
82+
<groupId>joda-time</groupId>
83+
<artifactId>joda-time</artifactId>
84+
<version>2.1</version>
85+
</dependency>
86+
<dependency>
87+
<groupId>com.squareup.okhttp</groupId>
88+
<artifactId>mockwebserver</artifactId>
89+
<version>1.5.4</version>
90+
<scope>test</scope>
91+
</dependency>
8692
</dependencies>
87-
93+
8894
<build>
8995
<sourceDirectory>src</sourceDirectory>
9096
<testSourceDirectory>test</testSourceDirectory>
@@ -134,7 +140,7 @@
134140
</plugin>
135141
</plugins>
136142
</build>
137-
143+
138144
<reporting>
139145
<plugins>
140146
<plugin>
@@ -158,5 +164,5 @@
158164
</plugin>
159165
</plugins>
160166
</reporting>
161-
167+
162168
</project>

src/cn/jpush/api/common/IHttpClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
public interface IHttpClient {
44

55
public static final String CHARSET = "UTF-8";
6+
public static final String CONTENT_TYPE_JSON = "application/json";
7+
public static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";
8+
69
public static final String RATE_LIMIT_QUOTA = "X-Rate-Limit-Limit";
710
public static final String RATE_LIMIT_Remaining = "X-Rate-Limit-Remaining";
811
public static final String RATE_LIMIT_Reset = "X-Rate-Limit-Reset";

src/cn/jpush/api/common/NativeHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public ResponseWrapper sendRequest(String url, String content, String method, St
6565

6666
if (METHOD_POST.equals(method)) {
6767
conn.setDoOutput(true); //POST Request
68-
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
68+
conn.setRequestProperty("Content-Type", CONTENT_TYPE_JSON);
6969
byte[] data = content.getBytes(CHARSET);
7070
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
7171
out = conn.getOutputStream();

src/cn/jpush/api/push/PushClient.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
* Can be used directly.
1717
*/
1818
public class PushClient {
19-
private static String HOST_NAME_SSL = "https://api.jpush.cn";
20-
private static final String PUSH_PATH = "/v3/push";
19+
public static String HOST_NAME_SSL = "https://api.jpush.cn";
20+
public static final String PUSH_PATH = "/v3/push";
2121

2222
private NativeHttpClient _httpClient = new NativeHttpClient();;
2323

@@ -38,12 +38,14 @@ public class PushClient {
3838

3939
// Generated HTTP Basic authorization string.
4040
private final String _authCode;
41-
41+
private String _baseUrl;
4242

4343
public PushClient(String masterSecret, String appKey) {
4444
this._masterSecret = masterSecret;
4545
this._appKey = appKey;
46+
4647
this._authCode = ServiceHelper.getAuthorizationBase64(_appKey, _masterSecret);
48+
this._baseUrl = HOST_NAME_SSL + PUSH_PATH;
4749
ServiceHelper.checkBasic(appKey, masterSecret);
4850
}
4951

@@ -54,21 +56,23 @@ public PushClient(String masterSecret, String appKey, boolean apnsProduction, lo
5456
this._overallSettingEnabled = true;
5557
}
5658

59+
public void setBaseUrl(String baseUrl) {
60+
this._baseUrl = baseUrl;
61+
}
62+
5763
public PushResult sendPush(PushPayload pushPayload) {
5864
if (_overallSettingEnabled) {
5965
pushPayload.resetOptionsTimeToLive(_timeToLive);
6066
pushPayload.resetOptionsApnsProduction(_apnsProduction);
6167
}
6268

63-
String url = HOST_NAME_SSL + PUSH_PATH;
64-
ResponseWrapper response = _httpClient.sendPost(url, pushPayload.toString(), _authCode);
69+
ResponseWrapper response = _httpClient.sendPost(_baseUrl, pushPayload.toString(), _authCode);
6570

6671
return PushResult.fromResponse(response);
6772
}
6873

6974
public PushResult sendPush(String payloadString) {
70-
String url = HOST_NAME_SSL + PUSH_PATH;
71-
ResponseWrapper response = _httpClient.sendPost(url, payloadString, _authCode);
75+
ResponseWrapper response = _httpClient.sendPost(_baseUrl, payloadString, _authCode);
7276

7377
return PushResult.fromResponse(response);
7478
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package cn.jpush.api.push.mock;
2+
3+
import java.io.IOException;
4+
import java.net.URL;
5+
6+
import org.junit.After;
7+
import org.junit.AfterClass;
8+
import org.junit.BeforeClass;
9+
10+
import cn.jpush.api.push.PushClient;
11+
12+
import com.google.gson.JsonObject;
13+
import com.google.gson.JsonPrimitive;
14+
import com.squareup.okhttp.mockwebserver.MockWebServer;
15+
16+
public class BaseMockTests {
17+
public static final String REMOTE_HOST = "https://localhost:8081";
18+
public static final String REMOTE_PATH = "/v3/push";
19+
20+
public static final String appKey ="dd1066407b044738b6479275";
21+
public static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
22+
23+
public static final String CONTENT_TYPE_JSON = "application/json";
24+
25+
public static final int SUCCEED_RESULT_CODE = 0;
26+
public static final int LACK_OF_PARAMS = 1002;
27+
public static final int INVALID_PARAMS = 1003;
28+
public static final int AUTHENTICATION_FAIL = 1004;
29+
public static final int TOO_BIG = 1005;
30+
public static final int APPKEY_NOT_EXIST = 1008;
31+
32+
public static final String ALERT = "JPush Test - alert";
33+
public static final String MSG_CONTENT = "JPush Test - msgContent";
34+
35+
public static final String TAG1 = "tag1";
36+
public static final String TAG2 = "tag2";
37+
public static final String TAG_ALL = "tag_all";
38+
public static final String TAG_NO = "tag_no";
39+
public static final String ALIAS1 = "alias1";
40+
public static final String ALIAS2 = "alias2";
41+
public static final String ALIAS_NO = "alias_no";
42+
public static final String REGISTRATION_ID1 = "0900e8d85ef";
43+
public static final String REGISTRATION_ID2 = "0a04ad7d8b4";
44+
45+
protected static PushClient _client = null;
46+
protected static MockWebServer _mockServer = null;
47+
protected static URL _mockUrl = null;
48+
49+
@After
50+
public void after() {
51+
// validate input params
52+
53+
}
54+
55+
@BeforeClass
56+
public static void beforeClass() throws IOException {
57+
_mockServer = new MockWebServer();
58+
59+
_mockServer.play();
60+
_mockUrl = _mockServer.getUrl("/v3/push/");
61+
62+
_client = new PushClient(masterSecret, appKey);
63+
_client.setBaseUrl(_mockUrl.toString());
64+
}
65+
66+
@AfterClass
67+
public static void afterClass() throws IOException {
68+
_mockServer.shutdown();
69+
}
70+
71+
72+
protected String getResponseOK(int msgid, int sendno) {
73+
JsonObject json = new JsonObject();
74+
json.add("msg_id", new JsonPrimitive(msgid));
75+
json.add("sendno", new JsonPrimitive(sendno));
76+
return json.toString();
77+
}
78+
79+
protected String getResponseError(int msgid, int sendno, int errorCode, String errorMessage) {
80+
JsonObject json = new JsonObject();
81+
json.add("msg_id", new JsonPrimitive(msgid));
82+
json.add("sendno", new JsonPrimitive(sendno));
83+
84+
JsonObject error = new JsonObject();
85+
error.add("code", new JsonPrimitive(errorCode));
86+
error.add("message", new JsonPrimitive(errorMessage));
87+
88+
json.add("error", error);
89+
return json.toString();
90+
}
91+
92+
93+
94+
}
95+

test/cn/jpush/api/push/ExceptionTests.java renamed to test/cn/jpush/api/push/mock/ExceptionTests.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package cn.jpush.api.push;
2-
import static org.junit.Assert.assertEquals;
1+
package cn.jpush.api.push.mock;
2+
import static org.junit.Assert.*;
33

4-
import org.junit.Before;
54
import org.junit.Test;
65

76
import cn.jpush.api.JPushClient;
7+
import cn.jpush.api.push.PushResult;
88
import cn.jpush.api.push.model.Platform;
99
import cn.jpush.api.push.model.PushPayload;
1010
import cn.jpush.api.push.model.audience.Audience;
@@ -13,27 +13,7 @@
1313
import com.google.gson.JsonObject;
1414
import com.google.gson.JsonPrimitive;
1515

16-
public class ExceptionTests {
17-
private static final String appKey ="dd1066407b044738b6479275";
18-
private static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
19-
20-
private static final int LACK_OF_PARAMS = 1002;
21-
private static final int INVALID_PARAMS = 1003;
22-
private static final int AUTHENTICATION_FAIL = 1004;
23-
private static final int TOO_BIG = 1005;
24-
private static final int APPKEY_NOT_EXIST = 1008;
25-
26-
private static final String ALERT = "JPush Test - alert";
27-
private static final String MSG_CONTENT = "JPush Test - msgContent";
28-
private static final String TAG = "tag_api";
29-
private static final String ALIAS = "alias_api";
30-
31-
private JPushClient _client = null;
32-
33-
@Before
34-
public void before() {
35-
_client = new JPushClient(masterSecret, appKey);
36-
}
16+
public class ExceptionTests extends BaseMockTests {
3717

3818
@Test
3919
public void appKeyNotExist() {

0 commit comments

Comments
 (0)