package com.here.account.util;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.here.account.util.RefreshableResponseProvider.ExpiringResponse;
import com.here.account.util.RefreshableResponseProvider.ResponseRefresher;

public class RefreshableResponseProviderTest {

    RefreshableResponseProvider<MyExpiringResponse> refreshableResponseProvider;
    
    public class MyExpiringResponse implements ExpiringResponse {
        
        private long startTimeMillis;

        public MyExpiringResponse() {
            this.startTimeMillis = System.currentTimeMillis();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Long getExpiresIn() {
            // 10 minutes
            return 10*60L;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Long getStartTimeMilliseconds() {
            return startTimeMillis;
        }
        
    }
     Long refreshIntervalMillis;
     MyExpiringResponse initialToken;
     ResponseRefresher<MyExpiringResponse> refreshTokenFunction;
    
    @Before
    public void setUp() {
        refreshIntervalMillis = null;
        initialToken = new MyExpiringResponse();
        refreshTokenFunction = new ResponseRefresher<MyExpiringResponse>() {

            @Override
            public MyExpiringResponse refresh(MyExpiringResponse previous) {
                return new MyExpiringResponse();
            }
            
        };
        this.refreshableResponseProvider = new RefreshableResponseProvider<MyExpiringResponse>(
         refreshIntervalMillis,
         initialToken,
         refreshTokenFunction);
    }
    
    @After
    public void tearDown() {
        this.refreshableResponseProvider.shutdown();
    }
    
    @Test
    public void test_shutdown_multiple() {
        for (int i = 0; i < 3; i++) {
            tearDown();
        }
    }
    
}

@@ -25,16 +25,60 @@
 .util;%0A%0A
+import static org.junit.Assert.assertTrue;%0A%0A
 import o
@@ -1837,16 +1837,2154 @@
   @Test%0A
+    public void test_refreshIntervalMillis() %7B%0A        refreshIntervalMillis = 100L;%0A        initialToken = new MyExpiringResponse();%0A        refreshTokenFunction = new ResponseRefresher%3CMyExpiringResponse%3E() %7B%0A%0A            @Override%0A            public MyExpiringResponse refresh(MyExpiringResponse previous) %7B%0A                return new MyExpiringResponse();%0A            %7D%0A            %0A        %7D;%0A        this.refreshableResponseProvider = new RefreshableResponseProvider%3CMyExpiringResponse%3E(%0A         refreshIntervalMillis,%0A         initialToken,%0A         refreshTokenFunction);%0A%0A        for (int i = 0; i %3C 10; i++) %7B%0A            long actualNextRefreshInterval = refreshableResponseProvider.nextRefreshInterval();%0A            assertTrue(%22actualNextRefreshInterval %22+actualNextRefreshInterval%0A                    +%22 didn't match refreshIntervalMillis %22+refreshIntervalMillis,%0A                    refreshIntervalMillis == actualNextRefreshInterval);%0A        %7D%0A    %7D%0A    %0A    @Test%0A    public void test_refreshToken_fails() throws InterruptedException %7B%0A        refreshIntervalMillis = 100L;%0A        initialToken = new MyExpiringResponse();%0A        refreshTokenFunction = new ResponseRefresher%3CMyExpiringResponse%3E() %7B%0A%0A            @Override%0A            public MyExpiringResponse refresh(MyExpiringResponse previous) %7B%0A                throw new RuntimeException(%22simulate unable to refresh%22);%0A            %7D%0A            %0A        %7D;%0A        this.refreshableResponseProvider = new RefreshableResponseProvider%3CMyExpiringResponse%3E(%0A         refreshIntervalMillis,%0A         initialToken,%0A         refreshTokenFunction);%0A%0A        %0A        for (int i = 0; i %3C 10; i++) %7B%0A            Thread.sleep(100L);%0A            MyExpiringResponse response = refreshableResponseProvider.getUnexpiredResponse();%0A            assertTrue(%22response was null%22, null != response);%0A            long expiresIn = response.getExpiresIn();%0A            long expectedExpiresIn = 600L;%0A            assertTrue(%22expected expires in %22+expectedExpiresIn+%22 != actual expiresIn %22+expiresIn, %0A                    expectedExpiresIn == expiresIn);%0A        %7D%0A    %7D%0A%0A    %0A    %0A    @Test%0A
     publ

add unit test coverage for Java HttpProvider implementation.