// 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 HttpPostClient {
//   public PostResponseInfo issuePostRequest(URL url,
//       Map<String, String> connectionProperties, String requestBody)
//       throws IOException;
// }
// 
// Path: src/com/google/enterprise/adaptor/sharepoint/SamlAuthenticationHandler.java
// @VisibleForTesting
// public static class PostResponseInfo {
//   /** Non-null contents. */
//   private final String contents;
//   /** Non-null headers. */
//   private final Map<String, List<String>> headers;
// 
//   PostResponseInfo(
//       String contents, Map<String, List<String>> headers) {
//     this.contents = contents;
//     this.headers  = (headers == null)
//         ? new HashMap<String, List<String>>() 
//         : new HashMap<String, List<String>>(headers);
//   }
// 
//   public String getPostContents() {
//     return contents;
//   }
// 
//   public Map<String, List<String>> getPostResponseHeaders() {
//     return Collections.unmodifiableMap(headers);
//   }
// 
//   public String getPostResponseHeaderField(String header) {
//     if (headers == null || !headers.containsKey(header)) {
//       return null;
//     }
//     if (headers.get(header) == null || headers.get(header).isEmpty()) {
//       return null;
//     }
//     StringBuilder sbValues = new StringBuilder();
//     for(String value : headers.get(header)) {
//       if ("".equals(value)) {
//         continue;
//       }
//       sbValues.append(value);
//       sbValues.append(";");
//     }
//     return sbValues.toString();
//   }
// }


import static org.junit.Assert.assertEquals;
import com.google.enterprise.adaptor.sharepoint.FormsAuthenticationHandlerTest.MockScheduledExecutor;
import com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.HttpPostClient;
import com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.PostResponseInfo;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
  }
  
  @Test
  public void testNullSharePointUrl() {
    thrown.expect(NullPointerException.class);
    new LiveAuthenticationHandshakeManager.Builder(
        null, "username", "password").build();
  }
  
  @Test
  public void testExtractToken() throws IOException {
    LiveAuthenticationHandshakeManager manager 
        = new LiveAuthenticationHandshakeManager.Builder(
            "https://sharepoint.example.com", "username", "password").build();    
    
    assertEquals("t=This is live authentication token to extract",
        manager.extractToken(LIVE_AUTHENTICATION_RESPONSE));
  }  
  
  @Test
  public void testExtractTokenWithInvalidInput() throws IOException {
    LiveAuthenticationHandshakeManager manager 
        = new LiveAuthenticationHandshakeManager.Builder(
            "https://sharepoint.example.com", "username", "password").build();
    
    String tokenResponse = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
        + "<S:Envelope xmlns:S=\"http://www.w3.org/2003/05/soap-envelope\">"       
        + "<data>Something went wrong this is invalid</data>"
        + "</S:Envelope>";
    
// 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 HttpPostClient {
//   public PostResponseInfo issuePostRequest(URL url,
//       Map<String, String> connectionProperties, String requestBody)
//       throws IOException;
// }
// 
// Path: src/com/google/enterprise/adaptor/sharepoint/SamlAuthenticationHandler.java
// @VisibleForTesting
// public static class PostResponseInfo {
//   /** Non-null contents. */
//   private final String contents;
//   /** Non-null headers. */
//   private final Map<String, List<String>> headers;
// 
//   PostResponseInfo(
//       String contents, Map<String, List<String>> headers) {
//     this.contents = contents;
//     this.headers  = (headers == null)
//         ? new HashMap<String, List<String>>() 
//         : new HashMap<String, List<String>>(headers);
//   }
// 
//   public String getPostContents() {
//     return contents;
//   }
// 
//   public Map<String, List<String>> getPostResponseHeaders() {
//     return Collections.unmodifiableMap(headers);
//   }
// 
//   public String getPostResponseHeaderField(String header) {
//     if (headers == null || !headers.containsKey(header)) {
//       return null;
//     }
//     if (headers.get(header) == null || headers.get(header).isEmpty()) {
//       return null;
//     }
//     StringBuilder sbValues = new StringBuilder();
//     for(String value : headers.get(header)) {
//       if ("".equals(value)) {
//         continue;
//       }
//       sbValues.append(value);
//       sbValues.append(";");
//     }
//     return sbValues.toString();
//   }
// }


// Path: test/com/google/enterprise/adaptor/sharepoint/LiveAuthenticationHandshakeManagerTest.java
import static org.junit.Assert.assertEquals;
import com.google.enterprise.adaptor.sharepoint.FormsAuthenticationHandlerTest.MockScheduledExecutor;
import com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.HttpPostClient;
import com.google.enterprise.adaptor.sharepoint.SamlAuthenticationHandler.PostResponseInfo;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

  }
  
  @Test
  public void testNullSharePointUrl() {
    thrown.expect(NullPointerException.class);
    new LiveAuthenticationHandshakeManager.Builder(
        null, "username", "password").build();
  }
  
  @Test
  public void testExtractToken() throws IOException {
    LiveAuthenticationHandshakeManager manager 
        = new LiveAuthenticationHandshakeManager.Builder(
            "https://sharepoint.example.com", "username", "password").build();    
    
    assertEquals("t=This is live authentication token to extract",
        manager.extractToken(LIVE_AUTHENTICATION_RESPONSE));
  }  
  
  @Test
  public void testExtractTokenWithInvalidInput() throws IOException {
    LiveAuthenticationHandshakeManager manager 
        = new LiveAuthenticationHandshakeManager.Builder(
            "https://sharepoint.example.com", "username", "password").build();
    
    String tokenResponse = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
        + "<S:Envelope xmlns:S=\"http://www.w3.org/2003/05/soap-envelope\">"       
        + "<data>Something went wrong this is invalid</data>"
        + "</S:Envelope>";
    
    thrown.expect(IOException.class);