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;
17 import com.google.common.base.Optional;
18 import io.netty.channel.local.LocalAddress;
19 import java.net.InetSocketAddress;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.osgi.framework.BundleContext;
24 public class NetconfConfigUtilTest {
26 private BundleContext bundleContext;
29 public void setUp() throws Exception {
30 bundleContext = mock(BundleContext.class);
34 public void testNetconfConfigUtil() throws Exception {
35 assertEquals(NetconfConfigUtil.getNetconfLocalAddress(), new LocalAddress("netconf"));
37 doReturn("").when(bundleContext).getProperty("netconf.connectionTimeoutMillis");
38 assertEquals(NetconfConfigUtil.extractTimeoutMillis(bundleContext), NetconfConfigUtil.DEFAULT_TIMEOUT_MILLIS);
40 doReturn("a").when(bundleContext).getProperty("netconf.connectionTimeoutMillis");
41 assertEquals(NetconfConfigUtil.extractTimeoutMillis(bundleContext), NetconfConfigUtil.DEFAULT_TIMEOUT_MILLIS);
45 public void testgetPrivateKeyKey() throws Exception {
46 assertEquals(NetconfConfigUtil.getPrivateKeyKey(), "netconf.ssh.pk.path");
50 public void testgetNetconfServerAddressKey() throws Exception {
51 NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
52 assertEquals(NetconfConfigUtil.getNetconfServerAddressKey(prop), "netconf.tcp.address");
56 public void testExtractNetconfServerAddress() throws Exception {
57 NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
58 doReturn("").when(bundleContext).getProperty(anyString());
59 assertEquals(NetconfConfigUtil.extractNetconfServerAddress(bundleContext, prop), Optional.absent());
63 public void testExtractNetconfServerAddress2() throws Exception {
64 NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
65 doReturn("1.1.1.1").when(bundleContext).getProperty("netconf.tcp.address");
66 doReturn("20").when(bundleContext).getProperty("netconf.tcp.port");
67 Optional<InetSocketAddress> inetSocketAddressOptional = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, prop);
68 assertTrue(inetSocketAddressOptional.isPresent());
69 assertEquals(inetSocketAddressOptional.get(), new InetSocketAddress("1.1.1.1", 20));
73 public void testGetPrivateKeyPath() throws Exception {
74 doReturn("path").when(bundleContext).getProperty("netconf.ssh.pk.path");
75 assertEquals(NetconfConfigUtil.getPrivateKeyPath(bundleContext), "path");
78 @Test(expected = IllegalStateException.class)
79 public void testGetPrivateKeyPath2() throws Exception {
80 doReturn(null).when(bundleContext).getProperty("netconf.ssh.pk.path");
81 assertEquals(NetconfConfigUtil.getPrivateKeyPath(bundleContext), "path");