package be.demmel.jgws.cfg.jaxb;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.JAXBIntrospector;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.junit.Test;
import org.xml.sax.SAXException;
import static org.junit.Assert.*;

public class MarshallingTest {
	private static final String LOGIN_SERVER_XSD = "/loginserverConfiguration.xsd";
	private static final String GAME_SERVER_XSD = "/gameserverConfiguration.xsd";

    @Test
    public void marshallLoginServerConfiguration() throws JAXBException {
        ObjectFactory of = new ObjectFactory();
        Binding binding = of.createBinding();
        binding.setIp("ip1");
        binding.setPort(1);
        
        Jpa jpa = of.createJpa();
        jpa.setDriver("myDriver");
        jpa.setPassword("myPassword");
        jpa.setUrl("myUrl");
        jpa.setUser("myUser");
        
        Configuration configuration = of.createConfiguration();
        configuration.setBinding(binding);
        configuration.setJpa(jpa);
        configuration.setPortalUri("myUri");

        JAXBContext jc = JAXBContext.newInstance("be.demmel.jgws.cfg.jaxb");
        Marshaller m = jc.createMarshaller();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        m.marshal(of.createLoginserverConfiguration(configuration), baos);

        String expectedOutput = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:loginserverConfiguration xmlns:ns2=\"loginserverConfiguration\"><ns2:jpa><ns2:driver>myDriver</ns2:driver><ns2:url>myUrl</ns2:url><ns2:user>myUser</ns2:user><ns2:password>myPassword</ns2:password></ns2:jpa><ns2:binding><ns2:ip>ip1</ns2:ip><ns2:port>1</ns2:port></ns2:binding><ns2:portalUri>myUri</ns2:portalUri></ns2:loginserverConfiguration>";

        String resultingXml = baos.toString();
        assertEquals(expectedOutput, resultingXml);

        System.out.println("");
    }
    
    @Test
    public void marshallGameServerConfiguration() throws JAXBException {
        ObjectFactory of = new ObjectFactory();
        Binding binding = of.createBinding();
        binding.setIp("ip1");
        binding.setPort(1);
        
        Jpa jpa = of.createJpa();
        jpa.setDriver("myDriver");
        jpa.setPassword("myPassword");
        jpa.setUrl("myUrl");
        jpa.setUser("myUser");
        
        GameserverConfiguration configuration = of.createGameserverConfiguration();
        configuration.setBinding(binding);
        configuration.setJpa(jpa);
        configuration.setPortalUri("myUri");
        configuration.setPortalCallbackPort(1400);

        JAXBContext jc = JAXBContext.newInstance("be.demmel.jgws.cfg.jaxb");
        Marshaller m = jc.createMarshaller();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        m.marshal(configuration, baos);

        String expectedOutput = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><gameserverConfiguration xmlns:ns2=\"loginserverConfiguration\"><ns2:jpa><ns2:driver>myDriver</ns2:driver><ns2:url>myUrl</ns2:url><ns2:user>myUser</ns2:user><ns2:password>myPassword</ns2:password></ns2:jpa><ns2:binding><ns2:ip>ip1</ns2:ip><ns2:port>1</ns2:port></ns2:binding><ns2:portalUri>myUri</ns2:portalUri><portalCallbackPort>1400</portalCallbackPort></gameserverConfiguration>";
        String resultingXml = baos.toString();
                
        assertEquals(expectedOutput, resultingXml);

        System.out.println("");
    }

    @Test
    public void unmarshallLoginServerConfiguration() throws ClassNotFoundException, JAXBException, SAXException {        
        InputStream is = getClass().getResourceAsStream("/correctLoginServerConfiguration.xml");
        Configuration unmarshalledConfiguration = validateAndUnmarshal(Configuration.class, is, LOGIN_SERVER_XSD);
        assertEquals("jdbc:mysql://127.0.0.1:3306/jgws?useServerPrepStmts=false", unmarshalledConfiguration.getJpa().getUrl());
    }
    
    @Test(expected = JAXBException.class)
    public void wrongLoginServerConfigurationDetected() throws ClassNotFoundException, JAXBException, SAXException {
        InputStream is = getClass().getResourceAsStream("/incorrectLoginServerConfiguration.xml");   
        validateAndUnmarshal(Configuration.class, is, LOGIN_SERVER_XSD);
    }
    
    @Test
    public void unmarshallGameServerConfiguration() throws ClassNotFoundException, JAXBException, SAXException {        
        InputStream is = getClass().getResourceAsStream("/correctGameServerConfiguration.xml");
        GameserverConfiguration unmarshalledConfiguration = validateAndUnmarshal(GameserverConfiguration.class, is, GAME_SERVER_XSD);
        assertEquals(1400, unmarshalledConfiguration.getPortalCallbackPort());
    }
    
    @Test(expected = JAXBException.class)
    public void wrongGameServerConfigurationDetected() throws ClassNotFoundException, JAXBException, SAXException {
        InputStream is = getClass().getResourceAsStream("/incorrectGameServerConfiguration.xml");   
        validateAndUnmarshal(GameserverConfiguration.class, is, GAME_SERVER_XSD);
    }
    
    private static <T> T validateAndUnmarshal(Class<T> docClass, InputStream xmlFile, String schemaLocation) throws JAXBException, SAXException {
  		String packageName = docClass.getPackage().getName();
  		JAXBContext jc = JAXBContext.newInstance(packageName);
  		Unmarshaller u = jc.createUnmarshaller();

  		SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  		URL url = MarshallingTest.class.getResource(schemaLocation);
  		Schema mySchema = sf.newSchema(url);

  		u.setSchema(mySchema);
  		return (T) JAXBIntrospector.getValue(u.unmarshal(xmlFile));
  	}
}
