Add unit test for InstallableIntents

The following classes are covered:

- PathFlowIntent
- SingleSrcTreeFlowIntent
- SingleDstTreeFlowIntent

Change-Id: I5af927e0ea1bfc5929cb6ea96176b9f9e1a3f72e
diff --git a/src/test/java/net/onrc/onos/api/newintent/ConnectivityIntentTest.java b/src/test/java/net/onrc/onos/api/newintent/ConnectivityIntentTest.java
index 602ae0e..be84f25 100644
--- a/src/test/java/net/onrc/onos/api/newintent/ConnectivityIntentTest.java
+++ b/src/test/java/net/onrc/onos/api/newintent/ConnectivityIntentTest.java
@@ -5,18 +5,12 @@
 import net.onrc.onos.core.matchaction.match.PacketMatchBuilder;
 import net.onrc.onos.core.util.SwitchPort;
 
-import org.junit.Test;
-
-import java.util.Arrays;
-import java.util.HashSet;
 import java.util.Set;
 
-import static org.junit.Assert.*;
-
 /**
  * Base facilities to test various connectivity tests.
  */
-public abstract class ConnectivityIntentTest {
+public abstract class ConnectivityIntentTest extends IntentTest {
 
     public static final IntentId IID = new IntentId(123);
     public static final Match MATCH = (new PacketMatchBuilder()).build();
@@ -28,59 +22,4 @@
 
     public static final Set<SwitchPort> PS1 = itemSet(new SwitchPort[]{P1, P3});
     public static final Set<SwitchPort> PS2 = itemSet(new SwitchPort[]{P2, P3});
-
-
-    @Test
-    public void equalsAndHashCode() {
-        Intent one = createOne();
-        Intent like = createOne();
-        Intent another = createAnother();
-
-        assertTrue("should be equal", one.equals(like));
-        assertEquals("incorrect hashCode", one.hashCode(), like.hashCode());
-
-        assertFalse("should not be equal", one.equals(another));
-
-        assertFalse("should not be equal", one.equals(null));
-        assertFalse("should not be equal", one.equals("foo"));
-    }
-
-    @Test
-    public void testToString() {
-        Intent one = createOne();
-        Intent like = createOne();
-        assertEquals("incorrect toString", one.toString(), like.toString());
-    }
-
-    /**
-     * Creates a new intent, but always a like intent, i.e. all instances will
-     * be equal, but should not be the same.
-     *
-     * @return intent
-     */
-    protected abstract Intent createOne();
-
-    /**
-     * Creates another intent, not equals to the one created by
-     * {@link #createOne()} and with a different hash code.
-     *
-     * @return another intent
-     */
-    protected abstract Intent createAnother();
-
-
-    /**
-     * Produces a set of items from the supplied items.
-     *
-     * @param items items to be placed in set
-     * @param <T>   item type
-     * @return set of items
-     */
-    private static <T> Set<T> itemSet(T[] items) {
-        return new HashSet<>(Arrays.asList(items));
-    }
-
-    // TODO: move to the match-action related package
-    private static class NoAction implements Action {
-    }
 }
diff --git a/src/test/java/net/onrc/onos/api/newintent/IntentTest.java b/src/test/java/net/onrc/onos/api/newintent/IntentTest.java
new file mode 100644
index 0000000..dd83194
--- /dev/null
+++ b/src/test/java/net/onrc/onos/api/newintent/IntentTest.java
@@ -0,0 +1,70 @@
+package net.onrc.onos.api.newintent;
+
+import net.onrc.onos.core.matchaction.action.Action;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Base facilities to test various intent tests.
+ */
+public abstract class IntentTest {
+    /**
+     * Produces a set of items from the supplied items.
+     *
+     * @param items items to be placed in set
+     * @param <T>   item type
+     * @return set of items
+     */
+    protected static <T> Set<T> itemSet(T[] items) {
+        return new HashSet<>(Arrays.asList(items));
+    }
+
+    @Test
+    public void equalsAndHashCode() {
+        Intent one = createOne();
+        Intent like = createOne();
+        Intent another = createAnother();
+
+        assertTrue("should be equal", one.equals(like));
+        assertEquals("incorrect hashCode", one.hashCode(), like.hashCode());
+
+        assertFalse("should not be equal", one.equals(another));
+
+        assertFalse("should not be equal", one.equals(null));
+        assertFalse("should not be equal", one.equals("foo"));
+    }
+
+    @Test
+    public void testToString() {
+        Intent one = createOne();
+        Intent like = createOne();
+        assertEquals("incorrect toString", one.toString(), like.toString());
+    }
+
+    /**
+     * Creates a new intent, but always a like intent, i.e. all instances will
+     * be equal, but should not be the same.
+     *
+     * @return intent
+     */
+    protected abstract Intent createOne();
+
+    /**
+     * Creates another intent, not equals to the one created by
+     * {@link #createOne()} and with a different hash code.
+     *
+     * @return another intent
+     */
+    protected abstract Intent createAnother();
+
+    // TODO: move to the match-action related package
+    protected static class NoAction implements Action {
+    }
+}
diff --git a/src/test/java/net/onrc/onos/core/newintent/PathFlowIntentTest.java b/src/test/java/net/onrc/onos/core/newintent/PathFlowIntentTest.java
new file mode 100644
index 0000000..6c83e86
--- /dev/null
+++ b/src/test/java/net/onrc/onos/core/newintent/PathFlowIntentTest.java
@@ -0,0 +1,53 @@
+package net.onrc.onos.core.newintent;
+
+import net.onrc.onos.api.flowmanager.FlowId;
+import net.onrc.onos.api.flowmanager.PacketPathFlow;
+import net.onrc.onos.api.flowmanager.Path;
+import net.onrc.onos.api.newintent.IntentId;
+import net.onrc.onos.api.newintent.IntentTest;
+import net.onrc.onos.core.matchaction.action.Action;
+import net.onrc.onos.core.matchaction.match.PacketMatch;
+import net.onrc.onos.core.matchaction.match.PacketMatchBuilder;
+import net.onrc.onos.core.util.PortNumber;
+
+import java.util.Collections;
+
+/**
+ * Suite of tests of {@link PathFlowIntent}.
+ */
+public class PathFlowIntentTest extends IntentTest {
+
+    private final IntentId intentId1 = new IntentId(123);
+    private final IntentId intentId2 = new IntentId(456);
+    private final FlowId flowId1 = new FlowId("path1");
+    private final PacketMatch match = new PacketMatchBuilder().build();
+    private final PortNumber port = new PortNumber((short) 1);
+
+    /**
+     * Creates a PathFlowIntent.
+     *
+     * @return PathFlowIntent
+     */
+    @Override
+    protected PathFlowIntent createOne() {
+        return new PathFlowIntent(
+                intentId1,
+                new PacketPathFlow(flowId1, match, port,
+                        new Path(), Collections.<Action>emptyList(), 0, 0)
+        );
+    }
+
+    /**
+     * Creates another PathFlowIntent, which is different from the intent created by {@link #createOne()}.
+     *
+     * @return another PathFlowIntent
+     */
+    @Override
+    protected PathFlowIntent createAnother() {
+        return new PathFlowIntent(
+                intentId2,
+                new PacketPathFlow(flowId1, match, port,
+                        new Path(), Collections.<Action>emptyList(), 0, 0)
+        );
+    }
+}
diff --git a/src/test/java/net/onrc/onos/core/newintent/SingleDstTreeFlowIntentTest.java b/src/test/java/net/onrc/onos/core/newintent/SingleDstTreeFlowIntentTest.java
new file mode 100644
index 0000000..4ad202e
--- /dev/null
+++ b/src/test/java/net/onrc/onos/core/newintent/SingleDstTreeFlowIntentTest.java
@@ -0,0 +1,61 @@
+package net.onrc.onos.core.newintent;
+
+import net.onrc.onos.api.flowmanager.FlowId;
+import net.onrc.onos.api.flowmanager.FlowLink;
+import net.onrc.onos.api.flowmanager.SingleDstTreeFlow;
+import net.onrc.onos.api.flowmanager.Tree;
+import net.onrc.onos.api.newintent.IntentId;
+import net.onrc.onos.api.newintent.IntentTest;
+import net.onrc.onos.core.matchaction.action.Action;
+import net.onrc.onos.core.matchaction.action.OutputAction;
+import net.onrc.onos.core.matchaction.match.PacketMatch;
+import net.onrc.onos.core.matchaction.match.PacketMatchBuilder;
+import net.onrc.onos.core.util.PortNumber;
+import net.onrc.onos.core.util.SwitchPort;
+
+import java.util.Arrays;
+
+/**
+ * Suites of test of {@link SingleDstTreeFlowIntent}.
+ */
+public class SingleDstTreeFlowIntentTest extends IntentTest {
+
+    private final IntentId intentId1 = new IntentId(1L);
+    private final IntentId intentId2 = new IntentId(2L);
+    private final FlowId flowId1 = new FlowId("intent1");
+    private final PacketMatch match = new PacketMatchBuilder().build();
+    private final short port1 = (short) 1;
+    private final short port2 = (short) 2;
+    private final short port3 = (short) 3;
+    private final SwitchPort switchPort1 = new SwitchPort(1, port1);
+    private final SwitchPort switchPort2 = new SwitchPort(2, port2);
+    private final SwitchPort switchPort3 = new SwitchPort(3, port3);
+
+    @Override
+    protected SingleDstTreeFlowIntent createOne() {
+        SingleDstTreeFlow treeFlow = new SingleDstTreeFlow(flowId1,
+                match,
+                Arrays.asList(switchPort1, switchPort2),
+                createTree(),
+                Arrays.asList((Action) new OutputAction(new PortNumber(port3))));
+        return new SingleDstTreeFlowIntent(intentId1, treeFlow);
+    }
+
+    @Override
+    protected SingleDstTreeFlowIntent createAnother() {
+        SingleDstTreeFlow treeFlow = new SingleDstTreeFlow(flowId1,
+                match,
+                Arrays.asList(switchPort1, switchPort3),
+                createTree(),
+                Arrays.asList((Action) new OutputAction(new PortNumber(port2))));
+        return new SingleDstTreeFlowIntent(intentId2, treeFlow);
+    }
+
+    private Tree createTree() {
+        Tree tree = new Tree();
+        tree.add(new FlowLink(switchPort1, switchPort2));
+        tree.add(new FlowLink(switchPort1, switchPort3));
+
+        return tree;
+    }
+}
diff --git a/src/test/java/net/onrc/onos/core/newintent/SingleSrcTreeFlowIntentTest.java b/src/test/java/net/onrc/onos/core/newintent/SingleSrcTreeFlowIntentTest.java
new file mode 100644
index 0000000..3330e47
--- /dev/null
+++ b/src/test/java/net/onrc/onos/core/newintent/SingleSrcTreeFlowIntentTest.java
@@ -0,0 +1,72 @@
+package net.onrc.onos.core.newintent;
+
+import net.onrc.onos.api.flowmanager.FlowId;
+import net.onrc.onos.api.flowmanager.FlowLink;
+import net.onrc.onos.api.flowmanager.SingleSrcTreeFlow;
+import net.onrc.onos.api.flowmanager.Tree;
+import net.onrc.onos.api.newintent.IntentId;
+import net.onrc.onos.api.newintent.IntentTest;
+import net.onrc.onos.core.matchaction.action.OutputAction;
+import net.onrc.onos.core.matchaction.match.PacketMatch;
+import net.onrc.onos.core.matchaction.match.PacketMatchBuilder;
+import net.onrc.onos.core.util.Dpid;
+import net.onrc.onos.core.util.Pair;
+import net.onrc.onos.core.util.PortNumber;
+import net.onrc.onos.core.util.SwitchPort;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Suites of test of {@link SingleSrcTreeFlowIntent}.
+ */
+public class SingleSrcTreeFlowIntentTest extends IntentTest {
+
+    private final IntentId intentId1 = new IntentId(1L);
+    private final IntentId intentId2 = new IntentId(2L);
+    private final FlowId flowId1 = new FlowId("tree1");
+    private final FlowId flowId2 = new FlowId("tree2");
+    private final Dpid dpid1 = new Dpid(1);
+    private final Dpid dpid2 = new Dpid(2);
+    private final Dpid dpid3 = new Dpid(3);
+    private final PortNumber port1 = PortNumber.uint32(1);
+    private final PortNumber port2 = PortNumber.uint32(2);
+    private final PortNumber port3 = PortNumber.uint32(3);
+    private final OutputAction action1 = new OutputAction(port1);
+    private final OutputAction action2 = new OutputAction(port2);
+    private final OutputAction action3 = new OutputAction(port3);
+    private final PacketMatch match = new PacketMatchBuilder().build();
+
+    @Override
+    protected SingleSrcTreeFlowIntent createOne() {
+        Set<Pair<Dpid, OutputAction>> actions = new HashSet<>(Arrays.asList(
+                new Pair<>(dpid2, action2),
+                new Pair<>(dpid3, action3)
+        ));
+        SingleSrcTreeFlow tree = new SingleSrcTreeFlow(flowId1, match,
+                new SwitchPort(dpid1, port3), createTree(), actions
+        );
+        return new SingleSrcTreeFlowIntent(intentId1, tree);
+    }
+
+    @Override
+    protected SingleSrcTreeFlowIntent createAnother() {
+        Set<Pair<Dpid, OutputAction>> actions = new HashSet<>(Arrays.asList(
+                new Pair<>(dpid1, action1),
+                new Pair<>(dpid3, action3)
+        ));
+        SingleSrcTreeFlow tree = new SingleSrcTreeFlow(flowId2, match,
+                new SwitchPort(dpid2, port3), createTree(), actions
+        );
+        return new SingleSrcTreeFlowIntent(intentId2, tree);
+    }
+
+    private Tree createTree() {
+        Tree tree = new Tree();
+        tree.add(new FlowLink(dpid1, port1, dpid2, port2));
+        tree.add(new FlowLink(dpid1, port2, dpid3, port3));
+
+        return tree;
+    }
+}