2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.netconf.jsch;
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;
52 public class TestNetconfClientJsch {
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;
63 public ExpectedException expectedEx = ExpectedException.none();
67 netconfClientJsch = Mockito.spy(new NetconfClientJsch());
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));
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>"));
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);
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);
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);
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>"))
142 netconfClientJsch.connect(connectionDetails);
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);
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());
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();
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);
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);