Improve the unit test coverage for openstacknode API

Change-Id: I6ab0a00506bf1b48f29a071af8e199f3754e0d3e
diff --git a/apps/openstacknode/api/src/main/java/org/onosproject/openstacknode/api/DefaultOpenstackNode.java b/apps/openstacknode/api/src/main/java/org/onosproject/openstacknode/api/DefaultOpenstackNode.java
index ec811b3..ca4734e 100644
--- a/apps/openstacknode/api/src/main/java/org/onosproject/openstacknode/api/DefaultOpenstackNode.java
+++ b/apps/openstacknode/api/src/main/java/org/onosproject/openstacknode/api/DefaultOpenstackNode.java
@@ -334,6 +334,7 @@
                 .dpdkConfig(dpdkConfig)
                 .keystoneConfig(keystoneConfig)
                 .neutronConfig(neutronConfig)
+                .controllers(controllers)
                 .build();
     }
 
diff --git a/apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultDpdkConfigTest.java b/apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultDpdkConfigTest.java
new file mode 100644
index 0000000..d748e01
--- /dev/null
+++ b/apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultDpdkConfigTest.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.openstacknode.api;
+
+import com.google.common.collect.Lists;
+import com.google.common.testing.EqualsTester;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.List;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+import static org.onosproject.openstacknode.api.DpdkConfig.DatapathType.NETDEV;
+import static org.onosproject.openstacknode.api.DpdkConfig.DatapathType.NORMAL;
+import static org.onosproject.openstacknode.api.DpdkInterface.Type.DPDK_VHOST_USER;
+import static org.onosproject.openstacknode.api.DpdkInterface.Type.DPDK_VHOST_USER_CLIENT;
+
+/**
+ * Unit tests for DefaultDpdkConfig.
+ */
+public class DefaultDpdkConfigTest {
+
+    private static final DpdkConfig.DatapathType DATAPATH_TYPE_1 = NETDEV;
+    private static final DpdkConfig.DatapathType DATAPATH_TYPE_2 = NORMAL;
+
+    private static final String SOCKET_DIR_1 = "/var/lib/libvirt/qemu";
+    private static final String SOCKET_DIR_2 = "/var/lib/libvirt/kvm";
+
+    private static final List<DpdkInterface> DPDK_INTFS_1 = Lists.newArrayList();
+    private static final List<DpdkInterface> DPDK_INTFS_2 = Lists.newArrayList();
+
+    private DpdkConfig config1;
+    private DpdkConfig sameAsConfig1;
+    private DpdkConfig config2;
+
+    /**
+     * Tests class immutability.
+     */
+    @Test
+    public void testImmutability() {
+        assertThatClassIsImmutable(DefaultDpdkConfig.class);
+    }
+
+    /**
+     * Initial setup for this unit test.
+     */
+    @Before
+    public void setUp() {
+        DpdkInterface dpdkIntf1 = DefaultDpdkInterface.builder()
+                                        .intf("dpdk1")
+                                        .deviceName("br-int")
+                                        .mtu(1500L)
+                                        .pciAddress("0000:85:00.0")
+                                        .type(DPDK_VHOST_USER)
+                                        .build();
+
+        DpdkInterface dpdkIntf2 = DefaultDpdkInterface.builder()
+                                        .intf("dpdk2")
+                                        .deviceName("br-int")
+                                        .mtu(1500L)
+                                        .pciAddress("0000:85:00.0")
+                                        .type(DPDK_VHOST_USER_CLIENT)
+                                        .build();
+
+        DPDK_INTFS_1.add(dpdkIntf1);
+        DPDK_INTFS_2.add(dpdkIntf2);
+
+        config1 = DefaultDpdkConfig.builder()
+                                        .datapathType(DATAPATH_TYPE_1)
+                                        .socketDir(SOCKET_DIR_1)
+                                        .dpdkIntfs(DPDK_INTFS_1)
+                                        .build();
+
+        sameAsConfig1 = DefaultDpdkConfig.builder()
+                                        .datapathType(DATAPATH_TYPE_1)
+                                        .socketDir(SOCKET_DIR_1)
+                                        .dpdkIntfs(DPDK_INTFS_1)
+                                        .build();
+
+        config2 = DefaultDpdkConfig.builder()
+                                        .datapathType(DATAPATH_TYPE_2)
+                                        .socketDir(SOCKET_DIR_2)
+                                        .dpdkIntfs(DPDK_INTFS_2)
+                                        .build();
+    }
+
+    /**
+     * Checks equals method works as expected.
+     */
+    @Test
+    public void testEquality() {
+        new EqualsTester().addEqualityGroup(config1, sameAsConfig1)
+                .addEqualityGroup(config2)
+                .testEquals();
+    }
+
+    /**
+     * Test object construction.
+     */
+    @Test
+    public void testConstruction() {
+        DpdkConfig config = config1;
+
+        assertEquals(config.datapathType(), DATAPATH_TYPE_1);
+        assertEquals(config.socketDir(), SOCKET_DIR_1);
+        assertEquals(config.dpdkIntfs(), DPDK_INTFS_1);
+    }
+}
diff --git a/apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultDpdkInterfaceTest.java b/apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultDpdkInterfaceTest.java
new file mode 100644
index 0000000..f39139a
--- /dev/null
+++ b/apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultDpdkInterfaceTest.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.openstacknode.api;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Before;
+import org.junit.Test;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+import static org.onosproject.openstacknode.api.DpdkInterface.Type.DPDK_VHOST_USER;
+import static org.onosproject.openstacknode.api.DpdkInterface.Type.DPDK_VHOST_USER_CLIENT;
+
+/**
+ * Unit tests for DefaultDpdkInterface.
+ */
+public class DefaultDpdkInterfaceTest {
+
+    private static final String DEVICE_NAME_1 = "br-int";
+    private static final String DEVICE_NAME_2 = "br-tun";
+
+    private static final String INTF_NAME_1 = "dpdk0";
+    private static final String INTF_NAME_2 = "dpdk1";
+
+    private static final String PCI_ADDRESS_1 = "0000:85:00.0";
+    private static final String PCI_ADDRESS_2 = "0000:85:00.1";
+
+    private static final DpdkInterface.Type TYPE_1 = DPDK_VHOST_USER;
+    private static final DpdkInterface.Type TYPE_2 = DPDK_VHOST_USER_CLIENT;
+
+    private static final Long MTU_1 = 1500L;
+    private static final Long MTU_2 = 1600L;
+
+    private DpdkInterface dpdkIntf1;
+    private DpdkInterface sameAsDpdkIntf1;
+    private DpdkInterface dpdkIntf2;
+
+    /**
+     * Tests class immutability.
+     */
+    @Test
+    public void testImmutability() {
+        assertThatClassIsImmutable(DefaultDpdkInterface.class);
+    }
+
+    /**
+     * Initial setup for this unit test.
+     */
+    @Before
+    public void setUp() {
+        dpdkIntf1 = DefaultDpdkInterface.builder()
+                            .type(TYPE_1)
+                            .pciAddress(PCI_ADDRESS_1)
+                            .mtu(MTU_1)
+                            .deviceName(DEVICE_NAME_1)
+                            .intf(INTF_NAME_1)
+                            .build();
+
+        sameAsDpdkIntf1 = DefaultDpdkInterface.builder()
+                            .type(TYPE_1)
+                            .pciAddress(PCI_ADDRESS_1)
+                            .mtu(MTU_1)
+                            .deviceName(DEVICE_NAME_1)
+                            .intf(INTF_NAME_1)
+                            .build();
+
+        dpdkIntf2 = DefaultDpdkInterface.builder()
+                            .type(TYPE_2)
+                            .pciAddress(PCI_ADDRESS_2)
+                            .mtu(MTU_2)
+                            .deviceName(DEVICE_NAME_2)
+                            .intf(INTF_NAME_2)
+                            .build();
+    }
+
+    /**
+     * Tests object equality.
+     */
+    @Test
+    public void testEquality() {
+        new EqualsTester().addEqualityGroup(dpdkIntf1, sameAsDpdkIntf1)
+                .addEqualityGroup(dpdkIntf2)
+                .testEquals();
+    }
+
+    /**
+     * Test object construction.
+     */
+    @Test
+    public void testConstruction() {
+        DpdkInterface dpdkIntf = dpdkIntf1;
+
+        assertEquals(dpdkIntf.deviceName(), DEVICE_NAME_1);
+        assertEquals(dpdkIntf.intf(), INTF_NAME_1);
+        assertEquals(dpdkIntf.mtu(), MTU_1);
+        assertEquals(dpdkIntf.pciAddress(), PCI_ADDRESS_1);
+        assertEquals(dpdkIntf.type(), TYPE_1);
+    }
+}
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackAuthTest.java b/apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultOpenstackAuthTest.java
similarity index 94%
rename from apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackAuthTest.java
rename to apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultOpenstackAuthTest.java
index e602cf2..0a09248 100644
--- a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackAuthTest.java
+++ b/apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultOpenstackAuthTest.java
@@ -13,12 +13,10 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.onosproject.openstacknode.impl;
+package org.onosproject.openstacknode.api;
 
 import com.google.common.testing.EqualsTester;
 import org.junit.Test;
