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  import org.json.JSONObject;
22  import org.junit.Test;
23  
24  import java.util.Map;
25  
26  
27  public class RestfulJsonDataTest extends AbstractLargeRestfulTests {
28  
29    /***
30     * Expected response for app data in json:
31     *
32     * {
33     *  "entry" : {
34     *    "jane.doe" : {"count" : "7"},
35     *    "george.doe" : {"count" : "2"},
36     *    "maija.m" : {}, // TODO: Should this entry really be included if she doesn't have any data?
37     *  }
38     * }
39     *
40     * @throws Exception if test encounters an error
41     */
42    @Test
43    public void testGetFriendsAppDataJson() throws Exception {
44      // app id is mocked out
45      Map<String, String> extraParams = Maps.newHashMap();
46      extraParams.put("fields", "count");
47      String resp = getResponse("/appdata/john.doe/@friends/app", "GET", extraParams, null, "application/json");
48  
49      JSONObject data = getJson(resp).getJSONObject("entry");
50      assertEquals(3, data.length());
51  
52      JSONObject janesEntries = data.getJSONObject("jane.doe");
53      assertEquals(1, janesEntries.length());
54      assertEquals("7", janesEntries.getString("count"));
55  
56      JSONObject georgesEntries = data.getJSONObject("george.doe");
57      assertEquals(1, georgesEntries.length());
58      assertEquals("2", georgesEntries.getString("count"));
59    }
60  
61    /***
62     * Expected response for app data in json:
63     *
64     * {
65     *  "entry" : {
66     *    "john.doe" : {"count" : "0"},
67     *  }
68     * }
69     *
70     * @throws Exception if test encounters an error
71     */
72    @Test
73    public void testGetSelfAppDataJson() throws Exception {
74      // app id is mocked out
75      Map<String, String> extraParams = Maps.newHashMap();
76      extraParams.put("fields", null);
77      String resp = getResponse("/appdata/john.doe/@self/app", "GET", extraParams, null, "application/json");
78  
79      JSONObject data = getJson(resp).getJSONObject("entry");
80      assertEquals(1, data.length());
81  
82      JSONObject johnsEntries = data.getJSONObject("john.doe");
83      assertEquals(1, johnsEntries.length());
84      assertEquals("0", johnsEntries.getString("count"));
85    }
86  
87    /***
88     * Expected response for app data in json:
89     *
90     * {
91     *  "entry" : {
92     *    "john.doe" : {"count" : "0"},
93     *  }
94     * }
95     *
96     * @throws Exception if test encounters an error
97     */
98    @Test
99    public void testGetSelfAppDataJsonWithKey() throws Exception {
100     // app id is mocked out
101     Map<String, String> extraParams = Maps.newHashMap();
102     extraParams.put("fields", "count");
103     String resp = getResponse("/appdata/john.doe/@self/app", "GET", extraParams, null, "application/json");
104 
105     JSONObject data = getJson(resp).getJSONObject("entry");
106     assertEquals(1, data.length());
107 
108     JSONObject johnsEntries = data.getJSONObject("john.doe");
109     assertEquals(1, johnsEntries.length());
110     assertEquals("0", johnsEntries.getString("count"));
111   }
112 
113   /***
114    * Expected response for app data in json with non-existant key:
115    * TODO: Double check this output with the spec
116    *
117    * {
118    *  "entry" : {
119    *    "john.doe" : {},
120    *  }
121    * }
122    *
123    * @throws Exception if test encounters an error
124    */
125   @Test
126   public void testGetSelfAppDataJsonWithInvalidKeys() throws Exception {
127     // app id is mocked out
128     Map<String, String> extraParams = Maps.newHashMap();
129     extraParams.put("fields", "peabody");
130     String resp = getResponse("/appdata/john.doe/@self/app", "GET", extraParams, null, "application/json");
131 
132     JSONObject data = getJson(resp).getJSONObject("entry");
133     assertEquals(1, data.length());
134 
135     JSONObject johnsEntries = data.getJSONObject("john.doe");
136     assertEquals(0, johnsEntries.length());
137   }
138 
139   @Test
140   public void testDeleteAppData() throws Exception {
141     assertCount("0");
142 
143     // With the wrong field
144     Map<String, String> extraParams = Maps.newHashMap();
145     extraParams.put("fields", "peabody");
146     getResponse("/appdata/john.doe/@self/app", "DELETE", extraParams, null, "application/json");
147 
148     assertCount("0");
149 
150     extraParams.put("fields", "count");
151     getResponse("/appdata/john.doe/@self/app", "DELETE", extraParams, null, "application/json");
152 
153     assertCount(null);
154   }
155 
156   @Test
157   public void testUpdateAppData() throws Exception {
158     assertCount("0");
159 
160     Map<String, String> extraParams = Maps.newHashMap();
161     extraParams.put("fields", "count");
162     String postData = "{count : 5}";
163     getResponse("/appdata/john.doe/@self/app", "POST", extraParams, postData, null, "application/json");
164 
165     assertCount("5");
166   }
167 
168   private void assertCount(String expectedCount) throws Exception {
169     String resp = getResponse("/appdata/john.doe/@self/app", "GET", null, "application/json");
170     JSONObject data = getJson(resp).getJSONObject("entry");
171     assertEquals(1, data.length());
172 
173     JSONObject johnsEntries = data.getJSONObject("john.doe");
174 
175     if (expectedCount != null) {
176       assertEquals(1, johnsEntries.length());
177       assertEquals(expectedCount, johnsEntries.getString("count"));
178     } else {
179       assertEquals(0, johnsEntries.length());
180     }
181   }
182 
183   // TODO: support for indexBy??
184 
185 }