package eu.dnetlib.enabling.aas.utils;


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

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import eu.dnetlib.enabling.aas.utils.ResourceUtils;
import eu.dnetlib.enabling.aas.utils.ResourcesGenerator;

public class ResourceUtilsTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}
	
	@Test
	public void testConvertProfId() {
		String sourceXmlProfId = null;
		assertNull(ResourceUtils.convertProfId(sourceXmlProfId));
		sourceXmlProfId = "";
		assertNull(ResourceUtils.convertProfId(sourceXmlProfId));
		sourceXmlProfId = "someText";
		assertNull(ResourceUtils.convertProfId(sourceXmlProfId));
		sourceXmlProfId = "<RESOURCE_IDENTIFIER/>";
		assertNull(ResourceUtils.convertProfId(sourceXmlProfId));
		String value = "someValue";
		sourceXmlProfId = "<RESOURCE_IDENTIFIER value=\""+value+"\"/>";
		assertEquals(value, ResourceUtils.convertProfId(sourceXmlProfId));
	}
	
	@Test
	public void testConvertDocumentToString() throws ParserConfigurationException, SAXException, IOException {
		assertNull(ResourceUtils.convertDocumentToString(null));
		String resourceId = "someResourceId";
		String testProfile = ResourcesGenerator.generateTestProfile(resourceId);
		assertNotNull(testProfile);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);
        DocumentBuilder db = null;
        factory.setNamespaceAware(true);
        factory.setValidating(false);
    	db = factory.newDocumentBuilder();
    	Document doc = db.parse(new InputSource(new StringReader(testProfile)));
    	assertNotNull(doc);
    	String parsedTestProfile = ResourceUtils.convertDocumentToString(doc);
    	assertNotNull(parsedTestProfile);
    	assertEquals(testProfile, parsedTestProfile);
	}

	

}
