Refactor DOMDataBrokerImpl
[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 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;
22
23 public class NetconfConfigUtilTest {
24
25     private BundleContext bundleContext;
26
27     @Before
28     public void setUp() throws Exception {
29         bundleContext = mock(BundleContext.class);
30     }
31
32     @Test
33     public void testNetconfConfigUtil() throws Exception {
34         assertEquals(NetconfConfigUtil.getNetconfLocalAddress(), new LocalAddress("netconf"));
35
36         doReturn("").when(bundleContext).getProperty("netconf.connectionTimeoutMillis");
37         assertEquals(NetconfConfigUtil.extractTimeoutMillis(bundleContext), 5000);
38
39         doReturn("a").when(bundleContext).getProperty("netconf.connectionTimeoutMillis");
40         assertEquals(NetconfConfigUtil.extractTimeoutMillis(bundleContext), 5000);
41     }
42
43     @Test
44     public void testgetPrivateKeyKey() throws Exception {
45         assertEquals(NetconfConfigUtil.getPrivateKeyKey(), "netconf.ssh.pk.path");
46     }
47
48     @Test
49     public void testgetNetconfServerAddressKey() throws Exception {
50         NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
51         assertEquals(NetconfConfigUtil.getNetconfServerAddressKey(prop), "netconf.tcp.address");
52     }
53
54     @Test
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());
59     }
60
61     @Test
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));
69     }
70
71     @Test
72     public void testGetPrivateKeyPath() throws Exception {
73         doReturn("path").when(bundleContext).getProperty("netconf.ssh.pk.path");
74         assertEquals(NetconfConfigUtil.getPrivateKeyPath(bundleContext), "path");
75     }
76
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");
81     }
82 }