PageRenderTime 44ms CodeModel.GetById 25ms app.highlight 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/bbc/social/shindig/example/handler/RestfulJsonPeopleTest.java

https://github.com/thesmith/bbc-shindig-example
Java | 262 lines | 218 code | 32 blank | 12 comment | 0 complexity | fc2444d15ed93df0abee63890e408153 MD5 | raw file
  1package bbc.social.shindig.example.handler;
  2
  3import java.util.Date;
  4import java.util.Map;
  5import java.util.Set;
  6
  7import javax.servlet.http.HttpServletResponse;
  8
  9import org.apache.shindig.common.PropertiesModule;
 10import org.apache.shindig.protocol.ContentTypes;
 11import org.apache.shindig.protocol.DataServiceServlet;
 12import org.apache.shindig.protocol.HandlerRegistry;
 13import org.apache.shindig.protocol.conversion.BeanJsonConverter;
 14import org.apache.shindig.protocol.conversion.BeanXStreamConverter;
 15import org.apache.shindig.protocol.model.Enum;
 16import org.apache.shindig.protocol.model.EnumImpl;
 17import org.apache.shindig.social.core.model.AddressImpl;
 18import org.apache.shindig.social.core.model.BodyTypeImpl;
 19import org.apache.shindig.social.core.model.ListFieldImpl;
 20import org.apache.shindig.social.core.model.NameImpl;
 21import org.apache.shindig.social.core.model.OrganizationImpl;
 22import org.apache.shindig.social.core.model.PersonImpl;
 23import org.apache.shindig.social.core.model.UrlImpl;
 24import org.apache.shindig.social.core.util.BeanXStreamAtomConverter;
 25import org.apache.shindig.social.core.util.xstream.XStream081Configuration;
 26import org.apache.shindig.social.dataservice.integration.AbstractLargeRestfulTests;
 27import org.apache.shindig.social.opensocial.model.Address;
 28import org.apache.shindig.social.opensocial.model.Drinker;
 29import org.apache.shindig.social.opensocial.model.ListField;
 30import org.apache.shindig.social.opensocial.model.LookingFor;
 31import org.apache.shindig.social.opensocial.model.NetworkPresence;
 32import org.apache.shindig.social.opensocial.model.Organization;
 33import org.apache.shindig.social.opensocial.model.Person;
 34import org.apache.shindig.social.opensocial.model.Smoker;
 35import org.apache.shindig.social.opensocial.model.Url;
 36import org.easymock.EasyMock;
 37import org.json.JSONArray;
 38import org.json.JSONObject;
 39import org.junit.Test;
 40
 41import bbc.social.shindig.example.GuiceModule;
 42
 43import com.google.common.collect.Lists;
 44import com.google.common.collect.Maps;
 45import com.google.inject.Guice;
 46import com.google.inject.Injector;
 47import com.google.inject.Key;
 48import com.google.inject.TypeLiteral;
 49import com.google.inject.name.Names;
 50
 51/**
 52 * Duplication of
 53 * https://svn.apache.org/repos/asf/incubator/shindig/trunk/java/social
 54 * -api/src/test/java/org/apache/shindig/social/dataservice/integration/
 55 * RestfulJsonPeopleTest.java Added create friends to show how to extend
 56 * interface
 57 * 
 58 * @author bens
 59 */
 60public class RestfulJsonPeopleTest extends AbstractLargeRestfulTests {
 61  private Person canonical;
 62
 63  @SuppressWarnings( { "unchecked", "boxing" })
 64  @Override
 65  protected void setUp() throws Exception {
 66    Injector injector = Guice.createInjector(new PropertiesModule(),
 67        new GuiceModule());
 68    this.setResponse(EasyMock.createMock(HttpServletResponse.class));
 69
 70    // Set data service servlet
 71    DataServiceServlet servlet = new DataServiceServlet();
 72    HandlerRegistry dispatcher = injector.getInstance(HandlerRegistry.class);
 73    dispatcher.addHandlers(injector.getInstance(Key.get(
 74        new TypeLiteral<Set<Object>>() {
 75        }, Names.named("org.apache.shindig.social.handlers"))));
 76
 77    servlet.setHandlerRegistry(dispatcher);
 78    servlet.setBeanConverters(new BeanJsonConverter(injector),
 79        new BeanXStreamConverter(new XStream081Configuration(injector)),
 80        new BeanXStreamAtomConverter(new XStream081Configuration(injector)));
 81    this.setServlet(servlet);
 82    assertNotNull(servlet);
 83
 84    NameImpl name = new NameImpl("Sir Shin H. Digg Social Butterfly");
 85    name.setAdditionalName("H");
 86    name.setFamilyName("Digg");
 87    name.setGivenName("Shin");
 88    name.setHonorificPrefix("Sir");
 89    name.setHonorificSuffix("Social Butterfly");
 90    canonical = new PersonImpl("canonical", "Shin Digg", name);
 91
 92    canonical.setAboutMe("I have an example of every piece of data");
 93    canonical.setActivities(Lists.newArrayList("Coding Shindig"));
 94
 95    Address address = new AddressImpl(
 96        "PoBox 3565, 1 OpenStandards Way, Apache, CA");
 97    address.setCountry("US");
 98    address.setLatitude(28.3043F);
 99    address.setLongitude(143.0859F);
100    address.setLocality("who knows");
101    address.setPostalCode("12345");
102    address.setRegion("Apache, CA");
103    address.setStreetAddress("1 OpenStandards Way");
104    address.setType("home");
105    address.setFormatted("PoBox 3565, 1 OpenStandards Way, Apache, CA");
106    canonical.setAddresses(Lists.newArrayList(address));
107
108    canonical.setAge(33);
109    BodyTypeImpl bodyType = new BodyTypeImpl();
110    bodyType.setBuild("svelte");
111    bodyType.setEyeColor("blue");
112    bodyType.setHairColor("black");
113    bodyType.setHeight(1.84F);
114    bodyType.setWeight(74F);
115    canonical.setBodyType(bodyType);
116
117    canonical.setBooks(Lists.newArrayList("The Cathedral & the Bazaar",
118        "Catch 22"));
119    canonical.setCars(Lists.newArrayList("beetle", "prius"));
120    canonical.setChildren("3");
121    AddressImpl location = new AddressImpl();
122    location.setLatitude(48.858193F);
123    location.setLongitude(2.29419F);
124    canonical.setCurrentLocation(location);
125
126    canonical.setBirthday(new Date());
127    canonical.setDrinker(new EnumImpl<Drinker>(Drinker.SOCIALLY));
128    ListField email = new ListFieldImpl("work",
129        "shindig-dev@incubator.apache.org");
130    canonical.setEmails(Lists.newArrayList(email));
131
132    canonical.setEthnicity("developer");
133    canonical.setFashion("t-shirts");
134    canonical.setFood(Lists.newArrayList("sushi", "burgers"));
135    canonical.setGender(Person.Gender.male);
136    canonical.setHappiestWhen("coding");
137    canonical.setHasApp(true);
138    canonical
139        .setHeroes(Lists.newArrayList("Doug Crockford", "Charles Babbage"));
140    canonical.setHumor("none to speak of");
141    canonical.setInterests(Lists.newArrayList("PHP", "Java"));
142    canonical.setJobInterests("will work for beer");
143
144    Organization job1 = new OrganizationImpl();
145    job1.setAddress(new AddressImpl("1 Shindig Drive"));
146    job1.setDescription("lots of coding");
147    job1.setEndDate(new Date());
148    job1.setField("Software Engineering");
149    job1.setName("Apache.com");
150    job1.setSalary("$1000000000");
151    job1.setStartDate(new Date());
152    job1.setSubField("Development");
153    job1.setTitle("Grand PooBah");
154    job1.setWebpage("http://incubator.apache.org/projects/shindig.html");
155    job1.setType("job");
156
157    Organization job2 = new OrganizationImpl();
158    job2.setAddress(new AddressImpl("1 Skid Row"));
159    job2.setDescription("");
160    job2.setEndDate(new Date());
161    job2.setField("College");
162    job2.setName("School of hard Knocks");
163    job2.setSalary("$100");
164    job2.setStartDate(new Date());
165    job2.setSubField("Lab Tech");
166    job2.setTitle("Gopher");
167    job2.setWebpage("");
168    job2.setType("job");
169
170    canonical.setOrganizations(Lists.newArrayList(job1, job2));
171
172    canonical.setUpdated(new Date());
173    canonical.setLanguagesSpoken(Lists.newArrayList("English", "Dutch",
174        "Esperanto"));
175    canonical.setLivingArrangement("in a house");
176    Enum<LookingFor> lookingForRandom = new EnumImpl<LookingFor>(
177        LookingFor.RANDOM, "Random");
178    Enum<LookingFor> lookingForNetworking = new EnumImpl<LookingFor>(
179        LookingFor.NETWORKING, "Networking");
180    canonical.setLookingFor(Lists.newArrayList(lookingForRandom,
181        lookingForNetworking));
182    canonical.setMovies(Lists.newArrayList("Iron Man", "Nosferatu"));
183    canonical.setMusic(Lists.newArrayList("Chieftains", "Beck"));
184    canonical.setNetworkPresence(new EnumImpl<NetworkPresence>(
185        NetworkPresence.ONLINE));
186    canonical.setNickname("diggy");
187    canonical.setPets("dog,cat");
188    canonical.setPhoneNumbers(Lists.<ListField> newArrayList(new ListFieldImpl(
189        "work", "111-111-111"), new ListFieldImpl("mobile", "999-999-999")));
190
191    canonical.setPoliticalViews("open leaning");
192    canonical.setProfileSong(new UrlImpl(
193        "http://www.example.org/songs/OnlyTheLonely.mp3", "Feelin' blue",
194        "road"));
195    canonical.setProfileVideo(new UrlImpl(
196        "http://www.example.org/videos/Thriller.flv", "Thriller", "video"));
197
198    canonical.setQuotes(Lists.newArrayList("I am therfore I code", "Doh!"));
199    canonical.setRelationshipStatus("married to my job");
200    canonical.setReligion("druidic");
201    canonical.setRomance("twice a year");
202    canonical.setScaredOf("COBOL");
203    canonical.setSexualOrientation("north");
204    canonical.setSmoker(new EnumImpl<Smoker>(Smoker.NO));
205    canonical.setSports(Lists.newArrayList("frisbee", "rugby"));
206    canonical.setStatus("happy");
207    canonical.setTags(Lists.newArrayList("C#", "JSON", "template"));
208    canonical.setThumbnailUrl("http://www.example.org/pic/?id=1");
209    canonical.setUtcOffset(-8L);
210    canonical.setTurnOffs(Lists.newArrayList("lack of unit tests", "cabbage"));
211    canonical.setTurnOns(Lists.newArrayList("well document code"));
212    canonical.setTvShows(Lists.newArrayList("House", "Battlestar Galactica"));
213
214    canonical
215        .setUrls(Lists.<Url> newArrayList(new UrlImpl(
216            "http://www.example.org/?id=1", "my profile", "Profile"),
217            new UrlImpl("http://www.example.org/pic/?id=1",
218                "my awesome picture", "Thumbnail")));
219  }
220
221  @Test
222  public void testCreateFriends() throws Exception {
223    Map<String, String> extraParams = Maps.newHashMap();
224    extraParams.put("sortBy", "name");
225    extraParams.put("sortOrder", null);
226    extraParams.put("filterBy", null);
227    extraParams.put("startIndex", null);
228    extraParams.put("count", "20");
229    extraParams.put("fields", null);
230
231    // Currently, for Shindig @all == @friends
232    String resp = getResponse("/people/john.doe/@friends", "GET", extraParams,
233        null, ContentTypes.OUTPUT_JSON_CONTENT_TYPE);
234    JSONObject result = getJson(resp);
235
236    assertEquals(3, result.getInt("totalResults"));
237    assertEquals(0, result.getInt("startIndex"));
238
239    JSONArray people = result.getJSONArray("entry");
240
241    // The users should be in alphabetical order
242    assertPerson(people.getJSONObject(0), "george.doe", "George Doe");
243    assertPerson(people.getJSONObject(1), "jane.doe", "Jane Doe");
244
245    String postData = "{id: 'canonical'}";
246    resp = getResponse("/people/john.doe/@friends", "POST", postData, null,
247        ContentTypes.OUTPUT_JSON_CONTENT_TYPE);
248
249    resp = getResponse("/people/john.doe/@friends", "GET", extraParams, null,
250        ContentTypes.OUTPUT_JSON_CONTENT_TYPE);
251    result = getJson(resp);
252
253    assertEquals(4, result.getInt("totalResults"));
254  }
255
256  private void assertPerson(JSONObject person, String expectedId,
257      String expectedName) throws Exception {
258    assertEquals(expectedId, person.getString("id"));
259    assertEquals(expectedName, person.getJSONObject("name").getString(
260        "formatted"));
261  }
262}