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 com.google.common.collect.Maps;
21  
22  import org.apache.shindig.social.opensocial.util.XSDValidator;
23  
24  import org.junit.Test;
25  import org.w3c.dom.NodeList;
26  import org.xml.sax.InputSource;
27  
28  import java.io.StringReader;
29  import java.util.List;
30  import java.util.Map;
31  
32  import javax.xml.xpath.XPath;
33  import javax.xml.xpath.XPathConstants;
34  import javax.xml.xpath.XPathFactory;
35  
36  public class RestfulXmlDataTest extends AbstractLargeRestfulTests {
37  
38    private XPathFactory xpathFactory;
39  
40    @Override
41    protected void setUp() throws Exception {
42      super.setUp();
43      xpathFactory = XPathFactory.newInstance();
44  
45    }
46  
47    /***
48     * Expected response for app data in json:
49     *
50     * {
51     * "entry" : {
52     * "jane.doe" : {"count" : "7"},
53     * "george.doe" : {"count" : "2"},
54     * "maija.m" : {}, // TODO: Should this entry really be included if she
55     * doesn't have any data? } }
56     *
57     * @throws Exception
58     *           if test encounters an error
59     */
60    @Test
61    public void testGetFriendsAppDataJson() throws Exception {
62      // app id is mocked out
63      Map<String, String> extraParams = Maps.newHashMap();
64      extraParams.put("fields", "count");
65      String resp = getResponse("/appdata/john.doe/@friends/app", "GET",
66          extraParams, "xml", "application/xml");
67      
68      XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
69      
70      XPath xp = xpathFactory.newXPath();
71      NodeList result = (NodeList) xp.evaluate("/appdata/entry", new InputSource(
72          new StringReader(resp)), XPathConstants.NODESET);
73      assertEquals(3, result.getLength());
74  
75      Map<String, Map<String, List<String>>> v = childNodesToMapofMap(result);
76  
77      assertEquals(3, v.size());
78      assertTrue(v.containsKey("jane.doe"));
79      assertTrue(v.containsKey("george.doe"));
80      assertTrue(v.containsKey("maija.m"));
81      
82      assertEquals(1, v.get("jane.doe").size());
83      assertEquals(1, v.get("george.doe").size());
84      assertEquals(0, v.get("maija.m").size());
85  
86      assertEquals("7", v.get("jane.doe").get("count").get(0));
87      assertEquals("2", v.get("george.doe").get("count").get(0));
88    }
89  
90    /***
91     * Expected response for app data in json:
92     *
93     * { "entry" : {
94     * "john.doe" : {"count" : "0"}, } }
95     *
96     * @throws Exception
97     *           if test encounters an error
98     */
99    @Test
100   public void testGetSelfAppDataJson() throws Exception {
101     // app id is mocked out
102     Map<String, String> extraParams = Maps.newHashMap();
103     extraParams.put("fields", null);
104     String resp = getResponse("/appdata/john.doe/@self/app", "GET",
105         extraParams, "xml", "application/xml");
106     
107     XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
108 
109     XPath xp = xpathFactory.newXPath();
110     NodeList result = (NodeList) xp.evaluate("/appdata/entry", new InputSource(
111         new StringReader(resp)), XPathConstants.NODESET);
112 
113     Map<String, Map<String, List<String>>> v = childNodesToMapofMap(result);
114 
115     assertEquals(1, v.size());
116     assertTrue(v.containsKey("john.doe"));
117 
118     assertEquals(1, v.get("john.doe").size());
119 
120     assertEquals("0", v.get("john.doe").get("count").get(0));
121 
122   }
123 
124   /***
125    * Expected response for app data in json:
126    *
127    * { "entry" : { "john.doe" : {"count" : "0"}, } }
128    *
129    * @throws Exception
130    *           if test encounters an error
131    */
132   @Test
133   public void testGetSelfAppDataJsonWithKey() throws Exception {
134     // app id is mocked out
135     Map<String, String> extraParams = Maps.newHashMap();
136     extraParams.put("fields", "count");
137     String resp = getResponse("/appdata/john.doe/@self/app", "GET",
138         extraParams, "xml", "application/xml");
139 
140     XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
141 
142     XPath xp = xpathFactory.newXPath();
143     NodeList result = (NodeList) xp.evaluate("/appdata/entry", new InputSource(
144         new StringReader(resp)), XPathConstants.NODESET);
145 
146     Map<String, Map<String, List<String>>> v = childNodesToMapofMap(result);
147 
148     assertEquals(1, v.size());
149     assertTrue(v.containsKey("john.doe"));
150 
151     assertEquals(1, v.get("john.doe").size());
152 
153     assertEquals("0", v.get("john.doe").get("count").get(0));
154   }
155 
156   /***
157    * Expected response for app data in json with non-existant key: TODO: Double
158    * check this output with the spec
159    *
160    * { "entry" : { "john.doe" : {}, } }
161    *
162    * @throws Exception
163    *           if test encounters an error
164    */
165   @Test
166   public void testGetSelfAppDataJsonWithInvalidKeys() throws Exception {
167     // app id is mocked out
168     Map<String, String> extraParams = Maps.newHashMap();
169     extraParams.put("fields", "peabody");
170     String resp = getResponse("/appdata/john.doe/@self/app", "GET",
171         extraParams, "xml", "application/xml");
172 
173     XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
174 
175     XPath xp = xpathFactory.newXPath();
176     NodeList result = (NodeList) xp.evaluate("/appdata/entry", new InputSource(
177         new StringReader(resp)), XPathConstants.NODESET);
178 
179     Map<String, Map<String, List<String>>> v = childNodesToMapofMap(result);
180 
181     assertEquals(1, v.size());
182     assertTrue(v.containsKey("john.doe"));
183 
184     assertEquals(0, v.get("john.doe").size());
185   }
186 
187   @Test
188   public void testDeleteAppData() throws Exception {
189     assertCount("0");
190 
191     // With the wrong field
192     Map<String, String> extraParams = Maps.newHashMap();
193     extraParams.put("fields", "peabody");
194     String resp = getResponse("/appdata/john.doe/@self/app", "DELETE", extraParams, "xml",
195         "application/xml");
196 
197     XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
198 
199     assertCount("0");
200 
201     // should be xml ?
202     extraParams.put("fields", "count");
203     getResponse("/appdata/john.doe/@self/app", "DELETE", extraParams, "xml",
204         "application/xml");
205 
206     XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
207 
208     assertCount(null);
209   }
210 
211   @Test
212   public void testUpdateAppData() throws Exception {
213     assertCount("0");
214 
215     Map<String, String> extraParams = Maps.newHashMap();
216     extraParams.put("fields", "count");
217     // should be xml ?
218     String postData = XSDValidator.XMLDEC+"<map><entry><key>count</key><value>5</value></entry></map>";
219     String resp = getResponse("/appdata/john.doe/@self/app", "POST", extraParams, postData,
220         "xml", "application/xml");
221     
222     XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
223 
224 
225     assertCount("5");
226   }
227 
228   private void assertCount(String expectedCount) throws Exception {
229     String resp = getResponse("/appdata/john.doe/@self/app", "GET", "xml",
230         "application/xml");
231     
232     XSDValidator.validate(resp, XMLSCHEMA, XSDRESOURCE,false);
233 
234 
235     XPath xp = xpathFactory.newXPath();
236     NodeList result = (NodeList) xp.evaluate("/appdata/entry", new InputSource(
237         new StringReader(resp)), XPathConstants.NODESET);
238 
239     Map<String, Map<String, List<String>>> v = childNodesToMapofMap(result);
240 
241     assertEquals(1, v.size());
242     assertTrue(v.containsKey("john.doe"));
243 
244     if (expectedCount != null) {
245       assertEquals(1, v.get("john.doe").size());
246 
247       assertEquals(String.valueOf(expectedCount), v.get("john.doe")
248           .get("count").get(0));
249     } else {
250       assertEquals(0, v.get("john.doe").size());
251 
252     }
253   }
254 
255   // TODO: support for indexBy??
256 
257 }