package com.ericsson.ims.mtas.xdms.aggregated.service.binder;

import static org.junit.Assert.fail;

import javax.xml.bind.JAXBException;
import javax.xml.transform.stream.StreamSource;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.ericsson.ims.mtas.xdms.aggregated.service.document.AggregatedServiceDocument;
import com.ericsson.ims.mtas.xdms.aggregated.service.document.IAggregatedServiceDocument;
import com.ericsson.ims.mtas.xdms.schema.cai3g.Create;

public class AggregatedServiceBinderTest
{
	IAggregatedServiceBinder binder;

	IAggregatedServiceDocument document;

	String moType = "MMTel@http://schemas.ericsson.com/mtas/mmtel/cai3g";

	String id = "sip:lulseged.zerfu@ericsson.com";

	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
	}

	@Before
	public void setUp() throws Exception
	{
		binder = new AggregatedServiceBinder();
		document = new AggregatedServiceDocument();
	}

	@After
	public void tearDown() throws Exception
	{
		binder = null;
		document = null;
	}

	@Test
	public void testCreate()
	{
		try
		{
			Create create = document.createCreate(moType, id);
			binder.marshall(create);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			fail(e.getMessage());
		}
	}

	@Test
	public void testGet()
	{
		try
		{
			binder.marshall(document.createGet(moType, id));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			fail(e.getMessage());
		}
	}

	@Test
	public void testSet()
	{
		try
		{
			binder.marshall(document.createSet(moType, id));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			fail(e.getMessage());
		}
	}

	@Test
	public void testDelete()
	{
		try
		{
			binder.marshall(document.createDelete(moType, id));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			fail(e.getMessage());
		}
	}

	@Test
	public void testUnmarshall()
	{
		StreamSource source = new StreamSource(getClass().getResource("/xml/cai3g-set.xml").getFile());

		try
		{
			Object set = binder.unmarshall(source);

			binder.marshall(set);
		}
		catch (JAXBException e)
		{
			e.printStackTrace();
			fail("Unmarshalling failed!" + e.getMessage());
		}
	}

	@Test
	public void testUnmarshallEmpty()
	{
		StreamSource source = new StreamSource(getClass().getResource("/xml/cai3g-empty-set.xml").getFile());

		try
		{
			Object set = binder.unmarshall(source);

			binder.marshall(set);
		}
		catch (JAXBException e)
		{
			e.printStackTrace();
			fail("Unmarshalling failed!" + e.getMessage());
		}
	}

	@Test
	public void testUnmarshallForward()
	{
		StreamSource source = new StreamSource(getClass().getResource("/xml/cai3g-forward-set.xml").getFile());

		try
		{
			Object set = binder.unmarshall(source);

			binder.marshall(set);
		}
		catch (JAXBException e)
		{
			e.printStackTrace();
			fail("Unmarshalling failed!" + e.getMessage());
		}
	}
}
