package eu.stork.documentservice.tests;

import static org.junit.Assert.*;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import org.junit.Test;

import eu.stork.documentservice.DocumentService;
import eu.stork.documentservice.exceptions.DocumentServiceException;
import eu.stork.documentservice.utils.Utils;
import eu.stork.documentservice.utils.XmlHelper;

public class DocumentServiceTests {

	private static String xmlrequest = "<dss:SignRequest xmlns:dss=\"urn:oasis:names:tc:dss:1.0:core:schema\" RequestID=\"_d96b62a87d18f1095180b1f44c90b5fd\"><dss:InputDocuments><dss:Document><dss:Base64Data MimeType=\"text/plain\">VGVzdCB0ZXh0</dss:Base64Data></dss:Document></dss:InputDocuments></dss:SignRequest>";
	private static String docRequstLoc = "C:/Temp/AttrQueryRequestSdoc.xml"; 
	private static String docRequstLocMod = "C:/Temp/AttrQueryRequestSdocMod.xml";
	private static String destCountry = "AT";	
	private static String spID ="DEMO-SP";
	
	@Test
	public void testXmlParsing() {
		String reqId = null;
		try {
			reqId = XmlHelper.getRequestId(xmlrequest);
		} catch (DocumentServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertNotNull(reqId);
	}
	
	@Test
	public void testXmlParsingDocument() {
		String doc = null;
		try {
			doc = XmlHelper.getRequestDocumentData(xmlrequest);
		} catch (DocumentServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertNotNull(doc);
	}
	
	@Test
	public void testXmlParsingDocumentMime() {
		String mime = null;
		try {
			mime = XmlHelper.getRequestDocumentMime(xmlrequest);
		} catch (DocumentServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertNotNull(mime);
	}
	
	@Test
	public void testCheckTransferrequest()
	{		
		byte[] docBytes = Utils.readData(docRequstLoc);		
		try {
			String docId = XmlHelper.verifyRequestByte(docBytes);
			assertTrue(!docId.isEmpty());
		} catch (DocumentServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	@Test
	public void testCheckTransferrequestMod()
	{		
		byte[] docBytes = Utils.readData(docRequstLocMod);		
		try {
			XmlHelper.verifyRequestByte(docBytes);			
			fail("testCheckTransferrequestMod(...) should've thrown an DocumentServiceException!");
		} catch (DocumentServiceException e) {
			e.printStackTrace();
			//success("Exception thrown.");
		}		
	}
	
	@Test
	public void testAddDocument()
	{
		URL url = null;
		  try {
		   url = new URL("http://localhost:8080/DocumentService/DocumentService?wsdl");
		  } catch (MalformedURLException e) {
		   e.printStackTrace();
		  }
		  QName qname = new QName("http://stork.eu",
		    "DocumentService");

		  Service service = Service.create(url, qname);
		  DocumentService docservice = service.getPort(DocumentService.class);
		  
		  BindingProvider bp = (BindingProvider) docservice;
	      SOAPBinding binding = (SOAPBinding) bp.getBinding();
	      binding.setMTOMEnabled(true);
	      
	      try
	      {
		      String doc = XmlHelper.getRequestDocumentData(xmlrequest);
		      byte[] docData = Utils.decodeBase64String(doc, false);
		      String mime = XmlHelper.getRequestDocumentMime(xmlrequest);
			  String docid = docservice.addDocument(docData, xmlrequest, destCountry, spID, mime, "");	  
			  assertNotNull(docid);
	      }
	      catch (Exception ex)
	      {
	    	  ex.printStackTrace();
	      }
	}

}