Skip to content

Commit fe93624

Browse files
author
Javen
committed
1. Refactoring some base test class;
2. Begin to write device apis tests.
1 parent ec9f762 commit fe93624

15 files changed

+149
-55
lines changed

src/cn/jpush/api/JPushClient.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package cn.jpush.api;
22

33
import java.util.Map;
4+
import java.util.Set;
45

56
import cn.jpush.api.common.TimeUnit;
67
import cn.jpush.api.common.connection.HttpProxy;
78
import cn.jpush.api.common.resp.APIConnectionException;
89
import cn.jpush.api.common.resp.APIRequestException;
10+
import cn.jpush.api.common.resp.BooleanResult;
11+
import cn.jpush.api.common.resp.DefaultResult;
12+
import cn.jpush.api.device.AliasDeviceListResult;
13+
import cn.jpush.api.device.DeviceClient;
14+
import cn.jpush.api.device.TagAliasResult;
15+
import cn.jpush.api.device.TagListResult;
916
import cn.jpush.api.push.PushClient;
1017
import cn.jpush.api.push.PushResult;
1118
import cn.jpush.api.push.model.Message;
@@ -24,6 +31,7 @@
2431
public class JPushClient {
2532
private final PushClient _pushClient;
2633
private final ReportClient _reportClient;
34+
private final DeviceClient _deviceClient;
2735

2836
/**
2937
* Create a JPush Client.
@@ -34,16 +42,19 @@ public class JPushClient {
3442
public JPushClient(String masterSecret, String appKey) {
3543
_pushClient = new PushClient(masterSecret, appKey);
3644
_reportClient = new ReportClient(masterSecret, appKey);
45+
_deviceClient = new DeviceClient(masterSecret, appKey);
3746
}
3847

3948
public JPushClient(String masterSecret, String appKey, int maxRetryTimes) {
4049
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes);
41-
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes);
50+
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes);
51+
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes);
4252
}
4353

4454
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
4555
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy);
46-
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy);
56+
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy);
57+
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy);
4758
}
4859

4960
/**
@@ -59,6 +70,7 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
5970
public JPushClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) {
6071
_pushClient = new PushClient(masterSecret, appKey, apnsProduction, timeToLive);
6172
_reportClient = new ReportClient(masterSecret, appKey);
73+
_deviceClient = new DeviceClient(masterSecret, appKey);
6274
}
6375

6476

@@ -291,7 +303,51 @@ public PushResult sendMessageWithRegistrationID(String title, String msgContent,
291303

292304

293305

306+
// ----------------------- Device
294307

308+
public TagAliasResult getDeviceTagAlias(String registrationId)
309+
throws APIConnectionException, APIRequestException {
310+
return _deviceClient.getDeviceTagAlias(registrationId);
311+
}
295312

313+
public DefaultResult updateDeviceTagAlias(String registrationId, String alias, boolean clearTag,
314+
Set<String> tagsToAdd, Set<String> tagsToRemove)
315+
throws APIConnectionException, APIRequestException {
316+
return _deviceClient.updateDeviceTagAlias(registrationId, alias, clearTag, tagsToAdd, tagsToRemove);
317+
}
318+
319+
public TagListResult getTagList(String platform)
320+
throws APIConnectionException, APIRequestException {
321+
return _deviceClient.getTagList(platform);
322+
}
323+
324+
public BooleanResult isDeviceInTag(String theTag, String registrationID)
325+
throws APIConnectionException, APIRequestException {
326+
return _deviceClient.isDeviceInTag(theTag, registrationID);
327+
}
328+
329+
public DefaultResult addRemoveDevicesFromTag(String theTag,
330+
Set<String> toAddUsers, Set<String> toRemoveUsers)
331+
throws APIConnectionException, APIRequestException {
332+
return _deviceClient.addRemoveDevicesFromTag(theTag, toAddUsers,
333+
toRemoveUsers);
334+
}
335+
336+
public DefaultResult deleteTag(String theTag, String platform)
337+
throws APIConnectionException, APIRequestException {
338+
return _deviceClient.deleteTag(theTag, platform);
339+
}
340+
341+
public AliasDeviceListResult getAliasDeviceList(String alias,
342+
String platform) throws APIConnectionException, APIRequestException {
343+
return _deviceClient.getAliasDeviceList(alias, platform);
344+
}
345+
346+
public DefaultResult deleteAlias(String alias, String platform)
347+
throws APIConnectionException, APIRequestException {
348+
return _deviceClient.deleteAlias(alias, platform);
349+
}
350+
351+
296352
}
297353

src/cn/jpush/api/device/DeviceClient.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import cn.jpush.api.common.resp.APIConnectionException;
1010
import cn.jpush.api.common.resp.APIRequestException;
1111
import cn.jpush.api.common.resp.BaseResult;
12+
import cn.jpush.api.common.resp.BooleanResult;
1213
import cn.jpush.api.common.resp.DefaultResult;
1314
import cn.jpush.api.common.resp.ResponseWrapper;
1415
import cn.jpush.api.utils.StringUtils;
@@ -18,7 +19,7 @@
1819
import com.google.gson.JsonPrimitive;
1920

2021
public class DeviceClient {
21-
public static final String HOST_NAME_SSL = "https://devices.jpush.cn";
22+
public static final String HOST_NAME_SSL = "https://device.jpush.cn";
2223
public static final String DEVICE_PATH = "/v3/device";
2324
public static final String TAG_PATH = "/v3/tag";
2425
public static final String ALIAS_PATH = "/v3/alias";
@@ -97,12 +98,12 @@ public TagListResult getTagList(String platform) throws APIConnectionException,
9798
return BaseResult.fromResponse(response, TagListResult.class);
9899
}
99100

100-
public DefaultResult isDeviceInTag(String theTag, String registrationID) throws APIConnectionException, APIRequestException {
101+
public BooleanResult isDeviceInTag(String theTag, String registrationID) throws APIConnectionException, APIRequestException {
101102
String url = HOST_NAME_SSL + TAG_PATH + "/" + theTag + "/exist";
102103

103104
ResponseWrapper response = _httpClient.sendGet(url);
104105

105-
return BaseResult.fromResponse(response, DefaultResult.class);
106+
return BaseResult.fromResponse(response, BooleanResult.class);
106107
}
107108

108109
public DefaultResult addRemoveDevicesFromTag(String theTag, Set<String> toAddUsers, Set<String> toRemoveUsers) throws APIConnectionException, APIRequestException {
@@ -133,8 +134,11 @@ public DefaultResult addRemoveDevicesFromTag(String theTag, Set<String> toAddUse
133134
return BaseResult.fromResponse(response, DefaultResult.class);
134135
}
135136

136-
public DefaultResult deleteTag(String theTag) throws APIConnectionException, APIRequestException {
137+
public DefaultResult deleteTag(String theTag, String platform) throws APIConnectionException, APIRequestException {
137138
String url = HOST_NAME_SSL + TAG_PATH + "/" + theTag;
139+
if (null != platform) {
140+
url += "?platform=" + platform;
141+
}
138142

139143
ResponseWrapper response = _httpClient.sendDelete(url);
140144

@@ -144,20 +148,30 @@ public DefaultResult deleteTag(String theTag) throws APIConnectionException, API
144148

145149
// ------------- alias
146150

147-
public AliasDeviceListResult getAliasDeviceList(String alias) throws APIConnectionException, APIRequestException {
151+
public AliasDeviceListResult getAliasDeviceList(String alias, String platform) throws APIConnectionException, APIRequestException {
148152
String url = HOST_NAME_SSL + ALIAS_PATH + "/" + alias;
153+
if (null != platform) {
154+
url += "?platform=" + platform;
155+
}
149156

150157
ResponseWrapper response = _httpClient.sendGet(url);
151158

152159
return BaseResult.fromResponse(response, AliasDeviceListResult.class);
153160
}
154161

155-
public DefaultResult deleteAlias(String alias) throws APIConnectionException, APIRequestException {
162+
public DefaultResult deleteAlias(String alias, String platform) throws APIConnectionException, APIRequestException {
156163
String url = HOST_NAME_SSL + ALIAS_PATH + "/" + alias;
164+
if (null != platform) {
165+
url += "?platform=" + platform;
166+
}
157167

158168
ResponseWrapper response = _httpClient.sendDelete(url);
159169

160170
return BaseResult.fromResponse(response, DefaultResult.class);
161171
}
162172

163173
}
174+
175+
176+
177+

test/cn/jpush/api/BaseTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.jpush.api;
2+
3+
import org.junit.Before;
4+
5+
public abstract class BaseTest {
6+
7+
protected static final String APP_KEY ="dd1066407b044738b6479275";
8+
protected static final String MASTER_SECRET = "2b38ce69b1de2a7fa95706ea";
9+
10+
public static final String ALERT = "JPush Test - alert";
11+
public static final String MSG_CONTENT = "JPush Test - msgContent";
12+
13+
public static final String TAG1 = "tag1";
14+
public static final String TAG2 = "tag2";
15+
public static final String TAG_ALL = "tag_all";
16+
public static final String TAG_NO = "tag_no";
17+
public static final String ALIAS1 = "alias1";
18+
public static final String ALIAS2 = "alias2";
19+
public static final String ALIAS_NO = "alias_no";
20+
public static final String REGISTRATION_ID1 = "0900e8d85ef";
21+
public static final String REGISTRATION_ID2 = "0a04ad7d8b4";
22+
23+
24+
protected JPushClient jpushClient = null;
25+
26+
@Before
27+
public void before() {
28+
jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
29+
}
30+
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.jpush.api.device;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
7+
import cn.jpush.api.BaseTest;
8+
9+
public class DeviceNormalRemoteTest extends BaseTest {
10+
11+
12+
@Test
13+
public void testGetDeviceTagAlias_1() throws Exception {
14+
TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1);
15+
assertTrue(result.isResultOK());
16+
}
17+
18+
19+
}

test/cn/jpush/api/push/PushClientTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
import cn.jpush.api.common.APIRequestException;
1010
import cn.jpush.api.common.HttpProxy;
1111
import cn.jpush.api.push.model.PushPayload;
12+
import cn.jpush.api.BaseTest;
1213

13-
public class PushClientTest {
14-
private static final String appKey ="dd1066407b044738b6479275";
15-
private static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
14+
public class PushClientTest extends BaseTest {
1615

1716
@Test(expected = IllegalArgumentException.class)
1817
public void test_invalid_json() {
19-
PushClient pushClient = new PushClient(masterSecret, appKey);
18+
PushClient pushClient = new PushClient(MASTER_SECRET, APP_KEY);
2019

2120
try {
2221
pushClient.sendPush("{aaa:'a}");
@@ -29,7 +28,7 @@ public void test_invalid_json() {
2928

3029
@Test(expected = IllegalArgumentException.class)
3130
public void test_empty_string() {
32-
PushClient pushClient = new PushClient(masterSecret, appKey);
31+
PushClient pushClient = new PushClient(MASTER_SECRET, APP_KEY);
3332

3433
try {
3534
pushClient.sendPush("");

test/cn/jpush/api/push/remote/AlertOverrideTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import cn.jpush.api.push.model.notification.Notification;
1414
import cn.jpush.api.push.model.notification.WinphoneNotification;
1515

16-
public class AlertOverrideTest extends BaseRemoteTest {
16+
public class AlertOverrideTest extends BaseRemotePushTest {
1717

1818
@Test
1919
public void sendAlert_all() throws Exception {

test/cn/jpush/api/push/remote/AudienceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* alias_no: no Device
2929
*
3030
*/
31-
public class AudienceTest extends BaseRemoteTest {
31+
public class AudienceTest extends BaseRemotePushTest {
3232

3333
// one --------
3434

test/cn/jpush/api/push/remote/BaseRemoteTest.java renamed to test/cn/jpush/api/push/remote/BaseRemotePushTest.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
import org.junit.AfterClass;
77
import org.junit.BeforeClass;
88

9+
import cn.jpush.api.BaseTest;
910
import cn.jpush.api.push.PushClient;
1011

1112
import com.google.gson.JsonObject;
1213
import com.google.gson.JsonPrimitive;
1314

14-
public class BaseRemoteTest {
15-
public static final String appKey ="dd1066407b044738b6479275";
16-
public static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
15+
public class BaseRemotePushTest extends BaseTest {
1716

1817
public static final String CONTENT_TYPE_JSON = "application/json";
1918

@@ -24,19 +23,6 @@ public class BaseRemoteTest {
2423
public static final int TOO_BIG = 1005;
2524
public static final int APPKEY_NOT_EXIST = 1008;
2625
public static final int NO_TARGET = 1011;
27-
28-
public static final String ALERT = "JPush Test - alert";
29-
public static final String MSG_CONTENT = "JPush Test - msgContent";
30-
31-
public static final String TAG1 = "tag1";
32-
public static final String TAG2 = "tag2";
33-
public static final String TAG_ALL = "tag_all";
34-
public static final String TAG_NO = "tag_no";
35-
public static final String ALIAS1 = "alias1";
36-
public static final String ALIAS2 = "alias2";
37-
public static final String ALIAS_NO = "alias_no";
38-
public static final String REGISTRATION_ID1 = "0900e8d85ef";
39-
public static final String REGISTRATION_ID2 = "0a04ad7d8b4";
4026

4127
protected static PushClient _client = null;
4228

@@ -47,7 +33,7 @@ public void after() {
4733

4834
@BeforeClass
4935
public static void beforeClass() throws IOException {
50-
_client = new PushClient(masterSecret, appKey);
36+
_client = new PushClient(MASTER_SECRET, APP_KEY);
5137
}
5238

5339
@AfterClass

test/cn/jpush/api/push/remote/BasicFunctionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import cn.jpush.api.push.model.notification.Notification;
1616
import cn.jpush.api.push.model.notification.WinphoneNotification;
1717

18-
public class BasicFunctionsTest extends BaseRemoteTest {
18+
public class BasicFunctionsTest extends BaseRemotePushTest {
1919

2020
@Test
2121
public void sendSimpleNotification_Pall_Ndefault() throws Exception {

test/cn/jpush/api/push/remote/ExceptionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
import com.google.gson.JsonObject;
1515
import com.google.gson.JsonPrimitive;
1616

17-
public class ExceptionTest extends BaseRemoteTest {
17+
public class ExceptionTest extends BaseRemotePushTest {
1818

1919
@Test
2020
public void appKeyNotExist() {
2121
String appKey = "dd1066407b044738b6479274";
22-
JPushClient client = new JPushClient(masterSecret, appKey);
22+
JPushClient client = new JPushClient(MASTER_SECRET, appKey);
2323
PushPayload payload = PushPayload.alertAll(ALERT);
2424

2525
try {
@@ -34,7 +34,7 @@ public void appKeyNotExist() {
3434
@Test
3535
public void authenticationFail() {
3636
String masterSecret = "2b38ce69b1de2a7fa95706e2";
37-
JPushClient client = new JPushClient(masterSecret, appKey);
37+
JPushClient client = new JPushClient(masterSecret, APP_KEY);
3838
PushPayload payload = PushPayload.alertAll(ALERT);
3939
try {
4040
client.sendPush(payload);

0 commit comments

Comments
 (0)