Merge "Add MD-SAL artifacts"
[controller.git] / opendaylight / netconf / netconf-util / src / test / java / org / opendaylight / controller / netconf / util / osgi / NetconfConfigUtilTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.util.osgi;
10
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
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;
23
24 public class NetconfConfigUtilTest {
25
26     private BundleContext bundleContext;
27
28     @Before
29     public void setUp() throws Exception {
30         bundleContext = mock(BundleContext.class);
31     }
32
33     @Test
34     public void testNetconfConfigUtil() throws Exception {
35         assertEquals(NetconfConfigUtil.getNetconfLocalAddress(), new LocalAddress("netconf"));
36
37         doReturn("").when(bundleContext).getProperty("netconf.connectionTimeoutMillis");
38         assertEquals(NetconfConfigUtil.extractTimeoutMillis(bundleContext), 5000);
39
40         doReturn("a").when(bundleContext).getProperty("netconf.connectionTimeoutMillis");
41         assertEquals(NetconfConfigUtil.extractTimeoutMillis(bundleContext), 5000);
42     }
43
44     @Test
45     public void testgetPrivateKeyKey() throws Exception {
46         assertEquals(NetconfConfigUtil.getPrivateKeyKey(), "netconf.ssh.pk.path");
47     }
48
49     @Test
50     public void testgetNetconfServerAddressKey() throws Exception {
51         NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
52         assertEquals(NetconfConfigUtil.getNetconfServerAddressKey(prop), "netconf.tcp.address");
53     }
54
55     @Test
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());
60     }
61
62     @Test
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));
70     }
71
72     @Test
73     public void testGetPrivateKeyPath() throws Exception {
74         doReturn("path").when(bundleContext).getProperty("netconf.ssh.pk.path");
75         assertEquals(NetconfConfigUtil.getPrivateKeyPath(bundleContext), "path");
76     }
77
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");
82     }
83 }