// Path: test/com/google/enterprise/adaptor/sharepoint/FormsAuthenticationHandlerTest.java
// static class UnsupportedScheduledExecutor extends CallerRunsExecutor 
//     implements ScheduledExecutorService {
// 
//   public ScheduledFuture<?> schedule(Runnable command, long delay,
//       TimeUnit unit) {
//     throw new UnsupportedOperationException();
//   }
// 
//   public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay,
//       TimeUnit unit) {
//     throw new UnsupportedOperationException();
//   }
// 
//   public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
//       long initialDelay, long period, TimeUnit unit) {
//     throw new UnsupportedOperationException();
//   }
// 
//   public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
//       long initialDelay, long delay, TimeUnit unit) {
//     throw new UnsupportedOperationException(); 
//   }
//     
// }
// 
// Path: test/com/google/enterprise/adaptor/sharepoint/FormsAuthenticationHandlerTest.java
// static class MockScheduledExecutor 
//     extends UnsupportedScheduledExecutor {
//   long executionDelay;
//   TimeUnit executionTimeUnit;   
//     
//   @Override
//   public ScheduledFuture<?> schedule(Runnable command, long delay,
//       TimeUnit unit) {
//     executionDelay = delay;
//     executionTimeUnit = unit;
//     return null;
//   }
// }
// 
// Path: src/com/google/enterprise/adaptor/sharepoint/SamlAuthenticationHandler.java
// @VisibleForTesting
// public interface SamlHandshakeManager {
//   public String requestToken() throws IOException;
//   public String getAuthenticationCookie(String token) throws IOException;
// }


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import com.google.enterprise.adaptor.sharepoint.FormsAuthenticationHandlerTest.UnsupportedScheduledExecutor;
import com.google.enterprise.adaptor.sharepoint.FormsAuthenticationHandlerTest.MockScheduledExecutor;
import com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.SamlHandshakeManager;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
  public void testNullToken() throws IOException {
    SamlAuthenticationHandler handler = new SamlAuthenticationHandler.Builder(
        "username", "password", new UnsupportedScheduledExecutor(),
        new MockSamlHandshakeManager(null, null) {
          @Override public String getAuthenticationCookie(String token) {
            throw new UnsupportedOperationException();
          }      
        }).build();
    
    assertTrue(handler.isFormsAuthentication());
    thrown.expect(IOException.class);
    AuthenticationResult result = handler.authenticate();    
  }
  
  @Test
  public void testSAMLAutentication() throws IOException {
    SamlAuthenticationHandler handler 
        = new SamlAuthenticationHandler.Builder("username", "password", 
            new MockScheduledExecutor(),
            new MockSamlHandshakeManager("token", "AuthenticationCookie"))
            .build();    
    assertTrue(handler.isFormsAuthentication());
    AuthenticationResult result = handler.authenticate();
    assertNotNull(result);
    assertEquals("AuthenticationCookie", result.getCookie());
    assertEquals(600, result.getCookieTimeOut());
    assertEquals("NO_ERROR", result.getErrorCode());    
  }
  
  private static class UnsupportedSamlHandshakeManager 
// Path: test/com/google/enterprise/adaptor/sharepoint/FormsAuthenticationHandlerTest.java
// static class UnsupportedScheduledExecutor extends CallerRunsExecutor 
//     implements ScheduledExecutorService {
// 
//   public ScheduledFuture<?> schedule(Runnable command, long delay,
//       TimeUnit unit) {
//     throw new UnsupportedOperationException();
//   }
// 
//   public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay,
//       TimeUnit unit) {
//     throw new UnsupportedOperationException();
//   }
// 
//   public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
//       long initialDelay, long period, TimeUnit unit) {
//     throw new UnsupportedOperationException();
//   }
// 
//   public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
//       long initialDelay, long delay, TimeUnit unit) {
//     throw new UnsupportedOperationException(); 
//   }
//     
// }
// 
// Path: test/com/google/enterprise/adaptor/sharepoint/FormsAuthenticationHandlerTest.java
// static class MockScheduledExecutor 
//     extends UnsupportedScheduledExecutor {
//   long executionDelay;
//   TimeUnit executionTimeUnit;   
//     
//   @Override
//   public ScheduledFuture<?> schedule(Runnable command, long delay,
//       TimeUnit unit) {
//     executionDelay = delay;
//     executionTimeUnit = unit;
//     return null;
//   }
// }
// 
// Path: src/com/google/enterprise/adaptor/sharepoint/SamlAuthenticationHandler.java
// @VisibleForTesting
// public interface SamlHandshakeManager {
//   public String requestToken() throws IOException;
//   public String getAuthenticationCookie(String token) throws IOException;
// }


// Path: test/com/google/enterprise/adaptor/sharepoint/SamlAuthenticationHandlerTest.java
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import com.google.enterprise.adaptor.sharepoint.FormsAuthenticationHandlerTest.UnsupportedScheduledExecutor;
import com.google.enterprise.adaptor.sharepoint.FormsAuthenticationHandlerTest.MockScheduledExecutor;
import com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.SamlHandshakeManager;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

  public void testNullToken() throws IOException {
    SamlAuthenticationHandler handler = new SamlAuthenticationHandler.Builder(
        "username", "password", new UnsupportedScheduledExecutor(),
        new MockSamlHandshakeManager(null, null) {
          @Override public String getAuthenticationCookie(String token) {
            throw new UnsupportedOperationException();
          }      
        }).build();
    
    assertTrue(handler.isFormsAuthentication());
    thrown.expect(IOException.class);
    AuthenticationResult result = handler.authenticate();    
  }
  
  @Test
  public void testSAMLAutentication() throws IOException {
    SamlAuthenticationHandler handler 
        = new SamlAuthenticationHandler.Builder("username", "password", 
            new MockScheduledExecutor(),
            new MockSamlHandshakeManager("token", "AuthenticationCookie"))
            .build();    
    assertTrue(handler.isFormsAuthentication());
    AuthenticationResult result = handler.authenticate();
    assertNotNull(result);
    assertEquals("AuthenticationCookie", result.getCookie());
    assertEquals(600, result.getCookieTimeOut());
    assertEquals("NO_ERROR", result.getErrorCode());    
  }
  
  private static class UnsupportedSamlHandshakeManager 
      implements SamlHandshakeManager {