package test.abnahme.A;

import org.w3c.dom.Element;
import test.abnahme.AbnahmeTestCase;

import at.gv.egovernment.moa.id.AuthenticationException;
import at.gv.egovernment.moa.id.auth.AuthenticationServer;
import at.gv.egovernment.moa.id.auth.builder.VerifyXMLSignatureRequestBuilder;
import at.gv.egovernment.moa.id.auth.data.AuthenticationSession;
import at.gv.egovernment.moa.id.auth.data.IdentityLink;
import at.gv.egovernment.moa.id.auth.invoke.SignatureVerificationInvoker;
import at.gv.egovernment.moa.id.auth.parser.CreateXMLSignatureResponseParser;
import at.gv.egovernment.moa.id.auth.parser.InfoboxReadResponseParser;
import at.gv.egovernment.moa.id.auth.parser.VerifyXMLSignatureResponseParser;
import at.gv.egovernment.moa.id.auth.validator.CreateXMLSignatureResponseValidator;
import at.gv.egovernment.moa.id.config.auth.AuthConfigurationProvider;
import at.gv.egovernment.moa.id.data.AuthenticationData;

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

public class Test400GetAuthenticationData extends AbnahmeTestCase {

  private String samlArtifact;

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

  protected void setUp() throws Exception {
    super.setUp();
    String sessionID = startAuthentication();
    AuthenticationSession session = AuthenticationServer.getSession(sessionID);
    String infoboxReadResponse = readXmldata("InfoBoxReadResponse.xml");
    server.verifyIdentityLink(sessionID, infoboxReadResponse);
    InfoboxReadResponseParser irrp = new InfoboxReadResponseParser(infoboxReadResponse);
    IdentityLink idl = irrp.parseIdentityLink();
    Element domVerifyXMLSignatureRequest = new VerifyXMLSignatureRequestBuilder().build(idl, AuthConfigurationProvider.getInstance().getMoaSpAuthBlockTrustProfileID());
    Element domVerifyXMLSignatureResponse = new SignatureVerificationInvoker().verifyXMLSignature(domVerifyXMLSignatureRequest);
    new VerifyXMLSignatureResponseParser(domVerifyXMLSignatureResponse);
    //VerifyXMLSignatureResponseValidator.getInstance().validate(respParser.parseData(), AuthConfigurationProvider.getInstance().getIdentityLinkX509SubjectNames());
    //    System.out.println(createXMLSignatureRequest);
    String createXMLSignatureResponse = readXmldata("CreateXMLSignatureResponse.xml");
    CreateXMLSignatureResponseValidator.getInstance().validate(new CreateXMLSignatureResponseParser(createXMLSignatureResponse).parseResponse(), session);
    samlArtifact = server.verifyAuthenticationBlock(sessionID, createXMLSignatureResponse);
  }

  public void testA401() throws Exception {
    try {

      AuthenticationData authData = server.getAuthenticationData(samlArtifact);
//      authDataWriter(authData,"NEWA401");
      assertXmlEquals(clearSamlAssertion(authData.getSamlAssertion()), readXmldata("AuthenticationData.xml"));
      System.out.println("-----------------------\nTestfall " + this.getName() + " erfolgreich abgearbeitet! \n-----------------------");
    }
    catch (Exception e) {
      System.err.println("------ FEHLER IN " + this.getName() + ":" + e.getLocalizedMessage());
      throw e;
    }
  }

  public void testA451() throws Exception {
    try {
      try {
        AuthenticationData authData = server.getAuthenticationData("AAGu1JFbyGKqJ+3NAonwMu5bNyUc7kooeMK6bxeXBbnK6NL0DfuVJsGi");
        authDataWriter(authData, "A45");
        if (authData != null)
          fail();
      }
      catch (AuthenticationException e) {
        System.out.println("-----------------------\nFehler in " + this.getName() + " erfolgreich abgefangen: " + e.getLocalizedMessage() + "\n-----------------------");
      }
    }
    catch (Exception e) {
      System.err.println("------ FEHLER IN " + this.getName() + ":" + e.getLocalizedMessage());
      throw e;
    }
  }

  public void testA452() throws Exception {
    try {
      server.getAuthenticationData(samlArtifact);
      try {
        server.getAuthenticationData(samlArtifact);
        fail();
      }
      catch (AuthenticationException e) {
        System.out.println("-----------------------\nFehler in " + this.getName() + " erfolgreich abgefangen: " + e.getLocalizedMessage() + "\n-----------------------");
      }
    }
    catch (Exception e) {
      System.err.println("------ FEHLER IN " + this.getName() + ":" + e.getLocalizedMessage());
      throw e;
    }
  }
  public void testA453() throws Exception {
    try {
      server.setSecondsAuthDataTimeOut(-1000);
      server.cleanup();
      try {
        server.getAuthenticationData(samlArtifact);
        fail();
      }
      catch (AuthenticationException e) {
        System.out.println("-----------------------\nFehler in " + this.getName() + " erfolgreich abgefangen: " + e.getLocalizedMessage() + "\n-----------------------");
      }
    }
    catch (Exception e) {
      System.err.println("------ FEHLER IN " + this.getName() + ":" + e.getLocalizedMessage());
      throw e;
    }
  }

  public void testA454() throws Exception {
    try {
      try {
        server.getAuthenticationData("blabla123");
        fail();
      }
      catch (AuthenticationException e) {
        System.out.println("-----------------------\nFehler in " + this.getName() + " erfolgreich abgefangen: " + e.getLocalizedMessage() + "\n-----------------------");
      }
    }
    catch (Exception e) {
      System.err.println("------ FEHLER IN " + this.getName() + ":" + e.getLocalizedMessage());
      throw e;
    }
  }

}