package integration;

import jwm.ir.crawler.UrlFeed;
import jwm.ir.domain.Domain;
import jwm.ir.domain.Page;
import jwm.ir.utils.Db;
import jwm.ir.utils.DbImpl;
import jwm.ir.utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Assert;
import org.junit.Test;

import java.util.NoSuchElementException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by Jeff on 2016-07-25.
 */
public class UrlFeedIntegrationTest {

    @Test
    public void testUrlFeed() {

        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        if (sessionFactory.isClosed()) sessionFactory.openSession();

        // add some dummy db records
        int crawlerId = 1;
        Domain domain = new Domain("localhost", crawlerId);
        Page page1 = new Page(domain, "http://localhost/searchengine_test/page1.html");
        Page page2 = new Page(domain, "http://localhost/searchengine_test/page2.html");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.save(domain);
        session.save(page1);
        session.save(page2);
        tx.commit();
        session.close();

        Db db = new DbImpl(sessionFactory);
        BlockingQueue<String> output = new LinkedBlockingQueue<>();
        UrlFeed sut = new UrlFeed(db, output);

        // process the records
        sut.process();

        Assert.assertTrue("http://localhost/searchengine_test/page1.html".equals(output.remove()));
        Assert.assertTrue("http://localhost/searchengine_test/page2.html".equals(output.remove()));
        try {
            output.remove();
            Assert.fail("should throw a NPE; only 2 items in the queue");
        }
        catch (NoSuchElementException ex) { }


    }
}

@@ -46,16 +46,53 @@
 rlFeed;%0A
+import jwm.ir.crawler.UrlFeedRunner;%0A
 import j
@@ -112,16 +112,16 @@
 Domain;%0A
-
 import j
@@ -460,16 +460,55 @@
 gQueue;%0A
+import java.util.concurrent.Executors;%0A
 import j
@@ -547,16 +547,70 @@
 ngQueue;
+%0Aimport java.util.concurrent.ScheduledExecutorService;
 %0A%0A/**%0A *
@@ -2005,16 +2005,1065 @@
 x) %7B %7D%0A%0A
+    %7D%0A%0A    /**%0A     * Not an integration test per se; can just be run manually%0A     * to try out the UrlFeed/Runner%0A     * @param args%0A     */%0A    public static void main(String%5B%5D args) %7B%0A        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();%0A        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();%0A        Db db = new DbImpl(sessionFactory);%0A        final BlockingQueue%3CString%3E out = new LinkedBlockingQueue%3C%3E();%0A        UrlFeed feeder = new UrlFeed(db, out);%0A        UrlFeedRunner x = new UrlFeedRunner(executorService, feeder);%0A        x.start();%0A%0A%0A        Runnable listener = new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                while(true) %7B%0A                    try %7B%0A                        System.out.println(out.take());%0A                    %7D catch (InterruptedException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A                %7D%0A            %7D%0A        %7D;%0A%0A        Thread t = new Thread(listener);%0A        t.start();
 %0A    %7D%0A%7D

Put UrlFeedRunner (runner) into the integration test code file