-import org.onosproject.openstacknode.api.DefaultOpenstackAuth;
-import org.onosproject.openstacknode.api.OpenstackAuth;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
diff --git a/apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultOpenstackNodeTest.java b/apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultOpenstackNodeTest.java
index 2fe0d2a..c8feb87 100644
--- a/apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultOpenstackNodeTest.java
+++ b/apps/openstacknode/api/src/test/java/org/onosproject/openstacknode/api/DefaultOpenstackNodeTest.java
@@ -15,15 +15,29 @@
  */
 package org.onosproject.openstacknode.api;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.testing.EqualsTester;
+import org.junit.Before;
 import org.junit.Test;
 import org.onlab.packet.IpAddress;
 import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.ControllerInfo;
+
+import java.util.List;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.onosproject.openstacknode.api.DpdkConfig.DatapathType.NETDEV;
+import static org.onosproject.openstacknode.api.DpdkInterface.Type.DPDK_VHOST_USER;
+import static org.onosproject.openstacknode.api.NodeState.COMPLETE;
+import static org.onosproject.openstacknode.api.NodeState.DEVICE_CREATED;
+import static org.onosproject.openstacknode.api.OpenstackAuth.Perspective.PUBLIC;
+import static org.onosproject.openstacknode.api.OpenstackAuth.Protocol.HTTP;
 
 /**
  * Unit tests for DefaultOpenstackNode.
  */
