package eu.osci.helper;

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

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Logger;

import javax.xml.namespace.QName;

import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;


/**
 * Tests the function of the {@link EndpointLocator} 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 TestEndpointLocator
{

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

  EndpointLocator endpointLocator;

  EndpointLocator endpointLocator2;

  // EndpointLocator endpointLocatorStream;

  private static final String typeOfFirst = "http://www.osci.eu/2008/common/urn/messageTypes/LetterStyle";

  private static final String typeOfSecond = "http://www.osci.eu/2008/common/urn/messageTypes/LetterStyleNew";

  private static final String typeOfFirst2 = "http://www.osci.eu/2008/common/urn/messageTypes/LetterStyle2";

  private static final String typeOfSecond2 = "http://www.osci.eu/2008/common/urn/messageTypes/LetterStyleNew2";

  /**
   * Creates an instance of the EndpointLocator with the given URL (WSDL) to parse.
   */
  @Before
  public void setup() throws SAXException, IOException, URISyntaxException
  {

    endpointLocator = new EndpointLocator(
                                          new File(
                                                   TestEndpointLocator.class.getResource("/data/TestWSDL.wsdl")
                                                                            .toURI()));
    endpointLocator2 = new EndpointLocator(
                                           new File(
                                                    TestEndpointLocator.class.getResource("/data/TestWSDL2.wsdl")
                                                                             .toURI()));

    // endpointLocatorStream = new EndpointLocator(new URL("http://localhost:8080/OSCI2Endpoint/user/Alice"));
  }

  /**
   * Checks the list of TypeOfBusinessScenario out of the configured WSDL.<br>
   * Expected behaviour: Gets the expected results without any exceptions or errors.
   */
  @Test
  public void testGetTypeOfBusinessScenarioList()
  {
    LOG.fine("Start testGetTypeOfBusinessScenarioList");
    String[] typeList = endpointLocator.getTypeOfBusinessScenarioList();
    assertNotNull("TypeOfBusinessScenarios is null", typeList);
    assertEquals("Wrong count of TypeOfBusinessScenario", 2, typeList.length);
    assertEquals("TypeOfBusinessScenario not found", typeOfFirst, typeList[0]);
    assertEquals("TypeOfBusinessScenario not found", typeOfSecond, typeList[1]);
    // Second
    typeList = endpointLocator2.getTypeOfBusinessScenarioList();
    assertNotNull("TypeOfBusinessScenarios is null", typeList);
    assertEquals("Wrong count of TypeOfBusinessScenario", 4, typeList.length);
    assertEquals("TypeOfBusinessScenario not found", typeOfFirst, typeList[0]);
    assertEquals("TypeOfBusinessScenario not found", typeOfSecond, typeList[1]);
    assertEquals("TypeOfBusinessScenario not found", typeOfFirst2, typeList[2]);
    assertEquals("TypeOfBusinessScenario not found", typeOfSecond2, typeList[3]);
  }

  /**
   * Checks the PortList of the given WSDL.<br>
   * Expected behaviour: Gets the expected results without any exceptions or errors.
   */
  @Test
  public void testGetPortList()
  {
    LOG.fine("Start testGetPortList");
    QName[] portList = endpointLocator.getPortList();
    assertNotNull("PortList is null", portList);
    assertEquals("Wrong count of ports", 2, portList.length);
    assertEquals("Port not found", "TestWSDLSOAPPort", portList[0].getLocalPart());
    assertEquals("Port not found", "TestWSDLSOAPPort2", portList[1].getLocalPart());
    // Second
    portList = endpointLocator2.getPortList();
    assertNotNull("PortList is null", portList);
    assertEquals("Wrong count of ports", 4, portList.length);
    assertEquals("Port not found", "TestWSDLSOAPPort", portList[0].getLocalPart());
    assertEquals("Port not found", "TestWSDLSOAPPort2", portList[1].getLocalPart());
    assertEquals("Port not found", "TestWSDLSOAPPort3", portList[2].getLocalPart());
    assertEquals("Port not found", "TestWSDLSOAPPort4", portList[3].getLocalPart());
  }

  /**
   * Checks the ServiceList of the given WSDL.<br>
   * Expected behaviour: Gets the expected results without any exceptions or errors.
   */
  @Test
  public void testGetServiceList()
  {
    LOG.fine("Start testGetServiceList");
    QName[] serviceList = endpointLocator.getServiceList();
    assertNotNull("ServiceList is null", serviceList);
    assertEquals("Wrong count of service", 1, serviceList.length);
    assertEquals("Service not found", "TestWSDLService", serviceList[0].getLocalPart());
    // Second
    serviceList = endpointLocator2.getServiceList();
    assertNotNull("ServiceList is null", serviceList);
    assertEquals("Wrong count of service", 2, serviceList.length);
    assertEquals("Service not found", "TestWSDLService", serviceList[0].getLocalPart());
    assertEquals("Service not found", "TestWSDLService2", serviceList[1].getLocalPart());
  }

  /**
   * Checks the method getPortForTypeOfBusinessScenario to find the correct port for the given
   * TypeOfBusinessScenario value.<br>
   * Expected behaviour: Gets the expected results without any exceptions or errors.
   */
  @Test
  public void testGetPortForTypeOfBusinessScenario() throws Exception
  {
    LOG.fine("Start testGetPortForTypeOfBusinessScenario");
    QName port = endpointLocator.getPortForTypeOfBusinessScenario(typeOfFirst);
    assertNotNull("Port is null", port);
    assertEquals("Port not found", "TestWSDLSOAPPort", port.getLocalPart());
    port = endpointLocator.getPortForTypeOfBusinessScenario(typeOfSecond);
    assertNotNull("Port is null", port);
    assertEquals("Port not found", "TestWSDLSOAPPort2", port.getLocalPart());
    // Second
    port = endpointLocator2.getPortForTypeOfBusinessScenario(typeOfFirst2);
    assertNotNull("Port is null", port);
    assertEquals("Port not found", "TestWSDLSOAPPort3", port.getLocalPart());
    port = endpointLocator2.getPortForTypeOfBusinessScenario(typeOfSecond2);
    assertNotNull("Port is null", port);
    assertEquals("Port not found", "TestWSDLSOAPPort4", port.getLocalPart());
  }

  /**
   * Checks the method getServiceForTypeOfBusinessScenario to find the correct service for the given
   * TypeOfBusinessScenario value.<br>
   * Expected behaviour: Gets the expected results without any exceptions or errors.
   */
  @Test
  public void testGetServiceForTypeOfBusinessScenario()
  {
    LOG.fine("Start testGetServiceForTypeOfBusinessScenario");
    QName service = endpointLocator.getServiceForTypeOfBusinessScenario(typeOfFirst);
    assertNotNull("Service is null", service);
    assertEquals("Service not found", "TestWSDLService", service.getLocalPart());
    service = endpointLocator.getServiceForTypeOfBusinessScenario(typeOfSecond);
    assertNotNull("Service is null", service);
    assertEquals("Service not found", "TestWSDLService", service.getLocalPart());
    // Second
    service = endpointLocator2.getServiceForTypeOfBusinessScenario(typeOfFirst2);
    assertNotNull("Service is null", service);
    assertEquals("Service not found", "TestWSDLService2", service.getLocalPart());
    service = endpointLocator2.getServiceForTypeOfBusinessScenario(typeOfSecond2);
    assertNotNull("Service is null", service);
    assertEquals("Service not found", "TestWSDLService2", service.getLocalPart());
  }

}
