You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sf...@apache.org on 2014/08/20 19:32:52 UTC
[3/4] move tests
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/dd4e7650/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceTest.java
----------------------------------------------------------------------
diff --git a/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceTest.java b/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceTest.java
deleted file mode 100644
index c30edb7..0000000
--- a/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceTest.java
+++ /dev/null
@@ -1,868 +0,0 @@
-/*******************************************************************************
- * Copyright 2012 Apigee Corporation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- ******************************************************************************/
-package org.apache.usergrid.services.notifications.apns;
-
-import com.relayrides.pushy.apns.*;
-import com.relayrides.pushy.apns.util.*;
-import org.apache.commons.io.IOUtils;
-import org.apache.usergrid.persistence.model.util.UUIDGenerator;
-import org.apache.usergrid.services.notifications.AbstractServiceNotificationTest;
-import org.apache.usergrid.persistence.*;
-import org.apache.usergrid.persistence.entities.*;
-import org.apache.usergrid.persistence.index.query.Query;
-import org.apache.usergrid.services.notifications.*;
-import org.junit.After;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.InputStream;
-import java.net.SocketException;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-
-import org.apache.usergrid.services.ServiceAction;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.fail;
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
-
-import static org.apache.usergrid.services.notifications.NotificationsService.NOTIFIER_ID_POSTFIX;
-
-// todo: test reschedule on delivery time change
-// todo: test restart of queuing
-public class NotificationsServiceTest extends AbstractServiceNotificationTest {
-
- private static final Logger LOG = LoggerFactory.getLogger(NotificationsServiceTest.class);
-
- /**
- * set to true to run tests against actual Apple servers - but they may not
- * all run correctly
- */
- private static final boolean USE_REAL_CONNECTIONS = false;
- private static final String PROVIDER = USE_REAL_CONNECTIONS ? "apple" : "noop";
-
- private static final String PUSH_TOKEN = "29026b5a4d2761ef13843e8bcab9fc83b47f1dfbd1d977d225ab296153ce06d6";
-
- private Notifier notifier;
- private Device device1, device2;
- private Group group1;
- private User user1;
- private NotificationsService ns;
-
- @Override
- @Before
- public void before() throws Exception {
- super.before();
- // create apns notifier //
- NotificationsQueueManager.IS_TEST = true;
-
- app.clear();
- app.put("name", "apns");
- app.put("provider",PROVIDER);
- app.put("environment", USE_REAL_CONNECTIONS ? "development" : "mock");
- InputStream fis = getClass().getClassLoader().getResourceAsStream("pushtest_dev_recent.p12");
- byte[] certBytes = IOUtils.toByteArray(fis);
- app.put("p12Certificate", certBytes);
- fis.close();
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifiers").getEntity();
- notifier = app.getEm().get(e.getUuid(), Notifier.class);
- final String notifierKey = notifier.getName() + NOTIFIER_ID_POSTFIX;
-
- // create devices //
-
- app.clear();
- app.put(notifierKey, PUSH_TOKEN);
- app.put("name", "device1");
-
- e = app.testRequest(ServiceAction.POST, 1, "devices").getEntity();
- app.testRequest(ServiceAction.GET, 1, "devices", e.getUuid());
-
- device1 = app.getEm().get(e.getUuid(), Device.class);
- assertEquals(device1.getProperty(notifierKey), PUSH_TOKEN);
-
- app.clear();
- app.put(notifierKey, PUSH_TOKEN);
- app.put("name", "device2");
- e = app.testRequest(ServiceAction.POST, 1, "devices").getEntity();
- device2 = app.getEm().get(e.getUuid(), Device.class);
-
- // create User
- app.put( "username", "edanuff" );
- app.put( "email", "ed@anuff.com" );
-// user1 = (User)app.testRequest( ServiceAction.POST, 1, "users" ).getEntity();
-// app.getEm().createConnection(user1, "devices", device1);
-// app.getEm().createConnection(user1, "devices", device2);
-
- // create Group
- app.put( "path", "path" );
- app.put( "title", "group" );
- group1 = (Group) app.testRequest( ServiceAction.POST, 1, "groups" ).getEntity();
-
-// app.getEm().createConnection(group1, "users", user1);
-
- ns = getNotificationService();
- }
-
- @After
- public void after() throws Exception {
- }
-
- @Test
- public void singlePushNotification() 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("queued", System.currentTimeMillis());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device1);
-
-// // verify Query for CREATED state
- Query query = new Query();
- query.addFilter("state='"+Notification.State.STARTED.toString()+"'");
- Results results = app.getEm().searchCollection(
- app.getEm().getApplicationRef(), "notifications", query);
- Entity entity = results.getEntitiesMap().get(notification.getUuid());
- assertNotNull(entity);
-
- // perform push //
-
- notification = scheduleNotificationAndWait(notification);
-
- // verify Query for FINISHED state
- query = new Query();
- query.addEqualityFilter("state", Notification.State.FINISHED.toString());
- results = app.getEm().searchCollection(app.getEm().getApplicationRef(),
- "notifications", query);
- entity = results.getEntitiesMap().get(notification.getUuid());
- assertNotNull(entity);
-
- checkReceipts(notification, 1);
- checkStatistics(notification, 1, 0);
- }
-
- @Test
- public void pushWithNoValidDevicesShouldComplete() throws Exception {
-
- // create unrelated notifier
-
- app.clear();
- app.put("name", "gcm");
- app.put("provider", PROVIDER);
- app.put("environment", "development");
- app.put("apiKey", "xxx");
- InputStream fis = getClass().getClassLoader().getResourceAsStream(
- "pushtest_dev_recent.p12");
- byte[] certBytes = IOUtils.toByteArray(fis);
- app.put("p12Certificate", certBytes);
-
- app.testRequest(ServiceAction.POST, 1, "notifiers").getEntity()
- .toTypedEntity();
- String key = "gcm" + NOTIFIER_ID_POSTFIX;
-
- // create unrelated device
-
- app.clear();
- app.put(key, PUSH_TOKEN);
- Entity e = app.testRequest(ServiceAction.POST, 1, "devices")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "devices", e.getUuid());
-
- Device device = app.getEm().get(e.getUuid(), Device.class);
- assertEquals(device.getProperty(key), PUSH_TOKEN);
-
- // 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("started", System.currentTimeMillis());
- app.put("queued", System.currentTimeMillis());
-
- e = app.testRequest(ServiceAction.POST, 1, "notifications").getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device);
-
- // verify Query for CREATED state
- Query query = new Query();
- query.addEqualityFilter("state", Notification.State.STARTED.toString());
- Results results = app.getEm().searchCollection(
- app.getEm().getApplicationRef(), "notifications", query);
- Entity entity = results.getEntitiesMap().get(notification.getUuid());
- assertNotNull(entity);
-
- // perform push //
-
- ns.getQueueManager().processBatchAndReschedule(notification, null);
-
- // verify Query for FINISHED state
- query = new Query();
- query.addEqualityFilter("state", Notification.State.FINISHED.toString());
- results = app.getEm().searchCollection(app.getEm().getApplicationRef(),
- "notifications", query);
- entity = results.getEntitiesMap().get(notification.getUuid());
- assertNotNull(entity);
-
- notification = (Notification) entity.toTypedEntity();
- checkReceipts(notification, 0);
- checkStatistics(notification, 0, 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("queued", System.currentTimeMillis());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device1);
-
- // delay until the scheduler has time to run
- Thread.sleep(500);
-
- // verify Query for SCHEDULED state
- Query query = new Query();
- query.addEqualityFilter("state",
- Notification.State.SCHEDULED.toString());
- Results results = app.getEm().searchCollection(
- app.getEm().getApplicationRef(), "notifications", query);
- Entity entity = results.getEntitiesMap().get(notification.getUuid());
- assertNotNull(entity);
-
- try {
- e = app.testRequest(ServiceAction.DELETE, 1, "notifications",
- e.getUuid()).getEntity();
- }catch (Exception deleteException){
- LOG.error("Couldn't delete",deleteException);
- }
- app.getEm().get(e.getUuid(), Notification.class);
- }
-
- @Test
- public void badPayloads() throws Exception {
-
- MockSuccessfulProviderAdapter.uninstall(ns);
-
- // bad payloads format
-
- app.clear();
- app.put("payloads", "{asdf}");
-
- try {
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- fail("invalid payload should have been rejected");
- } catch (IllegalArgumentException ex) {
- // ok
- }
-
- // bad notifier
-
- Map<String, String> payloads = new HashMap<String, String>(2);
- app.put("payloads", payloads);
- payloads.put("xxx", "");
- try {
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- fail("invalid payload should have been rejected");
- } catch (IllegalArgumentException ex) {
- // ok
- }
-
- // payload too long
-
- // need the real provider for this one...
- app.clear();
- app.put("name", "apns2");
- app.put("provider", "apple");
- app.put("environment", "development");
- InputStream fis = getClass().getClassLoader().getResourceAsStream(
- "pushtest_dev_recent.p12");
- byte[] certBytes = IOUtils.toByteArray(fis);
- app.put("p12Certificate", certBytes);
- fis.close();
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifiers")
- .getEntity();
- Notifier notifier2 = app.getEm().get(e.getUuid(), Notifier.class);
-
- payloads.clear();
- StringBuilder sb = new StringBuilder();
- sb.append("{\"x\":\"");
- while (sb.length() < 255) {
- sb.append("x");
- }
- sb.append("\"}");
- payloads.put(notifier2.getUuid().toString(), sb.toString());
-
- app.clear();
- app.put("payloads", payloads);
-
- try {
- app.testRequest(ServiceAction.POST, 1, "notifications").getEntity();
- fail("invalid payload should have been rejected");
- } catch (Exception ex) {
- assertEquals(ex.getMessage(),
- "java.lang.IllegalArgumentException: Apple APNs payloads must be 256 characters or less");
- // ok
- }
- }
-
- @Ignore
- // todo: how can I mock this?
- @Test
- public void badToken() throws Exception {
-
- // mock action (based on verified actual behavior) //
-
- if (!USE_REAL_CONNECTIONS) {
- ns.providerAdapters.put("apple",
- new MockSuccessfulProviderAdapter() {
- @Override
- public void sendNotification(String providerId,
- Notifier notifier, Object payload,
- Notification notification, TaskTracker tracker)
- throws Exception {
- APNsNotification apnsNotification = APNsNotification
- .create(providerId, payload.toString(),
- notification, tracker);
- apnsNotification.messageSent();
- apnsNotification
- .messageSendFailed( RejectedNotificationReason.INVALID_TOKEN);
- }
- });
- }
-
- // create push notification //
-
- HashMap<String, Object> properties = new LinkedHashMap<String, Object>();
- String payload = getPayload();
- Map<String, String> payloads = new HashMap<String, String>(1);
- payloads.put(notifier.getUuid().toString(), payload);
- properties.put("payloads", payloads);
- properties.put("queued", System.currentTimeMillis());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device1);
-
- // perform push //
- notification = scheduleNotificationAndWait(notification);
- checkStatistics(notification, 0, 1);
-
- notification = (Notification) app.getEm().get(notification)
- .toTypedEntity();
- checkReceipts(notification, 1);
- List<EntityRef> receipts = getNotificationReceipts(notification);
- Receipt receipt = app.getEm().get(receipts.get(0), Receipt.class);
- assertEquals(8, ((Long) receipt.getErrorCode()).longValue());
- }
-
- @Test
- public void twoDevicesOneNotifier() 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("queued", System.currentTimeMillis());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device1);
- ns.addDevice(notification, device2);
-
- // perform push //
- notification = scheduleNotificationAndWait(notification);
-
- checkReceipts(notification, 2);
- }
-
- @Test
- public void twoDevicesTwoNotifiers() throws Exception {
-
- // create a 2nd notifier //
- app.clear();
- app.put("name", "apns2");
- app.put("provider", PROVIDER);
- app.put("environment", "development");
- InputStream fis = getClass().getClassLoader().getResourceAsStream(
- "pushtest_dev_recent.p12");
- byte[] certBytes = IOUtils.toByteArray(fis);
- app.put("p12Certificate", certBytes);
- fis.close();
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifiers")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifiers", "apns2");
-
- Notifier notifier2 = app.getEm().get(e.getUuid(), Notifier.class);
- assertEquals(notifier2.getName(), "apns2");
- assertEquals(notifier2.getProvider(), PROVIDER);
- assertEquals(notifier2.getEnvironment(), "development");
-
- String key = notifier.getName() + NOTIFIER_ID_POSTFIX;
- String key2 = notifier2.getName() + NOTIFIER_ID_POSTFIX;
- device2.setProperty(key, null);
- device2.setProperty(key2, null);
- app.getEm().update(device2);
-
- // create push notification //
-
- app.clear();
- String payload = getPayload();
- Map<String, String> payloads = new HashMap<String, String>(1);
- payloads.put(notifier.getUuid().toString(), payload);
- payloads.put(notifier2.getUuid().toString(), payload);
- app.put("payloads", payloads);
- app.put("queued", System.currentTimeMillis());
-
- e = app.testRequest(ServiceAction.POST, 1, "notifications").getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device1);
- ns.addDevice(notification, device2);
-
- // perform push //
- notification = scheduleNotificationAndWait(notification);
-
- checkReceipts(notification, 2); //the second notifier isn't associated correctly so its 3 instead of 4
- }
-
- @Test
- public void oneDeviceTwoNotifiers() throws Exception {
-
- // create a 2nd notifier //
- Object nameValue = "apns2";
- Object environValue = "development";
-
- app.clear();
- app.put("name", nameValue);
- app.put("provider", PROVIDER);
- app.put("environment", environValue);
- InputStream fis = getClass().getClassLoader().getResourceAsStream(
- "pushtest_dev_recent.p12");
- byte[] certBytes = IOUtils.toByteArray(fis);
- app.put("p12Certificate", certBytes);
- fis.close();
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifiers")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifiers", nameValue);
-
- Notifier notifier2 = app.getEm().get(e.getUuid(), Notifier.class);
- assertEquals(notifier2.getName(), nameValue);
- assertEquals(notifier2.getProvider(), PROVIDER);
- assertEquals(notifier2.getEnvironment(), environValue);
-
- String key2 = notifier2.getName() + NOTIFIER_ID_POSTFIX;
- device1.setProperty(key2, PUSH_TOKEN);
- app.getEm().update(device1);
-
- // create push notification //
-
- app.clear();
- String payload = getPayload();
- Map<String, String> payloads = new HashMap<String, String>(1);
- payloads.put(notifier.getUuid().toString(), payload);
- payloads.put(notifier2.getUuid().toString(), payload);
- app.put("payloads", payloads);
- app.put("queued", System.currentTimeMillis());
-
- e = app.testRequest(ServiceAction.POST, 1, "notifications").getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device1);
-
- // perform push //
- notification = scheduleNotificationAndWait(notification);
-
- checkReceipts(notification, 2);
- }
-
- @Ignore
- // todo: how can I mock this?
- @Test
- public void badCertificate() throws Exception {
-
- // create an apns notifier with the wrong certificate //
-
- app.clear();
- app.put("name", "prod_apns");
- app.put("provider", PROVIDER);
- app.put("environment", "development");
-
- InputStream fis = getClass().getClassLoader().getResourceAsStream(
- "empty.p12");
- byte[] certBytes = IOUtils.toByteArray(fis);
- app.put("p12Certificate", certBytes);
- fis.close();
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifiers")
- .getEntity();
- notifier = app.getEm().get(e.getUuid(), Notifier.class);
-
- // mock error (based on verified actual behavior) //
- if (!USE_REAL_CONNECTIONS) {
- ns.providerAdapters.put("apple",
- new MockSuccessfulProviderAdapter() {
- @Override
- public void testConnection(Notifier notifier)
- throws ConnectionException {
- Exception e = new SocketException(
- "Connection closed by remote host");
- throw new ConnectionException(e.getMessage(), e);
- }
- });
- }
-
- // 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("queued", System.currentTimeMillis());
-
- e = app.testRequest(ServiceAction.POST, 1, "notifications").getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device1);
-
- // perform push //
-
- try {
- ns.getQueueManager().processBatchAndReschedule(notification,null);
- fail("testConnection() should have failed");
- } catch (Exception ex) {
- // good, there should be an error
- }
-
- // verify Query for FAILED state
- Query query = new Query();
- query.addEqualityFilter("state", Notification.State.FAILED.toString());
- Results results = app.getEm().searchCollection(
- app.getEm().getApplicationRef(), "notifications", query);
- Entity entity = results.getEntitiesMap().get(notification.getUuid());
- assertNotNull(entity);
- }
-
- @Ignore
- // todo: how can I mock this?
- @Test
- public void inactiveDeviceUpdate() throws Exception {
-
- // mock action (based on verified actual behavior) //
- if (!USE_REAL_CONNECTIONS) {
- ns.providerAdapters.put("apple",
- new MockSuccessfulProviderAdapter() {
- @Override
- public Map<String, Date> getInactiveDevices(
- Notifier notifier, EntityManager em)
- throws Exception {
- return Collections.singletonMap(PUSH_TOKEN,
- new Date());
- }
- });
- }
-
- // 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("queued", System.currentTimeMillis());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device1);
- ns.addDevice(notification, device2);
-
- assertNotNull(device1.getProperty(notifier.getName()
- + NOTIFIER_ID_POSTFIX));
- assertNotNull(device2.getProperty(notifier.getName()
- + NOTIFIER_ID_POSTFIX));
-
- // perform push //
- notification = scheduleNotificationAndWait(notification);
-
- // check provider IDs //
-
- device1 = app.getEm().get(device1, Device.class);
- assertNull(device1
- .getProperty(notifier.getName() + NOTIFIER_ID_POSTFIX));
- device2 = app.getEm().get(device2, Device.class);
- assertNull(device2
- .getProperty(notifier.getName() + NOTIFIER_ID_POSTFIX));
- }
-
- @Test
- public void batchTest() throws Exception {
-
- final int NUM_DEVICES = 50;
-
- 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("queued", System.currentTimeMillis());
-
- // create a notification
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
- final Notification notification = (Notification) e.toTypedEntity();
-
- // create a bunch of devices and add them to the notification
- app.clear();
- app.put(notifier.getName() + NOTIFIER_ID_POSTFIX, PUSH_TOKEN);
- for (int i = 0; i < NUM_DEVICES; i++) {
- Entity entity = app.getEm().create("device", app.getProperties());
- ns.addDevice(notification, entity);
- }
-
- // perform push //
- int oldBatchSize = NotificationsQueueManager.BATCH_SIZE;
- NotificationsQueueManager.BATCH_SIZE = 10;
- try {
- ExecutorService pool = Executors
- .newFixedThreadPool(APNsAdapter.MAX_CONNECTION_POOL_SIZE);
- pool.submit(new Runnable() {
- @Override
- public void run() {
- try {
- scheduleNotificationAndWait(notification);
- }catch (Exception e){}
- }});
- } finally {
- NotificationsQueueManager.BATCH_SIZE = oldBatchSize;
- }
-
- // check receipts //
- checkReceipts(notification, NUM_DEVICES);
- checkStatistics(notification, NUM_DEVICES, 0);
- }
-
- @Ignore("Run only if you need to.")
- @Test
- public void loadTest() throws Exception {
-
- MockSuccessfulProviderAdapter.install(ns, true);
-
- final int NUM_DEVICES = 10000;
-
- 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("queued", System.currentTimeMillis());
-
- // create a notification
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
- Notification notification = (Notification) e.toTypedEntity();
-
- // create a bunch of devices and add them to the notification
- app.clear();
- app.put(notifier.getName() + NOTIFIER_ID_POSTFIX, PUSH_TOKEN);
- for (int i = 0; i < NUM_DEVICES; i++) {
- Entity entity = app.getEm().create("device", app.getProperties());
- ns.addDevice(notification, entity);
- }
-
- long time = System.currentTimeMillis();
- LOG.error("START DELIVERY OF {} NOTIFICATIONS", NUM_DEVICES);
-
- // perform push //
- notification = scheduleNotificationAndWait(notification);
- LOG.error("END DELIVERY OF {} NOTIFICATIONS ({})", NUM_DEVICES,
- System.currentTimeMillis() - time);
-
- // check receipts //
- checkReceipts(notification, NUM_DEVICES);
- checkStatistics(notification, NUM_DEVICES, 0);
- }
-
- private String getPayload(){
- ApnsPayloadBuilder builder = new ApnsPayloadBuilder();
- builder.setAlertBody("Hello, World!");
- builder.setSoundFileName("chime");
- String payload = builder.buildWithDefaultMaximumLength();
- return payload;
- }
- // todo: can't do the following tests here. do it in the REST tier...
- // private Notification postNotification(String path) throws Exception {
- // HashMap<String, Object> properties = new LinkedHashMap<String, Object>();
- // String payload =
- // APNS.newPayload().alertBody("Hello, World!").sound("chime").build();
- // Map<String, String> payloads = new HashMap<String, String>(1);
- // payloads.put(notifier.getUuid().toString(), payload);
- // properties.put("payloads", payloads);
- //
- // Entity e = testRequest(sm, ServiceAction.POST, 1, properties,
- // path).getEntity();
- // Thread.sleep(1000); // this sucks
- // Notification notification = app.getEm().get(e, Notification.class);
- // return notification;
- // }
- //
- // @Test
- // public void matrixPushDevice() throws Exception {
- //
- // Notification notification = postNotification("devices/" +
- // device1.getName() + "/notifications");
- // checkReceipts(notification, 1);
- // checkStatistics(notification, 1, 0);
- // }
- //
- // @Test
- // public void matrixPushViaUser() throws Exception {
- //
- // Notification notification = postNotification("users/" + user1.getName() +
- // "/notifications");
- // checkReceipts(notification, 2);
- // checkStatistics(notification, 2, 0);
- // }
- //
- // @Test
- // public void matrixPushViaGroup() throws Exception {
- //
- // Notification notification = postNotification("devices/" +
- // device1.getName() + "/notifications");
- // checkReceipts(notification, 2);
- // checkStatistics(notification, 2, 0);
- // }
- //
- // @Test
- // public void matrixPushDeviceQuery() throws Exception {
- //
- // Notification notification = postNotification("devices;ql=name=" +
- // device1.getName() + "/notifications");
- // checkReceipts(notification, 1);
- // checkStatistics(notification, 1, 0);
- // }
- //
- // @Test
- // public void matrixPushUserQuery() throws Exception {
- //
- // Notification notification = postNotification("users;ql=/notifications");
- // checkReceipts(notification, 2);
- // checkStatistics(notification, 2, 0);
- // }
-}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/dd4e7650/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceIT.java
----------------------------------------------------------------------
diff --git a/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceIT.java b/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceIT.java
new file mode 100644
index 0000000..989c2ce
--- /dev/null
+++ b/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceIT.java
@@ -0,0 +1,546 @@
+/*******************************************************************************
+ * Copyright 2012 Apigee Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.usergrid.services.notifications.gcm;
+
+import org.apache.usergrid.services.notifications.AbstractServiceNotificationIT;
+import org.apache.usergrid.persistence.entities.Notification;
+import org.apache.usergrid.persistence.entities.Notifier;
+import org.apache.usergrid.persistence.entities.Receipt;
+import org.apache.usergrid.services.notifications.NotificationsService;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.usergrid.services.notifications.ConnectionException;
+import org.apache.usergrid.services.notifications.TaskTracker;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.util.*;
+import org.apache.usergrid.persistence.Entity;
+import org.apache.usergrid.persistence.EntityRef;
+import org.apache.usergrid.persistence.entities.Device;
+import org.apache.usergrid.services.ServiceAction;
+
+import static org.junit.Assert.*;
+import static org.apache.usergrid.services.notifications.NotificationsService.NOTIFIER_ID_POSTFIX;
+
+public class NotificationsServiceIT extends AbstractServiceNotificationIT {
+
+ private static final Logger LOG = LoggerFactory
+ .getLogger(NotificationsServiceIT.class);
+
+ /**
+ * set to true to run tests against actual GCM servers - but they may not
+ * all run correctly
+ */
+ private static final boolean USE_REAL_CONNECTIONS = false;
+ private static final String PROVIDER = USE_REAL_CONNECTIONS ? "google"
+ : "noop";
+
+ private static final String API_KEY = "AIzaSyCIH_7WC0mOqBGMOXyQnFgrBpOePgHvQJM";
+ private static final String PUSH_TOKEN = "APA91bGxRGnMK8tKgVPzSlxtCFvwSVqx0xEPjA06sBmiK0kQsiwUt6ipSYF0iPRHyUgpXle0P8OlRWJADkQrcN7yxG4pLMg1CVmrqDu8tfSe63mZ-MRU2IW0cOhmosqzC9trl33moS3OvT7qjDjkP4Qq8LYdwwYC5A";
+
+ private Notifier notifier;
+ private Device device1, device2;
+ private NotificationsService ns;
+
+ @Override
+ @Before
+ public void before() throws Exception {
+ super.before();
+
+ // create gcm notifier //
+
+ app.clear();
+ app.put("name", "gcm");
+ app.put("provider", PROVIDER);
+ app.put("environment", "development");
+ app.put("apiKey", API_KEY);
+
+ notifier = (Notifier) app
+ .testRequest(ServiceAction.POST, 1, "notifiers").getEntity()
+ .toTypedEntity();
+ String key = notifier.getName() + NOTIFIER_ID_POSTFIX;
+
+ // create devices //
+
+ app.clear();
+ app.put(key, PUSH_TOKEN);
+
+ Entity e = app.testRequest(ServiceAction.POST, 1, "devices")
+ .getEntity();
+ app.testRequest(ServiceAction.GET, 1, "devices", e.getUuid());
+
+ device1 = app.getEm().get(e.getUuid(), Device.class);
+ assertEquals(device1.getProperty(key), PUSH_TOKEN);
+
+ app.put(key, PUSH_TOKEN);
+ e = app.testRequest(ServiceAction.POST, 1, "devices").getEntity();
+ device2 = app.getEm().get(e.getUuid(), Device.class);
+ ns = getNotificationService();
+ }
+
+ @Test
+ public void emptyPushNotification() throws Exception {
+
+ app.clear();
+ app.put("name", "foo");
+ app.put("provider", PROVIDER);
+ app.put("environment", "development");
+ app.put("apiKey", API_KEY);
+ Notifier n = (Notifier) app
+ .testRequest(ServiceAction.POST, 1, "notifiers").getEntity()
+ .toTypedEntity();
+
+ app.clear();
+ String payload = "Hello, World!";
+ Map<String, String> payloads = new HashMap<String, String>(1);
+ payloads.put("foo", payload);
+ app.put("payloads", payloads);
+ app.put("queued", System.currentTimeMillis());
+
+ Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
+ .getEntity();
+ app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
+
+ Notification notification = app.getEm().get(e.getUuid(),
+ Notification.class);
+ ns.addDevice(notification, device1);
+
+ // perform push //
+ notification = scheduleNotificationAndWait(notification);
+ checkReceipts(notification, 0);
+ }
+
+ @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());
+
+ Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
+ .getEntity();
+ app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
+
+ Notification notification = app.getEm().get(e.getUuid(),
+ Notification.class);
+ assertEquals(
+ notification.getPayloads().get(notifier.getUuid().toString()),
+ payload);
+
+ ns.addDevice(notification, device1);
+
+ // perform push //
+ notification = scheduleNotificationAndWait(notification);
+ checkReceipts(notification, 1);
+ }
+
+ @Test
+ public void singlePushNotificationViaUser() throws Exception {
+
+ app.clear();
+
+ app.put("username", "asdf");
+ app.put("email", "asdf@adsf.com");
+ Entity user = app.testRequest(ServiceAction.POST, 1, "users")
+ .getEntity();
+ assertNotNull(user);
+ Entity device = app.testRequest(ServiceAction.POST, 1, "users",
+ user.getUuid(), "devices", device1.getUuid()).getEntity();
+ assertEquals(device.getUuid(), device1.getUuid());
+
+ 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());
+
+ Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
+ .getEntity();
+ app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
+
+ Notification notification = app.getEm().get(e.getUuid(),
+ Notification.class);
+ ns.addDevice(notification, device1);
+
+ // perform push //
+ notification = scheduleNotificationAndWait(notification);
+ checkReceipts(notification, 1);
+ }
+
+ @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());
+
+ Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
+ .getEntity();
+ app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
+
+ Notification notification = app.getEm().get(e.getUuid(),
+ Notification.class);
+ assertEquals(
+ notification.getPayloads().get(notifier.getUuid().toString()),
+ payload);
+
+ ns.addDevice(notification, device2);
+ ns.addDevice(notification, device1);
+
+ // 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 = scheduleNotificationAndWait(notification);
+
+ checkReceipts(notification, 2);
+ } finally {
+ field.setInt(GCMAdapter.class, multicastSize);
+ }
+ }
+
+ @Ignore
+ // todo: how can I mock this?
+ @Test
+ public void providerIdUpdate() throws Exception {
+
+ // mock action (based on verified actual behavior) //
+ final String newProviderId = "newProviderId";
+ ns.providerAdapters.put("google", new MockSuccessfulProviderAdapter() {
+ @Override
+ public void sendNotification(String providerId, Notifier notifier,
+ Object payload, Notification notification,
+ TaskTracker tracker) throws Exception {
+ tracker.completed(newProviderId);
+ }
+ });
+
+ // create push notification //
+
+ 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());
+
+ Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
+ .getEntity();
+ app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
+
+ Notification notification = app.getEm().get(e.getUuid(),
+ Notification.class);
+ assertEquals(
+ notification.getPayloads().get(notifier.getUuid().toString()),
+ payload);
+
+ ns.addDevice(notification, device1);
+
+ // perform push //
+ notification = scheduleNotificationAndWait(notification);
+ checkReceipts(notification, 1);
+
+ Device device = (Device) app.getEm().get(device1).toTypedEntity();
+ assertEquals(newProviderId,
+ device.getProperty(notifier.getName() + NOTIFIER_ID_POSTFIX));
+ }
+
+ @Test
+ public void badPayloads() throws Exception {
+
+ // bad payloads format
+
+ app.clear();
+ app.put("payloads", "{asdf}");
+
+ try {
+ app.testRequest(ServiceAction.POST, 1, "notifications");
+ fail("invalid payload should have been rejected");
+ } catch (IllegalArgumentException ex) {
+ // ok
+ }
+
+ // bad notifier
+
+ Map<String, String> payloads = new HashMap<String, String>(2);
+ app.put("payloads", payloads);
+ payloads.put("xxx", "");
+ try {
+ app.testRequest(ServiceAction.POST, 1, "notifications");
+ fail("invalid payload should have been rejected");
+ } catch (IllegalArgumentException ex) {
+ // ok
+ }
+
+ // payload too long
+
+ // need the real provider for this one...
+ app.clear();
+ app.put("name", "gcm2");
+ app.put("provider", "google");
+ app.put("environment", "development");
+ app.put("apiKey", API_KEY);
+ Entity e = app.testRequest(ServiceAction.POST, 1, "notifiers")
+ .getEntity();
+ Notifier notifier2 = app.getEm().get(e.getUuid(), Notifier.class);
+
+ payloads.clear();
+ StringBuilder sb = new StringBuilder();
+ sb.append("{\"x\":\"");
+ while (sb.length() < 4080) {
+ sb.append("x");
+ }
+ sb.append("\"}");
+ payloads.put(notifier2.getUuid().toString(), sb.toString());
+
+ app.clear();
+ app.put("payloads", payloads);
+
+ try {
+ app.testRequest(ServiceAction.POST, 1, "notifications");
+ fail("invalid payload should have been rejected");
+ } catch (Exception ex) {
+ assertEquals("java.lang.IllegalArgumentException: GCM payloads must be 4096 characters or less",
+ ex.getMessage());
+ // ok
+ }
+ }
+
+ @Ignore
+ // todo: how can I mock this?
+ @Test
+ public void badToken() throws Exception {
+
+ // mock action (based on verified actual behavior) //
+ if (!USE_REAL_CONNECTIONS) {
+ ns.providerAdapters.put("google",
+ new MockSuccessfulProviderAdapter() {
+ @Override
+ public void sendNotification(String providerId,
+ Notifier notifier, Object payload,
+ Notification notification, TaskTracker tracker)
+ throws Exception {
+ tracker.failed("InvalidRegistration",
+ "InvalidRegistration");
+ }
+ });
+ }
+
+ // create push notification //
+
+ 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());
+
+ Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
+ .getEntity();
+ app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
+
+ Notification notification = app.getEm().get(e.getUuid(),
+ Notification.class);
+ assertEquals(
+ notification.getPayloads().get(notifier.getUuid().toString()),
+ payload);
+
+ // device w/ bad token
+ app.clear();
+ app.put(notifier.getName() + NOTIFIER_ID_POSTFIX, PUSH_TOKEN + "x");
+
+ e = app.testRequest(ServiceAction.POST, 1, "devices").getEntity();
+ device1 = app.getEm().get(e.getUuid(), Device.class);
+
+ ns.addDevice(notification, device1);
+
+ // perform push //
+ notification = scheduleNotificationAndWait(notification);
+
+ List<EntityRef> receipts = getNotificationReceipts(notification);
+ assertEquals(1, receipts.size());
+ Receipt receipt = app.getEm().get(receipts.get(0), Receipt.class);
+ assertEquals("InvalidRegistration", receipt.getErrorCode());
+ }
+
+ @Ignore
+ // todo: how can I mock this?
+ @Test
+ public void badAPIKey() throws Exception {
+
+ if (!USE_REAL_CONNECTIONS) {
+ // mock action (based on verified actual behavior) //
+ ns.providerAdapters.put("google",
+ new MockSuccessfulProviderAdapter() {
+ @Override
+ public void sendNotification(String providerId,
+ Notifier notifier, Object payload,
+ Notification notification, TaskTracker tracker)
+ throws Exception {
+ Exception e = new IOException();
+ throw new ConnectionException(e.getMessage(), e);
+ }
+ });
+ }
+
+ // create push notification //
+
+ 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());
+
+ Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
+ .getEntity();
+ app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
+
+ Notification notification = app.getEm().get(e.getUuid(),
+ Notification.class);
+ assertEquals(
+ notification.getPayloads().get(notifier.getUuid().toString()),
+ payload);
+
+ ns.addDevice(notification, device1);
+
+ // save bad API key
+ app.getEm().setProperty(notifier, "apiKey", API_KEY + "x");
+
+ // perform push //
+
+ try {
+ ns.getQueueManager().processBatchAndReschedule(notification, null);
+ fail("Should have received a ConnectionException");
+ } catch (ConnectionException ex) {
+ // good
+ }
+ }
+
+ @Ignore("Run only if you need to.")
+ @Test
+ public void loadTest() throws Exception {
+
+ final int NUM_DEVICES = 10000;
+
+ // create notification //
+
+ HashMap<String, Object> properties = new LinkedHashMap<String, Object>();
+ String payload = "Hello, World!";
+ Map<String, String> payloads = new HashMap<String, String>(1);
+ payloads.put(notifier.getUuid().toString(), payload);
+ properties.put("payloads", payloads);
+ properties.put("queued", System.currentTimeMillis());
+
+ Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
+ .getEntity();
+ app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
+
+ Notification notification = app.getEm().get(e.getUuid(),
+ Notification.class);
+ assertEquals(
+ notification.getPayloads().get(notifier.getUuid().toString()),
+ payload);
+
+ // create a bunch of devices and add them to the notification
+ properties = new LinkedHashMap<String, Object>();
+ properties.put(notifier.getName() + NOTIFIER_ID_POSTFIX, PUSH_TOKEN);
+ for (int i = 0; i < NUM_DEVICES; i++) {
+ Entity entity = app.getEm().create("device", properties);
+ ns.addDevice(notification, entity);
+ }
+
+ long time = System.currentTimeMillis();
+ LOG.error("START DELIVERY OF {} NOTIFICATIONS", NUM_DEVICES);
+
+ // perform push //
+ notification = scheduleNotificationAndWait(notification);
+ LOG.error("END DELIVERY OF {} NOTIFICATIONS ({})", NUM_DEVICES,
+ System.currentTimeMillis() - time);
+
+ // check receipts //
+ checkReceipts(notification, NUM_DEVICES);
+ checkStatistics(notification, NUM_DEVICES, 0);
+ }
+
+ // @Test
+ // public void inactiveDeviceUpdate() throws Exception {
+ //
+ // if (!USE_REAL_CONNECTIONS) {
+ // // mock action (based on verified actual behavior) //
+ // NotificationsService.providerAdapters.put("apple", new
+ // MockSuccessfulProviderAdapter() {
+ // public Map<String,Date> getInactiveDevices(Notifier notifier,
+ // EntityManager em) throws Exception {
+ // return Collections.singletonMap(PUSH_TOKEN, new Date());
+ // }
+ // });
+ // }
+ //
+ // // create push notification //
+ //
+ // HashMap<String, Object> properties = new LinkedHashMap<String, Object>();
+ // String payload =
+ // APNS.newPayload().alertBody("Hello, World!").sound("chime").build();
+ // Map<String, String> payloads = new HashMap<String, String>(1);
+ // payloads.put(notifier.getUuid().toString(), payload);
+ // properties.put("payloads", payloads);
+ // properties.put("queued", System.currentTimeMillis());
+ //
+ // Entity e = testRequest(sm, ServiceAction.POST, 1, properties,
+ // "notifications").getEntity();
+ // testRequest(sm, ServiceAction.GET, 1, null, "notifications",
+ // e.getUuid());
+ //
+ // Notification notification = em.get(e.getUuid(), Notification.class);
+ // assertEquals(notification.getPayloads().get(notifier.getUuid().toString()),
+ // payload);
+ //
+ // ns.addDevice(notification, device1);
+ // ns.addDevice(notification, device2);
+ //
+ // assertNotNull(device1.getProperty(notifier.getName() +
+ // NOTIFIER_ID_POSTFIX));
+ // assertNotNull(device2.getProperty(notifier.getName() +
+ // NOTIFIER_ID_POSTFIX));
+ //
+ // // perform push //
+ // notification = scheduleNotificationAndWait(notification);
+ //
+ // // check provider IDs //
+ //
+ // device1 = em.get(device1, Device.class);
+ // assertNull(device1.getProperty(notifier.getName() +
+ // NOTIFIER_ID_POSTFIX));
+ // device2 = em.get(device2, Device.class);
+ // assertNull(device2.getProperty(notifier.getName() +
+ // NOTIFIER_ID_POSTFIX));
+ // }
+}
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/dd4e7650/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceTest.java
----------------------------------------------------------------------
diff --git a/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceTest.java b/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceTest.java
deleted file mode 100644
index 7be8e88..0000000
--- a/stack/services/src/test/java/org/apache/usergrid/services/notifications/gcm/NotificationsServiceTest.java
+++ /dev/null
@@ -1,546 +0,0 @@
-/*******************************************************************************
- * Copyright 2012 Apigee Corporation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- ******************************************************************************/
-package org.apache.usergrid.services.notifications.gcm;
-
-import org.apache.usergrid.services.notifications.AbstractServiceNotificationTest;
-import org.apache.usergrid.persistence.entities.Notification;
-import org.apache.usergrid.persistence.entities.Notifier;
-import org.apache.usergrid.persistence.entities.Receipt;
-import org.apache.usergrid.services.notifications.NotificationsService;
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.apache.usergrid.services.notifications.ConnectionException;
-import org.apache.usergrid.services.notifications.TaskTracker;
-
-import java.io.IOException;
-import java.lang.reflect.Field;
-import java.util.*;
-import org.apache.usergrid.persistence.Entity;
-import org.apache.usergrid.persistence.EntityRef;
-import org.apache.usergrid.persistence.entities.Device;
-import org.apache.usergrid.services.ServiceAction;
-
-import static org.junit.Assert.*;
-import static org.apache.usergrid.services.notifications.NotificationsService.NOTIFIER_ID_POSTFIX;
-
-public class NotificationsServiceTest extends AbstractServiceNotificationTest {
-
- private static final Logger LOG = LoggerFactory
- .getLogger(NotificationsServiceTest.class);
-
- /**
- * set to true to run tests against actual GCM servers - but they may not
- * all run correctly
- */
- private static final boolean USE_REAL_CONNECTIONS = false;
- private static final String PROVIDER = USE_REAL_CONNECTIONS ? "google"
- : "noop";
-
- private static final String API_KEY = "AIzaSyCIH_7WC0mOqBGMOXyQnFgrBpOePgHvQJM";
- private static final String PUSH_TOKEN = "APA91bGxRGnMK8tKgVPzSlxtCFvwSVqx0xEPjA06sBmiK0kQsiwUt6ipSYF0iPRHyUgpXle0P8OlRWJADkQrcN7yxG4pLMg1CVmrqDu8tfSe63mZ-MRU2IW0cOhmosqzC9trl33moS3OvT7qjDjkP4Qq8LYdwwYC5A";
-
- private Notifier notifier;
- private Device device1, device2;
- private NotificationsService ns;
-
- @Override
- @Before
- public void before() throws Exception {
- super.before();
-
- // create gcm notifier //
-
- app.clear();
- app.put("name", "gcm");
- app.put("provider", PROVIDER);
- app.put("environment", "development");
- app.put("apiKey", API_KEY);
-
- notifier = (Notifier) app
- .testRequest(ServiceAction.POST, 1, "notifiers").getEntity()
- .toTypedEntity();
- String key = notifier.getName() + NOTIFIER_ID_POSTFIX;
-
- // create devices //
-
- app.clear();
- app.put(key, PUSH_TOKEN);
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "devices")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "devices", e.getUuid());
-
- device1 = app.getEm().get(e.getUuid(), Device.class);
- assertEquals(device1.getProperty(key), PUSH_TOKEN);
-
- app.put(key, PUSH_TOKEN);
- e = app.testRequest(ServiceAction.POST, 1, "devices").getEntity();
- device2 = app.getEm().get(e.getUuid(), Device.class);
- ns = getNotificationService();
- }
-
- @Test
- public void emptyPushNotification() throws Exception {
-
- app.clear();
- app.put("name", "foo");
- app.put("provider", PROVIDER);
- app.put("environment", "development");
- app.put("apiKey", API_KEY);
- Notifier n = (Notifier) app
- .testRequest(ServiceAction.POST, 1, "notifiers").getEntity()
- .toTypedEntity();
-
- app.clear();
- String payload = "Hello, World!";
- Map<String, String> payloads = new HashMap<String, String>(1);
- payloads.put("foo", payload);
- app.put("payloads", payloads);
- app.put("queued", System.currentTimeMillis());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- ns.addDevice(notification, device1);
-
- // perform push //
- notification = scheduleNotificationAndWait(notification);
- checkReceipts(notification, 0);
- }
-
- @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());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device1);
-
- // perform push //
- notification = scheduleNotificationAndWait(notification);
- checkReceipts(notification, 1);
- }
-
- @Test
- public void singlePushNotificationViaUser() throws Exception {
-
- app.clear();
-
- app.put("username", "asdf");
- app.put("email", "asdf@adsf.com");
- Entity user = app.testRequest(ServiceAction.POST, 1, "users")
- .getEntity();
- assertNotNull(user);
- Entity device = app.testRequest(ServiceAction.POST, 1, "users",
- user.getUuid(), "devices", device1.getUuid()).getEntity();
- assertEquals(device.getUuid(), device1.getUuid());
-
- 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());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- ns.addDevice(notification, device1);
-
- // perform push //
- notification = scheduleNotificationAndWait(notification);
- checkReceipts(notification, 1);
- }
-
- @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());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device2);
- ns.addDevice(notification, device1);
-
- // 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 = scheduleNotificationAndWait(notification);
-
- checkReceipts(notification, 2);
- } finally {
- field.setInt(GCMAdapter.class, multicastSize);
- }
- }
-
- @Ignore
- // todo: how can I mock this?
- @Test
- public void providerIdUpdate() throws Exception {
-
- // mock action (based on verified actual behavior) //
- final String newProviderId = "newProviderId";
- ns.providerAdapters.put("google", new MockSuccessfulProviderAdapter() {
- @Override
- public void sendNotification(String providerId, Notifier notifier,
- Object payload, Notification notification,
- TaskTracker tracker) throws Exception {
- tracker.completed(newProviderId);
- }
- });
-
- // create push notification //
-
- 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());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device1);
-
- // perform push //
- notification = scheduleNotificationAndWait(notification);
- checkReceipts(notification, 1);
-
- Device device = (Device) app.getEm().get(device1).toTypedEntity();
- assertEquals(newProviderId,
- device.getProperty(notifier.getName() + NOTIFIER_ID_POSTFIX));
- }
-
- @Test
- public void badPayloads() throws Exception {
-
- // bad payloads format
-
- app.clear();
- app.put("payloads", "{asdf}");
-
- try {
- app.testRequest(ServiceAction.POST, 1, "notifications");
- fail("invalid payload should have been rejected");
- } catch (IllegalArgumentException ex) {
- // ok
- }
-
- // bad notifier
-
- Map<String, String> payloads = new HashMap<String, String>(2);
- app.put("payloads", payloads);
- payloads.put("xxx", "");
- try {
- app.testRequest(ServiceAction.POST, 1, "notifications");
- fail("invalid payload should have been rejected");
- } catch (IllegalArgumentException ex) {
- // ok
- }
-
- // payload too long
-
- // need the real provider for this one...
- app.clear();
- app.put("name", "gcm2");
- app.put("provider", "google");
- app.put("environment", "development");
- app.put("apiKey", API_KEY);
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifiers")
- .getEntity();
- Notifier notifier2 = app.getEm().get(e.getUuid(), Notifier.class);
-
- payloads.clear();
- StringBuilder sb = new StringBuilder();
- sb.append("{\"x\":\"");
- while (sb.length() < 4080) {
- sb.append("x");
- }
- sb.append("\"}");
- payloads.put(notifier2.getUuid().toString(), sb.toString());
-
- app.clear();
- app.put("payloads", payloads);
-
- try {
- app.testRequest(ServiceAction.POST, 1, "notifications");
- fail("invalid payload should have been rejected");
- } catch (Exception ex) {
- assertEquals("java.lang.IllegalArgumentException: GCM payloads must be 4096 characters or less",
- ex.getMessage());
- // ok
- }
- }
-
- @Ignore
- // todo: how can I mock this?
- @Test
- public void badToken() throws Exception {
-
- // mock action (based on verified actual behavior) //
- if (!USE_REAL_CONNECTIONS) {
- ns.providerAdapters.put("google",
- new MockSuccessfulProviderAdapter() {
- @Override
- public void sendNotification(String providerId,
- Notifier notifier, Object payload,
- Notification notification, TaskTracker tracker)
- throws Exception {
- tracker.failed("InvalidRegistration",
- "InvalidRegistration");
- }
- });
- }
-
- // create push notification //
-
- 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());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- // device w/ bad token
- app.clear();
- app.put(notifier.getName() + NOTIFIER_ID_POSTFIX, PUSH_TOKEN + "x");
-
- e = app.testRequest(ServiceAction.POST, 1, "devices").getEntity();
- device1 = app.getEm().get(e.getUuid(), Device.class);
-
- ns.addDevice(notification, device1);
-
- // perform push //
- notification = scheduleNotificationAndWait(notification);
-
- List<EntityRef> receipts = getNotificationReceipts(notification);
- assertEquals(1, receipts.size());
- Receipt receipt = app.getEm().get(receipts.get(0), Receipt.class);
- assertEquals("InvalidRegistration", receipt.getErrorCode());
- }
-
- @Ignore
- // todo: how can I mock this?
- @Test
- public void badAPIKey() throws Exception {
-
- if (!USE_REAL_CONNECTIONS) {
- // mock action (based on verified actual behavior) //
- ns.providerAdapters.put("google",
- new MockSuccessfulProviderAdapter() {
- @Override
- public void sendNotification(String providerId,
- Notifier notifier, Object payload,
- Notification notification, TaskTracker tracker)
- throws Exception {
- Exception e = new IOException();
- throw new ConnectionException(e.getMessage(), e);
- }
- });
- }
-
- // create push notification //
-
- 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());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- ns.addDevice(notification, device1);
-
- // save bad API key
- app.getEm().setProperty(notifier, "apiKey", API_KEY + "x");
-
- // perform push //
-
- try {
- ns.getQueueManager().processBatchAndReschedule(notification, null);
- fail("Should have received a ConnectionException");
- } catch (ConnectionException ex) {
- // good
- }
- }
-
- @Ignore("Run only if you need to.")
- @Test
- public void loadTest() throws Exception {
-
- final int NUM_DEVICES = 10000;
-
- // create notification //
-
- HashMap<String, Object> properties = new LinkedHashMap<String, Object>();
- String payload = "Hello, World!";
- Map<String, String> payloads = new HashMap<String, String>(1);
- payloads.put(notifier.getUuid().toString(), payload);
- properties.put("payloads", payloads);
- properties.put("queued", System.currentTimeMillis());
-
- Entity e = app.testRequest(ServiceAction.POST, 1, "notifications")
- .getEntity();
- app.testRequest(ServiceAction.GET, 1, "notifications", e.getUuid());
-
- Notification notification = app.getEm().get(e.getUuid(),
- Notification.class);
- assertEquals(
- notification.getPayloads().get(notifier.getUuid().toString()),
- payload);
-
- // create a bunch of devices and add them to the notification
- properties = new LinkedHashMap<String, Object>();
- properties.put(notifier.getName() + NOTIFIER_ID_POSTFIX, PUSH_TOKEN);
- for (int i = 0; i < NUM_DEVICES; i++) {
- Entity entity = app.getEm().create("device", properties);
- ns.addDevice(notification, entity);
- }
-
- long time = System.currentTimeMillis();
- LOG.error("START DELIVERY OF {} NOTIFICATIONS", NUM_DEVICES);
-
- // perform push //
- notification = scheduleNotificationAndWait(notification);
- LOG.error("END DELIVERY OF {} NOTIFICATIONS ({})", NUM_DEVICES,
- System.currentTimeMillis() - time);
-
- // check receipts //
- checkReceipts(notification, NUM_DEVICES);
- checkStatistics(notification, NUM_DEVICES, 0);
- }
-
- // @Test
- // public void inactiveDeviceUpdate() throws Exception {
- //
- // if (!USE_REAL_CONNECTIONS) {
- // // mock action (based on verified actual behavior) //
- // NotificationsService.providerAdapters.put("apple", new
- // MockSuccessfulProviderAdapter() {
- // public Map<String,Date> getInactiveDevices(Notifier notifier,
- // EntityManager em) throws Exception {
- // return Collections.singletonMap(PUSH_TOKEN, new Date());
- // }
- // });
- // }
- //
- // // create push notification //
- //
- // HashMap<String, Object> properties = new LinkedHashMap<String, Object>();
- // String payload =
- // APNS.newPayload().alertBody("Hello, World!").sound("chime").build();
- // Map<String, String> payloads = new HashMap<String, String>(1);
- // payloads.put(notifier.getUuid().toString(), payload);
- // properties.put("payloads", payloads);
- // properties.put("queued", System.currentTimeMillis());
- //
- // Entity e = testRequest(sm, ServiceAction.POST, 1, properties,
- // "notifications").getEntity();
- // testRequest(sm, ServiceAction.GET, 1, null, "notifications",
- // e.getUuid());
- //
- // Notification notification = em.get(e.getUuid(), Notification.class);
- // assertEquals(notification.getPayloads().get(notifier.getUuid().toString()),
- // payload);
- //
- // ns.addDevice(notification, device1);
- // ns.addDevice(notification, device2);
- //
- // assertNotNull(device1.getProperty(notifier.getName() +
- // NOTIFIER_ID_POSTFIX));
- // assertNotNull(device2.getProperty(notifier.getName() +
- // NOTIFIER_ID_POSTFIX));
- //
- // // perform push //
- // notification = scheduleNotificationAndWait(notification);
- //
- // // check provider IDs //
- //
- // device1 = em.get(device1, Device.class);
- // assertNull(device1.getProperty(notifier.getName() +
- // NOTIFIER_ID_POSTFIX));
- // device2 = em.get(device2, Device.class);
- // assertNull(device2.getProperty(notifier.getName() +
- // NOTIFIER_ID_POSTFIX));
- // }
-}