package test.ccn.data.query;

import java.io.IOException;
import java.util.ArrayList;

import org.junit.Assert;
import org.junit.Test;

import test.ccn.library.LibraryTestBase;

import com.parc.ccn.data.ContentName;
import com.parc.ccn.data.ContentObject;
import com.parc.ccn.data.MalformedContentNameStringException;
import com.parc.ccn.data.query.CCNFilterListener;
import com.parc.ccn.data.query.CCNInterestListener;
import com.parc.ccn.data.query.Interest;

/**
 * Test sending interests across ccnd
 * Requires a running ccnd
 * 
 * @author rasmusse
 *
 */

public class InterestEndToEndTest extends LibraryTestBase implements CCNFilterListener, CCNInterestListener {
	private Interest _interestSent;
	private String _prefix = "/interestEtoETest/test-" + rand.nextInt(10000);
	private final static int TIMEOUT = 3000;

	
	@Test
	public void testInterestEndToEnd() throws MalformedContentNameStringException, IOException, InterruptedException {
		getLibrary.registerFilter(ContentName.fromNative(_prefix), this);
		_interestSent = new Interest(ContentName.fromNative(_prefix + "/simpleTest"));
		doTest();
		_interestSent = new Interest(ContentName.fromNative(_prefix + "/simpleTest2"));
		_interestSent.maxSuffixComponents(3);
		_interestSent.minSuffixComponents(4);
		doTest();
	}

	public int handleInterests(ArrayList<Interest> interests) {
		Assert.assertTrue(_interestSent.equals(interests.get(0)));
		synchronized(this) {
			notify();
		}
		return 0;
	}
	
	private void doTest() throws IOException, InterruptedException {
		long startTime = System.currentTimeMillis();
		putLibrary.expressInterest(_interestSent, this);
		synchronized (this) {
			wait(TIMEOUT);
		}
		Assert.assertTrue((System.currentTimeMillis() - startTime) < TIMEOUT);
	}

	public Interest handleContent(ArrayList<ContentObject> results,
			Interest interest) {
		// TODO Auto-generated method stub
		return null;
	}

}

@@ -1225,31 +1225,165 @@
 nts(
-3);%0A%09%09_interestSent.min
+4);%0A%09%09_interestSent.minSuffixComponents(3);%0A%09%09doTest();%0A%09%09_interestSent = new Interest(ContentName.fromNative(_prefix + %22/simpleTest2%22));%0A%09%09_interestSent.max
 Suff
@@ -1391,25 +1391,25 @@
 xComponents(
-4
+1
 );%0A%09%09doTest(

Fix bugs, add new test refs #603