Test enhancements for DocumentPath, UiTopology, DistributedClusterStore

Change-Id: Ib4fb1e72555bde20237eef036efa24072c1ce41b
diff --git a/core/api/src/test/java/org/onosproject/store/service/DocumentPathTest.java b/core/api/src/test/java/org/onosproject/store/service/DocumentPathTest.java
index 71b274f..d267814 100644
--- a/core/api/src/test/java/org/onosproject/store/service/DocumentPathTest.java
+++ b/core/api/src/test/java/org/onosproject/store/service/DocumentPathTest.java
@@ -22,7 +22,9 @@
 
 import java.util.Arrays;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
 /**
  * Unit tests for {@link DocumentPath}.
@@ -34,10 +36,13 @@
         DocumentPath path = path("root.a.b");
         assertEquals(path.pathElements(), Arrays.asList("root", "a", "b"));
         assertEquals(path("root.a"), path.parent());
+        assertEquals(path("b"), path.childPath());
     }
 
     @Test
     public void testAncestry() {
+        DocumentPath path = path("root");
+        assertEquals(path.childPath(), null);
         DocumentPath path1 = path("root.a.b");
         DocumentPath path2 = path("root.a.d");
         DocumentPath path3 = path("root.a.b.c");
@@ -50,6 +55,32 @@
         assertFalse(path3.isDescendentOf(path2));
     }
 
+    @Rule
+    public final ExpectedException exception = ExpectedException.none();
+
+    @Test
+    public void testExceptions() {
+        DocumentPath parentPath = path("root.a.b");
+        DocumentPath path2 = exceptions("nodeName", parentPath);
+        exception.expect(IllegalDocumentNameException.class);
+        DocumentPath path1 = exceptions("node|name", parentPath);
+    }
+
+    @Test
+    public void comparePaths() {
+        DocumentPath one = path("root");
+        DocumentPath four = path("root.a.b.c.d");
+        DocumentPath difFour = path("root.e.c.b.a");
+        assertEquals(-1, one.compareTo(four));
+        assertEquals(1, four.compareTo(one));
+        assertEquals(4, difFour.compareTo(four));
+        assertEquals(0, difFour.compareTo(difFour));
+    }
+
+    private static DocumentPath exceptions(String nodeName, DocumentPath path) {
+        return new DocumentPath(nodeName, path);
+    }
+
     private static DocumentPath path(String path) {
         return DocumentPath.from(path.replace(".", DocumentPath.DEFAULT_SEPARATOR));
     }
diff --git a/core/api/src/test/java/org/onosproject/ui/model/topo/UiTopologyTest.java b/core/api/src/test/java/org/onosproject/ui/model/topo/UiTopologyTest.java
index cb6a316..d33e988 100644
--- a/core/api/src/test/java/org/onosproject/ui/model/topo/UiTopologyTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/model/topo/UiTopologyTest.java
@@ -16,25 +16,52 @@
 
 package org.onosproject.ui.model.topo;
 
+
 import org.junit.Before;
 import org.junit.Test;
+import org.onlab.packet.ChassisId;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.cluster.DefaultControllerNode;
+import org.onosproject.cluster.NodeId;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DefaultDevice;
+import org.onosproject.net.DefaultEdgeLink;
+import org.onosproject.net.DefaultHost;
 import org.onosproject.net.DefaultLink;
+import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.EdgeLink;
+import org.onosproject.net.Host;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
 import org.onosproject.net.Link;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.provider.ProviderId;
+import org.onosproject.net.region.DefaultRegion;
+import org.onosproject.net.region.Region;
 import org.onosproject.net.region.RegionId;
 import org.onosproject.ui.model.AbstractUiModelTest;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
+
+import com.google.common.collect.Sets;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 import static org.onosproject.net.DeviceId.deviceId;
 import static org.onosproject.net.PortNumber.portNumber;
 
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.is;
+
 /**
  * Unit tests for {@link UiTopology}.
  */
@@ -69,6 +96,26 @@
     private static final String REG_LINK_CLASS = "UiRegionLink";
     private static final String REG_DEV_LINK_CLASS = "UiRegionDeviceLink";
 