-public class DefaultOpenstackNodeTest extends OpenstackNodeTest {
+public final class DefaultOpenstackNodeTest extends OpenstackNodeTest {
 
     private static final IpAddress TEST_IP = IpAddress.valueOf("10.100.0.3");
 
@@ -31,6 +45,55 @@
     private static final String HOSTNAME_2 = "hostname_2";
     private static final Device DEVICE_1 = createDevice(1);
     private static final Device DEVICE_2 = createDevice(2);
+
+    private static final IpAddress MANAGEMENT_IP = IpAddress.valueOf("10.10.10.10");
+    private static final IpAddress DATA_IP = IpAddress.valueOf("20.20.20.20");
+
+    private static final String VLAN_INTF = "eth0";
+    private static final String UPLINK_PORT = "eth1";
+
+    private static final String SSH_AUTH_ID = "admin";
+    private static final String SSH_AUTH_PASSWORD = "nova";
+
+    private static final String OS_AUTH_USERNAME = "admin";
+    private static final String OS_AUTH_PASSWORD = "nova";
+    private static final String OS_AUTH_PROJECT = "admin";
+    private static final String OS_AUTH_VERSION = "v2.0";
+    private static final OpenstackAuth.Protocol OS_AUTH_PROTOCOL = HTTP;
+    private static final OpenstackAuth.Perspective OS_AUTH_PERSPECTIVE = PUBLIC;
+    private static final String OS_ENDPOINT = "keystone:35357/v2.0";
+
+    private static final String META_PROXY_SECRET = "nova";
+    private static final boolean META_USE_SERVICE = true;
+    private static final String META_IP = "30.30.30.30";
+    private static final int META_PORT = 8775;
+
+    private static final String DPDK_INTF_NAME = "dpdk1";
+    private static final Long DPDK_INTF_MTU = 1500L;
+    private static final String DPDK_INTF_DEV_NAME = "br-int";
+    private static final String DPDK_INTF_PCI_ADDRESS = "0000:85:00.0";
+    private static final DpdkInterface.Type DPDK_INTF_TYPE = DPDK_VHOST_USER;
+
+    private static final DpdkConfig.DatapathType DPDK_DATAPATH_TYPE = NETDEV;
+    private static final String DPDK_SOCKET_DIR = "/var/lib/libvirt/qemu";
+
+    private static final String PHY_INTF_NETWORK = "mgmtnetwork";
+    private static final String PHY_INTF_NAME = "eth3";
+
+    private static final IpAddress CONTROLLER_IP = IpAddress.valueOf("40.40.40.40");
+    private static final int CONTROLLER_PORT = 6653;
+    private static final String TCP = "tcp";
+
+    private static final OpenstackSshAuth SSH_AUTH = initSshAuth();
+    private static final OpenstackAuth AUTH = initAuth();
+    private static final KeystoneConfig KEYSTONE_CONFIG = initKeystoneConfig();
+    private static final NeutronConfig NEUTRON_CONFIG = initNeutronConfig();
+    private static final DpdkConfig DPDK_CONFIG = initDpdkConfig();
+    private static final List<OpenstackPhyInterface> PHY_INTFS = initPhyIntfs();
+    private static final List<ControllerInfo> CONTROLLERS = initControllers();
+
+    private OpenstackNode refNode;
+
     private static final OpenstackNode OS_NODE_1 = createNode(
             HOSTNAME_1,
             OpenstackNode.NodeType.COMPUTE,
@@ -51,6 +114,29 @@
             NodeState.INIT);
 
     /**
+     * Initial setup for this unit test.
+     */
+    @Before
+    public void setUp() {
+        refNode = DefaultOpenstackNode.builder()
+                .hostname(HOSTNAME_1)
+                .type(OpenstackNode.NodeType.COMPUTE)
+                .managementIp(MANAGEMENT_IP)
+                .dataIp(DATA_IP)
+                .intgBridge(DEVICE_1.id())
+                .vlanIntf(VLAN_INTF)
+                .uplinkPort(UPLINK_PORT)
+                .state(NodeState.COMPLETE)
+                .sshAuthInfo(SSH_AUTH)
+                .keystoneConfig(KEYSTONE_CONFIG)
+                .neutronConfig(NEUTRON_CONFIG)
+                .dpdkConfig(DPDK_CONFIG)
+                .phyIntfs(PHY_INTFS)
+                .controllers(CONTROLLERS)
+                .build();
+    }
+
+    /**
      * Checks equals method works as expected.
      */
     @Test
@@ -61,6 +147,48 @@
     }
 
     /**
+     * Test object construction.
+     */
+    @Test
+    public void testConstruction() {
+        checkCommonProperties(refNode);
+        assertEquals(refNode.state(), COMPLETE);
+        assertEquals(refNode.intgBridge(), DEVICE_1.id());
+    }
+
+    /**
+     * Checks the functionality of update state method.
+     */
+    @Test
+    public void testUpdateState() {
+        OpenstackNode updatedNode = refNode.updateState(DEVICE_CREATED);
+
+        checkCommonProperties(updatedNode);
+        assertEquals(updatedNode.state(), DEVICE_CREATED);
+    }
+
+    /**
+     * Checks the functionality of update int bridge method.
+     */
+    @Test
+    public void testUpdateIntBridge() {
+        OpenstackNode updatedNode = refNode.updateIntbridge(DeviceId.deviceId("br-tun"));
+
+        checkCommonProperties(updatedNode);
+        assertEquals(updatedNode.intgBridge(), DeviceId.deviceId("br-tun"));
+    }
+
+    /**
+     * Checks the functionality of from method.
+     */
+    @Test
+    public void testFrom() {
+        OpenstackNode updatedNode = DefaultOpenstackNode.from(refNode).build();
+
+        assertEquals(updatedNode, refNode);
+    }
+
+    /**
      * Checks building a node without hostname fails with proper exception.
      */
     @Test(expected = IllegalArgumentException.class)
@@ -116,4 +244,87 @@
                 .state(NodeState.INIT)
                 .build();
     }
