1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 * <response>
61 * <activity>
62 * <id>1</id>
63 * <userId>john.doe</userId>
64 * <title>yellow</title>
65 * <body>body</body>
66 * </activity>
67 * </response>
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 * <response xmlns="http://ns.opensocial.org/2008/opensocial"
98 * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
99 * xsi:schemaLocation="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">
100 * <activity>
101 * <itemsPerPage>10</itemsPerPage>
102 * <startIndex>0</startIndex>
103 * <totalResults>1</totalResults>
104 * <entry>
105 * <appId></appId>
106 * <body></body>
107 * <bodyId></bodyId>
108 * <externalId></externalId>
109 * <id></id>
110 * <mediaItems>
111 * <mimeType></mimeType>
112 * <type></type>
113 * <url></url>
114 * </mediaItems>
115 * <postedTime></postedTime>
116 * <priority></priority>
117 * <streamFaviconUrl></streamFaviconUrl>
118 * <streamSourceUrl></streamSourceUrl>
119 * <streamTitle></streamTitle>
120 * <streamUrl></streamUrl>
121 * <title></title>
122 * <titleId></titleId>
123 * <url></url>
124 * <userId></userId>
125 * </entry>
126 * </activity>
127 * </response>
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 * <?xml version="1.0" encoding="UTF-8"?>
157 * <response xmlns="http://ns.opensocial.org/2008/opensocial"
158 * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
159 * xsi:schemaLocation="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">
160 * <activity>
161 * <itemsPerPage>3</itemsPerPage>
162 * <startIndex>0</startIndex>
163 * <totalResults>10</totalResults>
164 * <entry>
165 * <appId></appId>
166 * <body></body>
167 * <bodyId></bodyId>
168 * <externalId></externalId>
169 * <id></id>
170 * <mediaItems>
171 * <mimeType></mimeType>
172 * <type></type>
173 * <url></url>
174 * </mediaItems>
175 * <postedTime></postedTime>
176 * <priority></priority>
177 * <streamFaviconUrl></streamFaviconUrl>
178 * <streamSourceUrl></streamSourceUrl>
179 * <streamTitle></streamTitle>
180 * <streamUrl></streamUrl>
181 * <title></title>
182 * <titleId></titleId>
183 * <url></url>
184 * <userId></userId>
185 * </entry>
186 * </activity>
187 * </response>
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
253 }