+    private Host host;
+    private UiHost uiHost;
+    private UiDeviceLink deviceLink;
+    private EdgeLink edgeLink;
+    private UiEdgeLink uiEdgeLink;
+    private UiClusterMember mem1;
+    private UiRegion region1;
+    private UiDevice dev1;
+    private UiLinkId linkId;
+
+    private static final ProviderId PID = new ProviderId("of", "bar");
+    private static final VlanId VID = VlanId.vlanId((short) 20);
+    private static final MacAddress MAC_ADDRESS = MacAddress.valueOf("00:11:00:00:00:01");
+    private static final HostId HID = HostId.hostId(MAC_ADDRESS, VID);
+    private static final DeviceId DID = deviceId("of:foo");
+    private static final HostLocation LOC = new HostLocation(DID, PortNumber.portNumber(8), 123L);
+    private static final ConnectPoint CP = new ConnectPoint(DEV_X, PortNumber.portNumber(8));
+    private static final Set<IpAddress> IPSET = Sets.newHashSet(IpAddress.valueOf("10.0.0.1"),
+            IpAddress.valueOf("10.0.0.2"));
+    private static final String MID = "id1";
 
     private UiTopology topo;
     private UiDeviceLink devLink;
@@ -177,4 +224,61 @@
         yBranch = branch(ROOT, R3);
         verifySynth(R3, REG_DEV_LINK_CLASS, R1.id(), DEV_Y_ID);
     }
+
+    @Test
+    public void mockTopology() {
+        host = new DefaultHost(PID, HID, MAC_ADDRESS, VID, LOC, IPSET);
+        uiHost = new UiHost(topo, host);
+        deviceLink = new UiDeviceLink(topo, DX1_DY2);
+        edgeLink = DefaultEdgeLink.createEdgeLink(CP, true);
+        linkId = UiLinkId.uiLinkId(edgeLink);
+        uiEdgeLink = new UiEdgeLink(topo, linkId);
+        mem1 = clusterMember(MID, 001);
+        region1 = region();
+        dev1 = device();
+        Set<DeviceId> deviceIds = new HashSet<>();
+        Set<HostId> hostIds = new HashSet<>();
+
+        topo.add(uiHost);
+        topo.add(mem1);
+        topo.add(region1);
+        topo.add(dev1);
+        topo.add(deviceLink);
+        topo.add(uiEdgeLink);
+
+
+        assertTrue(topo.allRegions().contains(region1));
+        assertTrue(topo.allClusterMembers().contains(mem1));
+        assertTrue(topo.findClusterMember(NodeId.nodeId("id1")).equals(mem1));
+        assertTrue(topo.findRegion(R1).equals(region1));
+        assertTrue(topo.findDevice(DID).equals(dev1));
+        assertTrue(topo.findHost(HID).equals(uiHost));
+        assertTrue(topo.findDeviceLink(DX1_DY2).equals(deviceLink));
+
+        deviceIds.add(DID);
+        assertTrue(topo.deviceSet(deviceIds).contains(dev1));
+        hostIds.add(HID);
+        assertTrue(topo.hostSet(hostIds).contains(uiHost));
+
+        topo.clear();
+        assertThat(topo.allClusterMembers(), is(empty()));
+        assertThat(topo.allDeviceLinks(), is(empty()));
+        assertThat(topo.allDevices(), is(empty()));
+        assertThat(topo.allHosts(), is(empty()));
+        assertThat(topo.allRegions(), is(empty()));
+    }
+    private UiClusterMember clusterMember(String nodeId, int ipAddress) {
+        return new UiClusterMember(topo, new DefaultControllerNode(NodeId.nodeId(nodeId),
+                IpAddress.valueOf(ipAddress)));
+    }
+    private UiRegion region() {
+        return new UiRegion(topo, (new DefaultRegion(R1, "reg1", Region.Type.METRO, DefaultAnnotations.EMPTY,
+                null)));
+    }
+    private UiDevice device() {
+        return new UiDevice(topo, new DefaultDevice(PID, DID, Device.Type.SWITCH, "whitebox",
+                "1.1.x", "3.9.1", "43331", new ChassisId()));
+    }
+
+
 }
