Improve Nfdc tests

Move some tests that do not require a real NFD to a separate
NfdcTest class so that they automatically run in CI builds.
Use Tlv0_3WireFormat in all tests.

Change-Id: I853c24277e8bbe63ce68d27458e7c9d429959f77
diff --git a/src/test/java/com/intel/jndn/management/NdnPingClientIT.java b/src/test/java/com/intel/jndn/management/NdnPingClientIT.java
index 762ae7c..18a6269 100644
--- a/src/test/java/com/intel/jndn/management/NdnPingClientIT.java
+++ b/src/test/java/com/intel/jndn/management/NdnPingClientIT.java
@@ -15,17 +15,16 @@
 
 import net.named_data.jndn.Face;
 import net.named_data.jndn.Name;
-import net.named_data.jndn.security.SecurityException;
+import net.named_data.jndn.encoding.Tlv0_3WireFormat;
+import net.named_data.jndn.encoding.WireFormat;
+
 import org.junit.Before;
 import org.junit.Test;
-
-import java.io.IOException;
-
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 /**
- * Testing basic pining using real NFD instance (NFD must be run locally while executing the test).
+ * Testing basic pinging using real NFD instance (NFD must be run locally while executing the test).
  *
  * @author Andrew Brown <andrew.brown@intel.com>
  */
@@ -33,12 +32,14 @@
   private Face face;
 
   @Before
