package eu.dnetlib.social.producer;

import java.io.IOException;
import java.util.Collection;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;

import com.rabbitmq.client.ConnectionFactory;

import eu.dnetlib.social.SocialUploadable;
import eu.dnetlib.social.YouTubeObject;

/**
 * Test for the octobot enqueuer/producer. Please note it works only if rabbitmq is up and running.
 * 
 * @author alessia
 * 
 */
public class SocialEnqueuerTest {
	private static final Log log = LogFactory.getLog(SocialEnqueuerTest.class);
	ConnectionFactory connectionFactory;
	SocialUploadable objectToUpload;
	SRWReader srwReader;
	SocialEnqueuer enqueuer;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		System.setProperty("jaxp.debug", "1");
		connectionFactory = new ConnectionFactory();
		connectionFactory.setUsername("guest");
		connectionFactory.setPassword("guest");
		connectionFactory.setVirtualHost("/");
		connectionFactory.setHost("localhost");
		connectionFactory.setPort(5672);
		enqueuer = new SocialEnqueuer();
		enqueuer.setConnectionFactory(connectionFactory);
		srwReader = new SRWReader();
		//change this to the srw path file
		srwReader.setSrwPathFile("/var/lib/uTube/proof-social-sites-1.xml");
		srwReader.setPwd("ISTI4Hope");
		srwReader.setUserName("alessia.bardi@isti.cnr.it");
	}

	/**
	 * Test method for {@link producer.SocialEnqueuer#produce(common.SocialUploadable)}.
	 * 
	 * @throws ParserConfigurationException
	 * @throws IOException
	 * @throws SAXException
	 * @throws XPathExpressionException
	 * @throws JSONException
	 */
	@Test
	public void testProduce() throws XPathExpressionException, SAXException, IOException, ParserConfigurationException {
		Collection<YouTubeObject> objects = srwReader.extractFromDocument();
		for (YouTubeObject o : objects)
			enqueuer.produce(o);
		log.debug("All objects enqueued. Now consumers should do something!;");
	}

}
