2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.netconf.util.osgi;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import com.google.common.base.Optional;
17 import io.netty.channel.local.LocalAddress;
18 import java.net.InetSocketAddress;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.osgi.framework.BundleContext;
23 public class NetconfConfigUtilTest {
25 private BundleContext bundleContext;
28 public void setUp() throws Exception {
29 bundleContext = mock(BundleContext.class);
33 public void testNetconfConfigUtil() throws Exception {
34 assertEquals(NetconfConfigUtil.getNetconfLocalAddress(), new LocalAddress("netconf"));
36 doReturn("").when(bundleContext).getProperty("netconf.connectionTimeoutMillis");
37 assertEquals(NetconfConfigUtil.extractTimeoutMillis(bundleContext), 5000);
39 doReturn("a").when(bundleContext).getProperty("netconf.connectionTimeoutMillis");
40 assertEquals(NetconfConfigUtil.extractTimeoutMillis(bundleContext), 5000);
44 public void testgetPrivateKeyKey() throws Exception {
45 assertEquals(NetconfConfigUtil.getPrivateKeyKey(), "netconf.ssh.pk.path");
49 public void testgetNetconfServerAddressKey() throws Exception {
50 NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
51 assertEquals(NetconfConfigUtil.getNetconfServerAddressKey(prop), "netconf.tcp.address");
55 public void testExtractNetconfServerAddress() throws Exception {
56 NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
57 doReturn("").when(bundleContext).getProperty(anyString());
58 assertEquals(NetconfConfigUtil.extractNetconfServerAddress(bundleContext, prop), Optional.absent());
62 public void testExtractNetconfServerAddress2() throws Exception {
63 NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
64 doReturn("1.1.1.1").when(bundleContext).getProperty("netconf.tcp.address");
65 doReturn("20").when(bundleContext).getProperty("netconf.tcp.port");
66 Optional<InetSocketAddress> inetSocketAddressOptional = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, prop);
67 assertTrue(inetSocketAddressOptional.isPresent());
68 assertEquals(inetSocketAddressOptional.get(), new InetSocketAddress("1.1.1.1", 20));
72 public void testGetPrivateKeyPath() throws Exception {
73 doReturn("path").when(bundleContext).getProperty("netconf.ssh.pk.path");
74 assertEquals(NetconfConfigUtil.getPrivateKeyPath(bundleContext), "path");
77 @Test(expected = IllegalStateException.class)
78 public void testGetPrivateKeyPath2() throws Exception {
79 doReturn(null).when(bundleContext).getProperty("netconf.ssh.pk.path");
80 assertEquals(NetconfConfigUtil.getPrivateKeyPath(bundleContext), "path");