-  public void setUp() throws SecurityException {
+  public void setUp() {
+    WireFormat.setDefaultWireFormat(Tlv0_3WireFormat.get());
+
     face = new Face("localhost");
   }
 
   @Test
-  public void testPingLocal() throws IOException, ManagementException {
+  public void testPingLocal() {
     boolean hasSucceeded = NdnPingClient.pingLocal(face);
     assertTrue(hasSucceeded);
   }
diff --git a/src/test/java/com/intel/jndn/management/NfdcIT.java b/src/test/java/com/intel/jndn/management/NfdcIT.java
index 7baaf22..98e11cf 100644
--- a/src/test/java/com/intel/jndn/management/NfdcIT.java
+++ b/src/test/java/com/intel/jndn/management/NfdcIT.java
@@ -14,34 +14,24 @@
 package com.intel.jndn.management;
 
 import com.intel.jndn.management.enums.Strategies;
+import com.intel.jndn.management.types.ForwarderStatus;
 import com.intel.jndn.management.types.RibEntry;
 import com.intel.jndn.management.types.StrategyChoice;
-import com.intel.jndn.mock.MockFace;
 import com.intel.jndn.mock.MockKeyChain;
-import net.named_data.jndn.ControlResponse;
-import net.named_data.jndn.Data;
-import net.named_data.jndn.DigestSha256Signature;
 import net.named_data.jndn.Face;
-import net.named_data.jndn.Interest;
-import net.named_data.jndn.KeyLocator;
-import net.named_data.jndn.MetaInfo;
 import net.named_data.jndn.Name;
-import net.named_data.jndn.encoding.EncodingException;
 import net.named_data.jndn.encoding.Tlv0_3WireFormat;
 import net.named_data.jndn.encoding.WireFormat;
 import net.named_data.jndn.security.KeyChain;
 import net.named_data.jndn.security.SecurityException;
-import org.junit.Before;
-import org.junit.Test;
 
-import java.io.IOException;
 import java.util.List;
 import java.util.Random;
-import java.util.logging.Logger;
 
+import org.junit.Before;
+import org.junit.Test;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
@@ -51,9 +41,7 @@
  * @author Andrew Brown <andrew.brown@intel.com>
  */
 public class NfdcIT {
-  private static final Logger LOG = Logger.getLogger(NfdcIT.class.getName());
   private Face face;
-  private MockFace mockFace;
   private Face noKeyChainFace;
 
   @Before
@@ -61,87 +49,53 @@
     WireFormat.setDefaultWireFormat(Tlv0_3WireFormat.get());
 
     face = new Face("localhost");
-    mockFace = new MockFace(new MockFace.Options().setEnablePacketLogging(false).setEnableRegistrationReply(false));
-    noKeyChainFace = new Face("localhost"); // don't set command signing info
     KeyChain keyChain = MockKeyChain.configure(new Name("/tmp/identity"));
     face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName());
-  }
-
-  @Test
-  public void testGetKeyLocator() throws Exception {
-    KeyLocator keyLocator = Nfdc.getKeyLocator(face);
-    assertNotNull(keyLocator);
-    LOG.info("Connected to NFD with key locator: " + keyLocator.getKeyName().toUri());
-
-    assertThrows(ManagementException.class, () -> Nfdc.getKeyLocator(mockFace));
-  }
-
-  @Test
-  public void testFailOfGetKeyLocator() throws Exception {
-    mockFace.onSendInterest.add(new MockFace.SignalOnSendInterest() {
-      @Override
-      public void emit(final Interest interest) {
-        Data data = new Data();
-        data.setName(new Name(interest.getName()).appendVersion(0).appendSegment(0));
-
-        MetaInfo meta = new MetaInfo();
-        meta.setFinalBlockId(data.getName().get(-1));
-        data.setMetaInfo(meta);
-
-        data.setSignature(new DigestSha256Signature());
-
-        LOG.info(data.getSignature().toString());
-
-        // don't set anything else
-        try {
-          mockFace.receive(data);
-        } catch (EncodingException e) {
-          LOG.severe("Failed to set receive data: " + e);
-        }
-      }
-    });
-
-    Exception exception = assertThrows(ManagementException.class, () -> Nfdc.getKeyLocator(mockFace));
-    assertEquals("No key locator available.", exception.getMessage());
+    noKeyChainFace = new Face("localhost"); // don't set command signing info
   }
 
   @Test
   public void testGetForwarderStatus() throws Exception {
-    assertTrue(Nfdc.getForwarderStatus(face).getStartTimestamp() > 0);
-    assertThrows(ManagementException.class, () -> Nfdc.getForwarderStatus(mockFace));
+    ForwarderStatus status = Nfdc.getForwarderStatus(face);
+    assertTrue(status.getStartTimestamp() > 0);
+    assertTrue(status.getCurrentTimestamp() > 0);
+    assertTrue(status.getNFibEntries() > 0);
+    assertTrue(status.getNInInterests() > 0);
+    assertTrue(status.getNOutData() > 0);
+  }
+
+  @Test
+  public void testGetChannelStatusList() throws Exception {
+    assertFalse(Nfdc.getChannelStatusList(face).isEmpty());
   }
 
   @Test
   public void testGetFaceList() throws Exception {
     assertFalse(Nfdc.getFaceList(face).isEmpty());
-    assertThrows(ManagementException.class, () -> Nfdc.getFaceList(mockFace));
   }
 
   @Test
   public void testGetFibList() throws Exception {
     assertFalse(Nfdc.getFibList(face).isEmpty());
-    assertThrows(ManagementException.class, () -> Nfdc.getFibList(mockFace));
   }
 
   @Test
   public void testGetRouteList() throws Exception {
     assertFalse(Nfdc.getRouteList(face).isEmpty());
-    assertThrows(ManagementException.class, () -> Nfdc.getRouteList(mockFace));
   }
 
   @Test
-  public void testRoutes() throws EncodingException, IOException, ManagementException, InterruptedException {
+  public void testRoutes() throws Exception {
     Nfdc.register(face, new Name("/my/route/to/app/face"), 333);
     int faceId = Nfdc.createFace(face, "udp4://127.0.0.1:56363");
     Nfdc.register(face, "udp4://127.0.0.1:56363", new Name("/my/test/route"), 999);
     Nfdc.register(face, faceId, new Name("/"), 555);
 
-    // check that route is created
     Thread.sleep(1000); // NFD registers the route asynchronously
 
+    // check that route is created
     boolean found = false;
     for (RibEntry route : Nfdc.getRouteList(face)) {
-      LOG.info("Found route: " + route.getName().toUri());
       if (route.getName().equals(new Name("/my/test/route"))) {
         found = true;
       }
@@ -155,7 +109,6 @@
 
     // remove face
     Nfdc.destroyFace(face, faceId);
-
     Thread.sleep(1000); // wait for face to be destroyed
 
     Exception exception = assertThrows(ManagementException.class, () -> {
@@ -165,29 +118,10 @@
   }
 
   @Test
-  public void testFailOfRegister() throws Exception {
-    assertThrows(ManagementException.class, () -> Nfdc.register(mockFace, new Name("/my/route/to/app/face"), 333));
-  }
-
-  @Test
-  public void testFailOfUnregister() throws Exception {
-    assertThrows(ManagementException.class, () -> Nfdc.unregister(mockFace, new Name("/my/route/to/app/face")));
-  }
-
-  @Test
-  public void testFailOfCreateFace() throws Exception {
-    assertThrows(ManagementException.class, () -> Nfdc.createFace(mockFace, "udp4://127.0.0.1:56363"));
-  }
-
-  @Test
-  public void testFailOfDestroyFace() throws Exception {
-    assertThrows(ManagementException.class, () -> Nfdc.destroyFace(mockFace, 1));
-  }
-
-  @Test
   public void testStrategies() throws Exception {
     Name prefix = new Name("/test/strategy").append("random" + new Random().nextInt());
     List<StrategyChoice> choices = Nfdc.getStrategyList(face);
+    assertFalse(choices.isEmpty());
     int oldSize = choices.size();
 
     Nfdc.setStrategy(face, prefix, Strategies.RANDOM);
@@ -201,13 +135,6 @@
 
     choices = Nfdc.getStrategyList(face);
     assertEquals(oldSize, choices.size());
-
-    assertThrows(ManagementException.class, () -> Nfdc.getStrategyList(mockFace));
-  }
-
-  @Test
-  public void testFailOfUnsetStrategy() throws Exception {
-    assertThrows(ManagementException.class, () -> Nfdc.unsetStrategy(mockFace, new Name("/")));
   }
 
   @Test
@@ -216,37 +143,4 @@
                    Nfdc.setStrategy(noKeyChainFace, new Name("/test"), Strategies.BEST_ROUTE);
                  });
   }
-
-  @Test
-  public void testFailOfSetStrategyWithNon200Code() throws Exception {
-    mockFace.onSendInterest.add(new MockFace.SignalOnSendInterest() {
-      @Override
-      public void emit(final Interest interest) {
-        ControlResponse response = new ControlResponse();
-        response.setStatusCode(300);
-        response.setStatusText("Test FAIL");
-
-        Data data = new Data();
-        data.setName(interest.getName());
-        data.setContent(response.wireEncode());
-
-        try {
-          mockFace.receive(data);
-        } catch (EncodingException e) {
-          LOG.severe("Failed to set receive data: " + e);
-        }
-      }
-    });
-
-    Exception exception = assertThrows(ManagementException.class, () -> {
-                                         Nfdc.setStrategy(mockFace, new Name("/"), Strategies.MULTICAST);
-                                       });
-    assertEquals("Action failed, forwarder returned: 300 Test FAIL", exception.getMessage());
-  }
-
-  @Test
-  public void testGetChannelStatus() throws Exception {
-    assertFalse(Nfdc.getChannelStatusList(face).isEmpty());
-    assertThrows(ManagementException.class, () -> Nfdc.getChannelStatusList(mockFace));
-  }
 }
diff --git a/src/test/java/com/intel/jndn/management/NfdcTest.java b/src/test/java/com/intel/jndn/management/NfdcTest.java
new file mode 100644
index 0000000..fe9966c
--- /dev/null
+++ b/src/test/java/com/intel/jndn/management/NfdcTest.java
@@ -0,0 +1,213 @@
+/*
+ * jndn-management
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 3, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ */
+package com.intel.jndn.management;
+
+import com.intel.jndn.management.enums.Strategies;
+import com.intel.jndn.mock.MockFace;
+import com.intel.jndn.mock.MockKeyChain;
+import net.named_data.jndn.ControlResponse;
+import net.named_data.jndn.Data;
+import net.named_data.jndn.Interest;
+import net.named_data.jndn.KeyLocator;
+import net.named_data.jndn.KeyLocatorType;
+import net.named_data.jndn.MetaInfo;
+import net.named_data.jndn.Name;
+import net.named_data.jndn.encoding.Tlv0_3WireFormat;
+import net.named_data.jndn.encoding.WireFormat;
+import net.named_data.jndn.security.KeyChain;
+import net.named_data.jndn.security.SecurityException;
+import net.named_data.jndn.security.SigningInfo;
+
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Nfdc unit tests.
+ */
+public class NfdcTest {
+  private MockFace mockFace;
+  private KeyChain keyChain;
+  private MockFace.SignalOnSendInterest replyWithEmptyData;
+
+  @Before
+  public void setUp() throws SecurityException {
+    WireFormat.setDefaultWireFormat(Tlv0_3WireFormat.get());
+
+    mockFace = new MockFace(new MockFace.Options());
+    keyChain = MockKeyChain.configure(new Name("/tmp/identity"));
+
+    replyWithEmptyData = new MockFace.SignalOnSendInterest() {
+      @Override
+      public void emit(final Interest interest) {
+        Data data = new Data();
+        data.setName(new Name(interest.getName()).appendVersion(0).appendSegment(0));
+        MetaInfo meta = new MetaInfo();
+        meta.setFinalBlockId(data.getName().get(-1));
+        data.setMetaInfo(meta);
+
+        try {
+          keyChain.sign(data);
+        } catch (Exception e) {
+          fail("Failed to sign data: " + e);
+        }
+
+        try {
+          mockFace.receive(data);
+        } catch (Exception e) {
+          fail("Failed to receive data on mock face: " + e);
+        }
+      }
+    };
+  }
+
+  @Test
+  public void testGetKeyLocator() throws Exception {
+    mockFace.onSendInterest.add(replyWithEmptyData);
+
+    KeyLocator keyLocator = Nfdc.getKeyLocator(mockFace);
+    assertEquals(KeyLocatorType.KEYNAME, keyLocator.getType());
+    assertNotEquals(0, keyLocator.getKeyName().size());
+  }
+
+  @Test
+  public void testGetKeyLocatorWithDigestSha256() throws Exception {
+    mockFace.onSendInterest.add(new MockFace.SignalOnSendInterest() {
+      @Override
+      public void emit(final Interest interest) {
+        Data data = new Data();
+        data.setName(new Name(interest.getName()).appendVersion(0).appendSegment(0));
+        MetaInfo meta = new MetaInfo();
+        meta.setFinalBlockId(data.getName().get(-1));
+        data.setMetaInfo(meta);
+
+        try {
+          keyChain.sign(data, new SigningInfo(SigningInfo.SignerType.SHA256));
+        } catch (Exception e) {
+          fail("Failed to sign data: " + e);
+        }
+
+        try {
+          mockFace.receive(data);
+        } catch (Exception e) {
+          fail("Failed to receive data on mock face: " + e);
+        }
+      }
+    });
+
+    Exception exception = assertThrows(ManagementException.class, () -> Nfdc.getKeyLocator(mockFace));
+    assertEquals("No key locator available.", exception.getMessage());
+  }
+
+  @Test
+  public void testGetChannelStatusList() throws Exception {
+    assertThrows(ManagementException.class, () -> Nfdc.getChannelStatusList(mockFace));
+
+    mockFace.onSendInterest.add(replyWithEmptyData);
+    assertTrue(Nfdc.getChannelStatusList(mockFace).isEmpty());
+  }
+
+  @Test
+  public void testGetFaceList() throws Exception {
+    assertThrows(ManagementException.class, () -> Nfdc.getFaceList(mockFace));
+
+    mockFace.onSendInterest.add(replyWithEmptyData);
+    assertTrue(Nfdc.getFaceList(mockFace).isEmpty());
+  }
+
+  @Test
+  public void testFailOfCreateFace() throws Exception {
+    mockFace.onSendInterest.add(replyWithEmptyData);
+    assertThrows(ManagementException.class, () -> Nfdc.createFace(mockFace, "udp4://127.0.0.1:56363"));
+  }
+
+  @Test
+  public void testFailOfDestroyFace() throws Exception {
+    mockFace.onSendInterest.add(replyWithEmptyData);
+    assertThrows(ManagementException.class, () -> Nfdc.destroyFace(mockFace, 1));
+  }
+
+  @Test
+  public void testGetFibList() throws Exception {
+    assertThrows(ManagementException.class, () -> Nfdc.getFibList(mockFace));
+
+    mockFace.onSendInterest.add(replyWithEmptyData);
+    assertTrue(Nfdc.getFibList(mockFace).isEmpty());
+  }
+
+  @Test
+  public void testGetRouteList() throws Exception {
+    assertThrows(ManagementException.class, () -> Nfdc.getRouteList(mockFace));
+
+    mockFace.onSendInterest.add(replyWithEmptyData);
+    assertTrue(Nfdc.getRouteList(mockFace).isEmpty());
+  }
+
+  @Test
+  public void testFailOfRegister() throws Exception {
+    mockFace.onSendInterest.add(replyWithEmptyData);
+    assertThrows(ManagementException.class, () -> Nfdc.register(mockFace, new Name("/my/route/to/app/face"), 333));
+  }
+
+  @Test
+  public void testFailOfUnregister() throws Exception {
+    mockFace.onSendInterest.add(replyWithEmptyData);
+    assertThrows(ManagementException.class, () -> Nfdc.unregister(mockFace, new Name("/my/route/to/app/face")));
+  }
+
+  @Test
+  public void testGetStrategyList() throws Exception {
+    assertThrows(ManagementException.class, () -> Nfdc.getStrategyList(mockFace));
+
+    mockFace.onSendInterest.add(replyWithEmptyData);
+    assertTrue(Nfdc.getStrategyList(mockFace).isEmpty());
+  }
+
+  @Test
+  public void testFailOfSetStrategyWithNon200Code() throws Exception {
+    mockFace.onSendInterest.add(new MockFace.SignalOnSendInterest() {
+      @Override
+      public void emit(final Interest interest) {
+        ControlResponse response = new ControlResponse();
+        response.setStatusCode(400);
+        response.setStatusText("test error");
+
+        Data data = new Data();
+        data.setName(interest.getName());
+        data.setContent(response.wireEncode());
+
+        try {
+          mockFace.receive(data);
+        } catch (Exception e) {
+          fail("Failed to receive data on mock face: " + e);
+        }
+      }
+    });
+
+    Exception exception = assertThrows(ManagementException.class, () -> {
+                                         Nfdc.setStrategy(mockFace, new Name("/"), Strategies.MULTICAST);
+                                       });
+    assertEquals("Action failed, forwarder returned: 400 test error", exception.getMessage());
+  }
+
+  @Test
+  public void testFailOfUnsetStrategy() throws Exception {
+    mockFace.onSendInterest.add(replyWithEmptyData);
+    assertThrows(ManagementException.class, () -> Nfdc.unsetStrategy(mockFace, new Name("/")));
+  }
+}