Fix for APPC-1271
[appc.git] / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / test / java / org / onap / appc / adapter / netconf / jsch / TestNetconfClientJsch.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Samsung
6  * ================================================================================
7  * Modifications Copyright (C) 2018 Ericsson
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.netconf.jsch;
25
26 import static org.hamcrest.CoreMatchers.allOf;
27 import static org.hamcrest.CoreMatchers.is;
28 import static org.hamcrest.CoreMatchers.isA;
29 import static org.hamcrest.CoreMatchers.containsString;
30 import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
31 import static org.junit.Assert.assertEquals;
32 import com.jcraft.jsch.ChannelSubsystem;
33 import com.jcraft.jsch.JSch;
34 import com.jcraft.jsch.JSchException;
35 import com.jcraft.jsch.Session;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.mockito.Mockito;
41 import org.onap.appc.adapter.netconf.NetconfConnectionDetails;
42 import org.onap.appc.adapter.netconf.internal.NetconfAdapter;
43 import org.onap.appc.exceptions.APPCException;
44 import org.powermock.reflect.Whitebox;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.OutputStream;
48 import java.util.Arrays;
49 import java.util.List;
50 import java.util.Properties;
51
52 public class TestNetconfClientJsch {
53
54     NetconfClientJsch netconfClientJsch;
55     private Session mockSession;
56     private JSch mockJSch;
57     private ChannelSubsystem mockChannel;
58     private InputStream mockInputStream;
59     private OutputStream mockOutputStream;
60     private NetconfAdapter mockNetconfAdapter;
61
62     @Rule
63     public ExpectedException expectedEx = ExpectedException.none();
64
65     @Before
66     public void SetUp() {
67         netconfClientJsch = Mockito.spy(new NetconfClientJsch());
68     }
69
70     private void setupForConnectTests() throws JSchException, IOException {
71         mockSession = Mockito.mock(Session.class);
72         mockJSch = Mockito.mock(JSch.class);
73         mockChannel = Mockito.mock(ChannelSubsystem.class);
74         mockInputStream = Mockito.mock(InputStream.class);
75         mockOutputStream = Mockito.mock(OutputStream.class);
76         mockNetconfAdapter = Mockito.mock(NetconfAdapter.class);
77         Mockito.doReturn(mockJSch).when(netconfClientJsch).getJSch();
78         Mockito.doReturn(mockSession).when(mockJSch).getSession(Mockito.anyString(),
79                 Mockito.anyString(), Mockito.anyInt());
80         Mockito.doReturn(mockChannel).when(mockSession).openChannel("subsystem");
81         Mockito.doReturn(mockInputStream).when(mockChannel).getInputStream();
82         Mockito.doReturn(mockOutputStream).when(mockChannel).getOutputStream();
83         Mockito.doReturn(mockNetconfAdapter).when(netconfClientJsch)
84                 .getNetconfAdapter(Mockito.any(InputStream.class), Mockito.any(OutputStream.class));
85     }
86
87     @Test
88     public void testConnect() throws APPCException, IOException, JSchException {
89         setupForConnectTests();
90         Mockito.doReturn("<hello>").when(mockNetconfAdapter).receiveMessage();
91         NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails();
92         connectionDetails.setHost("test");
93         connectionDetails.setPort(8080);
94         connectionDetails.setUsername("test");
95         connectionDetails.setPassword("test");
96         List<String> capabilities = Arrays.asList(
97             "<capability>urn:ietf:params:netconf:base:1.1</capability>\r\n");
98         connectionDetails.setCapabilities(capabilities);
99         Properties additionalProperties = new Properties();
100         additionalProperties.setProperty("testKey1", "testParam1");
101         connectionDetails.setAdditionalProperties(additionalProperties);
102         netconfClientJsch.connect(connectionDetails);
103         Mockito.verify(mockNetconfAdapter).sendMessage(
104                 Mockito.contains("<capability>urn:ietf:params:netconf:base:1.1</capability>"));
105     }
106
107     @Test
108     public void testConnectNullMessage() throws JSchException, IOException, APPCException {
109         setupForConnectTests();
110         NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails();
111         expectedEx.expect(APPCException.class);
112         expectedEx.expectMessage("Cannot establish connection to server");
113         netconfClientJsch.connect(connectionDetails);
114     }
115
116     @Test
117     public void testConnectNullMessageNonNullResponse()
118             throws JSchException, IOException, APPCException {
119         setupForConnectTests();
120         Mockito.doReturn("NOT NULL RESPONSE").when(mockNetconfAdapter).receiveMessage();
121         Mockito.doThrow(new JSchException()).when(mockChannel).connect(10000);
122         NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails();
123         expectedEx.expect(APPCException.class);
124         expectedEx.expectCause(allOf(isA(RuntimeException.class),
125                 hasProperty("message", is("Error closing netconf device"))));
126         netconfClientJsch.connect(connectionDetails);
127     }
128
129     @Test
130     public void testConnectErrorMessage() throws JSchException, IOException, APPCException {
131         setupForConnectTests();
132         Mockito.doReturn("<rpc-error>").when(mockNetconfAdapter).receiveMessage();
133         NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails();
134         expectedEx.expect(APPCException.class);
135         expectedEx
136                 .expectCause(allOf(isA(RuntimeException.class),
137                         hasProperty("cause", allOf(isA(IOException.class),
138                                 hasProperty("message",
139                                         containsString("Error response from netconf device:")),
140                                 hasProperty("message", containsString("<rpc-error>"))
141                         ))));
142         netconfClientJsch.connect(connectionDetails);
143     }
144
145     @Test
146     public void testConnectWithSuccessfulDisconnect()
147             throws JSchException, IOException, APPCException {
148         setupForConnectTests();
149         Mockito.doThrow(new JSchException()).when(mockChannel).connect(10000);
150         Mockito.doReturn("<ok/>").when(mockNetconfAdapter).receiveMessage();
151         NetconfConnectionDetails connectionDetails = new NetconfConnectionDetails();
152         expectedEx.expect(APPCException.class);
153         expectedEx.expectCause(allOf(isA(APPCException.class),
154                 hasProperty("message", is(JSchException.class.getName()))));
155         netconfClientJsch.connect(connectionDetails);
156     }
157
158     @Test
159     public void testGetConfiguration() throws IOException, APPCException {
160         mockNetconfAdapter = Mockito.mock(NetconfAdapter.class);
161         Whitebox.setInternalState(netconfClientJsch, "netconfAdapter", mockNetconfAdapter);
162         Mockito.doReturn("TEST RETURN VALUE").when(mockNetconfAdapter).receiveMessage();
163         assertEquals("TEST RETURN VALUE", netconfClientJsch.getConfiguration());
164     }
165
166     @Test
167     public void testGetConfigurationExceptionFlow() throws IOException, APPCException {
168         mockNetconfAdapter = Mockito.mock(NetconfAdapter.class);
169         Whitebox.setInternalState(netconfClientJsch, "netconfAdapter", mockNetconfAdapter);
170         Mockito.doThrow(new IOException()).when(mockNetconfAdapter).receiveMessage();
171         expectedEx.expect(APPCException.class);
172         expectedEx.expectMessage(IOException.class.getName());
173         netconfClientJsch.getConfiguration();
174     }
175
176     @Test
177     public void testConfigure() throws IOException, APPCException {
178         mockNetconfAdapter = Mockito.mock(NetconfAdapter.class);
179         Whitebox.setInternalState(netconfClientJsch, "netconfAdapter", mockNetconfAdapter);
180         Mockito.doReturn("<ok/>").when(mockNetconfAdapter).receiveMessage();
181         netconfClientJsch.configure(null);
182         Mockito.verify(netconfClientJsch).exchangeMessage(null);
183     }
184
185     @Test
186     public void testConfigureExceptionFlow() throws IOException, APPCException {
187         mockNetconfAdapter = Mockito.mock(NetconfAdapter.class);
188         Whitebox.setInternalState(netconfClientJsch, "netconfAdapter", mockNetconfAdapter);
189         Mockito.doThrow(new IOException()).when(mockNetconfAdapter).receiveMessage();
190         expectedEx.expect(APPCException.class);
191         expectedEx.expectMessage(IOException.class.getName());
192         netconfClientJsch.configure(null);
193     }
194 }