Add netconf-util tests for package org.opendaylight.netconf.util.osgi 72/47772/5
authorAndrej Mak <andrej.mak@pantheon.tech>
Mon, 31 Oct 2016 09:56:59 +0000 (10:56 +0100)
committerJakub Morvay <jmorvay@cisco.com>
Mon, 7 Nov 2016 11:22:34 +0000 (12:22 +0100)
Change-Id: I3a4d476a98c68c7a9fdf0043b09102c72b9fbb2d
Signed-off-by: Andrej Mak <andrej.mak@pantheon.tech>
Signed-off-by: Jakub Morvay <jmorvay@cisco.com>
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigUtilTest.java [new file with mode: 0644]
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivatorTest.java [new file with mode: 0644]
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationTest.java [new file with mode: 0644]

diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigUtilTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigUtilTest.java
new file mode 100644 (file)
index 0000000..7210640
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.netconf.util.osgi;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.ManagedService;
+
+public class NetconfConfigUtilTest {
+
+    @Mock
+    private ServiceReference<ManagedService> serviceRef;
+
+    @Mock
+    private ServiceReference<ManagedService> netconfConfigurationRef;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+    }
+
+    @Test
+    public void testGetNetconfConfigurationService() throws Exception {
+        final Collection<ServiceReference<ManagedService>> services = new ArrayList<>();
+        services.add(serviceRef);
+        services.add(netconfConfigurationRef);
+        final BundleContext context = mock(BundleContext.class);
+        doReturn(services).when(context).getServiceReferences(ManagedService.class, null);
+        final ManagedService service = mock(ManagedService.class);
+        doReturn(service).when(context).getService(serviceRef);
+        doReturn(NetconfConfiguration.getInstance()).when(context).getService(netconfConfigurationRef);
+        final java.util.Optional<NetconfConfiguration> netconfConfigurationOptional =
+                NetconfConfigUtil.getNetconfConfigurationService(context);
+        Assert.assertTrue(netconfConfigurationOptional.isPresent());
+        Assert.assertEquals(NetconfConfiguration.getInstance(), netconfConfigurationOptional.get());
+
+    }
+
+    @Test
+    public void testGetNetconfConfigurationServiceAbsent() throws Exception {
+        final Collection<ServiceReference<ManagedService>> services = new ArrayList<>();
+        services.add(serviceRef);
+        final BundleContext context = mock(BundleContext.class);
+        doReturn(services).when(context).getServiceReferences(ManagedService.class, null);
+        final ManagedService service = mock(ManagedService.class);
+        doReturn(service).when(context).getService(serviceRef);
+        final java.util.Optional<NetconfConfiguration> netconfConfigurationOptional =
+                NetconfConfigUtil.getNetconfConfigurationService(context);
+        Assert.assertFalse(netconfConfigurationOptional.isPresent());
+    }
+
+    @Test
+    public void testGetNetconfConfigurationServiceInvalidSyntax() throws Exception {
+        final BundleContext context = mock(BundleContext.class);
+        final InvalidSyntaxException exception = new InvalidSyntaxException("Invalid syntax", "filter");
+        doThrow(exception).when(context).getServiceReferences(ManagedService.class, null);
+        final java.util.Optional<NetconfConfiguration> netconfConfigurationOptional =
+                NetconfConfigUtil.getNetconfConfigurationService(context);
+        Assert.assertFalse(netconfConfigurationOptional.isPresent());
+    }
+}
diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivatorTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivatorTest.java
new file mode 100644 (file)
index 0000000..7ace84b
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.netconf.util.osgi;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ManagedService;
+
+public class NetconfConfigurationActivatorTest {
+
+    @Mock
+    private BundleContext context;
+    @Mock
+    private ServiceRegistration registration;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        doReturn(registration).when(context)
+                .registerService(eq(ManagedService.class), any(NetconfConfiguration.class), any());
+        doNothing().when(registration).unregister();
+    }
+
+    @Test
+    public void testStartStop() throws Exception {
+        final NetconfConfigurationActivator activator = new NetconfConfigurationActivator();
+        activator.start(context);
+        final Dictionary<String, String> props = new Hashtable<>();
+        props.put(Constants.SERVICE_PID, "netconf");
+        verify(context).registerService(eq(ManagedService.class), eq(NetconfConfiguration.getInstance()), eq(props));
+        activator.stop(context);
+        verify(registration).unregister();
+    }
+
+}
\ No newline at end of file
diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationTest.java
new file mode 100644 (file)
index 0000000..c80f388
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.netconf.util.osgi;
+
+import io.netty.channel.local.LocalAddress;
+import java.net.InetSocketAddress;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class NetconfConfigurationTest {
+
+    @Test
+    public void testUpdated() throws Exception {
+        final NetconfConfiguration config = NetconfConfiguration.getInstance();
+        Assert.assertEquals(new InetSocketAddress("0.0.0.0", 1830), config.getSshServerAddress());
+        Assert.assertEquals(new InetSocketAddress("127.0.0.1", 8383), config.getTcpServerAddress());
+        Assert.assertEquals("./configuration/RSA.pk", config.getPrivateKeyPath());
+        final Dictionary<String, String> newValues = new Hashtable<>();
+        final String newSshIp = "192.168.1.1";
+        final String newTcpIp = "192.168.1.2";
+        final int newSshPort = 1234;
+        final int newTcpPort = 4567;
+        final String newSshKeyPath = "./new_folder/configuration/RSA.pk";
+        newValues.put("ssh-address", newSshIp);
+        newValues.put("ssh-port", Integer.toString(newSshPort));
+        newValues.put("tcp-address", newTcpIp);
+        newValues.put("tcp-port", Integer.toString(newTcpPort));
+        newValues.put("ssh-pk-path", newSshKeyPath);
+        config.updated(newValues);
+        Assert.assertEquals(new InetSocketAddress(newSshIp, newSshPort), config.getSshServerAddress());
+        Assert.assertEquals(new InetSocketAddress(newTcpIp, newTcpPort), config.getTcpServerAddress());
+        Assert.assertEquals(newSshKeyPath, config.getPrivateKeyPath());
+    }
+
+    @Test
+    public void testUpdatedNull() throws Exception {
+        final NetconfConfiguration config = NetconfConfiguration.getInstance();
+        Assert.assertEquals(new InetSocketAddress("0.0.0.0", 1830), config.getSshServerAddress());
+        Assert.assertEquals(new InetSocketAddress("127.0.0.1", 8383), config.getTcpServerAddress());
+        Assert.assertEquals("./configuration/RSA.pk", config.getPrivateKeyPath());
+        final Dictionary<String, String> nullDictionary = null;
+        config.updated(nullDictionary);
+        Assert.assertEquals(new InetSocketAddress("0.0.0.0", 1830), config.getSshServerAddress());
+        Assert.assertEquals(new InetSocketAddress("127.0.0.1", 8383), config.getTcpServerAddress());
+        Assert.assertEquals("./configuration/RSA.pk", config.getPrivateKeyPath());
+    }
+}
\ No newline at end of file