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  import org.apache.shindig.social.opensocial.util.XSDValidator;
23  
24  import org.junit.Test;
25  import org.w3c.dom.Node;
26  import org.w3c.dom.NodeList;
27  import org.xml.sax.InputSource;
28  
29  import java.io.StringReader;
30  import java.util.List;
31  import java.util.Map;
32  
33  import javax.xml.xpath.XPath;
34  import javax.xml.xpath.XPathConstants;
35  import javax.xml.xpath.XPathFactory;
36  
37  public class RestfulXmlActivityTest extends AbstractLargeRestfulTests {
38    private static final String XMLSCHEMA = " xmlns=\"http://ns.opensocial.org/2008/opensocial\" \n"
39      + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
40      + " xsi:schemaLocation=\"http://ns.opensocial.org/2008/opensocial classpath:opensocial.xsd\" ";
41    private static final String XSDRESOURCE = "opensocial.xsd";
42    private Activity johnsActivity;
43    private XPathFactory xpathFactory;
44  
45    @Override
46    protected void setUp() throws Exception {
47      super.setUp();
48      johnsActivity = new ActivityImpl("1", "john.doe");
49      johnsActivity.setTitle("yellow");
50      johnsActivity.setBody("what a color!");
51  
52      xpathFactory = XPathFactory.newInstance();
53  
54    }
55  
56    /***
57     * Expected response for an activity in xml:
58     * 
59     * <pre>
60     * &lt;response&gt;
61     *    &lt;activity&gt;
62     *       &lt;id&gt;1&lt;/id&gt;
63     *       &lt;userId&gt;john.doe&lt;/userId&gt;
64     *       &lt;title&gt;yellow&lt;/title&gt;
65     *       &lt;body&gt;body&lt;/body&gt;
66     *    &lt;/activity&gt;
67     * &lt;/response&gt;
68     * </pre>
69     * 
70     * @throws Exception
71     *           if test encounters an error
72     */
73    @Test
74    public void testGetActivityJson() throws Exception {
75      String resp = getResponse("/activities/john.doe/@self/@app/1", "GET",
76          "xml", "application/xml");
77  
78      XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
79      
80      InputSource source = new InputSource(new StringReader(resp));
81      XPath xp = xpathFactory.newXPath();
82      NodeList result = (NodeList) xp.evaluate("/response/activity", source,
83          XPathConstants.NODESET);
84      assertEquals(1, result.getLength());
85      Node n = result.item(0);
86  
87      Map<String, List<String>> v = childNodesToMap(n);
88  
89      assertEquals(4, v.size());
90      assertActivitiesEqual(johnsActivity, v);
91    }
92  
93    /***
94     * Expected response for a list of activities in json:
95     * 
96     * <pre>
97     * &lt;response xmlns=&quot;http://ns.opensocial.org/2008/opensocial&quot;
98     *    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
99     *    xsi:schemaLocation=&quot;http://ns.opensocial.org/2008/opensocial file:/Users/ieb/Apache/shindig/trunk/java/social-api/src/test/resources/org/apache/shindig/social/opensocial/util/opensocial.xsd&quot;&gt;
100    *   &lt;activity&gt;
101    *     &lt;itemsPerPage&gt;10&lt;/itemsPerPage&gt;
102    *     &lt;startIndex&gt;0&lt;/startIndex&gt;
103    *     &lt;totalResults&gt;1&lt;/totalResults&gt;
104    *     &lt;entry&gt;
105    *       &lt;appId&gt;&lt;/appId&gt;
106    *       &lt;body&gt;&lt;/body&gt;
107    *       &lt;bodyId&gt;&lt;/bodyId&gt;
108    *       &lt;externalId&gt;&lt;/externalId&gt;
109    *       &lt;id&gt;&lt;/id&gt;
110    *       &lt;mediaItems&gt;
111    *         &lt;mimeType&gt;&lt;/mimeType&gt;
112    *         &lt;type&gt;&lt;/type&gt;
113    *         &lt;url&gt;&lt;/url&gt;
114    *       &lt;/mediaItems&gt;
115    *       &lt;postedTime&gt;&lt;/postedTime&gt;
116    *       &lt;priority&gt;&lt;/priority&gt;
117    *       &lt;streamFaviconUrl&gt;&lt;/streamFaviconUrl&gt;
118    *       &lt;streamSourceUrl&gt;&lt;/streamSourceUrl&gt;
119    *       &lt;streamTitle&gt;&lt;/streamTitle&gt;
120    *       &lt;streamUrl&gt;&lt;/streamUrl&gt;
121    *       &lt;title&gt;&lt;/title&gt;
122    *       &lt;titleId&gt;&lt;/titleId&gt;
123    *       &lt;url&gt;&lt;/url&gt;
124    *       &lt;userId&gt;&lt;/userId&gt;
125    *     &lt;/entry&gt;
126    *   &lt;/activity&gt;
127    * &lt;/response&gt;
128    * </pre>
129    * 
130    * @throws Exception
131    *           if test encounters an error
132    */
133   @Test
134   public void testGetActivitiesJson() throws Exception {
135     String resp = getResponse("/activities/john.doe/@self", "GET", "xml",
136         "application/xml");
137     XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
138     
139     XPath xp = xpathFactory.newXPath();
140     assertEquals("0", xp.evaluate("/response/startIndex", new InputSource(
141         new StringReader(resp))));
142     assertEquals("1", xp.evaluate("/response/totalResults", new InputSource(
143         new StringReader(resp))));
144     NodeList nl = (NodeList) xp.evaluate("/response/entry/activity",
145         new InputSource(new StringReader(resp)), XPathConstants.NODESET);
146     assertEquals(1, nl.getLength());
147 
148     assertActivitiesEqual(johnsActivity, childNodesToMap(nl.item(0)));
149   }
150 
151   /***
152    * Expected response for a list of activities in json:
153    * 
154    * 
155    * <pre>
156    * &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
157    * &lt;response xmlns=&quot;http://ns.opensocial.org/2008/opensocial&quot;
158    *    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
159    *    xsi:schemaLocation=&quot;http://ns.opensocial.org/2008/opensocial file:/Users/ieb/Apache/shindig/trunk/java/social-api/src/test/resources/org/apache/shindig/social/opensocial/util/opensocial.xsd&quot;&gt;
160    *   &lt;activity&gt;
161    *     &lt;itemsPerPage&gt;3&lt;/itemsPerPage&gt;
162    *     &lt;startIndex&gt;0&lt;/startIndex&gt;
163    *     &lt;totalResults&gt;10&lt;/totalResults&gt;
164    *     &lt;entry&gt;
165    *       &lt;appId&gt;&lt;/appId&gt;
166    *       &lt;body&gt;&lt;/body&gt;
167    *       &lt;bodyId&gt;&lt;/bodyId&gt;
168    *       &lt;externalId&gt;&lt;/externalId&gt;
169    *       &lt;id&gt;&lt;/id&gt;
170    *       &lt;mediaItems&gt;
171    *         &lt;mimeType&gt;&lt;/mimeType&gt;
172    *         &lt;type&gt;&lt;/type&gt;
173    *         &lt;url&gt;&lt;/url&gt;
174    *       &lt;/mediaItems&gt;
175    *       &lt;postedTime&gt;&lt;/postedTime&gt;
176    *       &lt;priority&gt;&lt;/priority&gt;
177    *       &lt;streamFaviconUrl&gt;&lt;/streamFaviconUrl&gt;
178    *       &lt;streamSourceUrl&gt;&lt;/streamSourceUrl&gt;
179    *       &lt;streamTitle&gt;&lt;/streamTitle&gt;
180    *       &lt;streamUrl&gt;&lt;/streamUrl&gt;
181    *       &lt;title&gt;&lt;/title&gt;
182    *       &lt;titleId&gt;&lt;/titleId&gt;
183    *       &lt;url&gt;&lt;/url&gt;
184    *       &lt;userId&gt;&lt;/userId&gt;
185    *     &lt;/entry&gt;
186    *   &lt;/activity&gt;
187    * &lt;/response&gt;
188    * </pre>
189    * 
190    * 
191    * @throws Exception
192    *           if test encounters an error
193    */
194   @Test
195   public void testGetFriendsActivitiesJson() throws Exception {
196     String resp = getResponse("/activities/john.doe/@friends", "GET", "xml",
197         "application/xml");
198 
199     XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
200  
201     XPath xp = xpathFactory.newXPath();
202     assertEquals("0", xp.evaluate("/response/startIndex", new InputSource(
203         new StringReader(resp))));
204     assertEquals("2", xp.evaluate("/response/totalResults", new InputSource(
205         new StringReader(resp))));
206     NodeList nl = (NodeList) xp.evaluate("/response/entry", new InputSource(
207         new StringReader(resp)), XPathConstants.NODESET);
208     assertEquals(2, nl.getLength());
209 
210   }
211 
212   private void assertActivitiesEqual(Activity activity,
213       Map<String, List<String>> result) {
214     assertEquals(activity.getId(), result.get("id").get(0));
215     assertEquals(activity.getUserId(), result.get("userId").get(0));
216     assertEquals(activity.getTitle(), result.get("title").get(0));
217     assertEquals(activity.getBody(), result.get("body").get(0));
218   }
219 
220   @Test
221   public void testCreateActivity() throws Exception {
222     String postData = XSDValidator.XMLDEC+"<activity><body>and dad.</body><title>hi mom!</title></activity>";
223     String createResponse = getResponse("/activities/john.doe/@self", "POST",
224         postData, "xml", "application/xml");
225 
226     XSDValidator.validate(createResponse, XMLSCHEMA, XSDRESOURCE,false);
227 
228     String resp = getResponse("/activities/john.doe/@self", "GET", "xml",
229         "application/xml");
230     
231     XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
232 
233     
234     XPath xp = xpathFactory.newXPath();
235     assertEquals("0", xp.evaluate("/response/startIndex", new InputSource(
236         new StringReader(resp))));
237     assertEquals("2", xp.evaluate("/response/totalResults", new InputSource(
238         new StringReader(resp))));
239     NodeList nl = (NodeList) xp.evaluate("/response/entry/activity", new InputSource(
240         new StringReader(resp)), XPathConstants.NODESET);
241     assertEquals(2, nl.getLength());
242 
243     Map<String, List<String>> v = childNodesToMap(nl.item(0));
244     if (v.containsKey("id")) {
245       v = childNodesToMap(nl.item(1));
246     }
247 
248     assertEquals("hi mom!", v.get("title").get(0));
249     assertEquals("and dad.", v.get("body").get(0));
250   }
251 
252   // TODO: Add tests for the fields= parameter
253 }