View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  The ASF licenses this file to You
4    * under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.  For additional information regarding
15   * copyright in this work, please see the NOTICE file in the top level
16   * directory of this distribution.
17   */
18  package org.apache.shindig.social.dataservice.integration;
19  
20  import org.apache.shindig.social.core.model.ActivityImpl;
21  import org.apache.shindig.social.opensocial.model.Activity;
22  
23  import org.json.JSONArray;
24  import org.json.JSONException;
25  import org.json.JSONObject;
26  import org.junit.Test;
27  
28  public class RestfulJsonActivityTest extends AbstractLargeRestfulTests {
29    Activity johnsActivity;
30  
31    @Override
32    protected void setUp() throws Exception {
33      super.setUp();
34      johnsActivity = new ActivityImpl("1", "john.doe");
35      johnsActivity.setTitle("yellow");
36      johnsActivity.setBody("what a color!");
37    }
38  
39    /***
40     * Expected response for an activity in json:
41     * { 'entry' : {
42     *     'id' : '1',
43     *     'userId' : 'john.doe',
44     *     'title' : 'yellow',
45     *     'body' : 'what a color!'
46     *   }
47     * }
48     *
49     * @throws Exception if test encounters an error
50     */
51    @Test
52    public void testGetActivityJson() throws Exception {
53      String resp = getResponse("/activities/john.doe/@self/@app/1", "GET", null, "application/json");
54      JSONObject result = getJson(resp);
55      assertActivitiesEqual(johnsActivity, result.getJSONObject("entry"));
56    }
57  
58    /***
59     * Expected response for a list of activities in json:
60     *
61     * {
62     *  "totalResults" : 1,
63     *  "startIndex" : 0
64     *  "itemsPerPage" : 10 // Note: the js doesn't support paging. Should rest?
65     *  "entry" : [
66     *     {<activity>} // layed out like above
67     *  ]
68     * }
69     *
70     * @throws Exception if test encounters an error
71     */
72    @Test
73    public void testGetActivitiesJson() throws Exception {
74      String resp = getResponse("/activities/john.doe/@self", "GET", null, "application/json");
75      JSONObject result = getJson(resp);
76  
77      assertEquals(1, result.getInt("totalResults"));
78      assertEquals(0, result.getInt("startIndex"));
79      assertActivitiesEqual(johnsActivity, result.getJSONArray("entry").getJSONObject(0));
80    }
81  
82    /***
83     * Expected response for a list of activities in json:
84     *
85     * {
86     *  "totalResults" : 3,
87     *  "startIndex" : 0
88     *  "itemsPerPage" : 10 // Note: the js doesn't support paging. Should rest?
89     *  "entry" : [
90     *     {<activity>} // layed out like above, except for jane.doe
91     *  ]
92     * }
93     *
94     * @throws Exception if test encounters an error
95     */
96    @Test
97    public void testGetFriendsActivitiesJson() throws Exception {
98      String resp = getResponse("/activities/john.doe/@friends", "GET", null, "application/json");
99      JSONObject result = getJson(resp);
100 
101     assertEquals(2, result.getInt("totalResults"));
102     assertEquals(0, result.getInt("startIndex"));
103   }
104 
105   private void assertActivitiesEqual(Activity activity, JSONObject result)
106       throws JSONException {
107     assertEquals(activity.getId(), result.getString("id"));
108     assertEquals(activity.getUserId(), result.getString("userId"));
109     assertEquals(activity.getTitle(), result.getString("title"));
110     assertEquals(activity.getBody(), result.getString("body"));
111   }
112 
113   @Test
114   public void testCreateActivity() throws Exception {
115     String postData = "{title : 'hi mom!', body : 'and dad.'}";
116     String createResponse = getResponse("/activities/john.doe/@self", "POST", postData, null, "application/json");
117 
118     String resp = getResponse("/activities/john.doe/@self", "GET", null, "application/json");
119     JSONObject result = getJson(resp);
120 
121     assertEquals(2, result.getInt("totalResults"));
122     assertEquals(0, result.getInt("startIndex"));
123 
124     JSONArray activities = result.getJSONArray("entry");
125     int newActivityIndex = 0;
126     if (activities.getJSONObject(0).has("id")) {
127       newActivityIndex = 1;
128     }
129 
130     JSONObject jsonActivity = activities.getJSONObject(newActivityIndex);
131     assertEquals("hi mom!", jsonActivity.getString("title"));
132     assertEquals("and dad.", jsonActivity.getString("body"));
133   }
134 
135   // TODO: Add tests for the fields= parameter
136 }