GcmSender extends AbstractSender<GcmClient> { @Override public void onNewDataComplete() { Map<String,String> payload = new HashMap<>(); payload.put(DATA_KEY_SOURCE, source.getId()); payload.put(DATA_KEY_DEBUG, Boolean.TRUE.toString()); String collapseKey = source.getId(); final List<String> devices = new ArrayList<>(); devices.add(client.getGcmClientId()); Message.Builder builder = new Message.Builder().collapseKey(collapseKey); for (Map.Entry<String, String> e : payload.entrySet()) { builder.addData(e.getKey(), e.getValue()); } MulticastResult multicastResult; try { multicastResult = gcmSender.send(builder.build(), devices, 5); } catch (IOException io) { setErrorResult(io); return; } List<Result> results = multicastResult.getResults(); for (int i = 0; i < devices.size(); i++) { String regId = devices.get(i); Result result = results.get(i); String messageId = result.getMessageId(); if (messageId != null) { Log.info("Succesfully sent message to device: " + regId + "; messageId = " + messageId); String canonicalRegId = result.getCanonicalRegistrationId(); if (canonicalRegId != null) { setUpdateClientResult( client, new GcmClient(client.getId(), canonicalRegId)); return; } } else { String error = result.getErrorCodeName(); if (error.equals(Constants.ERROR_NOT_REGISTERED)) { setRemoveClientResult(client); return; } else { setErrorResult(error); return; } } } setSuccessResult(); } @Inject  GcmSender(
			@Assisted DataSource source,
			@Assisted GcmClient client,
			com.google.android.gcm.server.Sender gcmSender); @Override void onNewDataStart(); @Override void onNewDataItem(ObjectNode data); @Override void onNewDataComplete();  }
@Test @SuppressWarnings("unchecked") public void testGcmError() throws Exception { final IOException ioe = new IOException("boom"); new Expectations() {{ gcmService.send((Message) any, (List) any, anyInt); result = ioe; }}; sender.onNewDataComplete(); Assert.assertEquals(SenderResult.Status.ERROR, sender.getSenderResult().getStatus()); Assert.assertEquals(ioe, sender.getSenderResult().getErrorCause()); }
@Test @SuppressWarnings("unchecked") public void testGcmUpdate() throws Exception { final String newDeviceId = "someNewDeviceId"; new Expectations() {{ gcmService.send((Message) any, (List) any, anyInt); result = gcmMulticastResult; gcmMulticastResult.getResults(); result = Arrays.asList(gcmResult); gcmResult.getMessageId(); result = "someMessageId"; gcmResult.getCanonicalRegistrationId(); result = newDeviceId; }}; sender.onNewDataComplete(); Assert.assertEquals(SenderResult.Status.UPDATE_CLIENT, sender.getSenderResult().getStatus()); Assert.assertEquals(newDeviceId, ((GcmClient) sender.getSenderResult().getNewClient()).getGcmClientId()); }
@Test @SuppressWarnings("unchecked") public void testGcmRemove() throws Exception { new Expectations() {{ gcmService.send((Message) any, (List) any, anyInt); result = gcmMulticastResult; gcmMulticastResult.getResults(); result = Arrays.asList(gcmResult); gcmResult.getErrorCodeName(); result = Constants.ERROR_NOT_REGISTERED; }}; sender.onNewDataComplete(); Assert.assertEquals(SenderResult.Status.REMOVE_CLIENT, sender.getSenderResult().getStatus()); }