package test.abnahme.P;
import java.util.Map;

import sun.misc.BASE64Decoder;
import test.abnahme.AbnahmeTestCase;

import at.gv.egovernment.moa.id.config.proxy.OAConfiguration;
import at.gv.egovernment.moa.id.config.proxy.OAProxyParameter;
import at.gv.egovernment.moa.id.config.proxy.ProxyConfigurationProvider;
import at.gv.egovernment.moa.id.data.AuthenticationData;
import at.gv.egovernment.moa.id.proxy.LoginParameterResolver;
import at.gv.egovernment.moa.id.proxy.LoginParameterResolverFactory;
import at.gv.egovernment.moa.util.Base64Utils;

/**
 * @author Stefan Knirsch
 * @version $Id$
 * 
 */

public class Test100LoginParameterResolver extends AbnahmeTestCase {

  private static final String CLIENT_IP_ADDRESS = "56.246.75.11";
  private OAConfiguration oaConf;
  private LoginParameterResolver lpr;

  public Test100LoginParameterResolver(String name) {
    super(name);
  }

  private void setUp(String publicURLPrefix)
    throws Exception {
      
    // get configuration data
    ProxyConfigurationProvider proxyConf = ProxyConfigurationProvider.getInstance();
    OAProxyParameter oaParam = proxyConf.getOnlineApplicationParameter(publicURLPrefix);
    oaConf = oaParam.getOaConfiguration();
    System.out.println("Parameter�bergabe: " + oaConf.getAuthType());

    // get login parameter resolver
    LoginParameterResolverFactory.initialize();
    lpr = LoginParameterResolverFactory.getLoginParameterResolver(publicURLPrefix);
  }
  public void testP101() throws Exception {
    try {
      // read configuration and set up LoginParameterResolver
      setUp("https://testP101:9443/");
      if (! oaConf.getAuthType().equals(OAConfiguration.BASIC_AUTH))
        fail();

      // assemble authentication data
      AuthenticationData authData = new AuthenticationData();
      authData.setFamilyName("Huber");
      authData.setGivenName("Hugo");

      // resolve login headers
      Map loginHeaders = lpr.getAuthenticationHeaders(oaConf, authData, CLIENT_IP_ADDRESS);

      // validate login headers
      assertEquals(1, loginHeaders.keySet().size());
      System.out.println("Header Authorization: " + loginHeaders.get("Authorization"));
      System.out.println("Decoded UserID:Password " + 
        new String(new BASE64Decoder().decodeBuffer(((String)loginHeaders.get("Authorization")).substring(6))));
      String userIDPassword = "Hugo:Huber";
      String credentials = Base64Utils.encode(userIDPassword.getBytes());
      assertEquals("Basic " + credentials, loginHeaders.get("Authorization"));
      System.out.println("-----------------------Testfall " + this.getName() + " erfolgreich abgearbeitet! -----------------------");
    }
    catch (Exception e) {
      System.err.println("------ FEHLER IN " + this.getName() + ":" + e.getLocalizedMessage());
      throw e;
    }

  }
  public void testP102() throws Exception {
    try {
      // read configuration and set up LoginParameterResolver
      setUp("https://testP102:9443/");
      if (! oaConf.getAuthType().equals(OAConfiguration.PARAM_AUTH))
        fail();

      // assemble authentication data
      AuthenticationData authData = new AuthenticationData();
      String DATE_OF_BIRTH = "1963-12-29";
      String VPK = "kp6hOq6LRAkLtrqm6EvDm6bMwJw=";
      authData.setDateOfBirth(DATE_OF_BIRTH);
      authData.setPBK(VPK);

      // resolve login parameters
      Map loginParameters = lpr.getAuthenticationParameters(oaConf, authData, CLIENT_IP_ADDRESS);

      // validate login headers
      assertEquals(2, loginParameters.keySet().size());
      System.out.println("Param1: " + loginParameters.get("Param1"));
      System.out.println("Param2: " + loginParameters.get("Param2"));
      assertEquals(DATE_OF_BIRTH, loginParameters.get("Param1"));
      assertEquals(VPK, loginParameters.get("Param2"));
      System.out.println("-----------------------Testfall " + this.getName() + " erfolgreich abgearbeitet! -----------------------");
    }
    catch (Exception e) {
      System.err.println("------ FEHLER IN " + this.getName() + ":" + e.getLocalizedMessage());
      throw e;
    }
  }

  public void testP103() throws Exception {
    try {
      // read configuration and set up LoginParameterResolver
      setUp("https://localhost:9443/");
      if (! oaConf.getAuthType().equals(OAConfiguration.HEADER_AUTH))
        fail();

      // assemble authentication data
      AuthenticationData authData = new AuthenticationData();
      boolean PUBLIC_AUTH = true;
      String BKZ = "FinanzamtWien23Leitstelle";
      boolean QUAL_CERT = false;
      String STAMMZAHL = "3456789012";
      authData.setPublicAuthority(PUBLIC_AUTH);
      authData.setPublicAuthorityCode(BKZ);
      authData.setQualifiedCertificate(QUAL_CERT);
      authData.setIdentificationValue(STAMMZAHL);

      // resolve login headers
      Map loginHeaders = lpr.getAuthenticationHeaders(oaConf, authData, CLIENT_IP_ADDRESS);

      // validate login headers
      assertEquals(5, loginHeaders.keySet().size());
      System.out.println("Header Param1: " + loginHeaders.get("Param1"));
      System.out.println("Header Param2: " + loginHeaders.get("Param2"));
      System.out.println("Header Param3: " + loginHeaders.get("Param3"));
      System.out.println("Header Param4: " + loginHeaders.get("Param4"));
      System.out.println("Header Param5: " + loginHeaders.get("Param5"));
      assertEquals(String.valueOf(PUBLIC_AUTH), loginHeaders.get("Param1"));
      assertEquals(BKZ, loginHeaders.get("Param2"));
      assertEquals(String.valueOf(QUAL_CERT), loginHeaders.get("Param3"));
      assertEquals(STAMMZAHL, loginHeaders.get("Param4"));
      assertEquals(CLIENT_IP_ADDRESS, loginHeaders.get("Param5"));
      System.out.println("-----------------------Testfall " + this.getName() + " erfolgreich abgearbeitet! -----------------------");
    }
    catch (Exception e) {
      System.err.println("------ FEHLER IN " + this.getName() + ":" + e.getLocalizedMessage());
      throw e;
    }
  }
}