Author: doll
Date: Tue May 20 12:26:03 2008
New Revision: 658399
URL: http://svn.apache.org/viewvc?rev=658399&view=rev
Log:
By changing the JSONWriter to remove all hoisting, the json people and activity
restful tests now pass.
Data tests are next.
Modified:
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/AbstractSocialEntityCollectionAdapter.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/json/JSONWriter.java
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/RestfulJsonActivityTest.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulJsonPeopleTest.java
Modified:
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/AbstractSocialEntityCollectionAdapter.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/AbstractSocialEntityCollectionAdapter.java?rev=658399&r1=658398&r2=658399&view=diff
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/AbstractSocialEntityCollectionAdapter.java
(original)
+++
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/AbstractSocialEntityCollectionAdapter.java
Tue May 20 12:26:03 2008
@@ -105,7 +105,6 @@
* @return the format and default to Format.JSON
*/
private Format getFormatTypeFromRequest(RequestContext request) {
- // TODO should gracefully handle introspection if format param is missing.
String format = request.getTarget().getParameter("format");
if (format != null && format.equals(Format.ATOM.getDisplayValue())) {
Modified:
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/json/JSONWriter.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/json/JSONWriter.java?rev=658399&r1=658398&r2=658399&view=diff
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/json/JSONWriter.java
(original)
+++
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/json/JSONWriter.java
Tue May 20 12:26:03 2008
@@ -18,29 +18,33 @@
package org.apache.shindig.social.abdera.json;
import org.apache.abdera.model.Base;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.model.Feed;
import org.apache.abdera.util.AbstractNamedWriter;
import org.apache.abdera.util.AbstractWriterOptions;
import org.apache.abdera.writer.NamedWriter;
import org.apache.abdera.writer.WriterOptions;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
+import java.util.List;
/*
* TODO: This file is copied and modified from Abdera code as we needed
* functionality different from the Abdera Json writer code base.
* This file definitely needs cleanup and heavy refactoring
*/
-public class JSONWriter
- extends AbstractNamedWriter
- implements NamedWriter {
-
+public class JSONWriter extends AbstractNamedWriter implements NamedWriter {
public static final String NAME = "json";
-
public static final String[] FORMATS = {
- "application/json",
+ "application/json",
};
public JSONWriter() {
@@ -60,7 +64,7 @@
public Object write(Base base, WriterOptions options) throws IOException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
- writeTo(base,out,options);
+ writeTo(base, out, options);
return new String(out.toByteArray(),options.getCharset());
} catch (IOException i) {
throw i;
@@ -69,18 +73,52 @@
}
}
- public void writeTo(Base base, OutputStream out, WriterOptions options)
throws IOException {
- writeTo(base,new OutputStreamWriter(out,options.getCharset()),options);
- }
+ public void writeTo(Base base, OutputStream out, WriterOptions options)
+ throws IOException {
+ writeTo(base,new OutputStreamWriter(out, options.getCharset()), options);
+ }
+
+ public void writeTo(Base base, java.io.Writer out, WriterOptions options)
+ throws IOException {
+
+ // The JSON format for OpenSocial rest doesn't do any hoisting.
+ // Thus, we just want to pull the main content object out.
+ // TODO: There's gotta be a better way to do this with abdera...
+
+ Element root = ((Document) base).getRoot();
+
+ if (root instanceof Entry) {
+ Entry entry = (Entry) root;
+ String json = entry.getContentElement().getValue();
+ out.write(json);
+
+ } else if (root instanceof Feed) {
+ Feed feed = (Feed) root;
+ List<Entry> entries = feed.getEntries();
+
+ JSONObject json = new JSONObject();
+
+ try {
+ // TODO: Add the top level items for real
+ json.put("startIndex", 0);
+ json.put("totalResults", entries.size());
+
+ JSONArray jsonArray = new JSONArray();
+ for (Entry entry : feed.getEntries()) {
+ String contentValue = entry.getContentElement().getValue();
+ JSONObject jsonItem = new JSONObject(contentValue);
+ jsonArray.put(jsonItem);
+ }
+ json.put("entry", jsonArray);
+
+ } catch (JSONException e) {
+ throw new RuntimeException(
+ "The atom Document could not be converted to JSON", e);
+ }
- public void writeTo(Base base, java.io.Writer out, WriterOptions options)
throws IOException {
- try {
- JSONUtil.toJson(base, out);
- if (options.getAutoClose()) out.close();
- } catch (Exception e) {
- e.printStackTrace();
- throw new IOException(e.getMessage());
+ out.write(json.toString());
}
+ out.flush();
}
}
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=658399&r1=658398&r2=658399&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 12:26:03 2008
@@ -91,6 +91,7 @@
// setup Simple Doe
simpleDoe = new Person("simple.doe", new Name("Simple Doe"));
+ simpleDoe.setUpdated(new Date());
}
public static void setPeople(ResponseItem<ApiCollection<Person>>
@@ -149,6 +150,7 @@
basicActivity = new Activity("1", "john.doe");
basicActivity.setTitle("yellow");
basicActivity.setBody("what a color!");
+ basicActivity.setUpdated(new Date());
}
public static void setActivity(ResponseItem<Activity> activityVal) {
Modified:
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulJsonActivityTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulJsonActivityTest.java?rev=658399&r1=658398&r2=658399&view=diff
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulJsonActivityTest.java
(original)
+++
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulJsonActivityTest.java
Tue May 20 12:26:03 2008
@@ -21,9 +21,12 @@
import org.apache.shindig.social.SocialApiTestsGuiceModule;
import org.apache.shindig.social.opensocial.model.Activity;
+import org.json.JSONObject;
+import org.json.JSONException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
@@ -68,9 +71,9 @@
@Test
public void testGetActivityJson() throws Exception {
resp = client.get(BASEURL + "/activities/john.doe/@self/1");
- // checkForGoodJsonResponse(resp);
- // JSONObject result = getJson(resp);
- // assertActivitiesEqual(activity, result);
+ checkForGoodJsonResponse(resp);
+ JSONObject result = getJson(resp);
+ assertActivitiesEqual(activity, result);
}
/**
@@ -82,7 +85,7 @@
* "link" : {"rel" : "next", "href" : "<???>"},
* "totalResults" : 1,
* "startIndex" : 0
- * "itemsPerPage" : 10
+ * "itemsPerPage" : 10 // Note: the js doesn't support paging. Should rest?
* "entry" : [
* {<activity>} // layed out like above
* ]
@@ -93,10 +96,21 @@
@Test
public void testGetActivitiesJson() throws Exception {
resp = client.get(BASEURL + "/activities/john.doe/@self");
- // checkForGoodJsonResponse(resp);
- // JSONObject result = getJson(resp);
- // assertActivitiesEqual(activity,
- // result.getJSONArray("entry").getJSONObject(0));
+ checkForGoodJsonResponse(resp);
+ JSONObject result = getJson(resp);
+
+ assertEquals(1, result.getInt("totalResults"));
+ assertEquals(0, result.getInt("startIndex"));
+ assertActivitiesEqual(activity,
+ result.getJSONArray("entry").getJSONObject(0));
+ }
+
+ private void assertActivitiesEqual(Activity activity, JSONObject result)
+ throws JSONException {
+ assertEquals(activity.getId(), result.getString("id"));
+ assertEquals(activity.getUserId(), result.getString("userId"));
+ assertEquals(activity.getTitle(), result.getString("title"));
+ assertEquals(activity.getBody(), result.getString("body"));
}
// TODO: Add tests for the fields= parameter
Modified:
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulJsonPeopleTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulJsonPeopleTest.java?rev=658399&r1=658398&r2=658399&view=diff
==============================================================================
---
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulJsonPeopleTest.java
(original)
+++
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulJsonPeopleTest.java
Tue May 20 12:26:03 2008
@@ -146,24 +146,31 @@
// Currently, for Shindig @all == @friends
resp = client.get(BASEURL + "/people/john.doe/@friends");
checkForGoodJsonResponse(resp);
- // TODO: This json doesn't parse right now
- // JSONObject result = getJson(resp);
+ JSONObject result = getJson(resp);
- // assertEquals(2, result.getInt("totalResults"));
- // assertEquals(0, result.getInt("startIndex"));
+ assertEquals(2, result.getInt("totalResults"));
+ assertEquals(0, result.getInt("startIndex"));
+ // TODO: Paging not handled yet
// assertEquals(10, result.getInt("itemsPerPage"));
- // JSONArray people = result.getJSONArray("entry");
+ 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"));
+ for (int i = 0; i < people.length(); i++) {
+ JSONObject person = people.getJSONObject(i);
+ String id = person.getString("id");
+ String name = person.getJSONObject("name").getString("unstructured");
+
+ // TODO: Clean this after we support sorting
+ if (id.equals("jane.doe")) {
+ assertEquals("Jane Doe", name);
+ } else {
+ assertEquals("simple.doe", id);
+ assertEquals("Simple Doe", name);
+ }
+ }
}
// TODO: Add tests for paging, sorting
// TODO: Add tests for fields parameter
// TODO: Add tests for networkDistance
-
}