You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by do...@apache.org on 2008/05/20 15:53:30 UTC

svn commit: r658247 - in /incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social: SocialApiTestsGuiceModule.java abdera/SocialApiProviderLargeTest.java

Author: doll
Date: Tue May 20 06:53:30 2008
New Revision: 658247

URL: http://svn.apache.org/viewvc?rev=658247&view=rev
Log:
Added more tests for the restful json format code. 

Note: most of these aren't running yet because they all fail. I am going to write a bunch of spec correct tests first and then make them past - tdd style. More tests coming shortly.


Modified:
    incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/SocialApiTestsGuiceModule.java
    incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderLargeTest.java

Modified: incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/SocialApiTestsGuiceModule.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/SocialApiTestsGuiceModule.java?rev=658247&r1=658246&r2=658247&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/SocialApiTestsGuiceModule.java (original)
+++ incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/SocialApiTestsGuiceModule.java Tue May 20 06:53:30 2008
@@ -87,20 +87,6 @@
 
       // setup Jane Doe
       janeDoe = new Person("jane.doe", new Name("Jane Doe"));
-      List<Phone> phones1 = new ArrayList<Phone>();
-      phones1.add(new Phone("+33H000000000", "home"));
-      phones1.add(new Phone("+33M000000000", "mobile"));
-      phones1.add(new Phone("+33W000000000", "work"));
-      janeDoe.setPhoneNumbers(phones1);
-
-      List<Address> addresses1 = new ArrayList<Address>();
-      addresses.add(new Address("My home address"));
-      janeDoe.setAddresses(addresses1);
-
-      List<Email> emails1 = new ArrayList<Email>();
-      emails1.add(new Email("jane.doe@work.bar", "work"));
-      emails1.add(new Email("jane.doe@home.bar", "home"));
-      janeDoe.setEmails(emails1);
       janeDoe.setUpdated(new Date());
 
       // setup Simple Doe
@@ -157,6 +143,14 @@
     public static ResponseItem<List<Activity>> activities;
     public static ResponseItem<Activity> activity;
 
+    public static Activity basicActivity;
+
+    static {
+      basicActivity = new Activity("1", "john.doe");
+      basicActivity.setTitle("yellow");
+      basicActivity.setBody("what a color!");
+    }
+
     public static void setActivity(ResponseItem<Activity> activityVal) {
       activity = activityVal;
     }

Modified: incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderLargeTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderLargeTest.java?rev=658247&r1=658246&r2=658247&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderLargeTest.java (original)
+++ incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/SocialApiProviderLargeTest.java Tue May 20 06:53:30 2008
@@ -21,7 +21,9 @@
 import org.apache.shindig.social.ResponseItem;
 import org.apache.shindig.social.SocialApiTestsGuiceModule;
 import org.apache.shindig.social.opensocial.model.ApiCollection;
+import org.apache.shindig.social.opensocial.model.Email;
 import org.apache.shindig.social.opensocial.model.Person;
+import org.apache.shindig.social.opensocial.model.Phone;
 
 import junit.framework.Assert;
 import org.apache.abdera.Abdera;
@@ -29,6 +31,7 @@
 import org.apache.abdera.model.Document;
 import org.apache.abdera.model.Entry;
 import org.apache.abdera.model.Feed;
+import org.apache.abdera.protocol.Response;
 import org.apache.abdera.protocol.Response.ResponseType;
 import org.apache.abdera.protocol.client.AbderaClient;
 import org.apache.abdera.protocol.client.ClientResponse;
@@ -36,15 +39,18 @@
 import org.apache.abdera.util.MimeTypeHelper;
 import org.apache.abdera.writer.Writer;
 import org.apache.abdera.writer.WriterFactory;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.logging.Logger;
@@ -89,16 +95,174 @@
     SocialApiTestsGuiceModule.MockPeopleService.setPeople(
         new ResponseItem<ApiCollection<Person>>(
             new ApiCollection<Person>(people)));
+
+    SocialApiTestsGuiceModule.MockPeopleService.setPerson(
+        new ResponseItem<Person>(SocialApiTestsGuiceModule
+            .MockPeopleService.johnDoe));
   }
 
   @After
   public void tearDown() throws Exception {
     SocialApiTestsGuiceModule.MockPeopleService.setPeople(null);
+    SocialApiTestsGuiceModule.MockPeopleService.setPerson(null);
     resp.release();
   }
 
