package eu.dnetlib.ariadneplus.workflows.nodes;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.Before;
import org.junit.Test;

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

/**
 * Created by Alessia Bardi on 09/02/2018.
 *
 * @author Alessia Bardi
 */
public class VirtuosoAriadnePlusIteratorTest {

	private VirtuosoAriadnePlusIterator it;

	@Before
	public void setup(){
		it = new VirtuosoAriadnePlusMockIterator();
	}

	@Test
	public void fillQueueTest() throws Exception {
		it.fillQueue();
		String s = it.getElements().take();
		while(!s.equals(VirtuosoAriadnePlusIterator.TERMINATOR)){
			System.out.println(s);
			s = it.getElements().take();
		}
		Map<String, Integer> errors = it.getErrors();
		for (Iterator<Entry<String, Integer>> it1 = errors.entrySet().iterator(); it1.hasNext(); ) {
			final Entry e = it1.next();
			System.out.println(e.getKey() +" - "+e.getValue());
		}
	}

	@Test
	public void testHasNext(){
		assertTrue(it.hasNext());
	}

	@Test
	public void testIterate(){
		while(it.hasNext())
			System.out.println(it.next());
	}

	@Test
	public void testCompleteXml(){
		String s = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><rdf:RDF/>";
		String completed = it.completeXML(s, "http://ariadneplus.d4science.org/handle/AriadnePlus/REG/Thing/Appellation/urp%40regione.umbria.it");
		System.out.println(completed);
	}

	@Test
	public void testRemoveXmlDeclaration(){
		String s = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><rdf:RDF";
		String res = s.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
		assertEquals("<rdf:RDF", res);
		s = "<rdf:RDF ";
		res = s.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
		assertEquals("<rdf:RDF", res);
	}
}
