/*
 * 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 static facebook4j.junit.URLMatchers.*;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

@RunWith(Enclosed.class)
public class TestUserMethodsTest {

    public static class createTestUser extends MockFacebookTestBase {
        @Test
        public void appId() throws Exception {
            facebook.setMockJSON("mock_json/testuser/installed.json");
            TestUser testUser = facebook.createTestUser("appId");
            assertThat(facebook.getHttpMethod(), is(RequestMethod.POST));
            assertThat(facebook.getEndpointURL(), is(pathOf("/appId/accounts/test-users")));
            assertThat(facebook.getEndpointURL(), hasParameter("locale", "en_US"));
            assertThat(facebook.getEndpointURL(), hasParameter("installed", "true"));

            assertThat(testUser.getId(), is("100008791899167"));
            assertThat(testUser.getEmail(), is("test_yzrvlnj_test@tfbnw.net"));
            assertThat(testUser.getLoginUrl(), is("https://developers.facebook.com/checkpoint/test-user-login/100008791899167/"));
            assertThat(testUser.getPassword(), is("1073749147"));
            assertThat(testUser.getAccessToken(), is("access_token"));
        }

        @Test
        public void installed_default() throws Exception {
            facebook.setMockJSON("mock_json/testuser/installed.json");
            TestUser testUser = facebook.createTestUser("appId", "test", "en_US", "user_about_me");
            assertThat(facebook.getHttpMethod(), is(RequestMethod.POST));
            assertThat(facebook.getEndpointURL(), is(pathOf("/appId/accounts/test-users")));
            assertThat(facebook.getEndpointURL(), hasParameter("name", "test"));
            assertThat(facebook.getEndpointURL(), hasParameter("locale", "en_US"));
            assertThat(facebook.getEndpointURL(), hasParameter("permissions", "user_about_me"));
            assertThat(facebook.getEndpointURL(), hasParameter("installed", "true"));

            assertThat(testUser.getId(), is("100008791899167"));
            assertThat(testUser.getEmail(), is("test_yzrvlnj_test@tfbnw.net"));
            assertThat(testUser.getLoginUrl(), is("https://developers.facebook.com/checkpoint/test-user-login/100008791899167/"));
            assertThat(testUser.getPassword(), is("1073749147"));
            assertThat(testUser.getAccessToken(), is("access_token"));
        }

        @Test
        public void installed_true() throws Exception {
            facebook.setMockJSON("mock_json/testuser/installed.json");
            TestUser testUser = facebook.createTestUser("appId", "test", "en_US", "user_about_me", true);
            assertThat(facebook.getHttpMethod(), is(RequestMethod.POST));
            assertThat(facebook.getEndpointURL(), is(pathOf("/appId/accounts/test-users")));
            assertThat(facebook.getEndpointURL(), hasParameter("name", "test"));
            assertThat(facebook.getEndpointURL(), hasParameter("locale", "en_US"));
            assertThat(facebook.getEndpointURL(), hasParameter("permissions", "user_about_me"));
            assertThat(facebook.getEndpointURL(), hasParameter("installed", "true"));

            assertThat(testUser.getId(), is("100008791899167"));
            assertThat(testUser.getEmail(), is("test_yzrvlnj_test@tfbnw.net"));
            assertThat(testUser.getLoginUrl(), is("https://developers.facebook.com/checkpoint/test-user-login/100008791899167/"));
            assertThat(testUser.getPassword(), is("1073749147"));
            assertThat(testUser.getAccessToken(), is("access_token"));
        }

        @Test
        public void notInstalled() throws Exception {
            facebook.setMockJSON("mock_json/testuser/not_installed.json");
            TestUser testUser = facebook.createTestUser("appId", "test", "en_US", "user_about_me", false);
            assertThat(facebook.getHttpMethod(), is(RequestMethod.POST));
            assertThat(facebook.getEndpointURL(), is(pathOf("/appId/accounts/test-users")));
            assertThat(facebook.getEndpointURL(), hasParameter("installed", "false"));

            assertThat(testUser.getId(), is("100008791899167"));
            assertThat(testUser.getEmail(), is("test_yzrvlnj_test@tfbnw.net"));
            assertThat(testUser.getLoginUrl(), is("https://developers.facebook.com/checkpoint/test-user-login/100008791899167/"));
            assertThat(testUser.getPassword(), is("1073749147"));
            assertThat(testUser.getAccessToken(), is(nullValue()));
        }

        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;
                        }
                    }

                }
            }
        }

    }

}

@@ -854,25 +854,17 @@
 atchers.
-nullValue
+*
 ;%0Aimport
@@ -5531,33 +5531,33 @@
 (%22mock_json/test
-U
+u
 ser/page%22 + page
@@ -5863,31 +5863,28 @@
       assert
-NotNull
+That
 (u.getId());
@@ -5881,16 +5881,36 @@
 .getId()
+, is(notNullValue())
 );%0A     
@@ -5938,15 +5938,12 @@
 sert
-NotNull
+That
 (u.g
@@ -5957,16 +5957,36 @@
 sToken()
+, is(notNullValue())
 );%0A     
@@ -6006,69 +6006,8 @@
   %7D%0A
-                    if (testUsersResponseList != null) %7B%0A    
     
@@ -6103,20 +6103,16 @@
         
-    
 if (pagi
@@ -6169,36 +6169,32 @@
                 
- 
-   
 facebook.setMock
@@ -6261,36 +6261,32 @@
                 
-    
 testUsersRespons
@@ -6321,20 +6321,16 @@
 aging);%0A
-    
         
@@ -6366,36 +6366,32 @@
                 
-    
 testUsersRespons
@@ -6408,34 +6408,8 @@
 ll;%0A
-                        %7D%0A
     

delete unnecessary if statement