Added unit tests for OpenstackNetworkManager and OpenstackRouterManager

Change-Id: I47ccd99331cffd03a83581c9e19b14971c690d7f
diff --git a/apps/openstacknetworking/BUCK b/apps/openstacknetworking/BUCK
index 86ec137..a3c8489 100644
--- a/apps/openstacknetworking/BUCK
+++ b/apps/openstacknetworking/BUCK
@@ -9,8 +9,15 @@
   '//lib:openstack4j-core',
 ]
 
+TEST_DEPS = [
+    '//lib:TEST_ADAPTERS',
+    '//core/api:onos-api-tests',
+    '//core/common:onos-core-common-tests',
+]
+
 osgi_jar_with_tests (
   deps = COMPILE_DEPS,
+  test_deps = TEST_DEPS,
   web_context = '/onos/openstacknetworking',
   api_title = 'OpenStack Networking App API',
   api_version = '1.0',
diff --git a/apps/openstacknetworking/pom.xml b/apps/openstacknetworking/pom.xml
index 6d0bd2c..8db7418 100644
--- a/apps/openstacknetworking/pom.xml
+++ b/apps/openstacknetworking/pom.xml
@@ -99,6 +99,18 @@
         </dependency>
         <dependency>
             <groupId>org.onosproject</groupId>
+            <artifactId>onos-api</artifactId>
+            <classifier>tests</classifier>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-core-common</artifactId>
+            <classifier>tests</classifier>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
             <artifactId>onos-app-openstacknode</artifactId>
             <version>${project.version}</version>
         </dependency>
diff --git a/apps/openstacknetworking/src/test/java/org/onosproject/openstacknetworking/impl/OpenstackNetworkManagerTest.java b/apps/openstacknetworking/src/test/java/org/onosproject/openstacknetworking/impl/OpenstackNetworkManagerTest.java
new file mode 100644
index 0000000..a33f626
--- /dev/null
+++ b/apps/openstacknetworking/src/test/java/org/onosproject/openstacknetworking/impl/OpenstackNetworkManagerTest.java
@@ -0,0 +1,593 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstacknetworking.impl;
+
+import com.google.common.collect.Lists;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.junit.TestTools;
+import org.onlab.junit.TestUtils;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.event.Event;
+import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent;
+import org.onosproject.openstacknetworking.api.OpenstackNetworkListener;
+import org.onosproject.store.service.TestStorageService;
+import org.openstack4j.model.network.Network;
+import org.openstack4j.model.network.Port;
+import org.openstack4j.model.network.Subnet;
+import org.openstack4j.openstack.networking.domain.NeutronNetwork;
+import org.openstack4j.openstack.networking.domain.NeutronPort;
+import org.openstack4j.openstack.networking.domain.NeutronSubnet;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.*;
+
+/**
+ * Unit tests for OpenStack network manager.
+ */
+public class OpenstackNetworkManagerTest {
+
+    private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
+
+    private static final String UNKNOWN_ID = "unknown_id";
+    private static final String UPDATED_NAME = "updated_name";
+
+    private static final String NETWORK_ID = "network_id";
+    private static final String NETWORK_NAME = "network_name";
+    private static final Network NETWORK = NeutronNetwork.builder()
+            .name(NETWORK_NAME)
+            .build();
+    private static final Network NETWORK_COPY = NeutronNetwork.builder()
+            .name("network")
+            .build();
+
+    private static final String SUBNET_ID = "subnet_id";
+    private static final Subnet SUBNET = NeutronSubnet.builder()
+            .networkId(NETWORK_ID)
+            .cidr("192.168.0.0/24")
+            .build();
+    private static final Subnet SUBNET_COPY = NeutronSubnet.builder()
+            .networkId(NETWORK_ID)
+            .cidr("192.168.0.0/24")
+            .build();
+
+    private static final String PORT_ID = "port_id";
+    private static final Port PORT = NeutronPort.builder()
+            .networkId(NETWORK_ID)
+            .fixedIp("192.168.0.1", SUBNET_ID)
+            .build();
+    private static final Port PORT_COPY = NeutronPort.builder()
+            .networkId(NETWORK_ID)
+            .fixedIp("192.168.0.1", SUBNET_ID)
+            .build();
+
+    private final TestOpenstackNetworkListener testListener = new TestOpenstackNetworkListener();
+
+    private OpenstackNetworkManager target;
+    private DistributedOpenstackNetworkStore osNetworkStore;
+
+    @Before
+    public void setUp() throws Exception {
+        NETWORK.setId(NETWORK_ID);
+        NETWORK_COPY.setId(NETWORK_ID);
+        SUBNET.setId(SUBNET_ID);
+        SUBNET_COPY.setId(SUBNET_ID);
+        PORT.setId(PORT_ID);
+        PORT_COPY.setId(PORT_ID);
+
+        osNetworkStore = new DistributedOpenstackNetworkStore();
+        TestUtils.setField(osNetworkStore, "coreService", new TestCoreService());
+        TestUtils.setField(osNetworkStore, "storageService", new TestStorageService());
+        osNetworkStore.activate();
+
+        target = new OpenstackNetworkManager();
+        target.coreService = new TestCoreService();
+        target.osNetworkStore = osNetworkStore;
+        target.addListener(testListener);
+        target.activate();
+    }
+
+    @After
+    public void tearDown() {
+        target.removeListener(testListener);
+        osNetworkStore.deactivate();
+        target.deactivate();
+        osNetworkStore = null;
+        target = null;
+    }
+
+    /**
+     * Tests if getting all networks returns the correct set of networks.
+     */
+    @Test
+    public void testGetNetworks() {
+        createBasicNetworks();
+        assertEquals("Number of network did not match", 1, target.networks().size());
+    }
+
+    /**
+     * Tests if getting a network with ID returns the correct network.
+     */
+    @Test
+    public void testGetNetworkById() {
+        createBasicNetworks();
+        assertTrue("Network did not match", target.network(NETWORK_ID) != null);
+        assertTrue("Network did not match", target.network(UNKNOWN_ID) == null);
+    }
+
+    /**
+     * Tests creating and removing a network, and checks if it triggers proper events.
+     */
+    @Test
+    public void testCreateAndRemoveNetwork() {
+        target.createNetwork(NETWORK);
+        assertEquals("Number of networks did not match", 1, target.networks().size());
+        assertTrue("Network was not created", target.network(NETWORK_ID) != null);
+
+        target.removeNetwork(NETWORK_ID);
+        assertEquals("Number of networks did not match", 0, target.networks().size());
+        assertTrue("Network was not removed", target.network(NETWORK_ID) == null);
+
+        validateEvents(OPENSTACK_NETWORK_CREATED, OPENSTACK_NETWORK_REMOVED);
+    }
+
+    /**
+     * Tests updating a network, and checks if it triggers proper events.
+     */
+    @Test
+    public void testCreateAndUpdateNetwork() {
+        target.createNetwork(NETWORK);
+        assertEquals("Number of networks did not match", 1, target.networks().size());
+        assertEquals("Network did not match", NETWORK_NAME, target.network(NETWORK_ID).getName());
+
+        final Network updated = NeutronNetwork.builder()
+                .from(NETWORK_COPY)
+                .name(UPDATED_NAME)
+                .build();
+        target.updateNetwork(updated);
+
+        assertEquals("Number of networks did not match", 1, target.networks().size());
+        assertEquals("Network did not match", UPDATED_NAME, target.network(NETWORK_ID).getName());
+        validateEvents(OPENSTACK_NETWORK_CREATED, OPENSTACK_NETWORK_UPDATED);
+    }
+
+    /**
+     * Tests if creating a null network fails with an exception.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testCreateNullNetwork() {
+        target.createNetwork(null);
+    }
+
+    /**
+     * Tests if creating a network with null ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateNetworkWithNullId() {
+        final Network testNet = NeutronNetwork.builder().build();
+        target.createNetwork(testNet);
+    }
+
+    /**
+     * Tests if creating a network with null name fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateNetworkWithNullName() {
+        final Network testNet = NeutronNetwork.builder().build();
+        testNet.setId(NETWORK_ID);
+        target.createNetwork(testNet);
+    }
+
+    /**
+     * Tests if creating a duplicate network fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateDuplicateNetwork() {
+        target.createNetwork(NETWORK);
+        target.createNetwork(NETWORK);
+    }
+
+    /**
+     * Tests if removing network with null ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testRemoveNetworkWithNull() {
+        target.removeNetwork(null);
+    }
+
+    /**
+     * Tests if updating a network with null name fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateNetworkWithNullName() {
+        final Network updated = NeutronNetwork.builder()
+                .name(null)
+                .build();
+        updated.setId(NETWORK_ID);
+        target.updateNetwork(updated);
+    }
+
+    /**
+     * Tests if updating an unregistered network fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateUnregisteredNetwork() {
+        target.updateNetwork(NETWORK);
+    }
+
+    /**
+     * Tests if updating a network with null ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateNetworkWithNullId() {
+        final Network testNet = NeutronNetwork.builder().build();
+        target.updateNetwork(testNet);
+    }
+
+
+    /**
+     * Tests if getting all subnets returns the correct set of subnets.
+     */
+    @Test
+    public void testGetSubnets() {
+        createBasicNetworks();
+        assertEquals("Number of subnet did not match", 1, target.subnets().size());
+    }
+
+    @Test
+    public void testGetSubnetsByNetworkId() {
+        createBasicNetworks();
+        assertEquals("Subnet did not match", 1, target.subnets(NETWORK_ID).size());
+        assertEquals("Subnet did not match", 0, target.subnets(UNKNOWN_ID).size());
+    }
+
+    /**
+     * Tests if getting a subnet with ID returns the correct subnet.
+     */
+    @Test
+    public void testGetSubnetById() {
+        createBasicNetworks();
+        assertTrue("Subnet did not match", target.subnet(SUBNET_ID) != null);
+        assertTrue("Subnet did not match", target.subnet(UNKNOWN_ID) == null);
+    }
+
+    /**
+     * Tests creating and removing a subnet, and checks if it triggers proper events.
+     */
+    @Test
+    public void testCreateAndRemoveSubnet() {
+        target.createSubnet(SUBNET);
+        assertEquals("Number of subnet did not match", 1, target.subnets().size());
+        assertTrue("Subnet was not created", target.subnet(SUBNET_ID) != null);
+
+        target.removeSubnet(SUBNET_ID);
+        assertEquals("Number of subnet did not match", 0, target.subnets().size());
+        assertTrue("Subnet was not removed", target.subnet(SUBNET_ID) == null);
+
+        validateEvents(OPENSTACK_SUBNET_CREATED, OPENSTACK_SUBNET_REMOVED);
+    }
+
+    /**
+     * Tests updating a subnet, and checks if it triggers proper events.
+     */
+    @Test
+    public void testCreateAndUpdateSubnet() {
+        target.createSubnet(SUBNET_COPY);
+        assertEquals("Number of subnet did not match", 1, target.subnets().size());
+        assertEquals("Subnet did not match", null, target.subnet(SUBNET_ID).getName());
+
+        // TODO fix NeutronSubnet.builder().from() in openstack4j
+        final Subnet updated = NeutronSubnet.builder()
+                .networkId(NETWORK_ID)
+                .cidr("192.168.0.0/24")
+                .name(UPDATED_NAME)
+                .build();
+        updated.setId(SUBNET_ID);
+        target.updateSubnet(updated);
+
+        assertEquals("Number of subnet did not match", 1, target.subnets().size());
+        assertEquals("Subnet did not match", UPDATED_NAME, target.subnet(SUBNET_ID).getName());
+
+        validateEvents(OPENSTACK_SUBNET_CREATED, OPENSTACK_SUBNET_UPDATED);
+    }
+
+    /**
+     * Tests if creating a null subnet fails with an exception.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testCreateNullSubnet() {
+        target.createSubnet(null);
+    }
+
+    /**
+     * Tests if creating a subnet with null ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateSubnetWithNullId() {
+        final Subnet testSubnet = NeutronSubnet.builder()
+                .networkId(NETWORK_ID)
+                .cidr("192.168.0.0/24")
+                .build();
+        target.createSubnet(testSubnet);
+    }
+
+    /**
+     * Tests if creating subnet with null network ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateSubnetWithNullNetworkId() {
+        final Subnet testSubnet = NeutronSubnet.builder()
+                .cidr("192.168.0.0/24")
+                .build();
+        testSubnet.setId(SUBNET_ID);
+        target.createSubnet(testSubnet);
+    }
+
+    /**
+     * Tests if creating a subnet with null CIDR fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateSubnetWithNullCidr() {
+        final Subnet testSubnet = NeutronSubnet.builder()
+                .networkId(NETWORK_ID)
+                .build();
+        testSubnet.setId(SUBNET_ID);
+        target.createSubnet(testSubnet);
+    }
+
+    /**
+     * Tests if creating a duplicate subnet fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateDuplicateSubnet() {
+        target.createSubnet(SUBNET);
+        target.createSubnet(SUBNET);
+    }
+
+    /**
+     * Tests if updating an unregistered subnet fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateUnregisteredSubnet() {
+        target.updateSubnet(SUBNET);
+    }
+
+    /**
+     * Tests if updating a null subnet fails with an exception.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testUpdateSubnetWithNull() {
+        target.updateSubnet(null);
+    }
+
+    /**
+     * Tests if updating a subnet with null ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateSubnetWithNullId() {
+        final Subnet testSubnet = NeutronSubnet.builder()
+                .networkId(NETWORK_ID)
+                .cidr("192.168.0.0/24")
+                .build();
+        target.updateSubnet(testSubnet);
+    }
+
+    /**
+     * Tests if updating a subnet with null network ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateSubnetWithNullNetworkId() {
+        final Subnet testSubnet = NeutronSubnet.builder()
+                .cidr("192.168.0.0/24")
+                .build();
+        testSubnet.setId(SUBNET_ID);
+        target.updateSubnet(testSubnet);
+    }
+
+    /**
+     * Tests if updating a subnet with null CIDR fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateSubnetWithNullCidr() {
+        final Subnet testSubnet = NeutronSubnet.builder()
+                .networkId(NETWORK_ID)
+                .build();
+        testSubnet.setId(SUBNET_ID);
+        target.updateSubnet(testSubnet);
+    }
+
+    /**
+     * Tests if getting all ports returns correct set of values.
+     */
+    @Test
+    public void testGetPorts() {
+        createBasicNetworks();
+        assertEquals("Number of port did not match", 1, target.ports().size());
+    }
+
+    /**
+     * Tests if getting a port with network ID returns correct set of values.
+     */
+    @Test
+    public void testGetPortsByNetworkId() {
+        createBasicNetworks();
+        assertEquals("Number of port did not match", 1, target.ports(NETWORK_ID).size());
+        assertEquals("Number of port did not match", 0, target.ports(UNKNOWN_ID).size());
+    }
+
+    /**
+     * Tests if getting a port with ID returns correct value.
+     */
+    @Test
+    public void testGetPortById() {
+        createBasicNetworks();
+        assertTrue("Port did not match", target.port(PORT_ID) != null);
+        assertTrue("Port did not match", target.port(UNKNOWN_ID) == null);
+    }
+
+    /**
+     * Tests creating and removing a port, and checks if proper event is triggered.
+     */
+    @Test
+    public void testCreateAndRemovePort() {
+        target.createPort(PORT);
+        assertEquals("Number of port did not match", 1, target.ports().size());
+        assertTrue("Port was not created", target.port(PORT_ID) != null);
+
+        target.removePort(PORT_ID);
+        assertEquals("Number of port did not match", 0, target.ports().size());
+        assertTrue("Port was not created", target.port(PORT_ID) == null);
+
+        validateEvents(OPENSTACK_PORT_CREATED, OPENSTACK_PORT_REMOVED);
+    }
+
+    /**
+     * Tests creating and updating a port, and checks if proper event is triggered.
+     */
+    @Test
+    public void testCreateAndUpdatePort() {
+        target.createPort(PORT);
+        assertEquals("Number of port did not match", 1, target.ports().size());
+        assertEquals("Port did not match", null, target.port(PORT_ID).getName());
+
+        final Port updated = NeutronPort.builder()
+                .from(PORT_COPY)
+                .name(UPDATED_NAME)
+                .build();
+        target.updatePort(updated);
+
+        assertEquals("Number of port did not match", 1, target.ports().size());
+        assertEquals("Port did not match", UPDATED_NAME, target.port(PORT_ID).getName());
+
+        validateEvents(OPENSTACK_PORT_CREATED, OPENSTACK_PORT_UPDATED);
+    }
+
+    /**
+     * Tests if creating a null port fails with an exception.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testCreateNullPort() {
+        target.createPort(null);
+    }
+
+    /**
+     * Tests if creating a port with null ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreatePortWithNullId() {
+        final Port testPort = NeutronPort.builder()
+                .networkId(NETWORK_ID)
+                .build();
+        target.createPort(testPort);
+    }
+
+    /**
+     * Tests if creating a port with null network ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreatePortWithNullNetworkId() {
+        final Port testPort = NeutronPort.builder().build();
+        testPort.setId(PORT_ID);
+        target.createPort(testPort);
+    }
+
+    /**
+     * Tests if creating a duplicate port fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void createDuplicatePort() {
+        target.createPort(PORT);
+        target.createPort(PORT);
+    }
+
+    /**
+     * Tests if updating an unregistered port fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateUnregisteredPort() {
+        target.updatePort(PORT);
+    }
+
+    /**
+     * Tests if updating a null port fails with an exception.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testUpdateNullPort() {
+        target.updatePort(null);
+    }
+
+    /**
+     * Tests if updating a port with null ID fails with exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdatePortWithNullId() {
+        final Port testPort = NeutronPort.builder()
+                .networkId(NETWORK_ID)
+                .build();
+        target.updatePort(testPort);
+    }
+
+    /**
+     * Tests if updating a port with null network ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdatePortWithNullNetworkId() {
+        final Port testPort = NeutronPort.builder().build();
+        testPort.setId(PORT_ID);
+        target.updatePort(testPort);
+    }
+
+    private void createBasicNetworks() {
+        target.createNetwork(NETWORK);
+        target.createSubnet(SUBNET);
+        target.createPort(PORT);
+    }
+
+    private static class TestCoreService extends CoreServiceAdapter {
+
+        @Override
+        public ApplicationId registerApplication(String name) {
+            return TEST_APP_ID;
+        }
+    }
+
+    private static class TestOpenstackNetworkListener implements OpenstackNetworkListener {
+        private List<OpenstackNetworkEvent> events = Lists.newArrayList();
+
+        @Override
+        public void event(OpenstackNetworkEvent event) {
+            events.add(event);
+        }
+    }
+
+    private void validateEvents(Enum... types) {
+        TestTools.assertAfter(100, () -> {
+            int i = 0;
+            assertEquals("Number of events did not match", types.length, testListener.events.size());
+            for (Event event : testListener.events) {
+                assertEquals("Incorrect event received", types[i], event.type());
+                i++;
+            }
+            testListener.events.clear();
+        });
+    }
+}
\ No newline at end of file
diff --git a/apps/openstacknetworking/src/test/java/org/onosproject/openstacknetworking/impl/OpenstackRouterManagerTest.java b/apps/openstacknetworking/src/test/java/org/onosproject/openstacknetworking/impl/OpenstackRouterManagerTest.java
new file mode 100644
index 0000000..9d47fa5
--- /dev/null
+++ b/apps/openstacknetworking/src/test/java/org/onosproject/openstacknetworking/impl/OpenstackRouterManagerTest.java
@@ -0,0 +1,283 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.openstacknetworking.impl;
+
+import com.google.common.collect.Lists;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.junit.TestTools;
+import org.onlab.junit.TestUtils;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.event.Event;
+import org.onosproject.openstacknetworking.api.OpenstackRouterEvent;
+import org.onosproject.openstacknetworking.api.OpenstackRouterListener;
+import org.onosproject.store.service.TestStorageService;
+import org.openstack4j.model.network.Router;
+import org.openstack4j.openstack.networking.domain.NeutronRouter;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.onosproject.openstacknetworking.api.OpenstackRouterEvent.Type.*;
+
+/**
+ * Unit tests for OpenStack router manager.
+ */
+public class OpenstackRouterManagerTest {
+
+    private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
+
+    private static final String UNKNOWN_ID = "unknown_id";
+    private static final String ROUTER_ID = "router_1";
+    private static final String ROUTER_NAME = "router_1";
+    private static final Router ROUTER = NeutronRouter.builder()
+            .id(ROUTER_ID)
+            .name(ROUTER_NAME)
+            .build();
+
+    private final TestOpenstackRouterListener testListener = new TestOpenstackRouterListener();
+
+    private OpenstackRouterManager target;
+    private DistributedOpenstackRouterStore osRouterStore;
+
+    @Before
+    public void setUp() throws Exception {
+        osRouterStore = new DistributedOpenstackRouterStore();
+        TestUtils.setField(osRouterStore, "coreService", new TestCoreService());
+        TestUtils.setField(osRouterStore, "storageService", new TestStorageService());
+        osRouterStore.activate();
+
+        target = new OpenstackRouterManager();
+        target.coreService = new TestCoreService();
+        target.osRouterStore = osRouterStore;
+        target.addListener(testListener);
+        target.activate();
+    }
+
+    @After
+    public void tearDown() {
+        target.removeListener(testListener);
+        osRouterStore.deactivate();
+        target.deactivate();
+        osRouterStore = null;
+        target = null;
+    }
+
+    /**
+     * Tests if getting all routers returns correct set of values.
+     */
+    @Test
+    public void testGetRouters() {
+        createBasicRouters();
+        assertEquals("Number of router did not match", 1, target.routers().size());
+    }
+
+    /**
+     * Tests if getting a router with ID returns correct value.
+     */
+    @Test
+    public void testGetRouterById() {
+        createBasicRouters();
+        assertTrue("Router did not exist", target.router(ROUTER_ID) != null);
+        assertTrue("Router did not exist", target.router(UNKNOWN_ID) == null);
+    }
+
+    /**
+     * Tests creating and removing a router, and checks if proper event is triggered.
+     */
+    @Test
+    public void testCreateAndRemoveRouter() {
+        target.createRouter(ROUTER);
+        assertEquals("Number of router did not match", 1, target.routers().size());
+        assertTrue("Router was not created", target.router(ROUTER_ID) != null);
+
+        target.removeRouter(ROUTER.getId());
+        assertEquals("Number of router did not match", 0, target.routers().size());
+        assertTrue("Router was not removed", target.router(ROUTER_ID) == null);
+
+        validateEvents(OPENSTACK_ROUTER_CREATED, OPENSTACK_ROUTER_REMOVED);
+    }
+
+    /**
+     * Tests creating and updating a router, and checks if proper event is triggered.
+     */
+    @Test
+    public void testCreateAndUpdateRouter() {
+        target.createRouter(ROUTER);
+        assertEquals("Number of router did not match", 1, target.routers().size());
+        assertEquals("Router did not match", ROUTER_NAME, target.router(ROUTER_ID).getName());
+
+        final Router updated = NeutronRouter.builder()
+                .id(ROUTER_ID)
+                .name("updated-name")
+                .build();
+
+        target.updateRouter(updated);
+        assertEquals("Number of router did not match", 1, target.routers().size());
+        assertEquals("Router did not match", "updated-name", target.router(ROUTER_ID).getName());
+
+        validateEvents(OPENSTACK_ROUTER_CREATED, OPENSTACK_ROUTER_UPDATED);
+    }
+
+    /**
+     * Tests adding and removing external gateway to a router, and checks if
+     * proper events are triggered.
+     */
+    @Test
+    public void testAddAndRemoveExternalGateway() {
+        target.createRouter(ROUTER);
+        assertTrue("Router did not match", target.router(ROUTER_ID).getExternalGatewayInfo() == null);
+
+        Router updated = NeutronRouter.builder()
+                .id(ROUTER_ID)
+                .name(ROUTER_NAME)
+                .externalGateway("test-network-id")
+                .build();
+
+        target.updateRouter(updated);
+        assertEquals("Router did not match",
+                "test-network-id",
+                target.router(ROUTER_ID).getExternalGatewayInfo().getNetworkId());
+
+        target.updateRouter(ROUTER);
+        assertTrue("Router did not match", target.router(ROUTER_ID).getExternalGatewayInfo() == null);
+
+        validateEvents(OPENSTACK_ROUTER_CREATED, OPENSTACK_ROUTER_UPDATED,
+                OPENSTACK_ROUTER_GATEWAY_ADDED,
+                OPENSTACK_ROUTER_UPDATED,
+                OPENSTACK_ROUTER_GATEWAY_REMOVED);
+    }
+
+    /**
+     * Tests if creating a router with null value fails with an exception.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testCreateRouterWithNull() {
+        target.createRouter(null);
+    }
+
+    /**
+     * Tests if creating a router with null ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateRouterWithNullId() {
+        final Router testRouter = NeutronRouter.builder()
+                .id(null)
+                .name(ROUTER_NAME)
+                .build();
+        target.createRouter(testRouter);
+    }
+
+    /**
+     * Tests if updating a router with null name fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateRouterWithNullName() {
+        final Router testRouter = NeutronRouter.builder()
+                .id(ROUTER_ID)
+                .name(null)
+                .build();
+        target.createRouter(testRouter);
+    }
+
+    /**
+     * Tests if creating a duplicate router fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateDuplicateRouter() {
+        target.createRouter(ROUTER);
+        target.createRouter(ROUTER);
+    }
+
+    /**
+     * Tests if updating a router with null value fails with an exception.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testUpdateRouterWithNull() {
+        target.updateRouter(null);
+    }
+
+    /**
+     * Tests if updating a router with null ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateRouterWithNullId() {
+        final Router testRouter = NeutronRouter.builder()
+                .id(null)
+                .name(ROUTER_NAME)
+                .build();
+        target.updateRouter(testRouter);
+    }
+
+    /**
+     * Tests if updating a router with null name fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateRouterWithNullName() {
+        final Router testRouter = NeutronRouter.builder()
+                .id(ROUTER_ID)
+                .name(null)
+                .build();
+        target.updateRouter(testRouter);
+    }
+
+    /**
+     * Tests if updating an unregistered router fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateUnregisteredRouter() {
+        target.updateRouter(ROUTER);
+    }
+
+    // TODO fix openstack4j floating IP data model and add unit tests
+
+    private void createBasicRouters() {
+        target.createRouter(ROUTER);
+    }
+
+    private static class TestCoreService extends CoreServiceAdapter {
+
+        @Override
+        public ApplicationId registerApplication(String name) {
+            return TEST_APP_ID;
+        }
+    }
+
+    private static class TestOpenstackRouterListener implements OpenstackRouterListener {
+        private List<OpenstackRouterEvent> events = Lists.newArrayList();
+
+        @Override
+        public void event(OpenstackRouterEvent event) {
+            events.add(event);
+        }
+    }
+
+    private void validateEvents(Enum... types) {
+        TestTools.assertAfter(100, () -> {
+            int i = 0;
+            assertEquals("Number of events did not match", types.length, testListener.events.size());
+            for (Event event : testListener.events) {
+                assertEquals("Incorrect event received", types[i], event.type());
+                i++;
+            }
+            testListener.events.clear();
+        });
+    }
+}