diff --git a/core/store/dist/src/test/java/org/onosproject/store/cluster/impl/DistributedClusterStoreTest.java b/core/store/dist/src/test/java/org/onosproject/store/cluster/impl/DistributedClusterStoreTest.java
index 4d25c56..512d162 100644
--- a/core/store/dist/src/test/java/org/onosproject/store/cluster/impl/DistributedClusterStoreTest.java
+++ b/core/store/dist/src/test/java/org/onosproject/store/cluster/impl/DistributedClusterStoreTest.java
@@ -17,21 +17,107 @@
 
 import org.junit.After;
 import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cfg.ComponentConfigAdapter;
+import org.onosproject.cluster.ClusterEvent;
+import org.onosproject.cluster.ClusterMetadataServiceAdapter;
+import org.onosproject.cluster.ClusterStore;
+import org.onosproject.cluster.ClusterStoreDelegate;
+import org.onosproject.cluster.ControllerNode;
+import org.onosproject.cluster.DefaultControllerNode;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.store.cluster.messaging.impl.NettyMessagingManager;
+
+import java.util.Set;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.assertFalse;
 
 /**
  * Unit test for DistributedClusterStore.
  */
 public class DistributedClusterStoreTest {
     DistributedClusterStore distributedClusterStore;
+    ClusterStore clusterStore;
+    NodeId nodeId;
+    ControllerNode local;
+    private static final NodeId NID1 = new NodeId("foo");
+    private static final NodeId NID2 = new NodeId("bar");
+    private static final NodeId NID3 = new NodeId("buz");
+
+    private static final IpAddress IP1 = IpAddress.valueOf("127.0.0.1");
+    private static final IpAddress IP2 = IpAddress.valueOf("127.0.0.2");
+    private static final IpAddress IP3 = IpAddress.valueOf("127.0.0.3");
+
+    private static final int PORT1 = 1;
+    private static final int PORT2 = 2;
+    private  static Set<ControllerNode> nodes;
+
+
+    private TestDelegate delegate = new TestDelegate();
+    private class TestDelegate implements ClusterStoreDelegate {
+    private ClusterEvent event;
+        @Override
+        public void notify(ClusterEvent event) {
+            this.event = event;
+        }
+    }
+
+
 
     @Before
     public void setUp() throws Exception {
         distributedClusterStore = new DistributedClusterStore();
+        distributedClusterStore.clusterMetadataService = new ClusterMetadataServiceAdapter() {
+            @Override
+            public ControllerNode getLocalNode() {
+                return new DefaultControllerNode(NID1, IP1);
+            }
+        };
+        distributedClusterStore.messagingService = new NettyMessagingManager();
+        distributedClusterStore.cfgService = new ComponentConfigAdapter();
         distributedClusterStore.activate();
+        clusterStore = distributedClusterStore;
     }
 
     @After
     public void tearDown() throws Exception {
         distributedClusterStore.deactivate();
     }
+
+    @Test
+    public void testEmpty() {
+        nodeId = new NodeId("newNode");
+        assertThat(clusterStore.getNode((nodeId)), is(nullValue()));
+        assertFalse(clusterStore.hasDelegate());
+        assertThat(clusterStore.getState(nodeId), is(ControllerNode.State.INACTIVE));
+    }
+
+    @Test
+    public void addNodes() {
+
+        clusterStore.setDelegate(delegate);
+        assertThat(clusterStore.hasDelegate(), is(true));
+        clusterStore.addNode(NID1, IP1, PORT1);
+        clusterStore.addNode(NID2, IP2, PORT2);
+        clusterStore.removeNode(NID1);
+
+        assertThat(clusterStore.getNode(NID1), is(nullValue()));
+        clusterStore.addNode(NID3, IP3, PORT2);
+
+        clusterStore.markFullyStarted(true);
+        assertThat(clusterStore.getState(clusterStore.getLocalNode().id()),
+                is(ControllerNode.State.READY));
+        clusterStore.markFullyStarted(false);
+        assertThat(clusterStore.getState(clusterStore.getLocalNode().id()),
+                is(ControllerNode.State.ACTIVE));
+        nodes = clusterStore.getNodes();
+        assertThat(nodes.size(), is(2));
+        clusterStore.markFullyStarted(true);
+
+        clusterStore.unsetDelegate(delegate);
+        assertThat(clusterStore.hasDelegate(), is(false));
+    }
 }