package eu.osci.messageparts;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.logging.Logger;

import org.junit.Test;

import com.sun.xml.ws.api.message.HeaderList;

import eu.osci.OSCIHeaderTag;
import eu.osci.helper.CryptoTools;
import eu.osci.ws._2008._05.transport.MsgTimeStampsType;
import eu.osci.ws._2008._05.transport.X509TokenContainerType;


/**
 * Tests the {@link OSCIFragmentParser} class.<br>
 * Copyright © 2010 Freie Hansestadt Bremen. <br>
 * Created by bremen online services Entwicklungs- und Betriebsgesellschaft mbH & Co. KG. <br>
 * This library (Starter Kit) can be used by anybody according to the Bremer Lizenz für freie
 * Softwarebibliotheken. The license terms can be viewed at www.osci.de or can be ordered at the
 * OSCI-Leitstelle (Die Senatorin für Finanzen, Freie Hansestadt Bremen, Postfach 10 15 40, 28015 Bremen).
 * 
 * @author R. Lindemann
 */
public class TestOSCIFragmentParser
{

  private static Logger LOG = Logger.getLogger(TestOSCIFragmentParser.class.getName());

  /**
   * Tests the creation of the MsgTimeStamps object.<br>
   * Expected behaviour: A well created MsgTimeStamps object.
   */
  @Test
  public void testMsgTimeStamps() throws Exception
  {
    MsgTimeStampsType msgTimeStamps = OSCIFragmentCreator.createOSCIMsgTimeStamps(2);
    HeaderList headerList = new HeaderList();
    headerList.add(OSCIFragmentCreator.createOSCIHeader(OSCIHeaderTag.MSGTIMESTAMPS, msgTimeStamps));
    // OSCIFragmentParser.getAddressingAction(headerList);
    MsgTimeStampsType parsed = (MsgTimeStampsType)OSCIFragmentParser.createOSCIHeader(headerList,
                                                                                      OSCIHeaderTag.MSGTIMESTAMPS);
    assertNotNull("Parsed object MsgTimeStampsType is null: ", parsed);
    assertEquals("Wrong time in TimeStamp",
                 msgTimeStamps.getObsoleteAfter().getDay(),
                 parsed.getObsoleteAfter().getDay());
    // assertEquals("Wrong time in TimeStamp", msgTimeStamps.getObsoleteAfter(), parsed.getObsoleteAfter());
    LOG.fine("Done");
  }

  /**
   * Creates a X509TokenContainer.
   */
  @Test
  public void testCreateOSCITokenContainer() throws Exception
  {
    ArrayList<X509Certificate> certList = new ArrayList<X509Certificate>();
    X509Certificate cert = CryptoTools.loadCertificate(getClass().getResourceAsStream("/META-INF/test_alice_signature.cer"));
    certList.add(cert);
    X509TokenContainerType container = OSCIFragmentCreator.createOSCITokenContainer(certList);
    assertEquals("Wrong count of certificates: ", 1, container.getX509TokenInfo().size());
    assertNotNull("Token container is empty: ", container.getX509TokenInfo().get(0).getX509Data());
  }

  /**
   *<br>
   * Expected behaviour:
   */
  @Test
  public void testAddX509CertificateToTokenContainer() throws Exception
  {
    ArrayList<X509Certificate> certList = new ArrayList<X509Certificate>();
    X509Certificate cert = CryptoTools.loadCertificate(getClass().getResourceAsStream("/META-INF/test_alice_signature.cer"));
    certList.add(cert);
    X509TokenContainerType container = OSCIFragmentCreator.createOSCITokenContainer(certList);
    X509Certificate cert2 = CryptoTools.loadCertificate(getClass().getResourceAsStream("/META-INF/test_bob_signature.cer"));
    OSCIFragmentCreator.addX509CertificateToTokenContainer(container, cert2);
    assertEquals("Wrong count of certificates: ", 2, container.getX509TokenInfo().size());
    assertNotNull("First Token container is empty: ", container.getX509TokenInfo().get(0).getX509Data());
    assertNotNull("Second Token container is empty: ", container.getX509TokenInfo().get(1).getX509Data());
  }

}
