Add MockFace
diff --git a/src/test/java/com/intel/jndn/mock/MockFaceTest.java b/src/test/java/com/intel/jndn/mock/MockFaceTest.java
new file mode 100644
index 0000000..77ce5a3
--- /dev/null
+++ b/src/test/java/com/intel/jndn/mock/MockFaceTest.java
@@ -0,0 +1,130 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.intel.jndn.mock;
+
+import java.io.IOException;
+import net.named_data.jndn.Data;
+import net.named_data.jndn.Interest;
+import net.named_data.jndn.Name;
+import net.named_data.jndn.OnData;
+import net.named_data.jndn.OnInterest;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.transport.Transport;
+import net.named_data.jndn.util.Blob;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class MockFaceTest {
+
+ /**
+ * Setup logging
+ */
+ private static final Logger logger = LogManager.getLogger();
+
+ /**
+ * Test of addResponse method, of class MockFace.
+ * @throws java.io.IOException
+ * @throws net.named_data.jndn.encoding.EncodingException
+ */
+ @Test
+ public void testWithResponses() throws IOException, EncodingException {
+ MockFace face = new MockFace();
+
+ // add response
+ Data response = new Data(new Name("/test/with/responses"));
+ response.setContent(new Blob("..."));
+ face.addResponse(new Name("/test/with/responses"), response);
+
+ // make request
+ final Counter count = new Counter();
+ logger.info("Express interest: /test/with/responses");
+ face.expressInterest(new Interest(new Name("/test/with/responses")), new OnData() {
+ @Override
+ public void onData(Interest interest, Data data) {
+ count.inc();
+ logger.debug("Received data");
+ assertEquals(data.getContent().buf(), new Blob("...").buf());
+ }
+ });
+
+ // process face until a response is received
+ int allowedLoops = 100;
+ while (count.get() == 0 && allowedLoops > 0) {
+ allowedLoops--;
+ face.processEvents();
+ }
+ assertEquals(1, count.get());
+ }
+
+ /**
+ * Test of removeResponse method, of class MockFace.
+ * @throws net.named_data.jndn.encoding.EncodingException
+ * @throws java.io.IOException
+ * @throws net.named_data.jndn.security.SecurityException
+ */
+ @Test
+ public void testWithHandlers() throws EncodingException, IOException, net.named_data.jndn.security.SecurityException {
+ MockFace face = new MockFace();
+
+ // add interest handler
+ logger.info("Register prefix: /test/with/responses");
+ face.registerPrefix(new Name("/test/with/handlers"), new OnInterest() {
+ @Override
+ public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) {
+ logger.debug("Received interest, responding: " + interest.getName().toUri());
+ Data response = new Data(new Name("/test/with/handlers"));
+ response.setContent(new Blob("..."));
+ try {
+ transport.send(response.wireEncode().buf());
+ } catch (IOException e) {
+ fail("Failed to send encoded data packet.");
+ }
+ }
+ }, null);
+
+ // make request
+ final Counter count = new Counter();
+ logger.info("Express interest: /test/with/responses");
+ face.expressInterest(new Interest(new Name("/test/with/handlers")), new OnData() {
+ @Override
+ public void onData(Interest interest, Data data) {
+ count.inc();
+ logger.debug("Received data");
+ assertEquals(data.getContent().buf(), new Blob("...").buf());
+ }
+ });
+
+ // process faces until a response is received
+ int allowedLoops = 100;
+ while (count.get() == 0 && allowedLoops > 0) {
+ allowedLoops--;
+ face.processEvents();
+ }
+ assertEquals(1, count.get());
+ }
+
+ /**
+ * Count reference
+ */
+ class Counter {
+
+ int count = 0;
+
+ public void inc() {
+ count++;
+ }
+
+ public int get() {
+ return count;
+ }
+ }
+}
diff --git a/src/test/java/com/intel/jndn/mock/MockTransportTest.java b/src/test/java/com/intel/jndn/mock/MockTransportTest.java
index 06ed987..44ad258 100644
--- a/src/test/java/com/intel/jndn/mock/MockTransportTest.java
+++ b/src/test/java/com/intel/jndn/mock/MockTransportTest.java
@@ -24,8 +24,7 @@
import static org.junit.Assert.*;
/**
- * Mock the transport class
- * TODO add face.registerPrefix() example
+ * Mock the transport class TODO add face.registerPrefix() example
*
* @author Andrew Brown <andrew.brown@intel.com>
*/
@@ -38,7 +37,7 @@
/**
* Test sending a Data packet.
- *
+ *
* @throws java.io.IOException
* @throws net.named_data.jndn.encoding.EncodingException
*/
@@ -62,16 +61,20 @@
assertEquals(data.getContent().buf(), new Blob("...").buf());
}
});
-
- while(count.get() == 0){
+
+ // process the face until one response
+ while (count.get() == 0) {
face.processEvents();
}
+
+ // check for sent packets
+ assertEquals(0, transport.getSentDataPackets().size());
+ assertEquals(1, transport.getSentInterestPackets().size());
}
-
-
+
/**
* Test sending multiple Data packets.
- *
+ *
* @throws java.io.IOException
* @throws net.named_data.jndn.encoding.EncodingException
*/
@@ -98,11 +101,16 @@
assertEquals(data.getContent().buf(), new Blob("...").buf());
}
});
-
- while(count.get() == 0){
+
+ // process the face until one response received
+ while (count.get() == 0) {
face.processEvents();
}
-
+
+ // check for sent packets
+ assertEquals(0, transport.getSentDataPackets().size());
+ assertEquals(1, transport.getSentInterestPackets().size());
+
// express interest again, but this time it should time out because there
// is no data left on the wire; the first processEvents() has already
// picked it up
@@ -115,28 +123,36 @@
count2.inc();
fail("Should not return data; data should already be cleared");
}
- }, new OnTimeout(){
+ }, new OnTimeout() {
@Override
public void onTimeout(Interest interest) {
count2.inc();
assertTrue(true);
}
});
-
- while(count2.get() == 0){
+
+ // process the face until timeout
+ while (count2.get() == 0) {
face.processEvents();
}
+
+ // check for sent packets
+ assertEquals(0, transport.getSentDataPackets().size());
+ assertEquals(2, transport.getSentInterestPackets().size());
}
-
+
/**
* Count reference
*/
- class Counter{
+ class Counter {
+
int count = 0;
- public void inc(){
+
+ public void inc() {
count++;
}
- public int get(){
+
+ public int get() {
return count;
}
}