View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations under the License.
17   */
18  package org.apache.shindig.social.opensocial.service;
19  
20  import org.apache.shindig.common.testing.FakeGadgetToken;
21  import org.apache.shindig.common.util.ImmediateFuture;
22  import org.apache.shindig.social.ResponseError;
23  import org.apache.shindig.social.core.model.PersonImpl;
24  import org.apache.shindig.social.opensocial.model.Person;
25  import org.apache.shindig.social.opensocial.spi.CollectionOptions;
26  import org.apache.shindig.social.opensocial.spi.GroupId;
27  import org.apache.shindig.social.opensocial.spi.PersonService;
28  import org.apache.shindig.social.opensocial.spi.RestfulCollection;
29  import org.apache.shindig.social.opensocial.spi.UserId;
30  import org.apache.shindig.social.opensocial.spi.SocialSpiException;
31  
32  import com.google.common.collect.ImmutableList;
33  import com.google.common.collect.ImmutableSortedSet;
34  import com.google.common.collect.Maps;
35  import com.google.common.collect.Sets;
36  
37  import junit.framework.TestCase;
38  import org.easymock.classextension.EasyMock;
39  
40  import java.util.List;
41  import java.util.Map;
42  import java.util.Set;
43  import java.util.concurrent.ExecutionException;
44  
45  public class PersonHandlerTest extends TestCase {
46    private PersonService personService;
47    private PersonHandler handler;
48    private FakeGadgetToken token;
49    private RestfulRequestItem request;
50  
51    private static final Set<String> DEFAULT_FIELDS = Sets.newHashSet(Person.Field.ID.toString(),
52        Person.Field.NAME.toString(),
53        Person.Field.THUMBNAIL_URL.toString());
54  
55    private static final Set<UserId> JOHN_DOE = Sets
56        .newHashSet(new UserId(UserId.Type.userId, "john.doe"));
57  
58    private static CollectionOptions DEFAULT_OPTIONS = new CollectionOptions();
59  
60    static {
61      DEFAULT_OPTIONS.setSortBy(PersonService.TOP_FRIENDS_SORT);
62      DEFAULT_OPTIONS.setSortOrder(PersonService.SortOrder.ascending);
63      DEFAULT_OPTIONS.setFilter(null);
64      DEFAULT_OPTIONS.setFilterOperation(PersonService.FilterOperation.contains);
65      DEFAULT_OPTIONS.setFilterValue("");
66      DEFAULT_OPTIONS.setFirst(0);
67      DEFAULT_OPTIONS.setMax(20);
68    }
69  
70    @Override
71    protected void setUp() throws Exception {
72      token = new FakeGadgetToken();
73      personService = EasyMock.createMock(PersonService.class);
74  
75      handler = new PersonHandler(personService);
76    }
77  
78    private void replay() {
79      EasyMock.replay(personService);
80    }
81  
82    private void verify() {
83      EasyMock.verify(personService);
84    }
85  
86    private void setPath(String path) {
87      Map<String, String> params = Maps.newHashMap();
88      params.put("sortBy", null);
89      params.put("sortOrder", null);
90      params.put("filterBy", null);
91      params.put("startIndex", null);
92      params.put("count", null);
93      params.put("fields", null);
94      this.setPathAndParams(path, params);
95    }
96  
97    private void setPathAndParams(String path, Map<String, String> params) {
98      request = new RestfulRequestItem(path, "GET", null, token, null);
99      for (Map.Entry<String, String> entry : params.entrySet()) {
100       request.setParameter(entry.getKey(), entry.getValue());
101     }
102   }
103 
104   public void testHandleGetAllNoParams() throws Exception {
105     setPath("/people/john.doe/@all");
106 
107     List<Person> personList = ImmutableList.of();
108     RestfulCollection<Person> data = new RestfulCollection<Person>(personList);
109 
110     EasyMock.expect(personService.getPeople(
111         JOHN_DOE,
112         new GroupId(GroupId.Type.all, null),
113         DEFAULT_OPTIONS,
114         DEFAULT_FIELDS,
115         token))
116         .andReturn(ImmediateFuture.newInstance(data));
117 
118     replay();
119     assertEquals(data, handler.handleGet(request).get());
120     verify();
121   }
122 
123   public void testHandleGetFriendsNoParams() throws Exception {
124     setPath("/people/john.doe/@friends");
125 
126     List<Person> personList = ImmutableList.of();
127     RestfulCollection<Person> data = new RestfulCollection<Person>(personList);
128     EasyMock.expect(personService.getPeople(
129         JOHN_DOE,
130         new GroupId(GroupId.Type.friends, null),
131         DEFAULT_OPTIONS,
132         DEFAULT_FIELDS,
133         token))
134         .andReturn(ImmediateFuture.newInstance(data));
135 
136     replay();
137     assertEquals(data, handler.handleGet(request).get());
138     verify();
139   }
140 
141   public void testHandleGetFriendsWithParams() throws Exception {
142     CollectionOptions options = new CollectionOptions();
143     options.setSortBy(Person.Field.NAME.toString());
144     options.setSortOrder(PersonService.SortOrder.descending);
145     options.setFilter(PersonService.TOP_FRIENDS_FILTER);
146     options.setFilterOperation(PersonService.FilterOperation.present);
147     options.setFilterValue("cassie");
148     options.setFirst(5);
149     options.setMax(10);
150 
151     Map<String, String> params = Maps.newHashMap();
152     params.put("sortBy", options.getSortBy());
153     params.put("sortOrder", options.getSortOrder().toString());
154     params.put("filterBy", options.getFilter());
155     params.put("filterOp", options.getFilterOperation().toString());
156     params.put("filterValue", options.getFilterValue());
157     params.put("startIndex", "5");
158     params.put("count", "10");
159     params.put("fields", "money,fame,fortune");
160 
161     setPathAndParams("/people/john.doe/@friends", params);
162 
163     List<Person> people = ImmutableList.of();
164     RestfulCollection<Person> data = new RestfulCollection<Person>(people);
165     EasyMock.expect(personService.getPeople(
166         JOHN_DOE,
167         new GroupId(GroupId.Type.friends, null), options,
168         ImmutableSortedSet.of("money", "fame", "fortune"), token))
169         .andReturn(ImmediateFuture.newInstance(data));
170 
171     replay();
172     assertEquals(data, handler.handleGet(request).get());
173     verify();
174   }
175 
176   public void testHandleGetFriendById() throws Exception {
177     setPath("/people/john.doe/@friends/jane.doe");
178 
179     Person data = new PersonImpl();
180     // TODO: We aren't passing john.doe to the service yet.
181     EasyMock.expect(personService.getPerson(new UserId(UserId.Type.userId, "jane.doe"),
182         DEFAULT_FIELDS, token)).andReturn(ImmediateFuture.newInstance(data));
183 
184     replay();
185     assertEquals(data, handler.handleGet(request).get());
186     verify();
187   }
188 
189   public void testHandleGetSelf() throws Exception {
190     setPath("/people/john.doe/@self");
191 
192     Person data = new PersonImpl();
193     EasyMock.expect(personService.getPerson(JOHN_DOE.iterator().next(),
194         DEFAULT_FIELDS, token)).andReturn(ImmediateFuture.newInstance(data));
195 
196     replay();
197     assertEquals(data, handler.handleGet(request).get());
198     verify();
199   }
200 
201   public void testHandleGetPlural() throws Exception {
202     setPath("/people/john.doe,jane.doe/@self");
203 
204     List<Person> people = ImmutableList.of();
205     RestfulCollection<Person> data = new RestfulCollection<Person>(people);
206     Set<UserId> userIdSet = Sets.newLinkedHashSet(JOHN_DOE);
207     userIdSet.add(new UserId(UserId.Type.userId, "jane.doe"));
208     EasyMock.expect(personService.getPeople(userIdSet,
209         new GroupId(GroupId.Type.self, null),
210         DEFAULT_OPTIONS,
211         DEFAULT_FIELDS,
212         token)).andReturn(ImmediateFuture.newInstance(data));
213 
214     replay();
215     assertEquals(data, handler.handleGet(request).get());
216     verify();
217   }
218 
219   public void testHandleDelete() throws Exception {
220     replay();
221     try {
222       handler.handleDelete(request);
223       fail();
224     } catch (SocialSpiException spe) {
225       assertEquals(ResponseError.BAD_REQUEST, spe.getError());
226     }
227 
228     verify();
229   }
230 
231   public void testHandlePut() throws Exception {
232     replay();
233 
234     try {
235       handler.handlePut(request).get();
236       fail();
237     } catch (SocialSpiException spe) {
238       assertEquals(ResponseError.NOT_IMPLEMENTED, spe.getError());
239     }
240 
241     verify();
242   }
243 
244   public void testHandlePost() throws Exception {
245     replay();
246 
247     try {
248       handler.handlePost(request).get();
249       fail();
250     } catch (SocialSpiException spe) {
251       assertEquals(ResponseError.NOT_IMPLEMENTED, spe.getError());
252     }
253 
254     verify();
255   }
256 }