package eu.osci.messagetypes;

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

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.junit.Test;

import com.sun.xml.ws.api.security.CallbackHandlerFeature;
import com.sun.xml.wss.XWSSecurityException;
import com.sun.xml.wss.impl.misc.DefaultCallbackHandler;

import eu.osci.helper.EndpointLocator;


/**
 * Tests the configuration with an own CallbackHandler.<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 TestDynamicConfig extends OSCIRequest
{

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

  /**
   * Sends an OSCI message with an implementation of a CallbackHandler as configuration object.<br>
   * Expected behaviour: Successful sending of the OSCI message without any exceptions.
   */
  @Test
  public void testSimpleSend() throws Exception
  {
    try
    {
      EndpointLocator endpointLocator = new EndpointLocator(new URL(MessageParent.URL_WSDL_USER));
      LOG.fine("Service: " + endpointLocator.getServiceList().length);
      endpointLocator.useTypeOfBusinessScenario(endpointLocator.getTypeOfBusinessScenarioList()[0]);
      CallbackHandlerFeature callbackFeature = new CallbackHandlerFeature(new MyCallbackHandler("osci", null));
      OSCIRequest req = new OSCIRequest(endpointLocator, callbackFeature);

      // OSCIResponse rsp = req.onlySendMessage(new StreamSource(
      // new ByteArrayInputStream(
      // MessageParent.XML_ENC_DATA.getBytes())));

      OSCIResponse rsp = req.sendMessage(new ByteArrayInputStream(MessageParent.XML_ENC_DATA.getBytes()));
      assertNull("Delivery receipt is not null", rsp.getDeliveryReceipt());
      assertNotNull("RelatesTo is null", rsp.getRelatesTo());
      assertEquals("Not the correct RelatesTo in response", req.getMessageId().getValue(), rsp.getRelatesTo()
                                                                                              .getValue());
    }
    catch (Exception ex)
    {
      LOG.log(Level.SEVERE, "Error", ex);
      fail("Sending simple OSCI request failed. Error: " + ex.getMessage());
    }
  }

  static class MyCallbackHandler extends DefaultCallbackHandler
  {

    public MyCallbackHandler(String clientOrServer, Properties assertions) throws XWSSecurityException
    {
      super(clientOrServer, assertions);
      // TODO Auto-generated constructor stub
    }

    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
    {
      for ( Callback tmpCallback : callbacks )
      {
        LOG.fine("################################################ \n handle() callback=" + tmpCallback);
      }
      super.handle(callbacks);
    }
  }
}
