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 com.google.common.base.Optional;
12 import io.netty.channel.local.LocalAddress;
13 import java.net.InetSocketAddress;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.netconf.util.NetconfUtil;
17 import org.osgi.framework.BundleContext;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.mockito.Matchers.anyString;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
25 public class NetconfConfigUtilTest {
27 private BundleContext bundleContext;
30 public void setUp() throws Exception {
31 bundleContext = mock(BundleContext.class);
35 public void testNetconfConfigUtil() throws Exception {
36 assertEquals(NetconfConfigUtil.getNetconfLocalAddress(), new LocalAddress("netconf"));
38 doReturn("").when(bundleContext).getProperty("netconf.connectionTimeoutMillis");
39 assertEquals(NetconfConfigUtil.extractTimeoutMillis(bundleContext), 5000);
41 doReturn("a").when(bundleContext).getProperty("netconf.connectionTimeoutMillis");
42 assertEquals(NetconfConfigUtil.extractTimeoutMillis(bundleContext), 5000);
46 public void testgetPrivateKeyKey() throws Exception {
47 assertEquals(NetconfConfigUtil.getPrivateKeyKey(), "netconf.ssh.pk.path");
51 public void testgetNetconfServerAddressKey() throws Exception {
52 NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
53 assertEquals(NetconfConfigUtil.getNetconfServerAddressKey(prop), "netconf.tcp.address");
57 public void testExtractNetconfServerAddress() throws Exception {
58 NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
59 doReturn("").when(bundleContext).getProperty(anyString());
60 assertEquals(NetconfConfigUtil.extractNetconfServerAddress(bundleContext, prop), Optional.absent());
64 public void testExtractNetconfServerAddress2() throws Exception {
65 NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
66 doReturn("1.1.1.1").when(bundleContext).getProperty("netconf.tcp.address");
67 doReturn("20").when(bundleContext).getProperty("netconf.tcp.port");
68 Optional<InetSocketAddress> inetSocketAddressOptional = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, prop);
69 assertTrue(inetSocketAddressOptional.isPresent());
70 assertEquals(inetSocketAddressOptional.get(), new InetSocketAddress("1.1.1.1", 20));
74 public void testGetPrivateKeyPath() throws Exception {
75 doReturn("path").when(bundleContext).getProperty("netconf.ssh.pk.path");
76 assertEquals(NetconfConfigUtil.getPrivateKeyPath(bundleContext), "path");
79 @Test(expected = IllegalStateException.class)
80 public void testGetPrivateKeyPath2() throws Exception {
81 doReturn(null).when(bundleContext).getProperty("netconf.ssh.pk.path");
82 assertEquals(NetconfConfigUtil.getPrivateKeyPath(bundleContext), "path");