Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getTestUsers implementation to manage paging #76

Merged
merged 6 commits into from
Jan 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions facebook4j-core/src/main/java/facebook4j/FacebookImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,13 @@ public List<TestUser> getTestUsers(String appId) throws FacebookException {
}
}


public ResponseList<TestUser> getTestUsers(String appId, Integer limit) throws FacebookException {
ensureAuthorizationEnabled();
HttpResponse res = get(conf.getRestBaseURL() + appId + "/accounts/test-users"+(limit != null? "?limit=" + limit : ""));
return factory.createTestUserList(res);
}

public boolean deleteTestUser(String testUserId) throws FacebookException {
ensureAuthorizationEnabled();
HttpResponse res = delete(conf.getRestBaseURL() + testUserId);
Expand Down
13 changes: 13 additions & 0 deletions facebook4j-core/src/main/java/facebook4j/api/TestUserMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;

import facebook4j.FacebookException;
import facebook4j.ResponseList;
import facebook4j.TestUser;

/**
Expand Down Expand Up @@ -49,6 +50,7 @@ public interface TestUserMethods {

/**
* Returns test users associated with a particular app.
* This method can't manage paging
* @param appId the ID of the app
* @return Test Users
* @throws FacebookException when Facebook service or network is unavailable
Expand All @@ -57,6 +59,17 @@ public interface TestUserMethods {
List<TestUser> getTestUsers(String appId) throws FacebookException;


/**
* Returns test users associated with a particular app.
* This method manage paging, call facebook.fetchNext to get next page
* @param appId the ID of the app
* @param limit number of elements per page (can be null to use default page size)
* @return Test Users first page
* @throws FacebookException when Facebook service or network is unavailable
* @see <a href="https://developers.facebook.com/docs/test_users/">Test Users - Facebook Developers</a>
*/
ResponseList<TestUser> getTestUsers(String appId, Integer limit) throws FacebookException;

/**
* Deletes the test user.
* @param testUserId the ID of the test user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@

import static facebook4j.internal.util.z_F4JInternalParseUtil.getRawString;
import facebook4j.FacebookException;
import facebook4j.Interest;
import facebook4j.ResponseList;
import facebook4j.TestUser;
import facebook4j.conf.Configuration;
import facebook4j.internal.http.HttpResponse;
import facebook4j.internal.org.json.JSONArray;
import facebook4j.internal.org.json.JSONException;
import facebook4j.internal.org.json.JSONObject;

/**
Expand All @@ -45,6 +50,35 @@
init(json);
}

/*package*/
static ResponseList<TestUser> createTestUserList(HttpResponse res, Configuration conf) throws FacebookException {
try {
if (conf.isJSONStoreEnabled()) {
DataObjectFactoryUtil.clearThreadLocalMap();
}
JSONObject json = res.asJSONObject();
JSONArray list = json.getJSONArray("data");
final int size = list.length();
ResponseList<TestUser> testUsers = new ResponseListImpl<TestUser>(size, json);
for (int i = 0; i < size; i++) {
JSONObject testUserJSONObject = list.getJSONObject(i);
TestUser testUser = new TestUserJSONImpl(testUserJSONObject);
if (conf.isJSONStoreEnabled()) {
DataObjectFactoryUtil.registerJSONObject(testUser, testUserJSONObject);
}
testUsers.add(testUser);
}
if (conf.isJSONStoreEnabled()) {
DataObjectFactoryUtil.registerJSONObject(testUsers, list);
}
return testUsers;
} catch (JSONException jsone) {
throw new FacebookException(jsone);
}
}



private void init(JSONObject json) throws FacebookException {
id = getRawString("id", json);
accessToken = getRawString("access_token", json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ public interface z_F4JInternalFactory extends java.io.Serializable {

TestUser createTestUser(JSONObject json) throws FacebookException;

ResponseList<TestUser> createTestUserList(HttpResponse res) throws FacebookException;

ResponseList<JSONObject> createJSONObjectList(HttpResponse res) throws FacebookException;
ResponseList<JSONObject> createJSONObjectList(JSONObject json) throws FacebookException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ public TestUser createTestUser(JSONObject json) throws FacebookException {
return new TestUserJSONImpl(json);
}

public ResponseList<TestUser> createTestUserList(HttpResponse res) throws FacebookException {
return TestUserJSONImpl.createTestUserList(res, conf);
}

public ResponseList<JSONObject> createJSONObjectList(HttpResponse res) throws FacebookException {
return ResponseListImpl.createJSONObjectList(res, conf);
}
Expand Down Expand Up @@ -565,6 +569,9 @@ public <T> ResponseList<T> createResponseList(HttpResponse res, Class<T> jsonObj
if (jsonObjectType == Offer.class) {
return (ResponseList<T>) createOfferList(res);
}
if (jsonObjectType == TestUser.class) {
return (ResponseList<T>) createTestUserList(res);
}
if (jsonObjectType == JSONObject.class) {
return (ResponseList<T>) createJSONObjectList(res);
}
Expand Down
65 changes: 65 additions & 0 deletions facebook4j-core/src/test/java/facebook4j/TestUserMethodsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2012 Ryuji Yamashita
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package facebook4j;

import facebook4j.internal.http.RequestMethod;

import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;

import java.net.URL;
import java.util.List;
import java.util.Locale;

import static facebook4j.junit.ISO8601DateMatchers.*;
import static facebook4j.junit.URLMatchers.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

@RunWith(Enclosed.class)
public class TestUserMethodsTest extends MockFacebookTestBase {

public static class getTestUsers extends MockFacebookTestBase {
@Test
public void me() throws Exception {
int page = 1;
facebook.setMockJSON("mock_json/testUser/page" + page + ".json");

ResponseList<TestUser> testUsersResponseList = facebook.getTestUsers("appId_XXXXXXXXXXXX", 10);
while (testUsersResponseList != null && testUsersResponseList.size() > 0) {
for (TestUser u : testUsersResponseList) {
assertNotNull(u.getId());
assertNotNull(u.getAccessToken());
}
if (testUsersResponseList != null) {
Paging<TestUser> paging = testUsersResponseList.getPaging();
if (paging != null && paging.getNext() != null) {
facebook.setMockJSON("mock_json/testUser/page" + ++page + ".json");
testUsersResponseList = facebook.fetchNext(paging);
} else {
testUsersResponseList = null;
}
}

}

}

}

}
36 changes: 36 additions & 0 deletions facebook4j-core/src/test/resources/mock_json/testUser/page1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"data": [
{
"id": "1379885932316530",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/1379885932316530/",
"access_token": "CAAUs5XD3IK4BAPJnYxLqqKtpZBnNaqNGr4PfcCWuowZC1PWZAmCHsAW4OFhdwnz1B3ZARpCNR4ShuFoGUwZAZCAMquGfRWzvYCzpy0Q3Q6tikMpDhZCoyI3QUfuZCEdSEi8bbK9mFheRgAaWs0H4K0v1YZAKEg2j73QIeFWB3Ea7xRXuWJQUeEHvpSdRWBIAXXZAcZD"
},
{
"id": "1377417215897043",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/1377417215897043/",
"access_token": "CAAUs5XD3IK4BAM7v3DsWbR00aCDcYxs6r67RoZByb6K1ooSouvcSP2jrTjmf6i28ZA1ZAqr4h2l3f6fFIzD0NvTMIpckvMJCHZAsnT16V079V2cPNPV8E3uRFjZCsae4k4PQyhCgWs3ieE9euCuHagC7puWTk8Q8ZBtuEETKA5anpNdgVbP2hRAgrEGZAwFRTQZD"
},
{
"id": "1380020645636256",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/1380020645636256/",
"access_token": "CAAUs5XD3IK4BAElROpszysmhBYyqjLnUQGzkHaEdwcdb6wxAVmU2Ep83gCFNg1xjb0ZCFqhtJUZBa2oxc7IJZBw7YzdwHxDorDlqvGUenBNHC3nowwoI2dEH2ih3ZATKLAAS2O6c8a7mqGqZCrSjcR4cfqIWJd997EAbfpXZAPWY5MpXZBelfy1iQQJeZBFCVQQZD"
},
{
"id": "1380064655632073",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/1380064655632073/",
"access_token": "CAAUs5XD3IK4BADcbXTFjihDSY5w4pjJjwkhQnqB9hnMy8M2ElR2kmM5ZCFCq7wZBa4lVOd40gRrXeC1SKxlRW9yUibbOR1eGeMNlbXmHFhHgP0tw1azlEXdBKGryJXZAWJiLBfIhfmVchZA1I0LaEhGInnkAWGvP982PaWbI7U86v1DYQWOeZCCb2SJuQLB0ZD"
},
{
"id": "1377730512532504",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/1377730512532504/",
"access_token": "CAAUs5XD3IK4BABZCZBgNxZAMW90NEYz4cYtkgDCYoJL6rA80LfkLJnl8hFvY7LXQkASJWfTOk9IVkU9R7MWgumkgzCspfoMYaViqrR5VsJ7BWmg6265ANwSelTZAP3EdAc6w12H2ccvSEkkYroQEh9qENhCNy1dkTeJ7GDMLAKN4n2Qp4GyWsZAaZA7t7gCf0ZD"
}
],
"paging": {
"cursors": {
"before": "MTM3OTg4NTkzMjMxNjUzMA==",
"after": "MTM3NzczMDUxMjUzMjUwNA=="
},
"next": "https://graph.facebook.com/v2.2/1456738837930158/accounts?pretty=0&type=test-users&limit=5&after=MTM3NzczMDUxMjUzMjUwNA=="
}
}
37 changes: 37 additions & 0 deletions facebook4j-core/src/test/resources/mock_json/testUser/page2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"data": [
{
"id": "1379557692348933",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/1379557692348933/",
"access_token": "CAAUs5XD3IK4BAKvIx4xLQgryk2cEr0fanjm6Xec7I0hsmMA8knkt5oHxky3kDddZBmiwEEcTZCLjclcKgd3TnKhffRs63KMGAhHlsxNIAJ7Msls57SIoovqAZC2RgZClSGgjTSKTwurF9G4xhy9ys7QPZA5sDpjjIVZCcCtG36Pe0WYUFmmH5RrSk5mRDLHZAQcTkqZBRPtXAwZDZD"
},
{
"id": "1376859759286323",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/1376859759286323/",
"access_token": "CAAUs5XD3IK4BAPu6ZAffMsitpYFgD0hCQ1Sj5pgTTdleQCI3IhZBp4dlmPBWWFEhPWjgSpc6QKUyOVXLlhGbjzaoL0nbZAb3ucl74CPu4CMrjhj0tD48N0tOtfdrjxHm27bLoPw6nsYO8sZC2zNz4VjS9XlzDKY1aP9SNFOH1B945iwLTGzsBwC8UWne2sVorbZBwKI4hCgZDZD"
},
{
"id": "1377686682536648",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/1377686682536648/",
"access_token": "CAAUs5XD3IK4BAORsfolBBmZChJBljLrFIrAEP2du1C58lqWiSbzNyCGt5h79Au00O2idR0hqWGsMA4pqkIHyCTWhZALTMbGGybXEpb6jxFa8zMi6ihTqABScBJ13PefpaOviKZAjSVd7svft06oOGHtYzZCeiB31gOtRM5AaLwKatohzZCIZB4l04TbqOsY6UmphuD6ZAF0RQZDZD"
},
{
"id": "1376700812635927",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/1376700812635927/",
"access_token": "CAAUs5XD3IK4BAAqlWwqUKr7NQZAUPZBsV4aTToNQBvcFlMsLWFpDrv3cxO122baKnkL9xjZCdtNiUiUGZAfikS2x5kbGVzSQo1S0ySeDKlonVx1E4JCpnqsIrUNKioBxY6JKTZCIPZCqmL5IZBTXZA2fYVX0c66lzNkntlCP8X73Mt2qyZBZCZBGC91TDF0JZBEpm6iu73ggaPRbDwZDZD"
},
{
"id": "1377571719214873",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/1377571719214873/",
"access_token": "CAAUs5XD3IK4BAIgnVkU4xsDtXZARP208BKIgp2Xo7Lw93nCCZC5J4cX4BZBqhMGA2lieJzgvymVc4D5Rvaq5hpC1SQtlmIKvbnTZC1ygBLzZB9aCYU7lZAWBNjtHoGUgvgRZAsjS0avsCdQB0Nk8kHvNeZB5pL6Pti4l1fIhQfhOC4EJXvXfYYqRvamVvWpZBiFF9pgsm51Xn0QZDZD"
}
],
"paging": {
"cursors": {
"before": "MTM3OTU1NzY5MjM0ODkzMw==",
"after": "MTM3NzU3MTcxOTIxNDg3Mw=="
},
"next": "https://graph.facebook.com/v2.2/1456738837930158/accounts?pretty=0&type=test-users&limit=5&after=MTM3NzU3MTcxOTIxNDg3Mw==",
"previous": "https://graph.facebook.com/v2.2/1456738837930158/accounts?pretty=0&type=test-users&limit=5&before=MTM3OTU1NzY5MjM0ODkzMw=="
}
}
26 changes: 26 additions & 0 deletions facebook4j-core/src/test/resources/mock_json/testUser/page3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"data": [
{
"id": "1388984414734356",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/1388984414734356/",
"access_token": "CAAUs5XD3IK4BAEahfBK29wEdVLtaknVikDLlJP7pfbF6vydac2L1xakqQQZAZB6tLqwYrWpugcTVMkN3Dh6kJaWGW37nCs4mtRrnHb9RjTsyWVdTL7S815InIbexSRduPUNl6uynrSZBBIb0zhISXTxC2d6GHBEYajZCHUfzOKtyOF4DjGRnKpqFdapbFJsB36VpfhqH0QZDZD"
},
{
"id": "100008692577545",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/100008692577545/",
"access_token": "CAAUs5XD3IK4BAMPfbmEIH4xYur34vNDw0i4g5EDAbqmGQJquJjNA1zlneh3WW13u6X3q3Jq9hrrgpcB7scYHLwdjhjiVys8436hy9GmrOHnu5Wd6ZAU6diGyHSyvze9sYpkqBlmGRC78hirNwl2ZCITb2ksiJpwpOphZA4ZBEEYBZBGGVMUoZBU19GIgqt10vHGgTI58mc0wZDZD"
},
{
"id": "1379332385701086",
"login_url": "https://developers.facebook.com/checkpoint/test-user-login/1379332385701086/",
"access_token": "CAAUs5XD3IK4BAGujfCzcLYYGr067rFhpW9iDIsOVAJJlmM3Om43aThZAbuVvzNajO3RRstx18kHQZB0CU8OQN36zfbeL7nYHRALrCLXEnN1vq9OwV3XJUMJ7CBVdbiVxmHdHO3t7AB1St8rZCSmXQPe9xVZBsKhxNngwUdvpxZB8gHKzosNAj7upiWesp2hpKwP2qy2r5LAZDZD"
}
],
"paging": {
"cursors": {
"before": "MTM3NzU3MzY4MjU0NDk3NQ==",
"after": "MTM3OTMzMjM4NTcwMTA4Ng=="
},
"previous": "https://graph.facebook.com/v2.2/1456738837930158/accounts?pretty=0&type=test-users&limit=50&before=MTM3NzU3MzY4MjU0NDk3NQ=="
}
}