ParsePush { public Task<Void> sendInBackground() { final State state = builder.build(); return ParseUser.getCurrentSessionTokenAsync().onSuccessTask(new Continuation<String, Task<Void>>() { @Override public Task<Void> then(Task<String> task) { String sessionToken = task.getResult(); return getPushController().sendInBackground(state, sessionToken); } }); } ParsePush();  ParsePush(ParsePush push); private  ParsePush(State.Builder builder); static Task<Void> subscribeInBackground(String channel); static void subscribeInBackground(String channel, SaveCallback callback); static Task<Void> unsubscribeInBackground(String channel); static void unsubscribeInBackground(String channel, SaveCallback callback); static Task<Void> sendMessageInBackground(String message,
                                                     ParseQuery<ParseInstallation> query); static void sendMessageInBackground(String message, ParseQuery<ParseInstallation> query,
                                               SendCallback callback); static Task<Void> sendDataInBackground(JSONObject data,
                                                  ParseQuery<ParseInstallation> query); static void sendDataInBackground(JSONObject data, ParseQuery<ParseInstallation> query,
                                            SendCallback callback); void setChannel(String channel); void setChannels(Collection<String> channels); void setQuery(ParseQuery<ParseInstallation> query); void setExpirationTime(long time); void setExpirationTimeInterval(long timeInterval); void clearExpiration(); void setPushTime(long time); void setData(JSONObject data); void setMessage(String message); Task<Void> sendInBackground(); void send(); void sendInBackground(SendCallback callback);  }
@Test public void testSendInBackgroundSuccess() throws Exception { ParsePushController controller = mock(ParsePushController.class); when(controller.sendInBackground(any(ParsePush.State.class), anyString())) .thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerPushController(controller); ParsePush push = new ParsePush(); JSONObject data = new JSONObject(); data.put("key", "value"); List<String> channels = new ArrayList<>(); channels.add("test"); channels.add("testAgain"); push.builder.expirationTime((long) 1000) .data(data) .channelSet(channels); ParseTaskUtils.wait(push.sendInBackground()); ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class); verify(controller, times(1)).sendInBackground(stateCaptor.capture(), anyString()); ParsePush.State state = stateCaptor.getValue(); assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE); assertEquals(2, state.channelSet().size()); assertTrue(state.channelSet().contains("test")); assertTrue(state.channelSet().contains("testAgain")); }
@Test public void testSendInBackgroundWithCallbackSuccess() throws Exception { ParsePushController controller = mock(ParsePushController.class); when(controller.sendInBackground(any(ParsePush.State.class), anyString())) .thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerPushController(controller); ParsePush push = new ParsePush(); JSONObject data = new JSONObject(); data.put("key", "value"); List<String> channels = new ArrayList<>(); channels.add("test"); channels.add("testAgain"); push.builder.expirationTime((long) 1000) .data(data) .channelSet(channels); final Semaphore done = new Semaphore(0); final Capture<Exception> exceptionCapture = new Capture<>(); push.sendInBackground(new SendCallback() { @Override public void done(ParseException e) { exceptionCapture.set(e); done.release(); } }); assertNull(exceptionCapture.get()); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class); verify(controller, times(1)).sendInBackground(stateCaptor.capture(), anyString()); ParsePush.State state = stateCaptor.getValue(); assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE); assertEquals(2, state.channelSet().size()); assertTrue(state.channelSet().contains("test")); assertTrue(state.channelSet().contains("testAgain")); }
@Test public void testSendInBackgroundFail() throws Exception { ParsePushController controller = mock(ParsePushController.class); ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error"); when(controller.sendInBackground(any(ParsePush.State.class), anyString())) .thenReturn(Task.<Void>forError(exception)); ParseCorePlugins.getInstance().registerPushController(controller); ParsePush push = new ParsePush(); JSONObject data = new JSONObject(); data.put("key", "value"); List<String> channels = new ArrayList<>(); channels.add("test"); channels.add("testAgain"); push.builder.expirationTime((long) 1000) .data(data) .channelSet(channels); Task<Void> pushTask = push.sendInBackground(); pushTask.waitForCompletion(); ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class); verify(controller, times(1)).sendInBackground(stateCaptor.capture(), anyString()); ParsePush.State state = stateCaptor.getValue(); assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE); assertEquals(2, state.channelSet().size()); assertTrue(state.channelSet().contains("test")); assertTrue(state.channelSet().contains("testAgain")); assertTrue(pushTask.isFaulted()); assertSame(exception, pushTask.getError()); }
@Test public void testSendInBackgroundWithCallbackFail() throws Exception { ParsePushController controller = mock(ParsePushController.class); final ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error"); when(controller.sendInBackground(any(ParsePush.State.class), anyString())) .thenReturn(Task.<Void>forError(exception)); ParseCorePlugins.getInstance().registerPushController(controller); ParsePush push = new ParsePush(); JSONObject data = new JSONObject(); data.put("key", "value"); List<String> channels = new ArrayList<>(); channels.add("test"); channels.add("testAgain"); push.builder.expirationTime((long) 1000) .data(data) .channelSet(channels); final Semaphore done = new Semaphore(0); final Capture<Exception> exceptionCapture = new Capture<>(); push.sendInBackground(new SendCallback() { @Override public void done(ParseException e) { exceptionCapture.set(e); done.release(); } }); assertSame(exception, exceptionCapture.get()); assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class); verify(controller, times(1)).sendInBackground(stateCaptor.capture(), anyString()); ParsePush.State state = stateCaptor.getValue(); assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE); assertEquals(2, state.channelSet().size()); assertTrue(state.channelSet().contains("test")); assertTrue(state.channelSet().contains("testAgain")); }