+
+  // Json tests
+  // TODO: split into sub files
+  // Note: most of these aren't annotated as tests because they don't pass yet
+
+  /**
+   * Expected response for john.doe's json:
+   *
+   * {
+   *   'id' : 'john.doe',
+   *   'name' : {'unstructured' : 'John Doe'},
+   *   'phoneNumbers' : [
+   *     { 'number' : '+33H000000000', 'type' : 'home'},
+   *     { 'number' : '+33M000000000', 'type' : 'mobile'},
+   *     { 'number' : '+33W000000000', 'type' : 'work'}
+   *   ],
+   *   'addresses' : [
+   *     {'unstructuredAddress' : 'My home address'}
+   *   ],
+   *   'emails' : [
+   *     { 'address' : 'john.doe@work.bar', 'type' : 'work'},
+   *     { 'address' : 'john.doe@home.bar', 'type' : 'home'}
+   *   ]
+   * }
+   */
+  @Test
+  public void testGetPersonJson() throws Exception {
+    resp = client.get(BASEURL + "/people/john.doe/@self");
+    checkForGoodJsonResponse(resp);
+    JSONObject result = getJson(resp);
+
+    Person johnDoe = SocialApiTestsGuiceModule.MockPeopleService.johnDoe;
+    assertEquals(johnDoe.getId(), result.getString("id"));
+
+    assertEquals(johnDoe.getName().getUnstructured(),
+        result.getJSONObject("name").getString("unstructured"));
+
+    assertEquals(johnDoe.getAddresses().get(0).getUnstructuredAddress(),
+        result.getJSONArray("addresses").getJSONObject(0)
+            .getString("unstructuredAddress"));
+
+    JSONArray phoneArray = result.getJSONArray("phoneNumbers");
+    assertEquals(3, phoneArray.length());
+
+    for (int i = 0; i < johnDoe.getPhoneNumbers().size(); i++) {
+      Phone expectedPhone = johnDoe.getPhoneNumbers().get(i);
+      JSONObject actualPhone = phoneArray.getJSONObject(i);
+      assertEquals(expectedPhone.getType(), actualPhone.getString("type"));
+      assertEquals(expectedPhone.getNumber(), actualPhone.getString("number"));
+    }
+
+    JSONArray emailArray = result.getJSONArray("emails");
+    assertEquals(2, emailArray.length());
+
+    for (int i = 0; i < johnDoe.getEmails().size(); i++) {
+      Email expectedEmail = johnDoe.getEmails().get(i);
+      JSONObject actualEmail = emailArray.getJSONObject(i);
+      assertEquals(expectedEmail.getType(), actualEmail.getString("type"));
+      assertEquals(expectedEmail.getAddress(),
+          actualEmail.getString("address"));
+    }
+  }
+
+  /**
+   * Expected response for a list of people in json:
+   * TODO: Fix the question marks...
+   *
+   * {
+   *  "author" : "<???>",
+   *  "link" : {"rel" : "next", "href" : "<???>"},
+   *  "totalResults" : 2,
+   *  "startIndex" : 0
+   *  "itemsPerPage" : 10
+   *  "entry" : [
+   *     {<jane doe>}, // layed out like above
+   *     {<simple doe>},
+   *  ]
+   * }
+   */
+  public void testGetPeopleJson() throws Exception {
+    resp = client.get(BASEURL + "/people/john.doe/@friends");
+    checkForGoodJsonResponse(resp);
+    JSONObject result = getJson(resp);
+
+    assertEquals(2, result.getInt("totalResults"));
+    assertEquals(0, result.getInt("startIndex"));
+    assertEquals(10, result.getInt("itemsPerPage"));
+
+    JSONArray people = result.getJSONArray("entry");
+
+    JSONObject janeDoe = people.getJSONObject(0);
+    assertEquals("jane.doe", janeDoe.getString("id"));
+
+    JSONObject simpleDoe = people.getJSONObject(1);
+    assertEquals("simple.doe", simpleDoe.getString("id"));
+  }
+
+  /**
+   * Expected response for an activity in json:
+   * {
+   *   'id' : '1',
+   *   'userId' : 'john.doe',
+   *   'title' : 'yellow',
+   *   'body' : 'what a color!'
+   * }
+   */
+  public void testGetActivityJson() throws Exception {
+    resp = client.get(BASEURL + "/activities/john.doe/@self/1");
+    checkForGoodJsonResponse(resp);
+    JSONObject result = getJson(resp);
+  }
+
+  /**
+   * Expected response for a list of activities in json:
+   * TODO: Fix the question marks...
+   *
+   * {
+   *  "author" : "<???>",
+   *  "link" : {"rel" : "next", "href" : "<???>"},
+   *  "totalResults" : 1,
+   *  "startIndex" : 0
+   *  "itemsPerPage" : 10
+   *  "entry" : [
+   *     {<activity>} // layed out like above
+   *  ]
+   * }
+   */
+  public void testGetActivitiesJson() throws Exception {
+    resp = client.get(BASEURL + "/activities/john.doe/@self");
+    checkForGoodJsonResponse(resp);
+    JSONObject result = getJson(resp);
+  }
+
+  /**
+   * Expected response for app data in json:
+   *
+   * {
+   *  "entry" : {
+   *    "jane.doe" : {"count" : 5},
+   *    "simple.doe" : {"count" : 7},
+   *  }
+   * }
+   */
+  public void testGetAppDataJson() throws Exception {
+    // app id is mocked out
+    resp = client.get(BASEURL + "/appdata/john.doe/@friends/appId");
+    checkForGoodJsonResponse(resp);
+    JSONObject result = getJson(resp);
+  }
+
+
+  // Atom tests
+
   @Test
