You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rave.apache.org by ja...@apache.org on 2011/07/20 13:08:45 UTC
svn commit: r1148702 - in /incubator/rave/trunk/rave-shindig/src:
main/java/org/apache/shindig/social/opensocial/jpa/spi/
test/java/org/apache/shindig/social/
test/java/org/apache/shindig/social/opensocial/
test/java/org/apache/shindig/social/opensocia...
Author: jasha
Date: Wed Jul 20 11:08:44 2011
New Revision: 1148702
URL: http://svn.apache.org/viewvc?rev=1148702&view=rev
Log:
RAVE-62 start adding unit tests (not complete) for PersonServiceDb.
Added:
incubator/rave/trunk/rave-shindig/src/test/java/org/apache/shindig/social/
incubator/rave/trunk/rave-shindig/src/test/java/org/apache/shindig/social/opensocial/
incubator/rave/trunk/rave-shindig/src/test/java/org/apache/shindig/social/opensocial/jpa/
incubator/rave/trunk/rave-shindig/src/test/java/org/apache/shindig/social/opensocial/jpa/spi/
incubator/rave/trunk/rave-shindig/src/test/java/org/apache/shindig/social/opensocial/jpa/spi/PersonServiceDbTest.java
Modified:
incubator/rave/trunk/rave-shindig/src/main/java/org/apache/shindig/social/opensocial/jpa/spi/PersonServiceDb.java
Modified: incubator/rave/trunk/rave-shindig/src/main/java/org/apache/shindig/social/opensocial/jpa/spi/PersonServiceDb.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-shindig/src/main/java/org/apache/shindig/social/opensocial/jpa/spi/PersonServiceDb.java?rev=1148702&r1=1148701&r2=1148702&view=diff
==============================================================================
--- incubator/rave/trunk/rave-shindig/src/main/java/org/apache/shindig/social/opensocial/jpa/spi/PersonServiceDb.java (original)
+++ incubator/rave/trunk/rave-shindig/src/main/java/org/apache/shindig/social/opensocial/jpa/spi/PersonServiceDb.java Wed Jul 20 11:08:44 2011
@@ -25,6 +25,8 @@ import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletResponse;
+import com.google.common.collect.Lists;
+import org.apache.commons.lang.StringUtils;
import org.apache.rave.os.DatabasePopulateContextListener;
import org.apache.rave.os.ShindigUtil;
import org.apache.shindig.auth.SecurityToken;
@@ -42,10 +44,6 @@ import org.apache.shindig.social.opensoc
import org.apache.shindig.social.opensocial.spi.UserId;
import org.json.JSONObject;
-import com.google.common.collect.Lists;
-import com.google.inject.Inject;
-import com.google.inject.name.Named;
-
/**
* Implements the PersonService from the SPI binding to the JPA model and providing queries to
* support the OpenSocial implementation.
@@ -195,103 +193,111 @@ public class PersonServiceDb implements
}
-
- /**
- * Add a filter clause specified by the collection options.
- *
- * @param sb the query builder buffer
- * @param collectionOptions the options
- * @param lastPos the last positional parameter that was used so far in the query
- * @return
- */
- private int addFilterClause(StringBuilder sb, FilterCapability filterable,
- CollectionOptions collectionOptions, int lastPos) {
- // this makes the filter value saf
- String filter = filterable.findFilterableProperty(collectionOptions.getFilter(),
- collectionOptions.getFilterOperation());
- String filterValue = collectionOptions.getFilterValue();
- int filterPos = 0;
- if (FilterSpecification.isValid(filter)) {
- if (FilterSpecification.isSpecial(filter)) {
- if (PersonService.HAS_APP_FILTER.equals(filter)) {
- // Retrieves all friends with any data for this application.
- // TODO: how do we determine which application is being talked about,
- // the assumption below is wrong
- filterPos = lastPos + 1;
- sb.append(" f.application_id = ?").append(filterPos);
- } else if (PersonService.TOP_FRIENDS_FILTER.equals(filter)) {
- // Retrieves only the user's top friends, this is defined here by the implementation
- // and there is an assumption that the sort order has already been applied.
- // to do this we need to modify the collections options
- // there will only ever b x friends in the list and it will only ever start at 1
-
- collectionOptions.setFirst(1);
- collectionOptions.setMax(20);
-
- } else if (PersonService.ALL_FILTER.equals(filter)) {
- // select all, ie no filtering
- } else if (PersonService.IS_WITH_FRIENDS_FILTER.equals(filter)) {
- filterPos = lastPos + 1;
- sb.append(" f.friend = ?").append(filterPos);
+ /**
+ * Add a filter clause specified by the collection options.
+ *
+ * @param sb the query {@link StringBuilder}
+ * @param filterable {@link FilterCapability}
+ * @param collectionOptions the options
+ * @param lastPos the last positional parameter that was used so far in the query
+ * @return position of the parameter for the filter
+ */
+ // TODO: if filter is special, it returns 0 and appends nothing to sb
+ int addFilterClause(StringBuilder sb, FilterCapability filterable,
+ CollectionOptions collectionOptions, int lastPos) {
+ // this makes the filter value saf
+ String filter = filterable.findFilterableProperty(collectionOptions.getFilter(),
+ collectionOptions.getFilterOperation());
+ String filterValue = collectionOptions.getFilterValue();
+ int filterPos = 0;
+ if (!FilterSpecification.isValid(filter)) {
+ return filterPos;
}
- } else {
- sb.append("p.").append(filter);
- switch (collectionOptions.getFilterOperation()) {
- case contains:
- filterPos = lastPos + 1;
- sb.append(" like ").append(" ?").append(filterPos);
- filterValue = '%' + filterValue + '%';
- collectionOptions.setFilter(filterValue);
- break;
- case equals:
- filterPos = lastPos + 1;
- sb.append(" = ").append(" ?").append(filterPos);
- break;
- case present:
- sb.append(" is not null ");
- break;
- case startsWith:
- filterPos = lastPos + 1;
- sb.append(" like ").append(" ?").append(filterPos);
- filterValue = '%' + filterValue + '%';
- collectionOptions.setFilter(filterValue);
- break;
+
+ if (FilterSpecification.isSpecial(filter)) {
+ if (PersonService.HAS_APP_FILTER.equals(filter)) {
+ // Retrieves all friends with any data for this application.
+ // TODO: how do we determine which application is being talked about,
+ // the assumption below is wrong
+ filterPos = lastPos + 1;
+ sb.append(" f.application_id = ?").append(filterPos);
+ } else if (PersonService.TOP_FRIENDS_FILTER.equals(filter)) {
+ // Retrieves only the user's top friends, this is defined here by the implementation
+ // and there is an assumption that the sort order has already been applied.
+ // to do this we need to modify the collections options
+ // there will only ever b x friends in the list and it will only ever start at 1
+
+ collectionOptions.setFirst(1);
+ collectionOptions.setMax(20);
+
+ } else if (PersonService.ALL_FILTER.equals(filter)) {
+ // select all, ie no filtering
+ } else if (PersonService.IS_WITH_FRIENDS_FILTER.equals(filter)) {
+ filterPos = lastPos + 1;
+ sb.append(" f.friend = ?").append(filterPos);
+ }
+ } else {
+ sb.append("p.").append(filter);
+ switch (collectionOptions.getFilterOperation()) {
+ case contains:
+ filterPos = lastPos + 1;
+ sb.append(" like ").append(" ?").append(filterPos);
+ filterValue = '%' + filterValue + '%';
+ collectionOptions.setFilter(filterValue);
+ break;
+ case equals:
+ filterPos = lastPos + 1;
+ sb.append(" = ").append(" ?").append(filterPos);
+ break;
+ case present:
+ sb.append(" is not null ");
+ break;
+ case startsWith:
+ filterPos = lastPos + 1;
+ sb.append(" like ").append(" ?").append(filterPos);
+ filterValue = '%' + filterValue + '%';
+ collectionOptions.setFilter(filterValue);
+ break;
+ }
}
- }
+
+ return filterPos;
}
- return filterPos;
- }
- /**
+ /**
* Add an order clause to the query string.
*
* @param sb the buffer for the query string
* @param collectionOptions the options to use for the order.
*/
- private void addOrderClause(StringBuilder sb, CollectionOptions collectionOptions) {
- String sortBy = collectionOptions.getSortBy();
- if (sortBy != null && sortBy.length() > 0) {
+ void addOrderClause(StringBuilder sb, CollectionOptions collectionOptions) {
+ String sortBy = collectionOptions.getSortBy();
+ if (StringUtils.isBlank(sortBy)) {
+ return;
+ }
if (PersonService.TOP_FRIENDS_SORT.equals(sortBy)) {
- // TODO sorting by friend.score doesn't work right now because of group by issue (see above TODO)
- // this assumes that the query is a join with the friends store.
- sb.append(" order by f.score ");
+ // TODO sorting by friend.score doesn't work right now because of group by issue (see above TODO)
+ // this assumes that the query is a join with the friends store.
+ sb.append(" order by f.score ");
} else {
- if ("name".equals(sortBy)) {
- // TODO Is this correct?
- // If sortBy is name then order by p.name.familyName, p.name.givenName.
- sb.append(" order by p.name.familyName, p.name.givenName ");
- } else {
- sb.append(" order by p.").append(sortBy);
- }
- switch (collectionOptions.getSortOrder()) {
- case ascending:
- sb.append(" asc ");
- break;
- case descending:
- sb.append(" desc ");
- break;
- }
+ if ("name".equals(sortBy)) {
+ // TODO Is this correct?
+ // If sortBy is name then order by p.name.familyName, p.name.givenName.
+ sb.append(" order by p.name.familyName, p.name.givenName ");
+ } else {
+ sb.append(" order by p.").append(sortBy);
+ }
+ if (collectionOptions.getSortOrder() == null) {
+ return;
+ }
+ switch (collectionOptions.getSortOrder()) {
+ case ascending:
+ sb.append(" asc ");
+ break;
+ case descending:
+ sb.append(" desc ");
+ break;
+ }
}
- }
}
}
Added: incubator/rave/trunk/rave-shindig/src/test/java/org/apache/shindig/social/opensocial/jpa/spi/PersonServiceDbTest.java
URL: http://svn.apache.org/viewvc/incubator/rave/trunk/rave-shindig/src/test/java/org/apache/shindig/social/opensocial/jpa/spi/PersonServiceDbTest.java?rev=1148702&view=auto
==============================================================================
--- incubator/rave/trunk/rave-shindig/src/test/java/org/apache/shindig/social/opensocial/jpa/spi/PersonServiceDbTest.java (added)
+++ incubator/rave/trunk/rave-shindig/src/test/java/org/apache/shindig/social/opensocial/jpa/spi/PersonServiceDbTest.java Wed Jul 20 11:08:44 2011
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.shindig.social.opensocial.jpa.spi;
+
+import org.apache.shindig.protocol.model.FilterOperation;
+import org.apache.shindig.protocol.model.SortOrder;
+import org.apache.shindig.social.opensocial.jpa.api.FilterCapability;
+import org.apache.shindig.social.opensocial.jpa.api.FilterSpecification;
+import org.apache.shindig.social.opensocial.spi.CollectionOptions;
+import org.apache.shindig.social.opensocial.spi.PersonService;
+import org.junit.Before;
+import org.junit.Test;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertFalse;
+import static junit.framework.Assert.assertTrue;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+
+/**
+ * Test for {@link PersonServiceDb}
+ */
+public class PersonServiceDbTest {
+ PersonServiceDb service;
+
+ @Before
+ public void setUp() throws Exception {
+ service = new PersonServiceDb();
+ }
+
+ /**
+ * Should return 0 as position to add (or actually ignore) if the filter is invalid
+ *
+ * @throws Exception if something goes wrong
+ */
+ @Test
+ public void testAddInvalidFilterClause() throws Exception {
+ final String nullString = null;
+ assertFalse(FilterSpecification.isValid(nullString));
+
+ StringBuilder sb = new StringBuilder();
+ FilterCapability filterable = createMock(FilterCapability.class);
+ CollectionOptions co = new CollectionOptions();
+ int lastPost = 20;
+ expect(filterable.findFilterableProperty(co.getFilter(), co.getFilterOperation())).andReturn(nullString);
+
+ replay(filterable);
+ int returnPos = service.addFilterClause(sb, filterable, co, lastPost);
+ assertEquals(0, returnPos);
+ verify(filterable);
+ }
+
+ /**
+ * Shows that a special filter will never return a String in case it's a special operation and
+ * returns position 0 ("do not add filter")
+ *
+ * @throws Exception if something goes wrong
+ */
+ @Test
+ public void testAddSpecialFilterClause() throws Exception {
+ final String specialOperation = FilterSpecification.SPECIAL_OPERATION;
+ assertTrue(FilterSpecification.isSpecial(specialOperation));
+
+ StringBuilder sb = new StringBuilder();
+ CollectionOptions co = new CollectionOptions();
+ co.setFilter(specialOperation);
+ co.setFilterOperation(FilterOperation.contains);
+ int lastPos = 20;
+ FilterCapability filterable = createMock(FilterCapability.class);
+ expect(filterable.findFilterableProperty(co.getFilter(), co.getFilterOperation())).andReturn(specialOperation);
+ replay(filterable);
+
+ int returnPos = service.addFilterClause(sb, filterable, co, lastPos);
+ assertEquals(0, returnPos);
+ assertEquals("", sb.toString());
+
+ verify(filterable);
+ }
+
+
+ @Test
+ public void testAddLikeFilterClause() throws Exception {
+ final String myProperty = "myProperty";
+ StringBuilder sb = new StringBuilder();
+ FilterCapability filterable = createMock(FilterCapability.class);
+ CollectionOptions co = new CollectionOptions();
+ co.setFilterOperation(FilterOperation.contains);
+ co.setFilterValue("myValue");
+ co.setFilter(myProperty);
+ int lastPost = 0;
+
+ expect(filterable.findFilterableProperty(myProperty, FilterOperation.contains))
+ .andReturn(myProperty);
+ replay(filterable);
+ int returnPos = service.addFilterClause(sb, filterable, co, lastPost);
+ // Using the "special filter"
+ assertEquals(1, returnPos);
+ assertEquals("p.myProperty like ?1", sb.toString());
+ assertEquals("%myValue%", co.getFilter());
+ verify(filterable);
+ }
+
+ @Test
+ public void testAddOrderClause() throws Exception {
+ StringBuilder sb = new StringBuilder();
+ CollectionOptions co = new CollectionOptions();
+
+ service.addOrderClause(sb, co);
+ assertEquals("", sb.toString());
+
+ co.setSortBy(PersonService.TOP_FRIENDS_SORT);
+ service.addOrderClause(sb, co);
+ assertEquals(" order by f.score ", sb.toString());
+
+ co.setSortBy("name");
+ sb = new StringBuilder();
+ service.addOrderClause(sb, co);
+ assertEquals(" order by p.name.familyName, p.name.givenName ", sb.toString());
+
+ sb = new StringBuilder();
+ co.setSortOrder(SortOrder.ascending);
+ service.addOrderClause(sb, co);
+ assertEquals(" order by p.name.familyName, p.name.givenName asc ", sb.toString());
+
+ sb = new StringBuilder();
+ co.setSortOrder(SortOrder.descending);
+ service.addOrderClause(sb, co);
+ assertEquals(" order by p.name.familyName, p.name.givenName desc ", sb.toString());
+
+ co.setSortBy("randomProperty");
+ co.setSortOrder(null);
+ sb = new StringBuilder();
+ service.addOrderClause(sb, co);
+ assertEquals(" order by p.randomProperty", sb.toString());
+
+ }
+}