/** {@inheritDoc} */ public Future<RestfulCollection<Group>> getGroups(UserId userId, CollectionOptions options, Set<String> fields, SecurityToken token) throws ProtocolException { List<Group> result = Lists.newArrayList(); String user = userId.getUserId(token); try { JSONArray groups = db.getJSONObject(GROUPS_TABLE).getJSONArray(user); for (int i = 0; i < groups.length(); i++) { JSONObject group = groups.getJSONObject(i); Group groupObj = filterFields(group, fields, Group.class); result.add(groupObj); } } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } return Futures.immediateFuture(new RestfulCollection<Group>(result)); }
/** {@inheritDoc} */ public Future<RestfulCollection<Group>> getGroups(UserId userId, CollectionOptions options, Set<String> fields, SecurityToken token) throws ProtocolException { List<Group> result = Lists.newArrayList(); String user = userId.getUserId(token); try { JSONArray groups = db.getJSONObject(GROUPS_TABLE).getJSONArray(user); for (int i = 0; i < groups.length(); i++) { JSONObject group = groups.getJSONObject(i); Group groupObj = filterFields(group, fields, Group.class); result.add(groupObj); } } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } return Futures.immediateFuture(new RestfulCollection<Group>(result)); }
/** {@inheritDoc} */ public Future<ActivityEntry> getActivityEntry(UserId userId, GroupId groupId, String appId, Set<String> fields, String activityId, SecurityToken token) throws ProtocolException { try { String user = userId.getUserId(token); if (db.getJSONObject(ACTIVITYSTREAMS_TABLE).has(user)) { JSONArray activityEntries = db.getJSONObject(ACTIVITYSTREAMS_TABLE).getJSONArray(user); for (int i = 0; i < activityEntries.length(); i++) { JSONObject activityEntry = activityEntries.getJSONObject(i); if (activityEntry.getString(ActivityEntry.Field.ID.toString()).equals(activityId)) { return Futures.immediateFuture(filterFields(activityEntry, fields, ActivityEntry.class)); } } } throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Activity not found: " + activityId); } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<ActivityEntry> getActivityEntry(UserId userId, GroupId groupId, String appId, Set<String> fields, String activityId, SecurityToken token) throws ProtocolException { try { String user = userId.getUserId(token); if (db.getJSONObject(ACTIVITYSTREAMS_TABLE).has(user)) { JSONArray activityEntries = db.getJSONObject(ACTIVITYSTREAMS_TABLE).getJSONArray(user); for (int i = 0; i < activityEntries.length(); i++) { JSONObject activityEntry = activityEntries.getJSONObject(i); if (activityEntry.getString(ActivityEntry.Field.ID.toString()).equals(activityId)) { return Futures.immediateFuture(filterFields(activityEntry, fields, ActivityEntry.class)); } } } throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Activity not found: " + activityId); } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<Activity> getActivity(UserId userId, GroupId groupId, String appId, Set<String> fields, String activityId, SecurityToken token) throws ProtocolException { try { String user = userId.getUserId(token); if (db.getJSONObject(ACTIVITIES_TABLE).has(user)) { JSONArray activities = db.getJSONObject(ACTIVITIES_TABLE).getJSONArray(user); for (int i = 0; i < activities.length(); i++) { JSONObject activity = activities.getJSONObject(i); if (activity.get(Activity.Field.USER_ID.toString()).equals(user) && activity.get(Activity.Field.ID.toString()).equals(activityId)) { return Futures.immediateFuture(filterFields(activity, fields, Activity.class)); } } } throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Activity not found"); } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<RestfulCollection<MessageCollection>> getMessageCollections(UserId userId, Set<String> fields, CollectionOptions options, SecurityToken token) throws ProtocolException { try { List<MessageCollection> result = Lists.newArrayList(); JSONObject messageCollections = db.getJSONObject(MESSAGE_TABLE).getJSONObject( userId.getUserId(token)); for (String msgCollId : JSONObject.getNames(messageCollections)) { JSONObject msgColl = messageCollections.getJSONObject(msgCollId); msgColl.put("id", msgCollId); JSONArray messages = msgColl.getJSONArray("messages"); int numMessages = (messages == null) ? 0 : messages.length(); msgColl.put("total", String.valueOf(numMessages)); msgColl.put("unread", String.valueOf(numMessages)); result.add(filterFields(msgColl, fields, MessageCollection.class)); } return ImmediateFuture.newInstance(new RestfulCollection<MessageCollection>(result)); } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
@Test @Ignore public void testPostMessage() throws Exception { MessageImpl message = new MessageImpl("A message body", "A title", Message.Type.PRIVATE_MESSAGE); message.setRecipients(recipients); EasyMock.expect(converter.convertToObject(null, Message.class)).andReturn(message); EasyMock.expect(messageService.createMessage(sender, "messageHandlerTest", "@outbox", message, token)).andReturn(Futures.immediateFuture((Void) null)); EasyMock.replay(messageService, converter); RestHandler operation = registry.getRestHandler("/messages/" + sender.getUserId() + "/@outbox", "POST"); Map<String,String[]> params = ImmutableMap.of(RequestItem.APP_ID, new String[]{"messageHandlerTest"}); operation.execute(params,null, token, converter).get(); EasyMock.verify(converter, messageService); } }
@Test public void testGetExpectedAppDataForPlural() throws Exception { DataCollection responseItem = db.getPersonData( ImmutableSet.of(CANON_USER, JOHN_DOE), SELF_GROUP, APP_ID, Collections.<String>emptySet(), new FakeGadgetToken()).get(); assertFalse(responseItem.getEntry().isEmpty()); assertFalse(responseItem.getEntry().get(CANONICAL_USER_ID).isEmpty()); assertSame(2, responseItem.getEntry().get(CANONICAL_USER_ID).size()); assertTrue(responseItem.getEntry().get(CANONICAL_USER_ID).containsKey("count")); assertTrue(responseItem.getEntry().get(CANONICAL_USER_ID).containsKey("size")); assertFalse(responseItem.getEntry().get(JOHN_DOE.getUserId()).isEmpty()); assertSame(1, responseItem.getEntry().get(JOHN_DOE.getUserId()).size()); assertTrue(responseItem.getEntry().get(JOHN_DOE.getUserId()).containsKey("count")); }
@Test public void testGetExpectedAppDataForPlural() throws Exception { DataCollection responseItem = db.getPersonData( ImmutableSet.of(CANON_USER, JOHN_DOE), SELF_GROUP, APP_ID, Collections.<String>emptySet(), new FakeGadgetToken()).get(); assertFalse(responseItem.getEntry().isEmpty()); assertFalse(responseItem.getEntry().get(CANONICAL_USER_ID).isEmpty()); assertSame(2, responseItem.getEntry().get(CANONICAL_USER_ID).size()); assertTrue(responseItem.getEntry().get(CANONICAL_USER_ID).containsKey("count")); assertTrue(responseItem.getEntry().get(CANONICAL_USER_ID).containsKey("size")); assertFalse(responseItem.getEntry().get(JOHN_DOE.getUserId()).isEmpty()); assertSame(1, responseItem.getEntry().get(JOHN_DOE.getUserId()).size()); assertTrue(responseItem.getEntry().get(JOHN_DOE.getUserId()).containsKey("count")); }
@Test public void testGetExpectedAppDataForPlural() throws Exception { DataCollection responseItem = db.getPersonData( ImmutableSet.of(CANON_USER, JOHN_DOE), SELF_GROUP, APP_ID, Collections.<String>emptySet(), new FakeGadgetToken()).get(); assertFalse(responseItem.getEntry().isEmpty()); assertFalse(responseItem.getEntry().get(CANONICAL_USER_ID).isEmpty()); assertSame(2, responseItem.getEntry().get(CANONICAL_USER_ID).size()); assertTrue(responseItem.getEntry().get(CANONICAL_USER_ID).containsKey("count")); assertTrue(responseItem.getEntry().get(CANONICAL_USER_ID).containsKey("size")); assertFalse(responseItem.getEntry().get(JOHN_DOE.getUserId()).isEmpty()); assertSame(1, responseItem.getEntry().get(JOHN_DOE.getUserId()).size()); assertTrue(responseItem.getEntry().get(JOHN_DOE.getUserId()).containsKey("count")); }
@Test @Ignore public void testPostMessage() throws Exception { MessageImpl message = new MessageImpl("A message body", "A title", Message.Type.PRIVATE_MESSAGE); message.setRecipients(recipients); EasyMock.expect(converter.convertToObject(null, Message.class)).andReturn(message); EasyMock.expect(messageService.createMessage(sender, "messageHandlerTest", "@outbox", message, token)).andReturn(Futures.immediateFuture((Void) null)); EasyMock.replay(messageService, converter); RestHandler operation = registry.getRestHandler("/messages/" + sender.getUserId() + "/@outbox", "POST"); Map<String,String[]> params = ImmutableMap.of(RequestItem.APP_ID, new String[]{"messageHandlerTest"}); operation.execute(params,null, token, converter).get(); EasyMock.verify(converter, messageService); } }
@Test public void testPostMessage() throws Exception { MessageImpl message = new MessageImpl("A message body", "A title", Message.Type.PRIVATE_MESSAGE); message.setRecipients(recipients); EasyMock.expect(converter.convertToObject(null, Message.class)).andReturn(message); EasyMock.expect(messageService.createMessage(sender, "messageHandlerTest", "@outbox", message, token)).andReturn(ImmediateFuture.newInstance((Void) null)); EasyMock.replay(messageService, converter); RestHandler operation = registry.getRestHandler("/messages/" + sender.getUserId() + "/@outbox", "POST"); Map<String,String[]> params = ImmutableMap.of(RequestItem.APP_ID, new String[]{"messageHandlerTest"}); operation.execute(params,null, token, converter).get(); EasyMock.verify(converter, messageService); } }
@Test public void testCreateMessage() throws Exception { // Create a new private message Message msg = new MessageImpl(); msg.setBody("Hello"); msg.setRecipients(Collections.singletonList(JANE_DOE.getUserId())); db.createMessage(JOHN_DOE, APP_ID, "privateMessage", msg, token).get(); // Check that the message was created RestfulCollection<Message> messages = db.getMessages( JANE_DOE, "privateMessage", Collections.<String>emptySet(), Collections.<String>singletonList(msg.getId()), new CollectionOptions(), token).get(); assertEquals(1, messages.getList().size()); } }
@Test public void testCreateMessage() throws Exception { // Create a new private message Message msg = new MessageImpl(); msg.setBody("Hello"); msg.setRecipients(Collections.singletonList(JANE_DOE.getUserId())); db.createMessage(JOHN_DOE, APP_ID, "privateMessage", msg, token).get(); // Check that the message was created RestfulCollection<Message> messages = db.getMessages( JANE_DOE, "privateMessage", Collections.<String>emptySet(), Collections.<String>singletonList(msg.getId()), new CollectionOptions(), token).get(); assertEquals(1, messages.getList().size()); } }
@Test public void testGetUserId() throws Exception { UserId owner = new UserId(UserId.Type.owner, "hello"); assertEquals("owner", owner.getUserId(new FakeGadgetToken().setOwnerId("owner"))); UserId viewer = new UserId(UserId.Type.viewer, "hello"); assertEquals("viewer", viewer.getUserId(new FakeGadgetToken().setViewerId("viewer"))); UserId me = new UserId(UserId.Type.me, "hello"); assertEquals("viewer", me.getUserId(new FakeGadgetToken().setViewerId("viewer"))); UserId user = new UserId(UserId.Type.userId, "hello"); assertEquals("hello", user.getUserId(new FakeGadgetToken())); }
@Test public void testGetUserId() throws Exception { UserId owner = new UserId(UserId.Type.owner, "hello"); assertEquals("owner", owner.getUserId(new FakeGadgetToken().setOwnerId("owner"))); UserId viewer = new UserId(UserId.Type.viewer, "hello"); assertEquals("viewer", viewer.getUserId(new FakeGadgetToken().setViewerId("viewer"))); UserId me = new UserId(UserId.Type.me, "hello"); assertEquals("viewer", me.getUserId(new FakeGadgetToken().setViewerId("viewer"))); UserId user = new UserId(UserId.Type.userId, "hello"); assertEquals("hello", user.getUserId(new FakeGadgetToken())); }
@Test public void testGetUserId() throws Exception { UserId owner = new UserId(UserId.Type.owner, "hello"); assertEquals("owner", owner.getUserId(new FakeGadgetToken().setOwnerId("owner"))); UserId viewer = new UserId(UserId.Type.viewer, "hello"); assertEquals("viewer", viewer.getUserId(new FakeGadgetToken().setViewerId("viewer"))); UserId me = new UserId(UserId.Type.me, "hello"); assertEquals("viewer", me.getUserId(new FakeGadgetToken().setViewerId("viewer"))); UserId user = new UserId(UserId.Type.userId, "hello"); assertEquals("hello", user.getUserId(new FakeGadgetToken())); }
@Test public void testFromJson() throws Exception { UserId owner = UserId.fromJson("@owner"); assertEquals(UserId.Type.owner, owner.getType()); UserId viewer = UserId.fromJson("@viewer"); assertEquals(UserId.Type.viewer, viewer.getType()); UserId me = UserId.fromJson("@me"); assertEquals(UserId.Type.me, me.getType()); UserId user = UserId.fromJson("john.doe"); assertEquals(UserId.Type.userId, user.getType()); assertEquals("john.doe", user.getUserId()); } }
@Test public void testFromJson() throws Exception { UserId owner = UserId.fromJson("@owner"); assertEquals(UserId.Type.owner, owner.getType()); UserId viewer = UserId.fromJson("@viewer"); assertEquals(UserId.Type.viewer, viewer.getType()); UserId me = UserId.fromJson("@me"); assertEquals(UserId.Type.me, me.getType()); UserId user = UserId.fromJson("john.doe"); assertEquals(UserId.Type.userId, user.getType()); assertEquals("john.doe", user.getUserId()); } }
@Test public void testFromJson() throws Exception { UserId owner = UserId.fromJson("@owner"); assertEquals(UserId.Type.owner, owner.getType()); UserId viewer = UserId.fromJson("@viewer"); assertEquals(UserId.Type.viewer, viewer.getType()); UserId me = UserId.fromJson("@me"); assertEquals(UserId.Type.me, me.getType()); UserId user = UserId.fromJson("john.doe"); assertEquals(UserId.Type.userId, user.getType()); assertEquals("john.doe", user.getUserId()); } }