View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.mobile.tests;
18  
19  import java.io.UnsupportedEncodingException;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.http.ProtocolVersion;
24  import org.apache.http.entity.StringEntity;
25  import org.apache.http.message.BasicHttpResponse;
26  import org.exoplatform.model.ExoAccount;
27  import org.exoplatform.singleton.AccountSetting;
28  import org.exoplatform.singleton.ServerSettingHelper;
29  import org.exoplatform.utils.ServerConfigurationUtils;
30  import org.junit.After;
31  import org.junit.Before;
32  import org.robolectric.annotation.Config;
33  import org.robolectric.shadows.ShadowLog;
34  import org.robolectric.shadows.httpclient.FakeHttpLayer.RequestMatcherBuilder;
35  import org.robolectric.shadows.httpclient.RequestMatcher;
36  import org.robolectric.util.ActivityController;
37  
38  import android.app.Activity;
39  import android.content.Context;
40  import android.content.Intent;
41  import android.content.SharedPreferences;
42  import android.os.Build;
43  import android.os.Bundle;
44  import android.util.Log;
45  
46  /**
47   * Created by The eXo Platform SAS Author : Philippe Aristote
48   * paristote@exoplatform.com Apr 15, 2014
49   */
50  @Config(sdk=Build.VERSION_CODES.LOLLIPOP) // API LEVEL 21
51  public abstract class ExoActivityTestUtils<A extends Activity> {
52  
53    final String          TAG_TEST                   = "eXo____Test____";
54  
55    final String          TEST_SERVER_NAME           = "testserver";
56  
57    final String          TEST_SERVER_URL            = "http://www.test.com";
58  
59    final String          TEST_HTTPS_SERVER_URL      = "https://www.test.com";
60  
61    final String          TEST_WRONG_SERVER_URL      = "test&%$'{}";
62  
63    final String          TEST_USER_NAME             = "testuser";
64  
65    final String          TEST_USER_PWD              = "testpwd";
66  
67    final String          TEST_USER_IDENTITY         = "testidentityid";
68  
69    final int             REQ_SOCIAL_VERSION_LATEST  = 0;
70  
71    final String          RESP_SOCIAL_VERSION_LATEST = "{\"version\":\"v1-alpha3\"}";
72  
73    final int             REQ_SOCIAL_IDENTITY        = 1;
74  
75    final int             REQ_SOCIAL_IDENTITY_2      = 2;
76  
77    final String          RESP_SOCIAL_IDENTITY       = "{" + "\"id\":\"" + TEST_USER_IDENTITY + "\","
78        + "\"providerId\":\"organization\"," + "\"remoteId\":\"" + TEST_USER_NAME + "\"," + "\"profile\":{\"avatarUrl\":\"\","
79        + "\"fullName\":\"" + TEST_USER_NAME + "\"}" + "}";
80  
81    final int             REQ_SOCIAL_NEWS            = 3;
82  
83    final String          RESP_SOCIAL_NEWS           =                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         // 1
84                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               // activity
85                                                       "{\"activities\":[{\"body\":\"\", \"appId\":\"\", \"identityId\":\"7ba7ff1a0a2106c63a820dfdda487a4a\", \"totalNumberOfComments\":1, \"templateParams\":{\"author\":\"fdrouet\", \"contenLink\":\"repository/collaboration/sites/intranet/web contents/Contributions/jmxtrans-addon\", \"contentName\":\"jmxtrans-addon\", \"dateCreated\":\"2014-04-17T11:57:18\", \"docSummary\":\"\", \"docTitle\":\"JMXTrans addon\", \"docTypeLabel\":\"Add-on\", \"docVersion\":\"0\", \"id\":\"6f1e77f8c06313bc4af5b62c1775b63d\", \"imagePath\":\"\", \"isSystemComment\":\"true\", \"lastModified\":\"\", \"message\":\"SocialIntegration.messages.emptyContent\", \"mimeType\":\"\", \"repository\":\"repository\", \"state\":\"draft\", \"systemComment\":\"\", \"workspace\":\"collaboration\"}, \"liked\":false, \"lastUpdated\":1397728639490, \"postedTime\":1397728639167, \"type\":\"contents:spaces\", \"posterIdentity\":{\"id\":\"7ba7ff1a0a2106c63a820dfdda487a4a\", \"providerId\":\"organization\", \"remoteId\":\"fdrouet\", \"profile\":{\"avatarUrl\":\"http://community.exoplatform.com:80/rest/jcr/repository/social/production/soc%3Aproviders/soc%3Aorganization/soc%3Afdrouet/soc%3Aprofile/soc%3Aavatar/?upd=1372169292623\", \"fullName\":\"Frederic DROUET\"} }, \"activityStream\":{\"title\":\"\", \"permaLink\":\"http://community.exoplatform.com:80/portal/intranet/activities/fdrouet\", \"prettyId\":\"fdrouet\", \"faviconUrl\":\"\", \"fullName\":\"Frederic DROUET\", \"type\":\"organization\"}, \"id\":\"6f1e78dac06313bc6eb98680ce104064\", \"title\":\"JMXTrans addon\", \"priority\":0.0, \"createdAt\":\"Thu Apr 17 11:57:19 +0200 2014\", \"likedByIdentities\":[], \"totalNumberOfLikes\":0, \"titleId\":\"\", \"comments\":[] } ] }";
86  
87    final int             REQ_SOCIAL_MY_CONNECTIONS  = 4;
88  
89    final String          RESP_SOCIAL_MY_CONNECTIONS =                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         // 1
90                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               // activity
91                                                       "{\"activities\":[{\"body\":\"\",\"appId\":\"\",\"identityId\":\"51998d9b0a2106c60330eb14726dc376\",\"totalNumberOfComments\":1,\"templateParams\":{},\"liked\":false,\"lastUpdated\":1396514460377,\"postedTime\":1396514164727,\"type\":\"DEFAULT_ACTIVITY\",\"posterIdentity\":{\"id\":\"51998d9b0a2106c60330eb14726dc376\",\"providerId\":\"organization\",\"remoteId\":\"patrice_lamarque\",\"profile\":{\"avatarUrl\":\"http://community.exoplatform.com:80/rest/jcr/repository/social/production/soc%3Aproviders/soc%3Aorganization/soc%3Apatrice_lamarque/soc%3Aprofile/soc%3Aavatar/?upd=1371464305999\",\"fullName\":\"Patrice Lamarque\"}},\"activityStream\":{\"title\":\"\",\"permaLink\":\"http://community.exoplatform.com:80/portal/intranet/activities/patrice_lamarque\",\"prettyId\":\"patrice_lamarque\",\"faviconUrl\":\"\",\"fullName\":\"Patrice Lamarque\",\"type\":\"organization\"},\"id\":\"26bb0ff9c06313bc307d3e126720561e\",\"title\":\"2 very cool add-ons have been added to the add-ons center today : <ul><li><a href=\"http://community.exoplatform.com/portal/intranet/addon-detail?content-id=/repository/collaboration/sites/intranet/web%20contents/Contributions/exo-atemis-extension\">Atemis</a></li><li><a href=\"http://community.exoplatform.com/portal/intranet/addon-detail?content-id=/repository/collaboration/sites/intranet/web%20contents/Contributions/video-wiki-macro\">Video Wiki Macro</a></li></ul>Try them out!\",\"priority\":0.0,\"createdAt\":\"Thu Apr 3 10:36:04 +0200 2014\",\"likedByIdentities\":[],\"totalNumberOfLikes\":8,\"titleId\":\"\",\"comments\":[]}]}";
92  
93    final int             REQ_SOCIAL_ALL_UPDATES     = 5;
94  
95    final String          RESP_SOCIAL_ALL_UPDATES    =                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         // 2
96                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               // activities
97                                                       "{\"activities\":[{\"body\":\"\",\"appId\":\"\",\"identityId\":\"f07184510a2106c6201ac1956140d95f\",\"totalNumberOfComments\":2,\"templateParams\":{},\"liked\":false,\"lastUpdated\":1398099158541,\"postedTime\":1398013281970,\"type\":\"DEFAULT_ACTIVITY\",\"posterIdentity\":{\"id\":\"7fe04a04c06313bc6121d1235b281f23\",\"providerId\":\"organization\",\"remoteId\":\"ilkay_aydemir10\",\"profile\":{\"avatarUrl\":\"http://community.exoplatform.com:80/rest/jcr/repository/social/production/soc%3Aproviders/soc%3Aorganization/soc%3Ailkay_aydemir10/soc%3Aprofile/soc%3Aavatar/?upd=1398009779213\",\"fullName\":\"ilkay Aydemir\"}},\"activityStream\":{\"title\":\"\",\"permaLink\":\"http://community.exoplatform.com:80/portal/intranet/activities/jmazziotta\",\"prettyId\":\"jmazziotta\",\"faviconUrl\":\"\",\"fullName\":\"Julie Mazziotta\",\"type\":\"organization\"},\"id\":\"8015c6b9c06313bc7bddbab957acc8b0\",\"title\":\"Are you the admin site?\",\"priority\":0.0,\"createdAt\":\"Sun Apr 20 19:01:21 +0200 2014\",\"likedByIdentities\":[],\"totalNumberOfLikes\":0,\"titleId\":\"\",\"comments\":[]},{\"body\":\"\",\"appId\":\"\",\"identityId\":\"d3c28a300a2106c658573c3c030bf9da\",\"totalNumberOfComments\":2,\"templateParams\":{},\"liked\":false,\"lastUpdated\":1398080445273,\"postedTime\":1397828057340,\"type\":\"DEFAULT_ACTIVITY\",\"posterIdentity\":{\"id\":\"51998d9b0a2106c60330eb14726dc376\",\"providerId\":\"organization\",\"remoteId\":\"patrice_lamarque\",\"profile\":{\"avatarUrl\":\"http://community.exoplatform.com:80/rest/jcr/repository/social/production/soc%3Aproviders/soc%3Aorganization/soc%3Apatrice_lamarque/soc%3Aprofile/soc%3Aavatar/?upd=1371464305999\",\"fullName\":\"Patrice Lamarque\"}},\"activityStream\":{\"title\":\"\",\"permaLink\":\"http://community.exoplatform.com:80/portal/g/:spaces:translations/translations\",\"prettyId\":\"translations\",\"faviconUrl\":\"\",\"fullName\":\"Translations\",\"type\":\"space\"},\"id\":\"750b7901c06313bc6477591b2d591ed4\",\"title\":\"<a href='/portal/intranet/profile/nguyenbaoan'>An Bao Nguyen</a> <a href='/portal/intranet/profile/tglenat'>Tristan Glenat</a> Shouldn't we move eXo Mobile Translations to <a href='http://translate.exoplatform.org/project/exo-platform/' target='_blank'>http://translate.exoplatform.org/project/exo-platform/</a> now ? It will make them easier to find for potential contributors.\",\"priority\":0.0,\"createdAt\":\"Fri Apr 18 15:34:17 +0200 2014\",\"likedByIdentities\":[],\"totalNumberOfLikes\":3,\"titleId\":\"\",\"comments\":[]}]}";
98  
99    final int             REQ_PLATFORM_INFO          = 6;
100 
101   final String          RESP_PLATFORM_INFO         = "{\"duration\":\"UNLIMITED\",\"platformEdition\":\"ENTERPRISE\",\"buildNumber\":null,\"productCode\":\"CWI-team-09LC0xLDA2L\",\"unlockKey\":\"aaabbbccc\",\"nbUsers\":null,\"dateOfKeyGeneration\":null,\"platformVersion\":\"4.0.4\",\"isMobileCompliant\":\"true\",\"platformBuildNumber\":\"20131225\",\"platformRevision\":\"aaabbbccc\",\"userHomeNodePath\":\"/Users/t___/te___/tes___/testuser\",\"runningProfile\":\"all\",\"currentRepoName\":\"repository\",\"defaultWorkSpaceName\":\"collaboration\"}";
102 
103   final int             REQ_JCR_USER               = 7;
104 
105   final int             REQ_JCR_USER_2             = 8;
106 
107   final String          RESP_JCR_USER              = "{}";
108 
109   final int             REQ_PLATFORM_INFO_USER_2   = 9;
110 
111   final String          RESP_PLATFORM_INFO_USER_2  = "{\"duration\":\"UNLIMITED\",\"platformEdition\":\"ENTERPRISE\",\"buildNumber\":null,\"productCode\":\"CWI-team-09LC0xLDA2L\",\"unlockKey\":\"aaabbbccc\",\"nbUsers\":null,\"dateOfKeyGeneration\":null,\"platformVersion\":\"4.0.4\",\"isMobileCompliant\":\"true\",\"platformBuildNumber\":\"20131225\",\"platformRevision\":\"aaabbbccc\",\"userHomeNodePath\":\"/Users/t___/te___/tes___/testuser_2\",\"runningProfile\":\"all\",\"currentRepoName\":\"repository\",\"defaultWorkSpaceName\":\"collaboration\"}";
112 
113   ActivityController<A> controller;
114 
115   A                     activity;
116 
117   @Before
118   public abstract void setup();
119 
120   @After
121   public void teardown() {
122     controller.destroy();
123   }
124 
125   public void create() {
126     activity = controller.create().start().resume().visible().get();
127   }
128 
129   public void createWithBundle(Bundle b) {
130     // passing the bundle here, no need to call
131     // controller.restoreInstanceState(b)
132     activity = controller.create(b).start().resume().visible().get();
133   }
134 
135   public void createWithIntent(Intent i) {
136     activity = controller.withIntent(i) // passing the intent here
137                          .create()
138                          .start()
139                          .resume()
140                          .visible()
141                          .get();
142   }
143 
144   public void createAndAttach() {
145     activity = controller.attach().create().start().resume().visible().get();
146   }
147 
148   public void createWithContext(Context ctx) {
149     activity = controller.withBaseContext(ctx).create().start().resume().visible().get();
150   }
151 
152   // UTILS
153 
154   public void enableLog() {
155     ShadowLog.stream = System.out;
156   }
157 
158   public void disableLog() {
159     ShadowLog.stream = null;
160   }
161 
162   /**
163    * Creates a Server Object with the default name, URL, username and password
164    * 
165    * @return a ServerObjInfo object
166    */
167   public ExoAccount getServerWithDefaultValues() {
168     ExoAccount srv = new ExoAccount();
169     srv.accountName = TEST_SERVER_NAME;
170     srv.serverUrl = TEST_SERVER_URL;
171     srv.username = TEST_USER_NAME;
172     srv.password = TEST_USER_PWD;
173     return srv;
174   }
175 
176   /**
177    * Sets the default language in the app's preferences
178    * 
179    * @param c the app's Context
180    * @param lang the language
181    */
182   public void setLanguageInPreferences(Context c, String lang) {
183     SharedPreferences.Editor prefs = c.getSharedPreferences("exo_preference", 0).edit();
184     prefs.putString("exo_prf_localize", lang);
185     prefs.commit();
186   }
187 
188   /**
189    * Sets the default server/account in the app's preferences
190    * 
191    * @param c the app's Context
192    * @param server the default server
193    */
194   public void setDefaultServerInPreferences(Context c, ExoAccount server) {
195     ArrayList<ExoAccount> serversList = new ArrayList<ExoAccount>(1);
196     serversList.add(server);
197 
198     ServerConfigurationUtils.generateXmlFileWithServerList(c, serversList, "ServerList.xml", "");
199     ServerSettingHelper.getInstance().setServerInfoList(serversList);
200 
201     SharedPreferences.Editor prefs = c.getSharedPreferences("exo_preference", 0).edit();
202     prefs.putString("exo_prf_domain_index", "0");
203     prefs.commit();
204   }
205 
206   public void selectAccountAtIndex(Context c, int idx) {
207     SharedPreferences.Editor prefs = c.getSharedPreferences("exo_preference", 0).edit();
208     prefs.putString("exo_prf_domain_index", String.valueOf(idx));
209     prefs.commit();
210   }
211 
212   /**
213    * Deletes all accounts in the app's preferences
214    * 
215    * @param c the app's context
216    */
217   public void deleteAllAccounts(Context c) {
218     ServerConfigurationUtils.generateXmlFileWithServerList(c, new ArrayList<ExoAccount>(), "ServerList.xml", "");
219     ServerSettingHelper.getInstance().setServerInfoList(new ArrayList<ExoAccount>());
220 
221     SharedPreferences.Editor prefs = c.getSharedPreferences("exo_preference", 0).edit();
222     prefs.remove("exo_prf_domain_index");
223     prefs.commit();
224   }
225 
226   /**
227    * Adds the servers in the app's preferences and selects the first one
228    * 
229    * @param c
230    * @param servers the list of ExoAccount objects to add
231    */
232   public void addServersInPreferences(Context c, ArrayList<ExoAccount> servers) {
233     addServersInPreferences(c, servers, 0);
234   }
235 
236   /**
237    * Adds the servers in the app's preferences and selects the one at the given
238    * position
239    * 
240    * @param c
241    * @param servers the list of ExoAccount objects to add
242    * @param posDefault the position of the server to set as selected
243    */
244   public void addServersInPreferences(Context c, ArrayList<ExoAccount> servers, int posDefault) {
245     if (servers != null && servers.size() > 0) {
246       Log.i(TAG_TEST, "Saving " + servers.size() + " accounts.");
247       ServerConfigurationUtils.generateXmlFileWithServerList(c, servers, "ServerList.xml", "");
248       ServerSettingHelper.getInstance().setServerInfoList(servers);
249       if (posDefault >= 0 && posDefault < servers.size()) {
250         SharedPreferences.Editor prefs = c.getSharedPreferences("exo_preference", 0).edit();
251         prefs.putString("exo_prf_domain_index", String.valueOf(posDefault));
252         prefs.commit();
253         Log.i(TAG_TEST, "Account selected: " + posDefault);
254         AccountSetting.getInstance().setDomainIndex(String.valueOf(posDefault));
255       }
256     }
257   }
258 
259   public List<ExoAccount> getAccounts(Context c) {
260     return ServerConfigurationUtils.getServerListFromFile(c, "ServerList.xml");
261   }
262 
263   public ExoAccount getCurrentAccount(Context c) {
264     List<ExoAccount> list = getAccounts(c);
265     SharedPreferences pref = c.getSharedPreferences("exo_preference", 0);
266     int current = Integer.parseInt(pref.getString("exo_prf_domain_index", "-1"));
267     return list.get(current);
268   }
269 
270   /**
271    * Creates x accounts and sets remember me and auto login options
272    * 
273    * @param x the number of accounts to create
274    * @param rm the Remember Me option
275    * @param al the Auto Login option
276    * @return a List of accounts
277    */
278   public ArrayList<ExoAccount> createXAccountsWithRMAndAL(int x, boolean rm, boolean al) {
279     ArrayList<ExoAccount> accounts = new ArrayList<ExoAccount>(x);
280 
281     for (int i = 1; i <= x; i++) {
282       ExoAccount acc = getServerWithDefaultValues();
283       acc.accountName = TEST_SERVER_NAME + " " + i;
284       acc.username = TEST_USER_NAME + "_" + i;
285       acc.isRememberEnabled = rm;
286       acc.isAutoLoginEnabled = al;
287       accounts.add(acc);
288     }
289 
290     return accounts;
291   }
292 
293   /**
294    * Creates x accounts with remember me and auto login activated
295    * 
296    * @param x the number of accounts to create
297    * @return a List of accounts
298    */
299   public ArrayList<ExoAccount> createXAccounts(int x) {
300     return createXAccountsWithRMAndAL(x, true, true);
301   }
302 
303   /**
304    * Creates a RequestMatcher to use in Robolectric.addHttpResponseRule() for
305    * the specified request
306    * 
307    * @param req the id of the request, e.g REQ_SOCIAL_IDENTITY
308    * @return a RequestMatcher object that matches the request id
309    */
310   public RequestMatcher getMatcherForRequest(int req) {
311     RequestMatcherBuilder m = new RequestMatcherBuilder();
312     switch (req) {
313     case REQ_SOCIAL_VERSION_LATEST:
314       m.path("rest/api/social/version/latest.json");
315       break;
316 
317     case REQ_SOCIAL_IDENTITY:
318       m.path("rest/private/api/social/v1-alpha3/portal/identity/organization/" + TEST_USER_NAME + ".json");
319       break;
320 
321     case REQ_SOCIAL_IDENTITY_2:
322       m.path("rest/private/api/social/v1-alpha3/portal/identity/" + TEST_USER_IDENTITY + ".json");
323       break;
324 
325     case REQ_SOCIAL_NEWS:
326       m.path("rest/private/api/social/v1-alpha3/portal/activity_stream/feed.json");
327       m.param("limit", "10");
328       break;
329 
330     case REQ_SOCIAL_ALL_UPDATES:
331       m.path("rest/private/api/social/v1-alpha3/portal/activity_stream/feed.json");
332       m.param("limit", "50");
333       break;
334 
335     case REQ_SOCIAL_MY_CONNECTIONS:
336       m.path("rest/private/api/social/v1-alpha3/portal/activity_stream/connections.json");
337       break;
338 
339     case REQ_PLATFORM_INFO:
340       m.path("rest/private/platform/info");
341       break;
342 
343     case REQ_JCR_USER:
344       m.path("rest/private/jcr/repository/collaboration/Users/t___/te___/tes___/testuser");
345       break;
346 
347     case REQ_JCR_USER_2:
348       m.path("rest/private/jcr/repository/collaboration/Users/t___/te___/tes___/testuser_2");
349       break;
350 
351     case REQ_PLATFORM_INFO_USER_2:
352       m.path("rest/private/platform/info");
353       break;
354     }
355     return m;
356   }
357 
358   /**
359    * Creates a BasicHttpResponse to use in Robolectric.addHttpResponseRule() for
360    * the specified request.<br/>
361    * Response details:<br/>
362    * - HTTP 1.1 <br/>
363    * - 200 OK <br/>
364    * - a content (entity) corresponding to the specified request, e.g.
365    * REQ_SOCIAL_VERSION_LATEST -> {"version":"v1-alpha3"}
366    * 
367    * @param req the id of the request, e.g REQ_SOCIAL_VERSION_LATEST
368    * @return the response that matches the request id
369    */
370   public BasicHttpResponse getResponseOKForRequest(int req) {
371     BasicHttpResponse resp = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK");
372     try {
373 
374       switch (req) {
375       case REQ_SOCIAL_VERSION_LATEST:
376         resp.setEntity(new StringEntity(RESP_SOCIAL_VERSION_LATEST));
377         break;
378 
379       case REQ_SOCIAL_IDENTITY:
380       case REQ_SOCIAL_IDENTITY_2:
381         resp.setEntity(new StringEntity(RESP_SOCIAL_IDENTITY));
382         break;
383 
384       case REQ_SOCIAL_NEWS:
385         resp.setEntity(new StringEntity(RESP_SOCIAL_NEWS));
386         break;
387 
388       case REQ_SOCIAL_ALL_UPDATES:
389         resp.setEntity(new StringEntity(RESP_SOCIAL_ALL_UPDATES));
390         break;
391 
392       case REQ_SOCIAL_MY_CONNECTIONS:
393         resp.setEntity(new StringEntity(RESP_SOCIAL_MY_CONNECTIONS));
394         break;
395 
396       case REQ_PLATFORM_INFO:
397         resp.setEntity(new StringEntity(RESP_PLATFORM_INFO));
398         break;
399       case REQ_PLATFORM_INFO_USER_2:
400         resp.setEntity(new StringEntity(RESP_PLATFORM_INFO_USER_2));
401         break;
402 
403       case REQ_JCR_USER:
404       case REQ_JCR_USER_2:
405         resp.setEntity(new StringEntity(RESP_JCR_USER));
406         break;
407       }
408 
409     } catch (UnsupportedEncodingException e) {
410       e.printStackTrace();
411     }
412 
413     return resp;
414   }
415 
416   /**
417    * Creates a BasicHttpResponse to use in Robolectric.addHttpResponseRule().
418    * <br/>
419    * Response details:
420    * <ul>
421    * <li>HTTP 1.1</li>
422    * <li>ErrorCode ERROR</li>
423    * </ul>
424    * 
425    * @param wantedStatus The wanted ErrorCode wanted in the response. It must be
426    *          between 400 and 600, otherwise the default value 404 is used.
427    * @return An error response
428    */
429   public BasicHttpResponse getResponseFailedWithStatus(int wantedStatus) {
430     int statusCode = 404;
431     if (wantedStatus >= 400 && wantedStatus < 600)
432       statusCode = wantedStatus;
433     return new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), statusCode, "ERROR");
434   }
435 
436 }