5ece53f83150f330cbaad3da26f3a628027cf36c
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / util / NetconfTopologyRPCProviderTest.java
1 /*
2  * Copyright (c) 2017 Brocade Communication Systems 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 package org.opendaylight.netconf.sal.connect.netconf.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.mockito.Mockito.when;
13
14 import java.lang.reflect.Method;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mock;
18 import org.mockito.MockitoAnnotations;
19 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.netconf.sal.connect.util.NetconfTopologyRPCProvider;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.AddNetconfNodeInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.AddNetconfNodeInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32
33 public class NetconfTopologyRPCProviderTest {
34     private static final NodeId NODE_ID = new NodeId("testing-node");
35     private static final String TOPOLOGY_ID = "testing-topology";
36     private static final String TEST_PWD =  "test";
37     private static final String ENC_PWD = "4o9/Hn3Pi4150YrP12N/1g==";
38
39     @Mock
40     private DataBroker dataBroker;
41
42     @Mock
43     private AAAEncryptionService encryptionService;
44
45     NetconfTopologyRPCProvider rpcProvider ;
46
47     @Before
48     public void setUp() {
49         MockitoAnnotations.initMocks(this);
50         when(encryptionService.encrypt(TEST_PWD)).thenReturn(ENC_PWD);
51         rpcProvider = new NetconfTopologyRPCProvider(dataBroker, encryptionService, TOPOLOGY_ID);
52     }
53
54     @Test
55     public void testEncryptPassword() throws Exception {
56
57         NetconfNode node = invokeEncryption(true);
58         assertNotEquals(TEST_PWD, ((LoginPassword)node.getCredentials()).getPassword());
59
60         node = invokeEncryption(false);
61         assertEquals(TEST_PWD, ((LoginPassword)node.getCredentials()).getPassword());
62     }
63
64     private NetconfNode invokeEncryption(boolean encrypt) throws Exception {
65         Method method = null;
66
67         method = NetconfTopologyRPCProvider.class.getDeclaredMethod("encryptPassword", AddNetconfNodeInput.class);
68
69         method.setAccessible(true);
70         NetconfNode node = null;
71
72         node = (NetconfNode)method.invoke(rpcProvider, getInput(encrypt));
73
74         return node;
75     }
76
77     private AddNetconfNodeInput getInput(boolean encrypt) {
78         AddNetconfNodeInputBuilder builder = new AddNetconfNodeInputBuilder();
79         builder.setCredentials(new LoginPasswordBuilder().setPassword(TEST_PWD).setUsername("test").build());
80         builder.setHost(new Host(new IpAddress(new Ipv4Address("10.18.16.188"))));
81         builder.setPort(new PortNumber(830));
82         builder.setTcpOnly(false);
83         builder.setNodeId(NODE_ID.toString());
84         builder.setEncrypt(encrypt);
85         return builder.build();
86     }
87
88 }