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