Skip to content

Commit c2ea859

Browse files
committed
Tests for setPushTime
1 parent fe277b8 commit c2ea859

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

Parse/src/main/java/com/parse/ParsePush.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public Builder(State state) {
7373
: new ParseQuery<>(new ParseQuery.State.Builder<ParseInstallation>(state.queryState()));
7474
this.expirationTime = state.expirationTime();
7575
this.expirationTimeInterval = state.expirationTimeInterval();
76+
this.pushTime = state.pushTime();
7677
this.pushToIOS = state.pushToIOS();
7778
this.pushToAndroid = state.pushToAndroid();
7879
// Since in state.build() we check data is not null, we do not need to check it again here.

Parse/src/test/java/com/parse/ParsePushControllerTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public void testBuildRESTSendPushCommandWithChannelSet() throws Exception {
8383

8484
// Verify command
8585
JSONObject jsonParameters = pushCommand.jsonParameters;
86+
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_PUSH_TIME));
8687
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_TIME));
8788
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_INTERVAL));
8889
// Verify device type and query
@@ -113,6 +114,7 @@ public void testBuildRESTSendPushCommandWithExpirationTime() throws Exception {
113114

114115
// Verify command
115116
JSONObject jsonParameters = pushCommand.jsonParameters;
117+
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_PUSH_TIME));
116118
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_INTERVAL));
117119
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_CHANNELS));
118120
// Verify device type and query
@@ -123,6 +125,36 @@ public void testBuildRESTSendPushCommandWithExpirationTime() throws Exception {
123125
assertEquals(1400000000, jsonParameters.getLong(ParseRESTPushCommand.KEY_EXPIRATION_TIME));
124126
}
125127

128+
@Test
129+
public void testBuildRESTSendPushCommandWithPushTime() throws Exception {
130+
ParseHttpClient restClient = mock(ParseHttpClient.class);
131+
ParsePushController controller = new ParsePushController(restClient);
132+
133+
// Build PushState
134+
JSONObject data = new JSONObject();
135+
data.put(ParsePush.KEY_DATA_MESSAGE, "hello world");
136+
long pushTime = System.currentTimeMillis() / 1000 + 1000;
137+
ParsePush.State state = new ParsePush.State.Builder()
138+
.data(data)
139+
.pushTime(pushTime)
140+
.build();
141+
142+
// Build command
143+
ParseRESTCommand pushCommand = controller.buildRESTSendPushCommand(state, "sessionToken");
144+
145+
// Verify command
146+
JSONObject jsonParameters = pushCommand.jsonParameters;
147+
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_TIME));
148+
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_INTERVAL));
149+
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_CHANNELS));
150+
// Verify device type and query
151+
assertEquals("{}", jsonParameters.get(ParseRESTPushCommand.KEY_WHERE).toString());
152+
assertEquals("hello world",
153+
jsonParameters.getJSONObject(ParseRESTPushCommand.KEY_DATA)
154+
.getString(ParsePush.KEY_DATA_MESSAGE));
155+
assertEquals(pushTime, jsonParameters.getLong(ParseRESTPushCommand.KEY_PUSH_TIME));
156+
}
157+
126158
@Test
127159
public void testBuildRESTSendPushCommandWithExpirationTimeInterval() throws Exception {
128160
ParseHttpClient restClient = mock(ParseHttpClient.class);
@@ -141,6 +173,7 @@ public void testBuildRESTSendPushCommandWithExpirationTimeInterval() throws Exce
141173

142174
// Verify command
143175
JSONObject jsonParameters = pushCommand.jsonParameters;
176+
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_PUSH_TIME));
144177
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_TIME));
145178
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_CHANNELS));
146179
// Verify device type and query
@@ -172,6 +205,7 @@ public void testBuildRESTSendPushCommandWithQuery() throws Exception {
172205

173206
// Verify command
174207
JSONObject jsonParameters = pushCommand.jsonParameters;
208+
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_PUSH_TIME));
175209
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_TIME));
176210
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_INTERVAL));
177211
assertFalse(jsonParameters.getJSONObject(ParseRESTPushCommand.KEY_WHERE)
@@ -206,6 +240,7 @@ public void testBuildRESTSendPushCommandWithPushToAndroid() throws Exception {
206240

207241
// Verify command
208242
JSONObject jsonParameters = pushCommand.jsonParameters;
243+
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_PUSH_TIME));
209244
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_TIME));
210245
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_INTERVAL));
211246
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_CHANNELS));
@@ -235,6 +270,7 @@ public void testBuildRESTSendPushCommandWithPushToIOS() throws Exception {
235270

236271
// Verify command
237272
JSONObject jsonParameters = pushCommand.jsonParameters;
273+
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_PUSH_TIME));
238274
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_TIME));
239275
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_INTERVAL));
240276
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_CHANNELS));
@@ -265,6 +301,7 @@ public void testBuildRESTSendPushCommandWithPushToIOSAndAndroid() throws Excepti
265301