-  public void testGetConnectionsForJohnDoe() throws IOException {
+  public void testGetPeopleAtom() throws IOException {
     resp = client.get(BASEURL + "/people/john.doe/@all?format=atom");
     checkForGoodAtomResponse(resp);
 
@@ -109,7 +273,7 @@
   }
 
   @Test
-  public void testGetJaneDoeProfileForJohnDoe() throws IOException {
+  public void testGetIndirectPersonAtom() throws IOException {
     resp = client.get(BASEURL + "/people/john.doe/@all/jane.doe?format=atom");
     checkForGoodAtomResponse(resp);
 
@@ -122,23 +286,46 @@
   }
 
   @Test
-  public void testGetInvalidProfileForJohnDoe() throws IOException {
+  public void testGetInvalidPersonAtom() throws IOException {
     resp = client.get(BASEURL + "/people/john.doe/@all/nobody?format=atom");
-    checkForBadAtomResponse(resp);
+    checkForBadResponse(resp);
   }
 
-  protected void checkForGoodAtomResponse(ClientResponse response){
+  protected void checkForGoodResponse(ClientResponse response,
+      String mimeType) {
     assertNotNull(response);
     assertEquals(ResponseType.SUCCESS, response.getType());
     assertTrue(MimeTypeHelper.isMatch(response.getContentType().toString(),
-        Constants.ATOM_MEDIA_TYPE));
+        mimeType));
+  }
+
+  protected void checkForGoodJsonResponse(ClientResponse response){
+    checkForGoodResponse(response, "application/json");
+  }
+
+  protected void checkForGoodAtomResponse(ClientResponse response){
+    checkForGoodResponse(response, Constants.ATOM_MEDIA_TYPE);
   }
 
-  protected void checkForBadAtomResponse(ClientResponse response){
+  protected void checkForBadResponse(ClientResponse response){
     assertNotNull(response);
     assertEquals(ResponseType.CLIENT_ERROR, response.getType());
   }
 
+  private JSONObject getJson(ClientResponse resp) throws IOException,
+      JSONException {
+    BufferedReader reader = new BufferedReader(resp.getReader());
+
+    StringBuffer json = new StringBuffer();
+    String line = reader.readLine();
+    while (line != null) {
+      json.append(line);
+      line = reader.readLine();
+    }
+
+    return new JSONObject(json.toString());
+  }
+
   protected void prettyPrint(Base doc) throws IOException {
     WriterFactory writerFactory = abdera.getWriterFactory();
     Writer writer = writerFactory.getWriter("prettyxml");