/*
 * 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.auth;

import facebook4j.conf.Configuration;
import facebook4j.conf.ConfigurationBuilder;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

@RunWith(Enclosed.class)
public class OAuthAuthorizationTest {

    public static class AppSecretProof {
        @Test
        public void defaultCacheSize() throws Exception {
            Configuration conf = new ConfigurationBuilder()
                    .setAppSecretProofEnabled(true)
                    .setOAuthAppSecret("1234567890123456")
                    .build();
            OAuthAuthorization auth = new OAuthAuthorization(conf);
            auth.setOAuthAccessToken(new AccessToken("access_token1"));
            String proof1 = auth.generateAppSecretProof();
            auth.setOAuthAccessToken(new AccessToken("access_token2"));
            String proof2 = auth.generateAppSecretProof();
            auth.setOAuthAccessToken(new AccessToken("access_token3"));
            auth.generateAppSecretProof();
            auth.setOAuthAccessToken(new AccessToken("access_token4"));
            auth.generateAppSecretProof();
            auth.setOAuthAccessToken(new AccessToken("access_token5"));
            auth.generateAppSecretProof();
            auth.setOAuthAccessToken(new AccessToken("access_token6"));
            auth.generateAppSecretProof();
            auth.setOAuthAccessToken(new AccessToken("access_token7"));
            auth.generateAppSecretProof();
            auth.setOAuthAccessToken(new AccessToken("access_token8"));
            auth.generateAppSecretProof();
            auth.setOAuthAccessToken(new AccessToken("access_token9"));
            auth.generateAppSecretProof();
            auth.setOAuthAccessToken(new AccessToken("access_token10"));
            auth.generateAppSecretProof();

            auth.setOAuthAppId("", "change appSecret value for proof compute result");
            auth.setOAuthAccessToken(new AccessToken("access_token1"));
            String cachedProof1 = auth.generateAppSecretProof();
            assertThat(proof1, is(cachedProof1));   // cache hit

            auth.setOAuthAccessToken(new AccessToken("access_token11"));
            auth.generateAppSecretProof();

            auth.setOAuthAccessToken(new AccessToken("access_token2"));
            String reComputedProof2 = auth.generateAppSecretProof();
            assertThat(reComputedProof2, is(not(proof2)));   // re-computed
        }

        @Test
        public void customCacheSize() throws Exception {
            Configuration conf = new ConfigurationBuilder()
                    .setAppSecretProofEnabled(true)
                    .setOAuthAppSecret("1234567890123456")
                    .setAppSecretProofCacheSize(3)
                    .build();
            OAuthAuthorization auth = new OAuthAuthorization(conf);
            auth.setOAuthAccessToken(new AccessToken("access_token1"));
            String proof1 = auth.generateAppSecretProof();
            auth.setOAuthAccessToken(new AccessToken("access_token2"));
            String proof2 = auth.generateAppSecretProof();
            auth.setOAuthAccessToken(new AccessToken("access_token3"));
            auth.generateAppSecretProof();

            auth.setOAuthAppId("", "change appSecret value for proof compute result");
            auth.setOAuthAccessToken(new AccessToken("access_token1"));
            String cachedProof1 = auth.generateAppSecretProof();
            assertThat(proof1, is(cachedProof1));   // cache hit

            auth.setOAuthAccessToken(new AccessToken("access_token4"));
            auth.generateAppSecretProof();

            auth.setOAuthAccessToken(new AccessToken("access_token2"));
            String reComputedProof2 = auth.generateAppSecretProof();
            assertThat(reComputedProof2, is(not(proof2)));   // re-computed
        }
    }
}

@@ -642,318 +642,1202 @@
 k4j.
-conf.Configuration;%0Aimport facebook4j.conf.ConfigurationBuilder;%0Aimport org.junit.Test;%0Aimport org.junit.experimental.runners.Enclosed;%0Aimport org.junit.runner.RunWith;%0A%0Aimport static org.hamcrest.CoreMatchers.*;%0Aimport static org.junit.Assert.*;%0A%0A@RunWith(Enclosed.class)%0Apublic class OAuthAuthorizationTest %7B
+Facebook;%0Aimport facebook4j.FacebookFactory;%0Aimport facebook4j.FacebookTestBase;%0Aimport facebook4j.conf.Configuration;%0Aimport facebook4j.conf.ConfigurationBuilder;%0Aimport facebook4j.junit.category.RealAPITests;%0Aimport org.junit.Test;%0Aimport org.junit.experimental.categories.Category;%0Aimport org.junit.experimental.runners.Enclosed;%0Aimport org.junit.runner.RunWith;%0A%0Aimport static org.hamcrest.CoreMatchers.*;%0Aimport static org.junit.Assert.*;%0A%0A@RunWith(Enclosed.class)%0Apublic class OAuthAuthorizationTest %7B%0A%0A    public static class TokenExpirationExtention extends FacebookTestBase %7B%0A        @Test%0A        @Category(RealAPITests.class)%0A        public void shortToLong() throws Exception %7B%0A            Facebook facebook = FacebookFactory.getSingleton();%0A            String oAuthAppId = p.getProperty(%22oauth.appId%22);%0A            String oAuthAppSecret = p.getProperty(%22oauth.appSecret%22);%0A            facebook.setOAuthAppId(oAuthAppId, oAuthAppSecret);%0A%0A            String shortLivedToken = %22your-short-lived-token%22;%0A            AccessToken extendedToken = facebook.extendTokenExpiration(shortLivedToken);%0A            assertThat(extendedToken.getToken(), is(not(shortLivedToken)));%0A        %7D%0A    %7D
 %0A%0A  

Add test for token expiration extention