Skip to content

Commit 10f4b5a

Browse files
author
Javen
committed
ongoing: test devices api
1 parent b9d8778 commit 10f4b5a

File tree

9 files changed

+174
-60
lines changed

9 files changed

+174
-60
lines changed

src/cn/jpush/api/JPushClient.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,15 @@ public TagAliasResult getDeviceTagAlias(String registrationId)
310310
return _deviceClient.getDeviceTagAlias(registrationId);
311311
}
312312

313-
public DefaultResult updateDeviceTagAlias(String registrationId, String alias, boolean clearTag,
313+
public DefaultResult updateDeviceTagAlias(String registrationId, boolean clearAlias, boolean clearTag)
314+
throws APIConnectionException, APIRequestException {
315+
return _deviceClient.updateDeviceTagAlias(registrationId, clearAlias, clearTag);
316+
}
317+
318+
public DefaultResult updateDeviceTagAlias(String registrationId, String alias,
314319
Set<String> tagsToAdd, Set<String> tagsToRemove)
315320
throws APIConnectionException, APIRequestException {
316-
return _deviceClient.updateDeviceTagAlias(registrationId, alias, clearTag, tagsToAdd, tagsToRemove);
321+
return _deviceClient.updateDeviceTagAlias(registrationId, alias, tagsToAdd, tagsToRemove);
317322
}
318323

319324
public TagListResult getTagList()

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public ResponseWrapper doRequest(String url, String content,
9090
private ResponseWrapper _doRequest(String url, String content,
9191
RequestMethod method) throws APIConnectionException, APIRequestException,
9292
SocketTimeoutException {
93-
LOG.debug("Send request to - " + url);
93+
94+
LOG.debug("Send request - " + method.toString() + " "+ url);
9495
if (null != content) {
9596
LOG.debug("Request Content - " + content);
9697
}
@@ -145,11 +146,14 @@ private ResponseWrapper _doRequest(String url, String content,
145146
} else {
146147
in = conn.getErrorStream();
147148
}
148-
InputStreamReader reader = new InputStreamReader(in, CHARSET);
149-
char[] buff = new char[1024];
150-
int len;
151-
while ((len = reader.read(buff)) > 0) {
152-
sb.append(buff, 0, len);
149+
150+
if (null != in) {
151+
InputStreamReader reader = new InputStreamReader(in, CHARSET);
152+
char[] buff = new char[1024];
153+
int len;
154+
while ((len = reader.read(buff)) > 0) {
155+
sb.append(buff, 0, len);
156+
}
153157
}
154158

155159
String responseContent = sb.toString();

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

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import cn.jpush.api.common.resp.DefaultResult;
1414
import cn.jpush.api.common.resp.ResponseWrapper;
1515

16+
import com.google.common.base.Preconditions;
1617
import com.google.gson.JsonArray;
1718
import com.google.gson.JsonObject;
1819
import com.google.gson.JsonPrimitive;
@@ -51,39 +52,53 @@ public TagAliasResult getDeviceTagAlias(String registrationId) throws APIConnect
5152
return BaseResult.fromResponse(response, TagAliasResult.class);
5253
}
5354

54-
public DefaultResult updateDeviceTagAlias(String registrationId, String alias, boolean clearTag,
55+
public DefaultResult updateDeviceTagAlias(String registrationId, boolean clearAlias, boolean clearTag) throws APIConnectionException, APIRequestException {
56+
Preconditions.checkArgument(clearAlias || clearTag, "It is not meaningful to do nothing.");
57+
58+
String url = HOST_NAME_SSL + DEVICE_PATH + "/" + registrationId;
59+
60+
JsonObject top = new JsonObject();
61+
if (clearAlias) {
62+
top.addProperty("alias", "");
63+
}
64+
if (clearTag) {
65+
top.addProperty("tags", "");
66+
}
67+
68+
ResponseWrapper response = _httpClient.sendPost(url, top.toString());
69+
70+
return DefaultResult.fromResponse(response, DefaultResult.class);
71+
}
72+
73+
public DefaultResult updateDeviceTagAlias(String registrationId, String alias,
5574
Set<String> tagsToAdd, Set<String> tagsToRemove) throws APIConnectionException, APIRequestException {
5675
String url = HOST_NAME_SSL + DEVICE_PATH + "/" + registrationId;
5776

5877
JsonObject top = new JsonObject();
5978
if (null != alias) {
6079
top.addProperty("alias", alias);
6180
}
62-
if (clearTag) {
63-
top.addProperty("tag", "");
64-
} else {
65-
JsonObject tagObject = new JsonObject();
66-
JsonArray tagsAdd = ServiceHelper.fromSet(tagsToAdd);
67-
if (tagsAdd.size() > 0) {
68-
tagObject.add("add", tagsAdd);
69-
}
70-
71-
JsonArray tagsRemove = ServiceHelper.fromSet(tagsToRemove);
72-
if (tagsRemove.size() > 0) {
73-
tagObject.add("remove", tagsRemove);
74-
}
75-
76-
if (tagObject.entrySet().size() > 0) {
77-
top.add("tag", tagObject);
78-
}
81+
82+
JsonObject tagObject = new JsonObject();
83+
JsonArray tagsAdd = ServiceHelper.fromSet(tagsToAdd);
84+
if (tagsAdd.size() > 0) {
85+
tagObject.add("add", tagsAdd);
86+
}
87+
88+
JsonArray tagsRemove = ServiceHelper.fromSet(tagsToRemove);
89+
if (tagsRemove.size() > 0) {
90+
tagObject.add("remove", tagsRemove);
91+
}
92+
93+
if (tagObject.entrySet().size() > 0) {
94+
top.add("tags", tagObject);
7995
}
8096

8197
ResponseWrapper response = _httpClient.sendPost(url, top.toString());
8298

8399
return DefaultResult.fromResponse(response, DefaultResult.class);
84100
}
85-
86-
101+
87102
// ------------- tags
88103

89104
public TagListResult getTagList() throws APIConnectionException, APIRequestException {
@@ -132,7 +147,7 @@ public DefaultResult addRemoveDevicesFromTag(String theTag, Set<String> toAddUse
132147
public DefaultResult deleteTag(String theTag, String platform) throws APIConnectionException, APIRequestException {
133148
String url = HOST_NAME_SSL + TAG_PATH + "/" + theTag;
134149
if (null != platform) {
135-
url += "/?platform=" + platform;
150+
url += "?platform=" + platform;
136151
}
137152

138153
ResponseWrapper response = _httpClient.sendDelete(url);
@@ -146,7 +161,7 @@ public DefaultResult deleteTag(String theTag, String platform) throws APIConnect
146161
public AliasDeviceListResult getAliasDeviceList(String alias, String platform) throws APIConnectionException, APIRequestException {
147162
String url = HOST_NAME_SSL + ALIAS_PATH + "/" + alias;
148163
if (null != platform) {
149-
url += "/?platform=" + platform;
164+
url += "?platform=" + platform;
150165
}
151166

152167
ResponseWrapper response = _httpClient.sendGet(url);
@@ -157,7 +172,7 @@ public AliasDeviceListResult getAliasDeviceList(String alias, String platform) t
157172
public DefaultResult deleteAlias(String alias, String platform) throws APIConnectionException, APIRequestException {
158173
String url = HOST_NAME_SSL + ALIAS_PATH + "/" + alias;
159174
if (null != platform) {
160-
url += "/?platform=" + platform;
175+
url += "?platform=" + platform;
161176
}
162177

163178
ResponseWrapper response = _httpClient.sendDelete(url);

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88

99
public class TagAliasResult extends BaseResult {
1010

11-
@Expose public List<Tag> tag;
11+
@Expose public List<String> tags;
1212
@Expose public String alias;
13-
14-
public static class Tag {
15-
@Expose public String tag;
16-
}
17-
13+
1814
}
1915

src/cn/jpush/api/examples/DevcieExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static void testUpdateDeviceTagAlias()
5454
tagsToRemove.add("tag4");
5555
tagsToRemove.add("tag3");
5656
DefaultResult result = jpushClient.updateDeviceTagAlias(
57-
REGISTRATION_ID1, ALIAS1, false, tagsToAdd, tagsToRemove);
57+
REGISTRATION_ID1, ALIAS1, tagsToAdd, tagsToRemove);
5858
assertTrue(result.isResultOK());
5959
}
6060

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.jpush.api.utils;
2+
3+
import java.util.Collections;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
7+
import org.junit.runners.BlockJUnit4ClassRunner;
8+
import org.junit.runners.model.FrameworkMethod;
9+
import org.junit.runners.model.InitializationError;
10+
11+
public class JUnitOrderedRunner extends BlockJUnit4ClassRunner {
12+
public JUnitOrderedRunner(Class<?> klass) throws InitializationError {
13+
super(klass);
14+
}
15+
16+
@Override
17+
protected List<FrameworkMethod> computeTestMethods() {
18+
List<FrameworkMethod> list = super.computeTestMethods();
19+
Collections.sort(list, new Comparator<FrameworkMethod>() {
20+
@Override
21+
public int compare(FrameworkMethod f1, FrameworkMethod f2) {
22+
TestOrder o1 = f1.getAnnotation(TestOrder.class);
23+
TestOrder o2 = f2.getAnnotation(TestOrder.class);
24+
25+
if (o1 == null || o2 == null)
26+
return -1;
27+
28+
return o1.order() - o2.order();
29+
}
30+
});
31+
return list;
32+
}
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cn.jpush.api.utils;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
6+
@Retention(RetentionPolicy.RUNTIME)
7+
public @interface TestOrder {
8+
public int order();
9+
}

test/cn/jpush/api/BaseTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public abstract class BaseTest {
2626
@Before
2727
public void before() {
2828
jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
29+
2930
}
3031

3132
}

0 commit comments

Comments
 (0)