266302
// Verify command
267303
JSONObject jsonParameters = pushCommand.jsonParameters;
304+
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_PUSH_TIME));
268305
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_TIME));
269306
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_EXPIRATION_INTERVAL));
270307
assertFalse(jsonParameters.has(ParseRESTPushCommand.KEY_CHANNELS));

Parse/src/test/java/com/parse/ParsePushStateTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void testDefaultsWithData() throws Exception {
5454

5555
assertEquals(null, state.expirationTime());
5656
assertEquals(null, state.expirationTimeInterval());
57+
assertEquals(null, state.pushTime());
5758
assertEquals(null, state.channelSet());
5859
JSONAssert.assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE);
5960
assertEquals(null, state.pushToAndroid());
@@ -68,6 +69,7 @@ public void testCopy() throws JSONException {
6869
ParsePush.State state = mock(ParsePush.State.class);
6970
when(state.expirationTime()).thenReturn(1L);
7071
when(state.expirationTimeInterval()).thenReturn(2L);
72+
when(state.pushTime()).thenReturn(3L);
7173
Set channelSet = Sets.newSet("one", "two");
7274
when(state.channelSet()).thenReturn(channelSet);
7375
JSONObject data = new JSONObject();
@@ -82,6 +84,7 @@ public void testCopy() throws JSONException {
8284
ParsePush.State copy = new ParsePush.State.Builder(state).build();
8385
assertSame(1L, copy.expirationTime());
8486
assertSame(2L, copy.expirationTimeInterval());
87+
assertSame(3L, copy.pushTime());
8588
Set channelSetCopy = copy.channelSet();
8689
assertNotSame(channelSet, channelSetCopy);
8790
assertTrue(channelSetCopy.size() == 2 && channelSetCopy.contains("one"));
@@ -151,6 +154,55 @@ public void testExpirationTimeIntervalNormalInterval() {
151154

152155
//endregion
153156

157+
//region testPushTime
158+
159+
@Test
160+
public void testPushTimeNullTime() {
161+
ParsePush.State.Builder builder = new ParsePush.State.Builder();
162+
163+
ParsePush.State state = builder
164+
.pushTime(null)
165+
.data(new JSONObject())
166+
.build();
167+
168+
assertEquals(null, state.pushTime());
169+
}
170+
171+
@Test
172+
public void testPushTimeNormalTime() {
173+
ParsePush.State.Builder builder = new ParsePush.State.Builder();
174+
175+
long time = System.currentTimeMillis() / 1000 + 1000;
176+
ParsePush.State state = builder
177+
.pushTime(time)
178+
.data(new JSONObject())
179+
.build();
180+
181+
assertEquals(time, state.pushTime().longValue());
182+
}
183+
184+
@Test(expected = IllegalArgumentException.class)
185+
public void testPushTimeInThePast() {
186+
ParsePush.State.Builder builder = new ParsePush.State.Builder();
187+
188+
ParsePush.State state = builder
189+
.pushTime(System.currentTimeMillis() - 1000)
190+
.data(new JSONObject())
191+
.build();
192+
}
193+
194+
@Test(expected = IllegalArgumentException.class)
195+
public void testPushTimeTwoWeeksFromNow() {
196+
ParsePush.State.Builder builder = new ParsePush.State.Builder();
197+
198+
ParsePush.State state = builder
199+
.pushTime(System.currentTimeMillis() + 60*60*24*7*3)
200+
.data(new JSONObject())
201+
.build();
202+
}
203+
204+
//endregion
205+
154206
//region testChannelSet
155207

156208
@Test(expected = IllegalArgumentException.class)

Parse/src/test/java/com/parse/ParsePushTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,26 @@ public void testClearExpiration() {
206206

207207
//endregion
208208

209+
//region testSetPushTime
210+
211+
// We only test a basic case here to make sure logic in ParsePush is correct, more comprehensive
212+
// builder test cases should be in ParsePushState test
213+
@Test
214+
public void testSetPushTime() throws Exception {
215+
ParsePush push = new ParsePush();
216+
long time = System.currentTimeMillis() / 1000 + 1000;
217+
push.setPushTime(time);
218+
219+
// Right now it is hard for us to test a builder, so we build a state to test the builder is
220+
// set correctly
221+
// We have to set message otherwise build() will throw an exception
222+
push.setMessage("message");
223+
ParsePush.State state = push.builder.build();
224+
assertEquals(time, state.pushTime().longValue());
225+
}
226+
227+
//endregion
228+
209229
//region testSetPushToIOS
210230

211231
// We only test a basic case here to make sure logic in ParsePush is correct, more comprehensive

0 commit comments

Comments
 (0)