private String getProviderId(EntityRef device, Notifier notifier) throws Exception { try { Object value = em.getProperty(device, notifier.getName() + NOTIFIER_ID_POSTFIX); if (value == null) { value = em.getProperty(device, notifier.getUuid() + NOTIFIER_ID_POSTFIX); } return value != null ? value.toString() : null; } catch (Exception e) { logger.error("Error getting notifier for device {}, proceeding with rest of batch", device, e); return null; } }
public void removeInactiveDevices( Map<String,Date> inactiveDeviceMap ){ final String notfierPostFix = ApplicationQueueManagerImpl.NOTIFIER_ID_POSTFIX; if (inactiveDeviceMap != null && inactiveDeviceMap.size() > 0) { if (logger.isDebugEnabled()) { logger.debug("processing {} inactive devices", inactiveDeviceMap.size()); } Map<String, Object> clearPushtokenMap = new HashMap<String, Object>( 2); clearPushtokenMap.put(notifier.getName() + notfierPostFix, ""); clearPushtokenMap.put(notifier.getUuid() + notfierPostFix, ""); // todo: this could be done in a single query for (Map.Entry<String, Date> entry : inactiveDeviceMap.entrySet()) { try { // name Query query = Query.fromQL( notifier.getName() + notfierPostFix + " = '" + entry.getKey() + "'"); Results results = entityManager.searchCollection(entityManager.getApplication(), "devices", query); for (Entity e : results.getEntities()) { entityManager.updateProperties(e, clearPushtokenMap); } }catch (Exception e){ logger.error("failed to remove token",e); } } } } }
private void replaceProviderId(EntityRef device, Notifier notifier, String newProviderId) throws Exception { Object value = em.getProperty(device, notifier.getName() + ApplicationQueueManager.NOTIFIER_ID_POSTFIX); if (value != null) { em.setProperty(device, notifier.getName() + ApplicationQueueManager.NOTIFIER_ID_POSTFIX, newProviderId); } else { value = em.getProperty(device, notifier.getUuid() + ApplicationQueueManager.NOTIFIER_ID_POSTFIX); if (value != null) { em.setProperty(device, notifier.getUuid() + ApplicationQueueManager.NOTIFIER_ID_POSTFIX, newProviderId); } } }
protected Map<Object, Notifier> getNotifierMap(Map payloads) throws Exception { Map<Object, Notifier> notifiers = new HashMap<Object, Notifier>( payloads.size() * 2); for (Object id : payloads.keySet()) { Identifier identifier = Identifier.from(id); Notifier notifier; if (identifier.isUUID()) { notifier = em.get(identifier.getUUID(), Notifier.class); } else { EntityRef ref = em.getAlias("notifier", identifier.getName()); notifier = em.get(ref, Notifier.class); } if (notifier != null) { notifiers.put(notifier.getUuid(), notifier); notifiers.put(notifier.getUuid().toString(), notifier); if (notifier.getName() != null) { notifiers.put(notifier.getName(), notifier); } } } return notifiers; }
Notifier notifier = notifierIterator.next(); String name = notifier.getName() != null ? notifier.getName() : ""; UUID uuid = notifier.getUuid() != null ? notifier.getUuid() : UUID.randomUUID(); ProviderAdapter providerAdapter = ProviderAdapterFactory.getProviderAdapter(notifier, em); notifierHashMap.put(name.toLowerCase(), providerAdapter);
@Test public void twoBatchNotification() throws Exception { app.clear(); String payload = "Hello, World!"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); Entity e = app.testRequest(ServiceAction.POST, 1, "devices", "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // reduce Batch size to 1 Field field = GCMAdapter.class.getDeclaredField("BATCH_SIZE"); field.setAccessible(true); int multicastSize = field.getInt(GCMAdapter.class); try { field.setInt(GCMAdapter.class, 1); // perform push // notification = notificationWaitForComplete(notification); checkReceipts(notification, 2); } finally { field.setInt(GCMAdapter.class, multicastSize); } }
app.getEntityManager().get(notifier.getUuid()); fail("Should have failed to retrieve the encrypted entity."); } catch (IllegalStateException e) {
@Test public void singlePushNotificationMultipleDevices() throws Exception { app.clear(); String payload = "Hello, World!"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); app.put("expire", System.currentTimeMillis() + 300000); // add 5 minutes to current time Entity e = app.testRequest(ServiceAction.POST, 1, "devices", "*", "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // perform push // notification = notificationWaitForComplete(notification); checkReceipts(notification, 2); }
@Test public void singlePushNotification() throws Exception { app.clear(); String payload = "Hello, World!"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); app.put("expire", System.currentTimeMillis() + 300000); // add 5 minutes to current time Entity e = app.testRequest(ServiceAction.POST, 1, "devices", device1.getUuid(), "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // perform push // notification = notificationWaitForComplete(notification); checkReceipts(notification, 1); }
@Test public void singlePushNotificationNoReceipts() throws Exception { app.clear(); String payload = "Hello, World!"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); app.put("saveReceipts",false ); app.put("expire", System.currentTimeMillis() + 300000); // add 5 minutes to current time Entity e = app.testRequest(ServiceAction.POST, 1, "devices", device1.getUuid(), "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // perform push // notification = notificationWaitForComplete(notification); checkReceipts(notification, 0); }
@Test public void singlePushNotificationWithMapPayload() throws Exception { app.clear(); String payload = "{\"message\":\"Hello, World!\", \"campaign\":\"Hello Campaign\"}"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); app.put("expire", System.currentTimeMillis() + 300000); // add 5 minutes to current time Entity e = app.testRequest(ServiceAction.POST, 1, "devices", device1.getUuid(), "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // perform push // notification = notificationWaitForComplete(notification); checkReceipts(notification, 1); }
@Test public void singlePushNotificationNoReceipts() throws Exception { app.clear(); String payload = "Hello, World!"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); app.put("saveReceipts",false ); app.put("expire", System.currentTimeMillis() + 300000); // add 5 minutes to current time Entity e = app.testRequest(ServiceAction.POST, 1, "devices", device1.getUuid(), "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // perform push // notification = notificationWaitForComplete(notification); checkReceipts(notification, 0); }
@Test public void scheduledNotification() throws Exception { // create push notification // app.clear(); String payload = getPayload(); Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("deliver", System.currentTimeMillis() + 240000); app.put("debug",true); Entity e = app.testRequest(ServiceAction.POST, 1,"devices",device1.getUuid(), "notifications") .getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // delay until the scheduler has time to run Thread.sleep(500); notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals(Notification.State.SCHEDULED, notification.getState()); }
@Ignore("turn this on and run individually when you want to verify. there is an issue with the aggregate counter, because of all the other tests" + "AND, there is an issue with the batcher where we have to turn it up/down to see the results in time. ") @Test public void singlePushNotificationWithCounters() throws Exception { long ts = System.currentTimeMillis() - ( 24 * 60 * 60 * 1000 ); app.clear(); String payload = "Hello, World!"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", false); app.put("expire", System.currentTimeMillis() + 300000); // add 5 minutes to current time Entity e = app.testRequest(ServiceAction.POST, 1, "devices", device1.getUuid(), "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // perform push // notification = notificationWaitForComplete(notification); // checkReceipts(notification, 1); verifyNotificationCounter( notification,"completed",ts,1 ); verifyNotificationCounter( notification,"failed",ts, 0 ); }
@Test public void singlePushNotificationHighPriority() throws Exception { app.clear(); String payload = "Hello, World!"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); app.put("expire", System.currentTimeMillis() + 300000); // add 5 minutes to current time app.put("priority", "high"); Entity e = app.testRequest(ServiceAction.POST, 1, "devices", device1.getUuid(), "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // perform push // notification = notificationWaitForComplete(notification); assertEquals("high", notification.getPriority()); checkReceipts(notification, 1); }
@Test public void scheduledNotificationAndEnsureSend() throws Exception { // create push notification // app.clear(); String payload = getPayload(); Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("deliver", System.currentTimeMillis() + 2000); app.put("debug",true); Entity e = app.testRequest(ServiceAction.POST, 1,"devices",device1.getUuid(), "notifications") .getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // delay until the scheduler has time to run Notification.State notificationState = Notification.State.SCHEDULED; int retry = 60; do { logger.info("Sleeping while the scheduler does its work " + retry); Thread.sleep(1000); notificationState = app.getEntityManager().get(e.getUuid(), Notification.class).getState(); } while (Notification.State.FINISHED != notificationState && --retry >= 0 ); assertEquals(Notification.State.FINISHED, notificationState); }
@Test public void singlePushNotificationWithInvalidPriority() throws Exception { app.clear(); String payload = "Hello, World!"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); app.put("expire", System.currentTimeMillis() + 300000); // add 5 minutes to current time app.put("priority", "not_a_priority"); Entity e = app.testRequest(ServiceAction.POST, 1, "devices", device1.getUuid(), "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload); // perform push // notification = notificationWaitForComplete(notification); // if priority is invalid, it should default to normal assertEquals("normal", notification.getPriority()); checkReceipts(notification, 1); }
@Test public void singlePushNotificationMapPayload() throws Exception { app.clear(); Map<String, Object> topLevel = new HashMap<>(); Map<String, String> mapPayload = new HashMap<String, String>(){{ put("key1", "value1"); put("key2", "value2"); }}; topLevel.put("enabler", mapPayload); Map<String, Object> payloads = new HashMap<>(1); payloads.put(notifier.getUuid().toString(), topLevel); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); app.put("expire", System.currentTimeMillis() + 300000); // add 5 minutes to current time Entity e = app.testRequest(ServiceAction.POST, 1, "devices", device1.getUuid(), "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); //assertEquals( // notification.getPayloads().get(notifier.getUuid().toString()), // payload); // perform push // notification = notificationWaitForComplete(notification); checkReceipts(notification, 1); }
String payload = "Hello, World!"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); assertEquals( notification.getPayloads().get(notifier.getUuid().toString()), payload);
@Test public void singlePushNotificationViaUser() throws Exception { app.clear(); // create user asdf app.put("username", "asdf"); app.put("email", "asdf@adsf.com"); User user = (User) app.testRequest(ServiceAction.POST, 1, "users").getEntity(); assertNotNull(user); // post an existing device to user's devices collection Entity device = app.testRequest(ServiceAction.POST, 1, "users", user.getUuid(), "devices", device1.getUuid()).getEntity(); assertEquals(device.getUuid(), device1.getUuid()); // create and post notification String payload = "Hello, World!"; Map<String, String> payloads = new HashMap<String, String>(1); payloads.put(notifier.getUuid().toString(), payload); app.put("payloads", payloads); app.put("queued", System.currentTimeMillis()); app.put("debug", true); Entity e = app.testRequest(ServiceAction.POST, 1, "users", user.getUuid(), "notifications").getEntity(); app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid()); // perform push // Notification notification = app.getEntityManager().get(e.getUuid(), Notification.class); notification = notificationWaitForComplete(notification); checkReceipts(notification, 1); }