+
+
+    private static OpenstackSshAuth initSshAuth() {
+        return DefaultOpenstackSshAuth.builder()
+                .id(SSH_AUTH_ID)
+                .password(SSH_AUTH_PASSWORD)
+                .build();
+    }
+
+    private static OpenstackAuth initAuth() {
+        return DefaultOpenstackAuth.builder()
+                .username(OS_AUTH_USERNAME)
+                .password(OS_AUTH_PASSWORD)
+                .project(OS_AUTH_PROJECT)
+                .protocol(OS_AUTH_PROTOCOL)
+                .version(OS_AUTH_VERSION)
+                .perspective(OS_AUTH_PERSPECTIVE)
+                .build();
+    }
+
+    private static KeystoneConfig initKeystoneConfig() {
+        return DefaultKeystoneConfig.builder()
+                .authentication(AUTH)
+                .endpoint(OS_ENDPOINT)
+                .build();
+    }
+
+    private static NeutronConfig initNeutronConfig() {
+        return DefaultNeutronConfig.builder()
+                .metadataProxySecret(META_PROXY_SECRET)
+                .novaMetadataIp(META_IP)
+                .novaMetadataPort(META_PORT)
+                .useMetadataProxy(META_USE_SERVICE)
+                .build();
+    }
+
+    private static DpdkConfig initDpdkConfig() {
+        DpdkInterface dpdkIntf = DefaultDpdkInterface.builder()
+                .intf(DPDK_INTF_NAME)
+                .deviceName(DPDK_INTF_DEV_NAME)
+                .mtu(DPDK_INTF_MTU)
+                .pciAddress(DPDK_INTF_PCI_ADDRESS)
+                .type(DPDK_INTF_TYPE)
+                .build();
+        List<DpdkInterface> dpdkIntfs = ImmutableList.of(dpdkIntf);
+
+        return DefaultDpdkConfig.builder()
+                .dpdkIntfs(dpdkIntfs)
+                .datapathType(DPDK_DATAPATH_TYPE)
+                .socketDir(DPDK_SOCKET_DIR)
+                .build();
+    }
+
+    private static List<OpenstackPhyInterface> initPhyIntfs() {
+        OpenstackPhyInterface phyIntf = DefaultOpenstackPhyInterface.builder()
+                .intf(PHY_INTF_NAME)
+                .network(PHY_INTF_NETWORK)
+                .build();
+
+        return ImmutableList.of(phyIntf);
+    }
+
+    private static List<ControllerInfo> initControllers() {
+        ControllerInfo controller = new ControllerInfo(CONTROLLER_IP, CONTROLLER_PORT, TCP);
+
+        return ImmutableList.of(controller);
+    }
+
+
+    private void checkCommonProperties(OpenstackNode node) {
+        assertEquals(node.hostname(), HOSTNAME_1);
+        assertEquals(node.type(), OpenstackNode.NodeType.COMPUTE);
+        assertEquals(node.managementIp(), MANAGEMENT_IP);
+        assertEquals(node.dataIp(), DATA_IP);
+        assertEquals(node.vlanIntf(), VLAN_INTF);
+        assertEquals(node.uplinkPort(), UPLINK_PORT);
+        assertEquals(node.sshAuthInfo(), SSH_AUTH);
+        assertEquals(node.keystoneConfig(), KEYSTONE_CONFIG);
+        assertEquals(node.neutronConfig(), NEUTRON_CONFIG);
+        assertEquals(node.dpdkConfig(), DPDK_CONFIG);
+        assertEquals(node.phyIntfs(), PHY_INTFS);
+        assertEquals(node.controllers(), CONTROLLERS);
+    }
 }